-
Notifications
You must be signed in to change notification settings - Fork 32
/
EventListener.oldie.js
140 lines (109 loc) · 3.44 KB
/
EventListener.oldie.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
// EventListener | CC0 | github.com/jonathantneal/EventListener
!this.addEventListener && this.attachEvent && (function (window, document) {
var registry = [];
// add
function addEventListener(type, listener) {
var target = this;
registry.unshift({
__listener: function (event) {
event.currentTarget = target;
event.pageX = event.clientX + document.documentElement.scrollLeft;
event.pageY = event.clientY + document.documentElement.scrollTop;
event.preventDefault = function () { event.returnValue = false };
event.relatedTarget = event.fromElement || null;
event.stopPropagation = function () { event.cancelBubble = true };
event.relatedTarget = event.fromElement || null;
event.target = event.srcElement || target;
event.timeStamp = +new Date;
listener.call(target, event);
},
listener: listener,
target: target,
type: type
});
target.attachEvent("on" + type, registry[0].__listener);
}
// remove
function removeEventListener(type, listener) {
for (var index = 0, length = registry.length; index < length; ++index) {
if (registry[index].target == this && registry[index].type == type && registry[index].listener == listener) {
return this.detachEvent("on" + type, registry.splice(index, 1)[0].__listener);
}
}
}
// dispatch
function dispatchEvent(eventObject) {
try {
return this.fireEvent("on" + eventObject.type, eventObject);
} catch (error) {
for (var index = 0, length = registry.length; index < length; ++index) {
if (registry[index].target == this && registry[index].type == eventObject.type) {
registry[index].__listener.call(this, eventObject);
}
}
}
}
// custom
function CustomEvent(type, canBubble, cancelable, detail) {
var event = document.createEventObject(), key;
event.type = type;
event.returnValue = !cancelable;
event.cancelBubble = !canBubble;
for (key in detail) {
event[key] = detail[key];
}
return event;
}
function _patchNode(node) {
if (node.dispatchEvent) {
return;
}
node.addEventListener = addEventListener;
node.removeEventListener = removeEventListener;
node.dispatchEvent = dispatchEvent;
var appendChild = node.appendChild, createElement = node.createElement, insertBefore = node.insertBefore;
if (appendChild) {
node.appendChild = function (node) {
var returnValue = appendChild(node);
_patchNodeList(node.all);
return returnValue;
};
}
if (createElement) {
node.createElement = function (nodeName) {
var returnValue = createElement(nodeName);
_patchNodeList(node.all);
return returnValue;
};
}
if (insertBefore) {
node.insertBefore = function (node, referenceElement) {
var returnValue = insertBefore(node, referenceElement);
_patchNodeList(node.all);
return returnValue;
};
}
if ("innerHTML" in node) {
node.attachEvent("onpropertychange", function (event) {
if (event.propertyName != "innerHTML") return;
_patchNodeList(node.all);
});
}
}
function _patchNodeList(nodeList) {
for (var i = 0, node; node = nodeList[i]; ++i) {
_patchNode(node);
}
}
document.attachEvent("onreadystatechange", function (event) {
if (document.readyState == "complete") {
_patchNodeList(document.all);
// ready
document.dispatchEvent(new CustomEvent("DOMContentLoaded", false, false));
}
});
_patchNode(window);
_patchNode(document);
_patchNodeList(document.all);
window.CustomEvent = CustomEvent;
})(this, document);