/
dit.js
477 lines (453 loc) · 14.9 KB
/
dit.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* dit | https://github.com/sofosogo/dit | MIT Licensed */
(function(){
var dit = window.dit = {
create: function( node, opt ){
node = parseNode( node );
node._opt = opt;
node._holders = {};
node._fields = {};
scan( node, opt, node._holders, node._fields, [] );
for( var k in this.proto ){
node[k] = this.proto[k];
}
return node;
},
proto: {
fill: function( data, append ){
hide( this );
fill( this._holders, this._fields, data || {}, this._opt, append );
return show( this );
},
append: function( data ){
return this.fill( data, true );
},
clean: function(){
hide( this );
clean( this._holders );
return show( this );
},
/* clone is not working in IE9 now.*/
clone: function(){
return this._html ? dit.create(this._html, this._opt) : null;
},
fetch: function(){
return fetch( this._fields );
},
opt: function( k, v ){
opt( this._opt, k, v );
return this;
}
}
},
alias = {
// imgsrc -> src, using imgsrc to avoid an error request.
"imgsrc": "src"
},
protos = {},
toString = Object.prototype.toString,
join = Array.prototype.join,
container = document.createElement("body"),
regex = /(\$?)\{\{\s*([\w\d\.]+)\s*:?\s*([^}]*)\}\}/ig;
function parseNode( node ){
var _n = node;
if( typeof node === "string" ){
container.innerHTML = node;
_n = container.firstChild;
_n._html = node;
}else if( node.jquery ){
_n = node[0];
}
if( _n && !_n._html ){
_n._html = _n.outerHTML;
}
return _n;
}
function fill( phs, fields, data, opt, append ){
var val, handlers;
for( var field in phs ){
val = evaluate( data, field );
if( opt && opt[field] !== void 0 ){
val = typeof opt[field] === "function" ? opt[field]( val, data ) : opt[field];
}
if( val === void 0 && append ) continue;
if( val === void 0 || val === null ) val = "";
// below is incorrect, because user action won't change phs[field].val.
// OR phs[field].val is a pointer;
// OR input etc. user can change its value manually.
// if( val === phs[field].val ) continue;
phs[field].val = val;
handlers = phs[field].handlers;
for( var i = 0; i < handlers.length; i++ ){
handlers[i].fill( val );
}
}
}
function clean( phs ){
var handlers;
for( var field in phs ){
handlers = phs[field].handlers;
phs[field].val = null;
for( var i = 0; i < handlers.length; i++ ){
handlers[i].clean();
}
}
}
function opt(opt, k, v ){
if( !k ) return ;
if( typeof k === "string" )
return opt[k] = v;
for( var i in k )
opt[i] = k[i];
}
function fetch( fields ){
var data = {};
for( var k in fields ){
assemble( data, k, fields[k].fetch() );
}
return data;
}
function scan( node, opt, phs, fields, prefix ){
if( node.nodeType === 3 ) return scanText( node, phs, prefix );
if( node.nodeType !== 1 ) return ;
if( node.getAttribute("bind") ){
prefix = prefix.concat( node.getAttribute("bind") );
node.removeAttribute("bind");
}
scanAttr( node, phs, prefix );
isFormField( node ) && scanFormField( node, phs, fields, prefix );
if( node.getAttribute("each") ){
var children = [];
while( node.hasChildNodes() ){
var child = node.removeChild(node.firstChild);
if (child.nodeType === 3 && /^\s*$/.test(child.nodeValue)) continue;
children.push(child);
}
var empty = children.length > 2 ? children.pop() : textNode("");
var handler = {
fill: function( arr ){
this.clean();
if( arr.jquery ) arr = arr[0];
if( arr.nodeType === 11 ) return this.node.appendChild(arr);
if( !arr || arr.length === 0 ) return this.node.appendChild(empty);
var num = children.length,
t;
for( var i = 0, j = 0; i < arr.length; i++ ){
t = children[i % num].cloneNode(true);
t = dit.create(t, opt).fill(arr[i]);
this.node.appendChild( t );
}
},
clean: function(){
this.node.innerHTML = "";
},
node: node
};
getHandlers(phs, prefix).push( handler );
return;
}
// node.childNodes changed by scanText function.
// fuck IE, we cannnot use the simpliest way below:
// var children = Array.prototype.slice.call( node.childNodes );
var children = [];
for( var i = 0, len = node.childNodes.length; i < len; i++ ){
children.push( node.childNodes[i] );
}
var child;
for( var i = 0, len = children.length; i < len; i++ ){
child = children[i];
if( child.nodeType === 1 ){
scan( child, opt, phs, fields, prefix );
}else if( child.nodeType === 3 ){
scanText( child, phs, prefix );
}
}
}
function scanAttr( node, phs, prefix ){
var attrs = node.attributes;
for( var i = 0; i < attrs.length; i++ ) {
var _ori = attrs[i].nodeValue,
_phs = [];
if( !_ori || attrs[i].name === "_html" || typeof _ori !== "string" ) continue;
_ori.replace(regex, function( match, has$, field, exp, startIdx ){
field = getField( prefix, field );
var ph = {f: field, m: match},
attr = attrs[i].nodeName,
attr = alias[attr] ? alias[attr] : attr,
handler = {
fill: function( v ){
var val = this._ori, ph, _tm;
for( var i = 0; i < this._phs.length; i++){
ph = this._phs[i];
_tm = phs[ph.f].val;
if( ph.convert )
_tm = ph.convert( _tm );
val = val.replace( ph.m, _tm );
}
attr === "class" ? node.className = val : node.setAttribute(attr, val);
},
clean: function(){
var val = this._ori.replace(regex, "");
attr === "class" ? node.className = val : node.setAttribute(attr, val);
},
_ori: _ori,
_phs: _phs
};
if( exp )
ph.convert = convert( field, exp );
_phs.push( ph );
getHandlers(phs, null, field).push( handler );
});
_phs = null;
}
}
function scanText( node, phs, prefix ){
var parent = node.parentNode;
var value = node.nodeValue;
var txt, idx = 0;
node.nodeValue.replace(regex, function( match, has$, field, exp, startIdx ){
if( txt = value.substring(idx, startIdx) ) parent.insertBefore( textNode(txt), node );
var dn = textNode( match );
var handler = {
fill: function( val ){
if( this.convert ) val = this.convert(val);
if( val.jquery ){
val = $.map(val.toArray(), function(it){return it;});
};
if( val.nodeType !== 1 && !isArray(val) || !has$ ) val = textNode( val );
var now = this._now;
if( isArray(now) ){
for( var i = 1; i < now.length; i++){
parent.removeChild( now[i] );
}
now = now[0];
}
if( isArray(val) ){
for( var i = 0; i < val.length; i++){
parent.insertBefore( val[i], now );
}
}else if(val){
parent.insertBefore( val, now );
}
parent.removeChild( now );
this._now = val;
},
clean: function(){
var now = this._now;
if( isArray(now) ){
for( var i = 1; i < now.length; i++){
parent.removeChild( now[i] );
}
now = now[0];
}
parent.replaceChild( this._empty, now );
this._now = this._empty;
},
_ori: dn,
_now: dn,
_empty: textNode("")
};
if( exp ) handler.convert = convert( field, exp );
getHandlers( phs, prefix, field ).push( handler );
parent.insertBefore( dn, node );
idx = startIdx + match.length;
});
if( txt = value.substring(idx) ) parent.insertBefore( textNode(txt), node );
parent.removeChild( node );
}
function scanFormField( node, phs, fields, prefix ){
var name = node.name,
// type = node.getAttribute("type"),
type = node.type,
isNum = node.getAttribute("number"),
key = getField( prefix, name ),
handler = fields[key];
// console.log(name + ': node.type = ' + node.type + ', node.getAttribute("type") = ' + node.getAttribute("type"))
if( !handler ){
handler = fields[key] = new (protos[type] || protos.normal)();
getHandlers( phs, key ).push( handler );
}
handler.isNum = handler.isNum || type === "number" || type ==="range" || isNum !== null;
if( type === "radio" || type === "checkbox" ){
if( !handler.nodes ) handler.nodes = [];
handler.nodes.push( node );
}else{
handler.nodes = node;
}
}
protos.normal = function(){};
protos.normal.prototype = {
fill: function( val ){
this.nodes.value = val || "";
},
clean: function(){
this.nodes.value = "";
},
fetch: function(){
return toNumber(this.nodes.value, this.isNum);
}
}
protos.radio = function(){};
protos.radio.prototype = {
fill: function( val ){
var nodes = this.nodes,
len = nodes.length,
v = "" + val;
for( var i = 0; i < len; i++ ){
nodes[i].checked = false;
if( nodes[i].value === v ){
nodes[i].checked = true;
}
}
},
clean: function(){
var nodes = this.nodes,
len = nodes.length;
for( var i = 0; i < len; i++ ){
nodes[i].checked = false;
}
},
fetch: function(){
var nodes = this.nodes,
len = nodes.length;
for( var i = 0; i < len; i++ ){
if( nodes[i].checked ){
return toNumber(nodes[i].value, this.isNum);
}
}
}
}
protos.checkbox = function(){};
protos.checkbox.prototype = {
fill: function( val ){
var nodes = this.nodes,
len = nodes.length;
!isArray( val ) && ( val = [val] );
for( var i = 0; i < len; i++ ){
nodes[i].checked = false;
for( var j = 0; val && j < val.length; j++ ){
if( nodes[i].value === "" + val[j] ) nodes[i].checked = true;
}
}
},
clean: function(){
var nodes = this.nodes,
len = nodes.length;
for( var i = 0; i < len; i++ ){
nodes[i].checked = false;
}
},
fetch: function(){
var nodes = this.nodes,
len = nodes.length;
var arr = [];
for( var i = 0; i < len; i++ ){
if( nodes[i].checked ){
arr.push( toNumber(nodes[i].value, this.isNum) );
}
}
if( len === 1 ) return arr[0];
return arr;
}
}
protos["select-multiple"] = function(){};
protos["select-multiple"].prototype = {
fill: function( val ){
var options = this.nodes.childNodes,
len = options.length;
for( var i = 0; i < len; i++ ){
if( options[i].nodeType !== 1 ) continue;
options[i].setAttribute("selected", "");
options[i].selected = false;
for( var j = 0; val && j < val.length; j++ ){
if( options[i].nodeType === 1 && options[i].value === val[j] ){
options[i].setAttribute("selected", "true"); // IE
options[i].selected = true; // FF and other
}
}
}
},
clean: function(){
var options = this.nodes.childNodes,
len = options.length;
for( var i = 0; i < len; i++ ){
if( options[i].nodeType === 1 ){
options[i].setAttribute("selected", ""); // IE
options[i].selected = false; // FF and other
}
}
},
fetch: function(){
var options = this.nodes.childNodes,
len = options.length,
arr = [];
for( var i = 0; i < len; i++ ){
if( options[i].nodeType === 1 && options[i].selected ){
val = options[i].value;
arr.push( toNumber(options[i].value, this.isNum) );
}
}
return arr;
}
}
function convert( field, exp ){
return new Function( 'v', 'return ' + exp );
}
function toNumber( val, isNum ){
isNum && ( val = parseInt(val) );
return val !== val ? void 0 : val; // val !== val <==> typeof val === "number" && isNaN(val)
}
function isFormField( node ){
var tagName = node.tagName.toLowerCase();
if( tagName === "input" || tagName === "textarea" || tagName === "select" ){
var type = node.type;
return type !== "image" && type !== "file" && type !== "button" && type !== "reset" && type !== "submit";
}
return false;
}
function getField( prefix, field ){
isArray( prefix ) && ( prefix = prefix.join(".") );
return prefix && field ? (prefix + "." + field) : (prefix || field);
}
function getHandlers( phs, prefix, field ){
field = getField( prefix, field );
if( !phs[field] ) phs[field] = { handlers: [] };
return phs[field].handlers;
}
function textNode( text ){
return document.createTextNode("" + text);
}
function isArray( obj ){
return obj && toString.call(obj) === "[object Array]";
}
function hide( node ){
node.oldCssText = node.style.cssText;
node.style.display = "none";
return node;
}
function show( node ){
node.style.cssText = node.oldCssText;
return node;
}
function evaluate( ctx, exp ){
if( !ctx || !exp ) return null;
var fs = exp.split("."),
data = ctx, i;
for( i = 0; data && i < fs.length; i++ ){
if( fs[i] ) data = data[ fs[i] ];
}
return data;
}
function assemble( data, field, val ){
if( !data || !field ) return null;
var fs = field.split(".");
var temp = data,
len = fs.length;
for( var i = 0; i < len - 1; i++ ){
if( !fs[i] ) continue;
temp[ fs[i] ] = temp[ fs[i] ] || {};
temp = temp[ fs[i] ];
}
temp[ fs[len-1] ] = val;
return data;
}
})();