1
0
Fork 0

Refactor js

This commit is contained in:
Gregory Eremin 2015-01-23 13:57:04 +07:00
parent 6bd7d2516c
commit 2ce470432c
1 changed files with 40 additions and 23 deletions

View File

@ -20,34 +20,14 @@ function drawForm(fields) {
var container = document.getElementById('fields');
for (var i = 0; i < fields.length; i++) {
var field = fields[i],
formGroup = document.createElement('div'),
label = document.createElement('label'),
formGroup = makeDivNode('form-group'),
label = makeLabelNode(field.path, field.title),
input;
formGroup.setAttribute('class', 'form-group');
label.setAttribute('for', field.path);
label.appendChild(document.createTextNode(field.title));
formGroup.appendChild(label);
if (field.options !== null) {
input = document.createElement('select');
if (field.is_required === false) {
var option = document.createElement('option');
option.setAttribute('value', '');
option.appendChild(document.createTextNode('Not selected'));
input.appendChild(option);
}
for (var j = 0; j < field.options.length; j++) {
var value = field.options[j],
option = document.createElement('option');
option.setAttribute('value', value);
option.appendChild(document.createTextNode(value));
input.appendChild(option);
}
input = makeSelectNode(field.options, !field.is_required)
} else {
input = document.createElement('input');
}
@ -72,6 +52,43 @@ function drawForm(fields) {
}
}
function makeDivNode(classes) {
var div = document.createElement('div');
div.setAttribute('class', classes);
return div;
}
function makeLabelNode(forId, text) {
var label = document.createElement('label'),
contents = document.createTextNode(text);
label.setAttribute('for', forId);
label.appendChild(contents);
return label;
}
function makeSelectNode(options, hasEmptyOption) {
var select = document.createElement('select');
if (hasEmptyOption === true) {
var option = document.createElement('option');
option.setAttribute('value', '');
option.appendChild(document.createTextNode('Not selected'));
select.appendChild(option);
}
for (var i = 0; i < options.length; i++) {
var value = options[i],
option = document.createElement('option');
option.setAttribute('value', value);
option.appendChild(document.createTextNode(value));
select.appendChild(option);
}
return select;
}
loadFields(drawForm);
// TODO: Support for various types