This repository has been archived by the owner on May 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
attribute.js
377 lines (279 loc) · 6.99 KB
/
attribute.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
* Module dependencies.
*/
var Emitter = require('emitter')
, event = require('event')
, domify = require('domify')
, templates = require('./template')
, type = require('type')
, minstache = require('minstache')
, val = require('val');
/**
* Expose `Attribute`.
*/
module.exports = Attribute;
/**
* safe options for the attribute schema
*/
var safe = ['options', 'type', 'title', 'repeat', 'properties'];
/**
* Initialize a new `Attribute` with a `name` and
* `properties`
*
* @param {String} name
* @param {Object} obj
* @api public
*/
function Attribute(name, schema) {
if (!name) throw Error('No name provided');
if (!schema) throw Error('No parameters provided');
for (var opt in schema) {
if (safe.indexOf(opt) == -1) continue;
this[opt] = schema[opt];
}
this.name = name;
}
/**
* Mixin emitter.
*/
Emitter(Attribute.prototype);
/**
* Creates elements for the particular type of fields
*
* @return {Attribute} self
* @api public
*/
Attribute.prototype.render = function() {
// check to see if repeats are enabled and then
// run .repeats()
if (this.repeat == "false") this.repeat = false;
if (this.repeat) {
this.view = this.repeats();
return this;
}
// check field types and run their associated render fns.
switch (this.type) {
case 'Date':
case 'Number':
case 'String':
// checks for specified options
this.view = this.options
? this.select()
: this.textbox();
break;
case 'Boolean':
this.view = this.checkbox();
break;
case 'Object':
this.view = this.object();
break;
}
return this;
}
/**
* Render `self` as a textbox and return dom
*
* @return {Element} dom
* @api private
*/
Attribute.prototype.textbox = function() {
var dom = domify(minstache(templates.textbox, this))[0];
this.node = dom.querySelector('input');
return dom;
}
/**
* Render `self` as a select box and return dom
*
* @return {Element} dom
* @api private
*/
Attribute.prototype.select = function() {
var dom = domify(minstache(templates.select, this))[0];
this.node = dom.querySelector('select');
val(this.node).options(this.options);
return dom;
};
/**
* Render `self` as a checkbox and return dom
*
* @return {Element} dom
* @api private
*/
Attribute.prototype.checkbox = function() {
var dom = domify(minstache(templates.checkbox, this))[0];
var input = this.node = dom.querySelector('input');
this.value = function(data){
return val(input).checked(data)
}
return dom;
}
/**
* Render `self` by iterating sub-properties
* and rendering their particular dom types
*
* @return {Element} dom
* @api private
*/
Attribute.prototype.object = function() {
var dom = domify(minstache(templates.object, this))[0]
this.node = dom.querySelector('.nested');
this.attributes = {};
for (var property in this.properties) {
var subParams = this.properties[property]
, subName = this.name + '.' + property
, subAttribute = new Attribute(subName, subParams);
this.node.appendChild(subAttribute.render().view);
this.attributes[property] = subAttribute;
}
this.value = objectValue.bind(this);
return dom;
}
/**
* Return new Attribute without repeat enabled
*
* @return {Attribute} attribute
* @api private
*/
Attribute.prototype.repeatAttribute = function(){
var name = this.name + this.repeatCount;
var attribute = new Attribute(name, this);
attribute.repeat = false;
return attribute.render();
}
/**
* Enables repeating of a certain attribute
*
* @return {Element} dom
* @api private
*/
Attribute.prototype.repeats = function() {
// set this.el to array
this.attributes = [];
this.value = arrayValue.bind(this);
// render array dom
var dom = domify(minstache(templates.repeats, this))[0];
// set container for repeats
this.repeatContainer = dom.querySelector('.repeats');
// bind click events
var add = dom.querySelector('.add');
event.bind(add, 'click', this.addRepeat.bind(this));
// set repeat count
this.repeatCount = 0;
// set repeat max by checking if Integer
if (max = parseInt(this.repeat)) {
this.repeatMax = max;
}
return dom;
}
/**
* Adds another field if multiples is enabled
*
* @return {Attribute} self
* @api private
*/
Attribute.prototype.addRepeat = function(){
var attribute = this.repeatAttribute()
, controls = domify(templates.controls)[0]
, add = controls.querySelector('.add')
, remove = controls.querySelector('.remove');
// only add repeat if within maximum
if (this.repeatMax) {
if (this.repeatCount >= this.repeatMax) return;
}
// adjust repeat count
this.repeatCount++;
// add nested attribute
this.attributes.push(attribute);
// create repeat container and append
// repeat clone and controls
var container = document.createElement('div');
container.className = 'repeat';
container.appendChild(attribute.node);
container.appendChild(controls);
// append container to repeatContainer
this.repeatContainer.appendChild(container);
// bind click events
event.bind(add, 'click', this.addRepeat.bind(this));
event.bind(remove, 'click', this.removeRepeat.bind(this, container, this.repeatCount - 1));
return this;
}
/**
* Removes `node` if multiples is enabled
*
* @param {Element} node
* @param {Integer} id
* @return {Attribute} self
* @api private
*/
Attribute.prototype.removeRepeat = function(node, id){
var add = node.querySelector('.add')
, remove = node.querySelector('.remove');
event.unbind(add);
event.unbind(remove);
this.repeatContainer.removeChild(node);
this.repeatCount--;
this.attributes.splice(id, 1);
return this;
}
/**
* Reset repeats by deleting dom and stuff
*
* @return {Attribute} self
* @api private
*/
Attribute.prototype.resetRepeats = function(){
this.repeatContainer.innerHTML = '';
this.repeatCount = 0;
this.attributes = [];
return this;
}
/**
* Set/Get value of current node
*
* @param {String} data
* @return {Attribute} self
* @api private
*/
Attribute.prototype.value = function(data) {
return val(this.node).value(data);
}
/**
* set and return value of nested attributes in object
*
* @return {Object} value
* @api private
*/
function objectValue(data){
value = {};
for (var attr in this.attributes) {
var attribute = this.attributes[attr];
if (data && data[attr]) {
value[attr] = attribute.value(data[attr]);
continue;
}
value[attr] = attribute.value();
}
return value;
}
/**
* set and return value of nested attributes in array
*
* @return {Array} value
* @api private
*/
function arrayValue(data){
// make sure enough repeated rows in place.
if (data && type(data) == 'array') {
this.resetRepeats();
for (var i = this.repeatCount; i < data.length; i++) {
this.addRepeat();
}
}
return this.attributes.map(function(attribute, index){
if (data && data[index]) {
return attribute.value(data[index]);
} else {
return attribute.value();
}
});
}