-
Notifications
You must be signed in to change notification settings - Fork 8
/
ampersand-array-input-view.js
205 lines (201 loc) · 6.3 KB
/
ampersand-array-input-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
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
/*$AMPERSAND_VERSION*/
var View = require('ampersand-view');
var without = require('lodash/without');
var FieldView = require('./lib/field-view');
var defaultTemplate = [
'<div>',
'<span data-hook="label"></span>',
'<div data-hook="field-container"></div>',
'<a data-hook="add-field" class="add-input">add</a>',
'<div data-hook="main-message-container" class="message message-below message-error">',
'<p data-hook="main-message-text"></p>',
'</div>',
'</div>'
].join('');
var defaultFieldTemplate = [
'<label>',
'<input>',
'<div data-hook="message-container" class="message message-below message-error">',
'<p data-hook="message-text"></p>',
'</div>',
'<a data-hook="remove-field">remove</a>',
'</label>'
].join('');
module.exports = View.extend({
initialize: function () {
if (!this.label) this.label = this.name;
this.fields = [];
// calculate default value if not provided
var defaultVal = [];
// make sure there's at least one
var num = this.minLength || 1;
while (num--) {
defaultVal.push('');
}
if (!this.value.length) this.value = defaultVal;
this.on('change:valid change:value', this.updateParent, this);
this.render();
},
render: function () {
if (this.rendered) return;
this.renderWithTemplate();
this.setValue(this.value);
this.rendered = true;
},
events: {
'click [data-hook~=add-field]': 'handleAddFieldClick'
},
bindings: {
'name': {
type: 'attribute',
selector: 'input',
name: 'name'
},
'label': {
hook: 'label'
},
'message': {
type: 'text',
hook: 'main-message-text'
},
'showMessage': {
type: 'toggle',
hook: 'main-message-container'
},
'canAdd': {
type: 'toggle',
hook: 'add-field'
}
},
props: {
name: ['string', true, ''],
value: ['array', true, function () { return []; }],
label: ['string', true, ''],
message: ['string', true, ''],
placeholder: ['string', true, ''],
requiredMessage: 'string',
validClass: ['string', true, 'input-valid'],
invalidClass: ['string', true, 'input-invalid'],
minLength: ['number', true, 0],
maxLength: ['number', true, 10],
tests: ['array', true, function () { return []; }],
template: ['string', true, defaultTemplate],
fieldTemplate: ['string', true, defaultFieldTemplate],
type: ['string', true, 'text']
},
session: {
shouldValidate: ['boolean', true, false],
fieldsValid: ['boolean', true, false],
fieldsRendered: ['number', true, 0],
rendered: ['boolean', true, false]
},
derived: {
fieldClass: {
deps: ['showMessage'],
fn: function () {
return this.showMessage ? this.invalidClass : this.validClass;
}
},
valid: {
deps: ['requiredMet', 'fieldsValid'],
fn: function () {
return this.requiredMet && this.fieldsValid;
}
},
showMessage: {
deps: ['valid', 'shouldValidate', 'message'],
fn: function () {
return !!(this.shouldValidate && this.message && !this.valid);
}
},
canAdd: {
deps: ['maxLength', 'fieldsRendered'],
fn: function () {
return this.fieldsRendered < this.maxLength;
}
},
requiredMet: {
deps: ['value', 'minLength'],
fn: function () {
return this.value.length >= this.minLength;
}
}
},
setValue: function (arr) {
if (arr.length > this.maxLength) throw Error('Field value length greater than maxLength setting');
this.clearFields();
arr.forEach(this.addField, this);
this.update();
},
beforeSubmit: function () {
this.fields.forEach(function (field) {
field.beforeSubmit();
});
this.shouldValidate = true;
if (!this.valid && !this.requiredMet) {
this.message = this.requiredMessage || this.getRequiredMessage();
}
},
handleAddFieldClick: function (e) {
e.preventDefault();
var field = this.addField('');
field.input.focus();
},
addField: function (value) {
var initOptions = {
value: value,
parent: this,
required: false,
tests: this.tests,
placeholder: this.placeholder,
type: this.type
};
var field = new FieldView(initOptions);
field.template = this.fieldTemplate;
field.render();
this.fieldsRendered += 1;
this.fields.push(field);
this._setRemoveableFields();
this.queryByHook('field-container').appendChild(field.el);
return field;
},
clearFields: function () {
this.fields.forEach(function (field) {
field.remove();
});
this.fields = [];
this.fieldsRendered = 0;
},
removeField: function (field) {
this.fields = without(this.fields, field);
field.remove();
this.fieldsRendered -= 1;
this._setRemoveableFields();
this.update();
},
_setRemoveableFields: function () {
var removable = this.fields.length > this.minLength;
this.fields.forEach(function(field) {
field.removable = removable;
});
},
update: function () {
var valid = true;
var value = this.fields.reduce(function (previous, field) {
if (field.value) previous.push(field.value);
if (!field.valid && valid) valid = false;
return previous;
}, []);
this.set({
value: value,
fieldsValid: valid
});
},
updateParent: function () {
if (this.parent) this.parent.update(this);
},
getRequiredMessage: function () {
var plural = this.minLength > 1;
return 'Enter at least ' + (this.minLength || 1) + ' item' + (plural ? 's.' : '.');
}
});