Skip to content

Commit dd50f86

Browse files
author
BrainFooLong
committed
code reformat to js standard
1 parent ac2f069 commit dd50f86

34 files changed

+918
-918
lines changed

.editorconfig

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ root = true
44
charset = utf-8
55
end_of_line = crlf
66
indent_style = space
7-
indent_size = 4
87
trim_trailing_whitespace = true
98
insert_final_newline = true
109

public/scripts/form.js

+115-115
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
"use strict";
1+
'use strict'
22

33
/**
44
* Form creator
55
*/
6-
var Form = {};
6+
var Form = {}
77

88
/**
99
* Create a form table by given props
@@ -15,124 +15,124 @@ var Form = {};
1515
* @return {JQuery}
1616
*/
1717
Form.create = function (container, formName, fields, onSubmit, values) {
18-
if (!values) values = {};
19-
var $form = $('<form>').attr("name", formName).attr("onsubmit", "return false").attr("id", "form-" + formName);
20-
for (var fieldName in fields) {
21-
var field = fields[fieldName];
22-
var currentValue = getObjectValue(values, fieldName);
23-
if (typeof currentValue == "undefined") currentValue = field.defaultValue;
24-
if (typeof currentValue == "undefined") currentValue = null;
25-
var $input = null;
26-
var $el = $('<div class="form-field type-' + field.type + '" data-name="' + fieldName + '">' +
27-
'<div class="form-label"></div>' +
28-
'<div class="form-input"></div>' +
29-
'</div>');
30-
if (field.label) {
31-
$el.find(".form-label").append($("<strong>").text(t(field.label)));
18+
if (!values) values = {}
19+
var $form = $('<form>').attr('name', formName).attr('onsubmit', 'return false').attr('id', 'form-' + formName)
20+
for (var fieldName in fields) {
21+
var field = fields[fieldName]
22+
var currentValue = getObjectValue(values, fieldName)
23+
if (typeof currentValue === 'undefined') currentValue = field.defaultValue
24+
if (typeof currentValue === 'undefined') currentValue = null
25+
var $input = null
26+
var $el = $('<div class="form-field type-' + field.type + '" data-name="' + fieldName + '">' +
27+
'<div class="form-label"></div>' +
28+
'<div class="form-input"></div>' +
29+
'</div>')
30+
if (field.label) {
31+
$el.find('.form-label').append($('<strong>').text(t(field.label)))
32+
}
33+
if (field.description) {
34+
$el.find('.form-label').append($('<small>').text(t(field.description)))
35+
}
36+
switch (field.type) {
37+
case 'textarea':
38+
$input = $('<textarea class="form-control autoheight" name="' + fieldName + '">')
39+
if (currentValue !== null) {
40+
$input.val(currentValue)
3241
}
33-
if (field.description) {
34-
$el.find(".form-label").append($("<small>").text(t(field.description)));
42+
break
43+
case 'number':
44+
$input = $('<input type="number" class="form-control" name="' + fieldName + ':number">')
45+
if (currentValue !== null) {
46+
$input.val(currentValue)
3547
}
36-
switch (field.type) {
37-
case "textarea":
38-
$input = $('<textarea class="form-control autoheight" name="' + fieldName + '">');
39-
if (currentValue !== null) {
40-
$input.val(currentValue);
41-
}
42-
break;
43-
case "number":
44-
$input = $('<input type="number" class="form-control" name="' + fieldName + ':number">');
45-
if (currentValue !== null) {
46-
$input.val(currentValue);
47-
}
48-
break;
49-
case "password":
50-
$input = $('<input type="password" class="form-control" name="' + fieldName + '">');
51-
if (currentValue !== null) {
52-
$input.val(currentValue);
53-
}
54-
break;
55-
case "text":
56-
$input = $('<input type="text" class="form-control" name="' + fieldName + '">');
57-
if (currentValue !== null) {
58-
$input.val(currentValue);
59-
}
60-
break;
61-
case "select":
62-
var name = fieldName;
63-
if (field.multiple) name += "[]";
64-
$input = $('<select class="form-control" name="' + name + '">');
65-
if (field.multiple) $input.attr("multiple", true);
66-
for (var valueKey in field.values) {
67-
$input.append($('<option>').attr("value", valueKey).text(t(field.values[valueKey])));
68-
}
69-
if (currentValue !== null) {
70-
$input.val(currentValue);
71-
}
72-
break;
73-
case "switch":
74-
$input = $('<select class="form-control" name="' + fieldName + ':boolean">');
75-
var fieldValues = ["true", "false"];
76-
for (var i = 0; i < fieldValues.length; i++) {
77-
var valueKey = fieldValues[i];
78-
$input.append($('<option>').attr("value", valueKey).text(t(valueKey == "true" ? "yes" : "no")));
79-
}
80-
if (currentValue !== null) {
81-
$input.val(currentValue ? "true" : "false");
82-
}
83-
break;
48+
break
49+
case 'password':
50+
$input = $('<input type="password" class="form-control" name="' + fieldName + '">')
51+
if (currentValue !== null) {
52+
$input.val(currentValue)
8453
}
85-
if ($input) {
86-
if (field.pattern) {
87-
$input.attr("pattern", field.pattern);
88-
}
89-
if (field.required) {
90-
$input.attr("required", true);
91-
}
92-
if (field.placeholder) {
93-
$input.attr("placeholder", t(field.placeholder));
94-
}
95-
if (field.attributes) {
96-
for (var i in field.attributes) {
97-
$input.attr("data-" + i, field.attributes[i]);
98-
}
99-
}
100-
$el.find(".form-input").append($input);
54+
break
55+
case 'text':
56+
$input = $('<input type="text" class="form-control" name="' + fieldName + '">')
57+
if (currentValue !== null) {
58+
$input.val(currentValue)
59+
}
60+
break
61+
case 'select':
62+
var name = fieldName
63+
if (field.multiple) name += '[]'
64+
$input = $('<select class="form-control" name="' + name + '">')
65+
if (field.multiple) $input.attr('multiple', true)
66+
for (var valueKey in field.values) {
67+
$input.append($('<option>').attr('value', valueKey).text(t(field.values[valueKey])))
68+
}
69+
if (currentValue !== null) {
70+
$input.val(currentValue)
10171
}
102-
$form.append($el);
103-
// if grouped
104-
if (field.attachTo) {
105-
var attachTo = $form.find("[name='" + field.attachTo + "']").closest(".form-field");
106-
if (attachTo.length) {
107-
if ($input) {
108-
attachTo.find(".form-input").append($input).addClass("form-group input-group form-inline");
109-
$el.remove();
110-
}
111-
}
72+
break
73+
case 'switch':
74+
$input = $('<select class="form-control" name="' + fieldName + ':boolean">')
75+
var fieldValues = ['true', 'false']
76+
for (var i = 0; i < fieldValues.length; i++) {
77+
var valueKey = fieldValues[i]
78+
$input.append($('<option>').attr('value', valueKey).text(t(valueKey == 'true' ? 'yes' : 'no')))
11279
}
80+
if (currentValue !== null) {
81+
$input.val(currentValue ? 'true' : 'false')
82+
}
83+
break
11384
}
114-
var $btn = $('<div><span data-name="save" data-translate="save" class="btn btn-info submit-form"></span></div>');
115-
if (Object.keys(values).length) {
116-
$btn.children().attr("data-translate", "save.edited");
117-
$btn.append('&nbsp;<a href="' + location.pathname + '" data-translate="cancel.edit" class="btn btn-default page-link"></a>')
85+
if ($input) {
86+
if (field.pattern) {
87+
$input.attr('pattern', field.pattern)
88+
}
89+
if (field.required) {
90+
$input.attr('required', true)
91+
}
92+
if (field.placeholder) {
93+
$input.attr('placeholder', t(field.placeholder))
94+
}
95+
if (field.attributes) {
96+
for (var i in field.attributes) {
97+
$input.attr('data-' + i, field.attributes[i])
98+
}
99+
}
100+
$el.find('.form-input').append($input)
118101
}
119-
$form.append($btn);
120-
lang.replaceInHtml($form);
121-
$form.find(":input").not("button").after('<span class="invalid">');
122-
container.append($form);
123-
$form.on("click", ".submit-form", function () {
124-
var f = $(this).closest("form");
125-
$form.find(".invalid").removeClass("invalid");
126-
if (f[0].checkValidity()) {
127-
var formDataJson = f.serializeJSON();
128-
onSubmit(formDataJson);
129-
} else {
130-
var firstInvalid = $form.find(":invalid").first();
131-
scrollTo("body", firstInvalid.closest(".form-field").offset().top);
132-
// on validation error trigger a fake submit button to enable validation UI popup
133-
$(this).after('<input type="submit">');
134-
$(this).next().trigger("click").remove();
102+
$form.append($el)
103+
// if grouped
104+
if (field.attachTo) {
105+
var attachTo = $form.find('[name=\'' + field.attachTo + '\']').closest('.form-field')
106+
if (attachTo.length) {
107+
if ($input) {
108+
attachTo.find('.form-input').append($input).addClass('form-group input-group form-inline')
109+
$el.remove()
135110
}
136-
});
137-
return $form;
138-
};
111+
}
112+
}
113+
}
114+
var $btn = $('<div><span data-name="save" data-translate="save" class="btn btn-info submit-form"></span></div>')
115+
if (Object.keys(values).length) {
116+
$btn.children().attr('data-translate', 'save.edited')
117+
$btn.append('&nbsp;<a href="' + location.pathname + '" data-translate="cancel.edit" class="btn btn-default page-link"></a>')
118+
}
119+
$form.append($btn)
120+
lang.replaceInHtml($form)
121+
$form.find(':input').not('button').after('<span class="invalid">')
122+
container.append($form)
123+
$form.on('click', '.submit-form', function () {
124+
var f = $(this).closest('form')
125+
$form.find('.invalid').removeClass('invalid')
126+
if (f[0].checkValidity()) {
127+
var formDataJson = f.serializeJSON()
128+
onSubmit(formDataJson)
129+
} else {
130+
var firstInvalid = $form.find(':invalid').first()
131+
scrollTo('body', firstInvalid.closest('.form-field').offset().top)
132+
// on validation error trigger a fake submit button to enable validation UI popup
133+
$(this).after('<input type="submit">')
134+
$(this).next().trigger('click').remove()
135+
}
136+
})
137+
return $form
138+
}

0 commit comments

Comments
 (0)