-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
248 lines (219 loc) · 6.19 KB
/
index.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
var React = require('react');
// dom
// event
// children
var dom = {
getWindow:function(componentOrElement){
var elem = React.findDOMNode(componentOrElement);
return (elem != null && elem == elem.window) ?
elem :
elem.nodeType === 9 ?
elem.defaultView || elem.parentWindow :
false;
},
getDocument:function(componentOrElement){
var elem = React.findDOMNode(componentOrElement);
return (elem && elem.ownerDocument) || document;
},
getComputedStyles:function(elem){
return this.getDocument(elem).defaultView.getComputedStyle(elem, null);
},
getOffset:function(DOMNode) {
var docElem = this.getDocument(DOMNode).documentElement;
var box = { top: 0, left: 0 };
if ( typeof DOMNode.getBoundingClientRect !== 'undefined' ) {
box = DOMNode.getBoundingClientRect();
}
return {
top: box.top + (window.pageYOffset || docElem.scrollTop) - (docElem.clientTop || 0),
left: box.left + (window.pageXOffset || docElem.scrollLeft) - (docElem.clientLeft || 0)
};
},
getPosition:function(elem, offsetParent) {
var offset,
parentOffset = {top: 0, left: 0};
if (this.getComputedStyles(elem).position === 'fixed' ) {
offset = elem.getBoundingClientRect();
} else {
if (!offsetParent) {
offsetParent = this.getOffsetParent(elem);
}
offset = this.getOffset(elem);
if ( offsetParent.nodeName !== 'HTML') {
parentOffset = this.getOffset(offsetParent);
}
parentOffset.top += parseInt(this.getComputedStyles(offsetParent).borderTopWidth, 10);
parentOffset.left += parseInt(this.getComputedStyles(offsetParent).borderLeftWidth, 10);
}
// Subtract parent offsets and element margins
return {
top: offset.top - parentOffset.top - parseInt(this.getComputedStyles(elem).marginTop, 10),
left: offset.left - parentOffset.left - parseInt(this.getComputedStyles(elem).marginLeft, 10)
};
},
getOffsetParent:function(elem){
var docElem = this.getDocument(elem).documentElement;
var offsetParent = elem.offsetParent || docElem;
while ( offsetParent && ( offsetParent.nodeName !== 'HTML' &&
this.getComputedStyles(offsetParent).position === 'static' ) ) {
offsetParent = offsetParent.offsetParent;
}
return offsetParent || docElem;
},
contains:function(elem, inner){
function ie8Contains(root, node) {
while (node) {
if (node === root) {
return true;
}
node = node.parentNode;
}
return false;
}
return (elem && elem.contains)
? elem.contains(inner)
: (elem && elem.compareDocumentPosition)
? elem === inner || !!(elem.compareDocumentPosition(inner) & 16)
: ie8Contains(elem, inner);
}
}
var EVENT_NAME_MAP = {
transitionend: {
'transition': 'transitionend',
'WebkitTransition': 'webkitTransitionEnd',
'MozTransition': 'mozTransitionEnd',
'OTransition': 'oTransitionEnd',
'msTransition': 'MSTransitionEnd'
},
animationend: {
'animation': 'animationend',
'WebkitAnimation': 'webkitAnimationEnd',
'MozAnimation': 'mozAnimationEnd',
'OAnimation': 'oAnimationEnd',
'msAnimation': 'MSAnimationEnd'
}
};
function detectEvents() {
var testEl = document.createElement('div');
var style = testEl.style;
if (!('AnimationEvent' in window)) {
delete EVENT_NAME_MAP.animationend.animation;
}
if (!('TransitionEvent' in window)) {
delete EVENT_NAME_MAP.transitionend.transition;
}
for (let baseEventName in EVENT_NAME_MAP) {
let baseEvents = EVENT_NAME_MAP[baseEventName];
for (let styleName in baseEvents) {
if (styleName in style) {
endEvents.push(baseEvents[styleName]);
break;
}
}
}
}
detectEvents();
var event = {
add:function(componentOrElement,type,handler){
var elem = React.findDOMNode(componentOrElement);
if (elem.addEventListener) {
elem.addEventListener(type, handler, false);
} else if (elem.attachEvent) {
elem.attachEvent('on' + type, function() {
handler.call(elem);
});
} else {
elem['on' + type] = handler;
}
},
remove:function(componentOrElement,type,handler){
var elem = React.findDOMNode(componentOrElement);
if (elem.removeEnentListener) {
elem.removeEnentListener(type, handler, false);
} else if (elem.datachEvent) {
elem.detachEvent('on' + type, handler);
} else {
elem['on' + type] = null;
}
},
one:function(componentOrElement,type,handler){
var elem = React.findDOMNode(componentOrElement);
var self = this;
this.add(elem,type,function(e){
handler.call(elem,e);
self.remove(elem,type,handler);
});
},
addEnd:function(elem,handler){
if (endEvents.length === 0) {
window.setTimeout(handler, 0);
return;
}
endEvents.forEach(function(endEvent) {
this.add(elem, endEvent, handler);
});
},
removeEnd:function(elem,handler){
if (endEvents.length === 0) {
return;
}
endEvents.forEach(function(endEvent) {
removeEventListener(elem, endEvent, handler);
});
},
preventDefault:function(e){
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
},
stopPropagation:function(e){
if (e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBubble = true;
}
}
}
var chileren = {
map:function(children, func, context){
var index = 0;
return React.Children.map(children, function (child) {
if (React.isValidElement(child)) {
var lastIndex = index;
index++;
return func.call(context, child, lastIndex);
}
return child;
});
},
forEach:function(children, func, context){
var index = 0;
return React.Children.forEach(children, function (child) {
if (React.isValidElement(child)) {
func.call(context, child, index);
index++;
}
});
},
count:function(children){
var count = 0;
React.Children.forEach(children, function (child) {
if (React.isValidElement(child)) { count++; }
});
return count;
},
toArray:function(children){
var ret = [];
React.Children.forEach(children, function (child) {
ret.push(child);
});
return ret;
}
}
module.exports = {
dom : dom,
event:event,
children:chileren
}