-
Notifications
You must be signed in to change notification settings - Fork 0
/
ampersand-fieldset-view.js
137 lines (132 loc) · 4.37 KB
/
ampersand-fieldset-view.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
var View = require('ampersand-view');
var InputView = require('ampersand-input-view');
var FormView = require('ampersand-form-view');
var some = require('lodash/some');
var concat = require('lodash/concat');
var set = require('lodash/set');
module.exports = View.extend({
initialize: function (opts) {
FormView.prototype.initialize.apply(this, arguments);
if (opts.value) this.setValue(opts.value);
},
template: [
'<fieldset>',
'<legend data-hook="legend"></legend>',
'<div data-hook="message-container" class="message message-below message-error">',
'<p data-hook="message-text"></p>',
'</div>',
'</fieldset>'
].join(''),
bindings: {
'name': {
type: 'attribute',
selector: 'fieldset',
name: 'name'
},
'tabindex': {
type: 'attribute',
selector: 'input, textarea',
name: 'tabindex'
},
'legend': [
{
hook: 'legend'
},
{
type: 'toggle',
hook: 'legend'
}
],
'message': {
type: 'text',
hook: 'message-text'
},
'showMessage': {
type: 'toggle',
hook: 'message-container'
},
'autofocus': {
type: 'booleanAttribute',
name: 'autofocus',
selector: 'input, textarea'
}
},
props: {
el: 'element',
name: 'string',
placeholder: ['string', true, ''],
legend: ['string', true, ''],
required: ['boolean', true, true],
readonly: ['boolean', true, false],
shouldValidate: ['boolean', true, false],
message: ['string', true, ''],
requiredMessage: ['string', true, 'All of the below fields are required.'],
validClass: ['string', true, 'fieldset-valid'],
invalidClass: ['string', true, 'fieldset-invalid'],
validityClassSelector: ['string', true, 'fieldset'],
tabindex: ['number', true, 0]
},
derived: {
valid: {
cache: false,
fn: function () {
return !some(concat(this._fieldViewsArray , this), function (fieldView) {
return fieldView.runTests();
});
}
},
value: {
fn: function () {
var res = {};
for (var key in this._fieldViews) {
if (this._fieldViews.hasOwnProperty(key)) {
// If field name ends with '[]', don't interpret
// as verbose form field...
if (key.match(/\[\]$/)) {
res[key] = this._fieldViews[key].value;
} else {
set(res, key, this._fieldViews[key].value);
}
}
}
return this.clean(res);
},
cache: false
}
},
addField: FormView.prototype.addField,
removeField: FormView.prototype.removeField,
getField: FormView.prototype.getField,
setValue: function (data) {
for (var name in data) {
if (data.hasOwnProperty(name)) {
FormView.prototype.setValue.call(this, name, data[name]);
}
}
},
setValues: FormView.prototype.setValues,
checkValid: FormView.prototype.checkValid,
beforeSubmit: FormView.prototype.beforeSubmit,
update: function () {
if (this.parent) this.parent.update(this);
},
remove: FormView.prototype.remove,
handleSubmit: FormView.prototype.handleSubmit,
reset: FormView.prototype.reset,
clear: FormView.prototype.clear,
clean: FormView.prototype.clean,
getErrorMessage: InputView.prototype.getErrorMessage,
runTests: InputView.prototype.runTests,
render: function () {
this.renderWithTemplate(this);
if (this.autoAppend) {
this.fieldContainerEl = this.query(this.fieldContainerEl || '[datahook~=field-container') || this.el;
}
this._fieldViewsArray.forEach(function renderEachField(fV) {
this.renderField(fV, true);
}, this);
},
renderField: FormView.prototype.renderField,
getValue: FormView.prototype.getValue,
getData: FormView.prototype.getData,
});