-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathmatreshka.js
3145 lines (2683 loc) · 76.3 KB
/
matreshka.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Matreshka v1.0.6 (2015-05-19)
JavaScript Framework by Andrey Gubanov
Released under the MIT license
More info: http://matreshka.io
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('xclass',factory);
} else {
// Browser globals
root.Class = factory();
}
}(this, function () {
var isArguments = function( o ) {
return !!o && ( o.toString() === '[object Arguments]' || typeof o === 'object' && o !== null && 'length' in o && 'callee' in o );
},
ie = (function() {
// Returns the version of Internet Explorer or a -1 (indicating the use of another browser).
var rv = -1,
ua, re;
if ( navigator.appName == 'Microsoft Internet Explorer' ) {
ua = navigator.userAgent;
re = new RegExp( 'MSIE ([0-9]{1,}[\.0-9]{0,})' );
if ( re.exec(ua) != null ) {
rv = parseFloat( RegExp.$1 );
}
}
return rv;
})(),
ieDocumentMode = document.documentMode,
ie8 = ieDocumentMode === 8,
err = 'Internet Explorer ' + ie + ' doesn\'t support Class function';
if( ~ie && ie < 8 ) {
throw Error( err );
} else if( ieDocumentMode < 8 ) {
throw Error( err + '. Switch your "Document Mode" to "Standards"' );
}
var Class = function( prototype ) {
var constructor = realConstructor = prototype.constructor !== Object ? prototype.constructor : function EmptyConstructor() {},
extend = prototype[ 'extends' ] = prototype[ 'extends' ] || prototype.extend,
extend_prototype = extend && extend.prototype,
implement = prototype[ 'implements' ] = prototype[ 'implements' ] || prototype.implement,
realConstructor = constructor,
parent = {};
delete prototype.extend;
delete prototype.implement;
if( extend_prototype ) {
for( var key in extend_prototype ) {
parent[ key ] = typeof extend_prototype[ key ] === 'function' ? ( function( value ) {
return function( context, args ) {
args = isArguments( args ) ? args : Array.prototype.slice.call( arguments, 1 );
return value.apply( context, args );
}
})( extend_prototype[ key ] ) : extend_prototype[ key ];
}
parent.constructor = ( function( value ) {
return function( context, args ) {
args = isArguments( args ) ? args : Array.prototype.slice.call( arguments, 1 );
return value.apply( context, args );
}
})( extend_prototype.constructor );
}
if( ie8 ) {
prototype.prototype = null;
prototype.constructor = null;
constructor = function() {
if( this instanceof constructor ) {
var r = new XDomainRequest;
for( var p in constructor.prototype ) if( p !== 'constructor' ) {
r[ p ] = constructor.prototype[ p ];
}
r.hasOwnProperty = constructor.prototype.hasOwnProperty;
realConstructor.apply( r, arguments );
return r;
} else {
realConstructor.apply( this, arguments );
}
};
prototype.constructor = constructor;
constructor.prototype = constructor.fn = prototype;
constructor.parent = parent;
extend && Class.IEInherits( constructor, extend );
} else {
prototype.constructor = constructor;
constructor.prototype = constructor.fn = prototype;
constructor.parent = parent;
extend && Class.inherits( constructor, extend );
}
implement && implement.validate( constructor.prototype );
constructor.same = function() {
return function() {
return constructor.apply( this, arguments );
};
};
if( this instanceof Class ) {
return new constructor;
} else {
return constructor;
}
};
Class.inherits = function( Child, Parent ) {
var prototype = Child.prototype,
F = function() {};
F.prototype = Parent.prototype;
Child.prototype = new F;
Child.prototype.constructor = Child;
for( var m in prototype ) {
Child.prototype[ m ] = prototype[ m ];
};
if( typeof Symbol != 'undefined' && prototype[ Symbol.iterator ] ) {
Child.prototype[ Symbol.iterator ] = prototype[ Symbol.iterator ];
}
Child.prototype.instanceOf = function( _Class ) {
return this instanceof _Class;
}
};
Class.IEInherits = function( Child, Parent ) {
var childHasOwn = Child.prototype.hasOwnProperty,
childConstructor = Child.prototype.constructor,
parentHasOwn,
objectHasOwn = Object.prototype.hasOwnProperty;
while ( Parent ) {
parentHasOwn = parentHasOwn || Parent.prototype.hasOwnProperty,
Child.prototype = ( function( pp, cp ) { // extending
var o = {},
i;
for( i in pp ) {
o[ i ] = pp[ i ]
}
for( i in cp ) {
o[ i ] = cp[ i ]
}
return o;
})( Parent.prototype, Child.prototype );
Parent = Parent.prototype && Parent.prototype[ 'extends' ] && Parent.prototype[ 'extends' ].prototype;
}
if( childHasOwn !== objectHasOwn ) {
Child.prototype.hasOwnProperty = childHasOwn;
} else if( parentHasOwn !== objectHasOwn ) {
Child.prototype.hasOwnProperty = parentHasOwn;
}
Child.prototype.constructor = childConstructor;
Child.prototype.instanceOf = function( _Class ) {
var PossibleParent = Child;
while( PossibleParent ) {
if( PossibleParent === _Class ) {
return true;
}
PossibleParent = PossibleParent.prototype[ 'extends' ]
}
return false;
}
};
Class.Interface = function Interface( parent, props ) {
var propsMap = {},
isArray = function( probArray ) {
return typeof probArray === 'object' && probArray !== null && 'length' in probArray;
},
properties,
list;
if( parent instanceof Interface ) {
for( var i in parent.propsMap ) propsMap[ i ] = 1;
properties = isArray( props ) ? props : [].slice.call( arguments, 1 );
} else {
properties = isArray( parent ) ? parent : arguments;
}
for( i = 0; i < properties.length; i++ ) {
propsMap[ properties[ i ] ] = 1;
}
this.propsMap = propsMap;
this.validate = function( prototype ) {
for( var i in this.propsMap ) {
if( typeof prototype[ i ] !== 'function' ) {
throw Error( 'Interface error: Method "' + i + '" is not implemented in '+ (prototype.constructor.name || prototype.name || 'given') +' prototype' );
}
}
}
};
Class.isXDR = ie8;
return Class;
}));
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('matreshka_dir/polyfills/addeventlistener',factory);
} else {
factory();
}
}(this, function () {
( function( win, doc, s_add, s_rem ) {
if( doc[s_add] ) return;
Element.prototype[ s_add ] = win[ s_add ] = doc[ s_add ] = function( on, fn, self ) {
return (self = this).attachEvent( 'on' + on, function(e){
var e = e || win.event;
e.target = e.target || e.srcElement;
e.preventDefault = e.preventDefault || function(){e.returnValue = false};
e.stopPropagation = e.stopPropagation || function(){e.cancelBubble = true};
e.which = e.button ? ( e.button === 2 ? 3 : e.button === 4 ? 2 : e.button ) : e.keyCode;
fn.call(self, e);
});
};
Element.prototype[ s_rem ] = win[ s_rem ] = doc[ s_rem ] = function( on, fn ) {
return this.detachEvent( 'on' + on, fn );
};
})( window, document, 'addEventListener', 'removeEventListener' );
}));
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('balalaika', [
'matreshka_dir/polyfills/addeventlistener'
], factory);
} else {
root.$b = factory();
}
}(this, function () {
// nsRegAndEvents is regesp for eventname.namespace and the list of all events
// fn is empty array and balalaika prototype
return ( function( window, document, fn, nsRegAndEvents, id, s_EventListener, s_MatchesSelector, i, j, k, l, $ ) {
$ = function( s, context ) {
return new $.i( s, context );
};
$.i = function( s, context ) {
fn.push.apply( this, !s ? fn : s.nodeType || s == window ? [s] : "" + s === s ? /</.test( s )
? ( ( i = document.createElement( context || 'div' ) ).innerHTML = s, i.children ) : (context&&$(context)[0]||document).querySelectorAll(s) : /f/.test(typeof s) ? /c/.test(document.readyState) ? s() : $(document).on('DOMContentLoaded', s) : s );
};
$.i[ l = 'prototype' ] = ( $.extend = function(obj) {
k = arguments;
for( i = 1; i < k.length; i++ ) {
if ( l = k[ i ] ) {
for (j in l) {
obj[j] = l[j];
}
}
}
return obj;
})( $.fn = $[ l ] = fn, { // $.fn = $.prototype = fn
on: function( n, f ) {
// n = [ eventName, nameSpace ]
n = n.split( nsRegAndEvents );
this.map( function( item ) {
// item.b$ is balalaika_id for an element
// i is eventName + id ("click75")
// nsRegAndEvents[ i ] is array of events (eg all click events for element#75) ([[namespace, handler], [namespace, handler]])
( nsRegAndEvents[ i = n[ 0 ] + ( item.b$ = item.b$ || ++id ) ] = nsRegAndEvents[ i ] || [] ).push([f, n[ 1 ]]);
// item.addEventListener( eventName, f )
item[ 'add' + s_EventListener ]( n[ 0 ], f );
});
return this;
},
off: function( n, f ) {
// n = [ eventName, nameSpace ]
n = n.split( nsRegAndEvents );
// l = 'removeEventListener'
l = 'remove' + s_EventListener;
this.map( function( item ) {
// k - array of events
// item.b$ - balalaika_id for an element
// n[ 0 ] + item.b$ - eventName + id ("click75")
k = nsRegAndEvents[ n[ 0 ] + item.b$ ];
// if array of events exist then i = length of array of events
if( i = k && k.length ) {
// while j = one of array of events
while( j = k[ --i ] ) {
// if( no f and no namespace || f but no namespace || no f but namespace || f and namespace )
if( ( !f || f == j[ 0 ] ) && ( !n[ 1 ] || n[ 1 ] == j[ 1 ] ) ) {
// item.removeEventListener( eventName, handler );
item[ l ]( n[ 0 ], j[ 0 ] );
// remove event from array of events
k.splice( i, 1 );
}
}
} else {
// if event added before using addEventListener, just remove it using item.removeEventListener( eventName, f )
!n[ 1 ] && item[ l ]( n[ 0 ], f );
}
});
return this;
},
is: function( s ) {
i = this[ 0 ];
j = !!i && ( i.matches
|| i[ 'webkit' + s_MatchesSelector ]
|| i[ 'moz' + s_MatchesSelector ]
|| i[ 'ms' + s_MatchesSelector ] );
return !!j && j.call( i, s );
}
});
return $;
})( window, document, [], /\.(.+)/, 0, 'EventListener', 'MatchesSelector' );
}));
// taken from https://github.com/remy/polyfills and modified
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('matreshka_dir/polyfills/classlist',factory);
} else {
factory();
}
}(this, function () {
var toggle = function (token, force) {
if( typeof force === 'boolean' ) {
this[ force ? 'add' : 'remove' ](token);
} else {
this[ !this.contains(token) ? 'add' : 'remove' ](token);
}
return this.contains(token);
};
if( window.DOMTokenList ) {
var a = document.createElement( 'a' );
a.classList.toggle( 'x', false );
if( a.className ) {
window.DOMTokenList.prototype.toggle = toggle;
}
}
if (typeof window.Element === "undefined" || "classList" in document.documentElement) return;
var prototype = Array.prototype,
push = prototype.push,
splice = prototype.splice,
join = prototype.join;
function DOMTokenList(el) {
this.el = el;
// The className needs to be trimmed and split on whitespace
// to retrieve a list of classes.
var classes = el.className.replace(/^\s+|\s+$/g, '').split(/\s+/);
for (var i = 0; i < classes.length; i++) {
push.call(this, classes[i]);
}
};
DOMTokenList.prototype = {
add: function (token) {
if (this.contains(token)) return;
push.call(this, token);
this.el.className = this.toString();
},
contains: function (token) {
return this.el.className.indexOf(token) != -1;
},
item: function (index) {
return this[index] || null;
},
remove: function (token) {
if (!this.contains(token)) return;
for (var i = 0; i < this.length; i++) {
if (this[i] == token) break;
}
splice.call(this, i, 1);
this.el.className = this.toString();
},
toString: function () {
return join.call(this, ' ');
},
toggle: toggle
};
window.DOMTokenList = DOMTokenList;
function defineElementGetter(obj, prop, getter) {
if (Object.defineProperty) {
Object.defineProperty(obj, prop, {
get: getter
});
} else {
obj.__defineGetter__(prop, getter);
}
}
defineElementGetter(Element.prototype, 'classList', function () {
return new DOMTokenList(this);
});
}));
( function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('matreshka_dir/balalaika-extended',[ 'balalaika', 'matreshka_dir/polyfills/classlist', ], factory);
} else {
factory( root.$b );
}
}(this, function ( $b ) {
var s_classList = 'classList',
_on, _off;
if( !$b ) {
throw new Error( 'Balalaika is missing' );
}
_on = $b.fn.on;
_off = $b.fn.off;
$b.extend( $b.fn, {
on: function( n, f ) {
n.split( /\s/ ).forEach( function( n ) {
_on.call( this, n, f );
}, this );
return this;
},
off: function( n, f ) {
n.split( /\s/ ).forEach( function( n ) {
_off.call( this, n, f );
}, this );
return this;
},
hasClass: function( className ) { return !!this[ 0 ] && this[ 0 ][ s_classList ].contains( className ); },
addClass: function( className ) {
this.forEach( function( item ) {
var classList = item[ s_classList ];
classList.add.apply( classList, className.split( /\s/ ) );
});
return this;
},
removeClass: function( className ) {
this.forEach( function( item ) {
var classList = item[ s_classList ];
classList.remove.apply( classList, className.split( /\s/ ) );
});
return this;
},
toggleClass: function( className, b ) {
this.forEach( function( item ) {
var classList = item[ s_classList ];
if( typeof b !== 'boolean' ) {
b = !classList.contains( className );
}
classList[ b ? 'add' : 'remove' ].apply( classList, className.split( /\s/ ) );
});
return this;
},
add: function( s ) {
var result = $b( this ),
ieIndexOf = function( a, e ) {
for( j = 0; j < a.length; j++ ) if( a[ j ] === e ) return j;
},
i, j;
s = $b( s ).slice();
[].push.apply( result, s );
for( i = result.length - s.length; i < result.length; i++ ) {
if( ( [].indexOf ? result.indexOf( result[ i ] ) : ieIndexOf( result, result[ i ] ) ) !== i ) { // @IE8
result.splice( i--, 1 );
}
}
return result;
},
not: function( s ) {
var result = $b( this ),
index,
i;
s = $b( s );
for( i = 0; i < s.length; i++ ) {
if( ~( index = result.indexOf( s[ i ] ) ) ) {
result.splice( index, 1 );
}
}
return result;
},
find: function( s ) {
var result = $b();
this.forEach( function( item ) {
result = result.add( $b( s, item ) );
});
return result;
}
});
// simple html parser
$b.parseHTML = function( html ) {
var node = document.createElement( 'div' ),
// wrapMap is taken from jQuery
wrapMap = {
option: [ 1, "<select multiple='multiple'>", "</select>" ],
legend: [ 1, "<fieldset>", "</fieldset>" ],
thead: [ 1, "<table>", "</table>" ],
tr: [ 2, "<table><tbody>", "</tbody></table>" ],
td: [ 3, "<table><tbody><tr>", "</tr></tbody></table>" ],
col: [ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ],
area: [ 1, "<map>", "</map>" ],
_: [ 0, "", "" ]
},
wrapper,
i;
html = html.replace( /^\s+|\s+$/g, '' );
wrapMap.optgroup = wrapMap.option;
wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead;
wrapMap.th = wrapMap.td;
wrapper = wrapMap[ /<([\w:]+)/.exec( html )[ 1 ] ] || wrapMap._;
node.innerHTML = wrapper[ 1 ] + html + wrapper[ 2 ];
i = wrapper[ 0 ];
while( i-- ) {
node = node.children[ 0 ];
}
return $b( node.children );
};
$b.create = function( tagName, props ) {
var el = document.createElement( tagName ),
i, j;
if( props ) for( i in props ) {
if( i == 'attributes' && typeof props[ i ] == 'object' ) {
for( j in props[ i ] ) if( props[ i ].hasOwnProperty( j ) ) {
el.setAttribute( j, props[ i ][ j ] );
}
} else if( el[ i ] && typeof props == 'object' ) {
el[ i ] = $b.extend( el[ i ] || {}, props[ i ] );
} else {
el[ i ] = props[ i ];
}
}
return el;
};
// @IE8 Balalaika fix. This browser doesn't support HTMLCollection and NodeList as second argument for .apply
// This part of code will be removed in Matreshka 1.0
(function( document, $, i, j, k, fn ) {
var bugs,
children = document.createElement( 'div' ).children;
try { [].push.apply( [], children ); }
catch( e ) { bugs = true; }
bugs = bugs || typeof children === 'function' || document.documentMode < 9;
if( bugs ) {
fn = $.i[ j = 'prototype' ];
$.i = function( s, context ) {
k = !s ? fn : s && s.nodeType || s == window ? [s] : typeof s == 'string' ? /</.test( s ) ? ( ( i = document.createElement( 'div' ) ).innerHTML = s, i.children ) : (context&&$(context)[0]||document).querySelectorAll(s) : /f/.test(typeof s) && (!s[0]&&!s[0].nodeType) ? /c/.test(document.readyState) ? s() : !function r(f){/in/(document.readyState)?setTimeout(r,9,f):f()}(s): s;
j = []; for (i = k ? k.length : 0; i--; j[i] = k[i]) {}
fn.push.apply( this, j );
};
$.i[ j ] = fn;
fn.is = function( selector ) {
var elem = this[ 0 ],
elems = elem.parentNode.querySelectorAll( selector ),
i;
for ( i = 0; i < elems.length; i++ ) { if( elems[ i ] === elem ) return true; }
return false;
};
}
return $;
})( document, $b );
return $b;
}));
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('matreshka_dir/dollar-lib',['matreshka_dir/balalaika-extended'], factory);
} else {
root.__DOLLAR_LIB = factory( root.$b );
}
}(this, function ( $b ) {
var neededMethods = 'on off is hasClass addClass removeClass toggleClass add not find'.split( /\s+/ ),
dollar = typeof $ == 'function' ? $ : null,
useDollar = true,
i;
if( dollar ) {
for( i = 0; i < neededMethods.length; i++ ) {
if( !dollar.prototype[ neededMethods[ i ] ] ) {
useDollar = false;
break;
}
}
if( !dollar.parseHTML ) {
useDollar = false;
}
} else {
useDollar = false;
}
return useDollar ? dollar : $b;
}));
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define( 'matreshka_dir/binders',factory );
} else {
root.__MK_BINDERS = factory();
}
}(this, function ( MK ) {
var oneWayBinder = function( f ) {
return { on: null, getValue: null, setValue: f };
},
binders;
return binders = {
innerHTML: function() {// @IE8
return oneWayBinder( function( v ) {
this.innerHTML = v === null ? '' : v + '';
});
},
className: function( className ) {
var not = !className.indexOf( '!' );
if( not ) {
className = className.replace( '!', '' );
}
return oneWayBinder( function( v ) {
this.classList.toggle( className, not ? !v : !!v );
});
},
property: function( propertyName ) {
return oneWayBinder( function( v ) {
this[ propertyName ] = v;
});
},
attribute: function( attributeName ) {
return oneWayBinder( function( v ) {
this.setAttribute( attributeName, v );
});
},
textarea: function() {
return binders.input( 'text' );
},
progress: function() {
return binders.input();
},
input: function( type ) {
var on;
switch( type ) {
case 'checkbox':
return {
on: 'click keyup',
getValue: function() { return this.checked; },
setValue: function( v ) { this.checked = v; }
};
case 'radio':
return {
on: 'click keyup',
getValue: function() { return this.value; },
setValue: function( v ) {
this.checked = this.value == v;
}
};
case 'submit':
case 'button':
case 'image':
case 'reset':
return {};
case 'hidden':
on = null;
break;
case 'file':
on = 'change';
break;
case 'text':
case 'password':
// IE8 requires to use 'keyup paste' instead of 'input'
on = document.documentMode == 8 ? 'keyup paste' : 'input';
break;
/* case 'date':
case 'datetime':
case 'datetime-local':
case 'month':
case 'time':
case 'week':
case 'file':
case 'range':
case 'color':
case 'search':
case 'email':
case 'tel':
case 'url':
case 'number': */
default: // other future (HTML6+) inputs
on = 'input';
}
return {
on: on,
getValue: function() { return this.value; },
setValue: function( v ) {
if( this.value != v ) {
this.value = v;
}
}
}
},
select: function( multiple ) {
var i;
if( multiple ) {
return {
on: 'change',
getValue: function() {
return [].slice.call( this.options )
.filter( function( o ) { return o.selected; })
.map( function( o ) { return o.value; });
},
setValue: function( v ) {
v = typeof v == 'string' ? [ v ] : v;
for( i = this.options.length - 1; i >= 0; i-- ) {
this.options[ i ].selected = ~v.indexOf( this.options[ i ].value );
}
}
};
} else {
return {
on: 'change',
getValue: function() { return this.value; },
setValue: function( v ) {
var _this = this,
options;
_this.value = v;
if( !v ) {
options = _this.options;
for( i = options.length - 1; i >= 0; i-- ) {
if( !options[ i ].value ) {
options[ i ].selected = true;
}
}
}
}
};
}
},
visibility: function( value ) {
value = typeof value == 'undefined' ? true : value;
return oneWayBinder( function( v ) {
this.style.display = value ? ( v ? '' : 'none' ) : ( v ? 'none' : '' );
});
}
};
}));
(function (root, factory) {
if (typeof define == 'function' && define.amd) {
define('matreshka_dir/matreshka-core',[
'xclass',
'balalaika',
'matreshka_dir/dollar-lib',
'matreshka_dir/binders'
], factory);
} else {
root.MK = root.Matreshka = factory( root.Class, root.$b, root.__DOLLAR_LIB, root.__MK_BINDERS );
}
}(this, function ( Class, $b, $, binders ) {
if( !Class ) {
throw Error( 'Class function is missing' );
}
if( ![].forEach ) {
throw Error( 'Internet Explorer 8 requires to use es5-shim: https://github.com/es-shims/es5-shim' );
}
/**
* @private
* @since 0.0.4
* @todo optimize
* @summary This object is used to map DOM nodes and their DOM events
*/
var domEventsMap = {
list: {},
// adds events to the map
add: function( o ) {
if( o.node ) {
if( typeof o.on == 'function' ) {
o.on.call( o.node, o.handler );
} else {
$( o.node ).on( o.on.split( /\s/ ).join( '.mk ' ) + '.mk', o.handler );
}
}
( this.list[ o.instance.__id ] = this.list[ o.instance.__id ] || [] ).push( o );
},
// removes events from the map
remove: function( o ) {
var evts = this.list[ o.instance.__id ],
evt, i;
if( !evts ) return;
for( i = 0; i < evts.length; i++ ) {
evt = evts[ i ];
if( evt.node !== o.node ) continue;
// remove Matreshka event
evt.mkHandler && o.instance._off( '_runbindings:' + o.key, evt.mkHandler );
// remove DOM event
$( o.node ).off( evt.on + '.mk', evt.handler );
this.list[ o.instance.__id ].splice( i--, 1 );
}
}
},
slice = [].slice,
trim = function( s ) { return s.trim ? s.trim() : s.replace(/^\s+|\s+$/g, '') },
/**
* @private
* @summary selectNodes selects nodes match to custom selectors such as :sandbox and :bound(KEY)
*/
selectNodes = function( _this, s ) {
var result = $(),
execResult,
bound,
selector;
// replacing :sandbox to :bound(sandbox)
s.replace( /:sandbox/g, ':bound(sandbox)' ).split( ',' ).forEach( function( s ) {
// if selector contains ":bound(KEY)" substring
if( execResult = /:bound\(([^(]*)\)(.*)/.exec( trim(s) ) ) {
// getting KEY from :bound(KEY)
bound = _this.$bound( execResult[1] );
// if native selector passed after :bound(KEY) is not empty string
// for example ":bound(KEY) .my-selector"
if( selector = trim( execResult[2] ) ) {
// if native selector contains children selector
// for example ":bound(KEY) > .my-selector"
if( selector.indexOf( '>' ) == 0 ) {
// selecting children
each( bound, function( node ) {
var r = MK.randomString();
node.setAttribute( r, r );
result = result.add( $( '['+r+'="'+r+'"]' + selector, node ) );
node.removeAttribute( r );
});
} else {
// if native selector doesn't contain children selector
result = result.add( bound.find( selector ) );
}
} else {
// if native selector is empty string
result = result.add( bound );
}
// if it's native selector
} else {
result = result.add( s );
}
});
return result;
};
var MK = Class({
//__special: null, // { <key>: { getter: f, $nodes: jQ, value: 4 }}
//__events: null,
isMK: true,
/**
* @private
* @member {boolean} Matreshka#isMKInitialized
* @summary Using for lazy initialization
*/
isMKInitialized: false,
on: function( names, callback, triggerOnInit, context, xtra ) {
var _this = this._initMK(),
t, i;
// if event-callback object is passed to the function
if( typeof names == 'object' && !(names instanceof Array) ) {
for( i in names ) if( names.hasOwnProperty( i ) ) {
_this.on( i, names[ i ], callback, triggerOnInit );
}
return _this;
}
// callback is required
if( !callback ) throw Error( 'callback is not function for event(s) "'+names+'"' );
names = names instanceof Array ? names : trim( names )
.replace( /\s+/g, ' ' ) // single spaces only
.split( /\s(?![^(]*\))/g ) // split by spaces
;
// allow to flip triggerOnInit and context
if( typeof triggerOnInit != 'boolean' && typeof triggerOnInit != 'undefined' ) {
t = context;
context = triggerOnInit;
triggerOnInit = t;
}
// for every name call _on method
for( i = 0; i < names.length; i++ ) {
_this._on( names[ i ], callback, context, xtra );
}
// trigger after event is initialized
if( triggerOnInit === true ) {
callback.call( context || _this, {
triggeredOnInit: true
});
}
return _this;
},
onDebounce: function( names, callback, debounceDelay, triggerOnInit, context, xtra ) {
var cbc;
// flip args
if( typeof debounceDelay != 'number' ) {
xtra = context;
context = triggerOnInit;
triggerOnInit = debounceDelay;
debounceDelay = 0;
};
cbc = MK.debounce( callback, debounceDelay );
// set reference to real callback for .off method
cbc._callback = callback;
return this.on( names, cbc, triggerOnInit, context, xtra );
},
// @TODO refactor
_on: function( name, callback, context, xtra ) {
// index of @
var indexOfET = name.indexOf( '@' ),
_this = this._initMK(),
ctx = context || _this,
delegatedReg = /^(.*?)\((.*)\)/,
evtName,
selector,
key_selector,
events,
ev,
key,
changeHandler, bindHandler, unbindHandler,
domEvtHandler, tmp, domEvtName;
// if @ exists in event name
if( ~indexOfET ) {
// a@b -> key=a name=b
key = name.slice( 0, indexOfET );
name = name.slice( indexOfET + 1 );
// called when value of delegated property is changed
changeHandler = function( evt ) {
var target = _this[ key ],
handler;
// if new value is Matreshka instance
if( target && target.isMK ) {
handler = function( evt ) {
if( !evt || !evt.private ) {
callback.apply( this, arguments );
}
};
handler._callback = callback;
// add event to the new value
target._on( name, handler, ctx );
}
// remove event handler from previous value
if( evt && evt.previousValue && evt.previousValue.isMK ) {
evt.previousValue._off( name, callback, context );
}
};