Commit cacbe0cf authored by Ryan Herbert's avatar Ryan Herbert
Browse files

make FormBuilder closeable

refactor the tokenizer method createCloseButton into a new Closeable
class to be reused in FormBuilder.

See #3017
parent d89114c7
......@@ -39,7 +39,8 @@ function loadAfterConf() {
require(["../germline"],
function() {
require(["../generic_axis"],
require(["../generic_axis",
"../closeable"],
function() {
require(["../compare",
"../menu",
......
function Closeable() {
}
Closeable.prototype = {
createCloseButton: function() {
var x = document.createElement('i');
x.className = "icon-cancel";
x.onclick = function() {this.parentNode.parentNode.removeChild(this.parentNode);};
return x;
}
}
......@@ -24,27 +24,29 @@ function FormBuilder() {
return FormBuilder.instance;
}
Closeable.call(this);
FormBuilder.instance = this;
this.index = 0;
}
FormBuilder.prototype = {
FormBuilder.prototype = Object.create(Closeable.prototype);
build_wrapper: function() {
FormBuilder.prototype.build_wrapper = function() {
var d = document.createElement('div');
d.className = "field_div";
return d;
},
}
build_label: function(txt, object, field) {
FormBuilder.prototype.build_label = function(txt, object, field) {
var l = document.createElement('label');
l.htmlFor = field + "_" + this.index;
l.id = object + "_" + field + "__label_" + this.index;
l.innerText = txt + ":";
return l;
},
}
build_input: function(id, className, name, input_type, set_type, required, placeholder) {
FormBuilder.prototype.build_input = function(id, className, name, input_type, set_type, required, placeholder) {
var i = document.createElement('input');
i.id = set_type + "_" + id + "_" + this.index;
i.className = "form-control " + className;
......@@ -60,9 +62,9 @@ FormBuilder.prototype = {
}
return i;
},
}
build_field: function(id, name, label, required, placeholder) {
FormBuilder.prototype.build_field = function(id, name, label, required, placeholder) {
if (typeof name === "undefined") {
name = id;
}
......@@ -75,31 +77,31 @@ FormBuilder.prototype = {
d.appendChild(this.build_label(label, this.type, id));
d.appendChild(this.build_input(id, 'string', name, 'text', this.type, required, placeholder));
return d;
},
}
build_textarea: function(id, className, name, set_type) {
FormBuilder.prototype.build_textarea = function(id, className, name, set_type) {
var t = document.createElement('textarea');
t.id = set_type + "_" + id + "_" + this.index;
t.className = "form-control " + className;
t.name = set_type + "[" + this.index + "][" + name + "]";
t.rows = 1;
return t;
},
}
build_fieldset: function(type) {
FormBuilder.prototype.build_fieldset = function(type) {
var f = document.createElement('fieldset');
f.name = type + this.index;
f.appendChild(this.build_legend(capitalise(type) + " " + (this.index+1)));
return f;
},
}
build_legend: function(text) {
FormBuilder.prototype.build_legend = function(text) {
var l = document.createElement('legend');
l.innerText = text;
return l;
},
}
build_date : function(id, object, name, label) {
FormBuilder.prototype.build_date = function(id, object, name, label) {
if (typeof name === "undefined") {
name = id;
}
......@@ -115,9 +117,7 @@ FormBuilder.prototype = {
i.title = "yyyy-mm-dd"
d.appendChild(i);
return d;
},
}
}
function SetFormBuilder() {
this.type = 'foobar';
......@@ -160,6 +160,7 @@ PatientFormBuilder.prototype = Object.create(SetFormBuilder.prototype);
PatientFormBuilder.prototype.build = function(index) {
this.index = index;
var fieldset = this.build_fieldset(this.type);
fieldset.appendChild(this.createCloseButton());
fieldset.appendChild(this.build_input('id', 'text', 'id', 'hidden', this.type));
fieldset.appendChild(this.set_id());
fieldset.appendChild(this.build_field('first_name', undefined, undefined, true));
......@@ -179,6 +180,7 @@ RunFormBuilder.prototype = Object.create(SetFormBuilder.prototype);
RunFormBuilder.prototype.build = function(index) {
this.index = index;
var fieldset = this.build_fieldset(this.type);
fieldset.appendChild(this.createCloseButton());
fieldset.appendChild(this.build_input('id', 'text', 'id', 'hidden', this.type));
fieldset.appendChild(this.set_id());
fieldset.appendChild(this.build_field('name', undefined, undefined, true));
......@@ -199,6 +201,7 @@ GenericFormBuilder.prototype = Object.create(SetFormBuilder.prototype);
GenericFormBuilder.prototype.build = function(index) {
this.index = index;
var fieldset = this.build_fieldset('set');
fieldset.appendChild(this.createCloseButton());
fieldset.appendChild(this.build_input('id', 'text', 'id', 'hidden', this.type));
fieldset.appendChild(this.build_field('name', undefined, undefined, true));
fieldset.appendChild(this.build_info());
......@@ -216,6 +219,7 @@ FileFormBuilder.prototype = Object.create(FormBuilder.prototype);
FileFormBuilder.prototype.build = function(index) {
this.index = index;
var fieldset = this.build_fieldset('file');
fieldset.appendChild(this.createCloseButton());
fieldset.appendChild(this.build_hidden_fields());
fieldset.appendChild(this.build_file_fieldset());
fieldset.appendChild(this.build_set_fieldset());
......
......@@ -25,13 +25,15 @@ function Tokeniser() {
return Tokeniser.instance;
}
Closeable.call(this);
Tokeniser.instance = this;
return this;
}
Tokeniser.prototype = {
Tokeniser.prototype = Object.create(Closeable.prototype);
setup: function(input, target, form_input) {
Tokeniser.prototype.setup = function(input, target, form_input) {
var self = this;
if ($(input).data('needs-tokeniser')) {
$(input).on("keydown", function(e) {
......@@ -42,26 +44,26 @@ Tokeniser.prototype = {
});
$(input).data('needs-tokeniser', false);
}
},
}
tokenise: function(input, target, form_input) {
Tokeniser.prototype.tokenise = function(input, target, form_input) {
var token = this.createToken($(input));
$(input).val("");
$(input).removeData('set-id');
target.appendChild(token);
form_input.value = this.readTokens(target);
},
}
readTokens: function(target) {
Tokeniser.prototype.readTokens = function(target) {
var nodes = $(target).children('.set_token');
return nodes.map(function callback() {
return $(this).data('set-id');
})
.get()
.join();
},
}
createToken: function($input) {
Tokeniser.prototype.createToken = function($input) {
var class_mapping = {
":p": "patient_token",
":r": "run_token",
......@@ -81,13 +83,4 @@ Tokeniser.prototype = {
$(token).data('set-id', set_id);
return token;
},
createCloseButton: function() {
var x = document.createElement('i');
x.className = "icon-cancel";
x.onclick = function() {this.parentNode.parentNode.removeChild(this.parentNode);};
return x;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment