forked from AmpersandJS/ampersand-form-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathampersand-form-view.js
187 lines (146 loc) · 4.86 KB
/
ampersand-form-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
/*$AMPERSAND_VERSION*/
var BBEvents = require('backbone-events-standalone');
var isFunction = require('amp-is-function');
var extend = require('amp-extend');
var result = require('amp-result');
var View = require('ampersand-view')
module.exports = View.extend({
initialize: function(opts) {
opts = opts || {};
this.validCallback = opts.validCallback || this.validCallback || function () {};
this.submitCallback = opts.submitCallback || this.submitCallback || function () {};
if (opts.data) this.data = opts.data;
if (opts.model) this.model = opts.model;
this.clean = opts.clean || function (res) { return res; };
this.valid = false;
this.preventDefault = opts.preventDefault === false ? false : true;
// storage for our fields
this._fieldViews = {};
this._fieldViewsArray = [];
// add all our fields
this.render();
(opts.fields || result(this, 'fields') || []).forEach(this.addField.bind(this));
this.checkValid(true);
},
props: {
rendered_template: ['boolean', true, false]
},
derived: {
// Overrides ampersand-view
// We have only rendered if we both have an element, and have populated it with our template
rendered: {
deps: ['el', 'rendered_template'],
fn: function () {
return !!this.el && this.rendered_template;
}
}
},
// TODO do I need these?
data: null,
// model: null,
fields: null,
template: "<form><p>You should pass a template to your AmpersandFormView.</p></form>",
addField: function (fieldDefinition) {
var fieldView = fieldDefinition.field_view;
this._fieldViews[fieldView.name] = fieldView;
this._fieldViewsArray.push(fieldView);
fieldView.parent = this;
fieldView.render();
var element = this.queryByHook(fieldDefinition.hook);
element.appendChild(fieldView.el);
},
removeField: function (name) {
var field = this.getField(name);
if (field) {
field.remove();
delete this._fieldViews[name];
this._fieldViewsArray.splice(this._fieldViewsArray.indexOf(field), 1);
}
},
getField: function (name) {
return this._fieldViews[name];
},
setValid: function (now, forceFire) {
var prev = this.valid;
this.valid = now;
if (prev !== now || forceFire) {
this.validCallback(now);
}
},
checkValid: function (forceFire) {
var valid = this._fieldViewsArray.every(function (field) {
return field.valid;
});
this.setValid(valid, forceFire);
return valid;
},
beforeSubmit: function () {
this._fieldViewsArray.forEach(function (field) {
if (field.beforeSubmit) field.beforeSubmit();
});
},
update: function (field) {
this.trigger('change:' + field.name, field);
// if this one's good check 'em all
if (field.valid) {
this.checkValid();
} else {
this.setValid(false);
}
},
remove: function () {
this.el.removeEventListener('submit', this.handleSubmit, false);
var parent = this.el.parentNode;
if (parent) parent.removeChild(this.el);
this._fieldViewsArray.forEach(function (field) {
field.remove();
});
},
handleSubmit: function (e) {
this.beforeSubmit();
this.checkValid();
if (!this.valid) {
e.preventDefault();
return false;
}
if (this.preventDefault) {
e.preventDefault();
this.submitCallback(this.getData());
return false;
}
},
getData: function () {
var res = {};
for (var key in this._fieldViews) {
res[key] = this._fieldViews[key].value;
}
return this.clean(res);
},
reset: function () {
this._fieldViewsArray.forEach(function (field) {
if (isFunction(field.reset)) {
field.reset();
}
});
},
clear: function () {
this._fieldViewsArray.forEach(function (field) {
if (isFunction(field.clear)) {
field.clear();
}
});
},
render: function () {
if (this.rendered) return;
this.renderWithTemplate()
// if (!this.el) {
// this.el = document.createElement('form');
// }
// if (this.autoAppend) {
// this.fieldContainerEl = this.el.querySelector('[data-hook~=field-container]') || this.el;
// }
this.handleSubmit = this.handleSubmit.bind(this);
this.el.addEventListener('submit', this.handleSubmit, false);
this.rendered_template = true; // TODO you're sure that this is inherited?
}
});