forked from bbbales2/ampersand-form-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ampersand-form-view.js
162 lines (133 loc) · 4.3 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
/*$AMPERSAND_VERSION*/
var BBEvents = require('backbone-events-standalone');
var extend = require('extend-object');
var result = function (obj, prop) {
if (typeof obj[prop] === 'function') return obj[prop]();
return obj[prop];
};
function FormView(opts) {
opts = opts || {};
this.el = opts.el;
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;
if (opts.fields) this.fields = opts.model;
this.clean = opts.clean || function (res) { return res; };
this.valid = false;
this.preventDefault = opts.preventDefault === false ? false : true;
this.autoAppend = opts.autoAppend === false ? false : true;
// storage for our fields
this._fieldViews = {};
this._fieldViewsArray = [];
if (this.initialize) this.initialize.apply(this, arguments);
this.render();
//defer till after returning from initialize
setTimeout(function () {
this.checkValid(true);
}.bind(this), 0);
}
extend(FormView.prototype, BBEvents, {
data: null,
model: null,
fields: null,
addField: function (fieldView) {
this._fieldViews[fieldView.name] = fieldView;
this._fieldViewsArray.push(fieldView);
if (this.fieldContainerEl) {
fieldView.parent = this;
fieldView.render();
this.fieldContainerEl.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);
},
render: function () {
if (this.rendered) return;
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 = true;
// add all our fields
(result(this, 'fields') || []).forEach(this.addField.bind(this));
}
});
FormView.extend = function (obj) {
var child = function () {
FormView.apply(this, arguments);
};
extend(child.prototype, FormView.prototype);
extend(child.prototype, obj);
return child;
};
module.exports = FormView;