-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
207 lines (178 loc) · 7.86 KB
/
application.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
(function($) {
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
var Formator = function($el, formStructure) {
this.$_el = $el;
this._structure = formStructure;
this._rowTpl = _.template('<div class="form_field"><label>{{ title }}</label>{{ content }}</div>');
this.init();
};
Formator.prototype = {
init: function() {
this.render();
this.$_el.find('form').on('submit.formator_submit', $.proxy(this._handleSubmit ,this));
},
render: function() {
var _formTpl = _.template('<form><div class="form_area"><div class="form_fields"></div><div class="form_submit"><input type="submit" value="{{ submit }}" name="commit"><span class="formator-spinner"></span></div></div></form>'),
$form = $(_formTpl({
'submit': this._structure.submit_txt
})),
$fields = $form.find('.form_fields'),
_sortedData = _.sortBy(this._structure.data, function(f) { return f.position; });
_.each(_sortedData, function(field) {
switch (field.type) {
case "string":
$fields.append(this._getInputRow(field));
break;
case "text":
$fields.append(this._getTextareaRow(field));
break;
case "radio":
$fields.append(this._getRadioBtnsRow(field));
break;
case "select":
$fields.append(this._getSelectRow(field));
break;
}
}, this);
this.$_el.append($form);
},
_getInputRow: function(field) {
var _inputTpl = _.template('<input type="text" name="formator-name-{{ position }}" value="{{ val }}" class="form_field_field form_field_textfield form_field_size_large">'),
contents = _inputTpl({
'val': field.value,
'position': field.position
}),
html = this._rowTpl({
'title': field.name,
'content': contents
});
return $(html).data('field', field);
},
_getTextareaRow: function(field) {
var _inputTpl = _.template('<textarea rows="4" name="formator-name-{{ position }}" cols="20" class="form_field_field form_field_textarea">{{ val }}</textarea>'),
contents = _inputTpl({
'val': field.value,
'position': field.position
}),
html = this._rowTpl({
'title': field.name,
'content': contents
});
return $(html).data('field', field);
},
_getRadioBtnsRow: function(field) {
var _radioTpl = _.template('<div><input type="radio" value="{{ name }}" name="formator-name-{{ position }}" class="form_field_field form_field_radio" {{ checked }}/>{{ name }}</div>'),
contents = '',
html;
_.each(field.options, function(option) {
contents += _radioTpl({
'position': field.position,
'name': option,
'checked': ((option == field.value) ? 'checked="checked"': '')
});
}, this);
html = this._rowTpl({
'title': field.name,
'content': contents
});
return $(html).data('field', field);
},
_getSelectRow: function(field) {
var _selectTpl = _.template('<select name="formator-name-{{ position }}" class="form_field_field form_field_select">{{ options }}</select>'),
_optionTpl = _.template('<option {{ selected }} value="{{ name }}">{{ name }}</option>'),
optContents = '',
contents, html;
_.each(field.options, function(option) {
optContents += _optionTpl({
'name': option,
'selected': ((option == field.value) ? 'selected="selected"': '')
});
}, this);
contents = _selectTpl({
'position': field.position,
'options': optContents
});
html = this._rowTpl({
'title': field.name,
'content': contents
});
return $(html).data('field', field);
},
_getSaveData: function() {
var $fields = this.$_el.find('.form_field'),
formData = _.clone(this._structure);
_.each($fields, function(f) {
var $f = $(f),
o = $f.data('field'),
value,
data = _.find(formData.data, function(d) { return d.position == o.position; });
if (_.contains(['text', 'string', 'select'], o.type)) {
value = $f.find('.form_field_field').val();
} else if (o.type == 'radio') {
value = $f.find('.form_field_field:checked').val();
}
if (o.bind) { formData[o.bind] = value || ''; }
data.value = value;
}, this);
return formData;
},
_validate: function() {
var valid = true,
$fields = this.$_el.find('.form_field');
$fields.removeClass('form_field_with_errors');
$fields.find('.form_field_error').remove();
_.each($fields, function(f) {
var $f = $(f),
o = $f.data('field'),
value;
if (o.required) {
var regexp = new RegExp(o.valid_pattern, "gi");
if (_.contains(['text', 'string', 'select'], o.type)) {
value = $f.find('.form_field_field').val();
} else if (o.type == 'radio') {
value = $f.find('.form_field_field:checked').val();
}
if (!regexp.test(value)) {
valid = false;
$f.addClass('form_field_with_errors');
$f.append('<div class="form_field_error">' + o.invalid_msg + '</div>');
}
}
}, this);
return valid;
},
_handleSubmit: function(event) {
event.preventDefault();
var valid = this._validate();
if (valid === true) {
this.save();
}
},
save: function() {
this.$_el.find('.formator-spinner').spin('tiny');
var data = this._getSaveData();
$.ajax({
url: data.url,
method: 'post',
data: JSON.stringify(data),
dataType: 'json',
success: $.proxy(function () {
this.$_el.find('.formator-spinner').spin(false);
alert(this._structure.success);
}, this),
error: $.proxy(function(jqXHR, textStatus, errorThrown) {
this.$_el.find('.formator-spinner').spin(false);
alert(textStatus);
}, this)
});
}
};
$.fn.formator = function(options) {
return this.each(function() {
var m = new Formator($(this), options);
$(this).data('Fromator', m);
});
};
})(jQuery);