forked from LeaVerou/bliss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbliss._.js
94 lines (74 loc) · 2.33 KB
/
bliss._.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
(function($) {
"use strict";
if (!Bliss || Bliss.shy) {
return;
}
var _ = Bliss.property;
// Methods requiring Bliss Full
$.add({
// Clone elements, with events
clone: function () {
var clone = this.cloneNode(true);
var descendants = $.$("*", clone).concat(clone);
$.$("*", this).concat(this).forEach(function(element, i, arr) {
$.events(descendants[i], element);
});
return clone;
}
}, {array: false});
// Define the _ property on arrays and elements
Object.defineProperty(Node.prototype, _, {
// Written for IE compatability (see #49)
get: function getter () {
Object.defineProperty(Node.prototype, _, {
get: undefined
});
Object.defineProperty(this, _, {
value: new $.Element(this)
});
Object.defineProperty(Node.prototype, _, {
get: getter
});
return this[_];
},
configurable: true
});
Object.defineProperty(Array.prototype, _, {
get: function () {
Object.defineProperty(this, _, {
value: new $.Array(this)
});
return this[_];
},
configurable: true
});
// Hijack addEventListener and removeEventListener to store callbacks
if (self.EventTarget && "addEventListener" in EventTarget.prototype) {
var addEventListener = EventTarget.prototype.addEventListener,
removeEventListener = EventTarget.prototype.removeEventListener,
filter = function(callback, capture, l){
return !(l.callback === callback && l.capture == capture);
};
EventTarget.prototype.addEventListener = function(type, callback, capture) {
if (this[_] && callback) {
var listeners = this[_].bliss.listeners = this[_].bliss.listeners || {};
listeners[type] = listeners[type] || [];
if (listeners[type].filter(filter.bind(null, callback, capture)).length === 0) {
listeners[type].push({callback: callback, capture: capture});
}
}
return addEventListener.call(this, type, callback, capture);
};
EventTarget.prototype.removeEventListener = function(type, callback, capture) {
if (this[_] && callback) {
var listeners = this[_].bliss.listeners = this[_].bliss.listeners || {};
listeners[type] = listeners[type] || [];
listeners[type] = listeners[type].filter(filter.bind(null, callback, capture));
}
return removeEventListener.call(this, type, callback, capture);
};
}
// Set $ and $$ convenience methods, if not taken
self.$ = self.$ || $;
self.$$ = self.$$ || $.$;
})(Bliss);