-
Notifications
You must be signed in to change notification settings - Fork 29
/
balalaika.js
75 lines (72 loc) · 2.75 KB
/
balalaika.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
window.$ = ( 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 || 'q' ) ).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 ];
return (i.matches
|| i[ 'webkit' + s_MatchesSelector ]
|| i[ 'moz' + s_MatchesSelector ]
|| i[ 'ms' + s_MatchesSelector ]
|| i[ 'o' + s_MatchesSelector ]).call( i, s );
}
});
return $;
})( window, document, [], /\.(.+)/, 0, 'EventListener', 'MatchesSelector' )