-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
487 lines (418 loc) · 13.2 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
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
/* eslint no-unused-expressions: 0 */
const reUnit = /width|height|top|left|right|bottom|margin|padding/i;
let _amId = 1;
const _amDisplay = {};
let requestAnimationFrame;
if (typeof window !== 'undefined') {
requestAnimationFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
} else {
requestAnimationFrame = function() {
throw new Error('requestAnimationFrame is not supported, maybe you are running in the server side');
};
}
function getAmId(obj) {
return obj._amId || (obj._amId = _amId++);
}
function setAmDisplay(elem, display) {
const id = getAmId(elem);
_amDisplay[`_am_${id}`] = display;
}
function getAmDisplay(elem) {
const id = getAmId(elem);
return _amDisplay[`_am_${id}`];
}
export default {
// el can be an Element, NodeList or selector
addClass(el, className) {
if (typeof el === 'string') el = document.querySelectorAll(el);
const els = (el instanceof NodeList) ? [].slice.call(el) : [el];
els.forEach(e => {
if (this.hasClass(e, className)) { return; }
if (e.classList) {
e.classList.add(className);
} else {
e.className += ' ' + className;
}
});
},
// el can be an Element, NodeList or selector
removeClass(el, className) {
if (typeof el === 'string') el = document.querySelectorAll(el);
const els = (el instanceof NodeList) ? [].slice.call(el) : [el];
els.forEach(e => {
if (this.hasClass(e, className)) {
if (e.classList) {
e.classList.remove(className);
} else {
e.className = e.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
}
});
},
// el can be an Element or selector
hasClass(el, className) {
if (typeof el === 'string') el = document.querySelector(el);
if (!el) {
return false;
}
if (el.classList) {
return el.classList.contains(className);
}
return new RegExp('(^| )' + className + '( |$)', 'gi').test(el.className);
},
// el can be an Element or selector
toggleClass(el, className) {
if (typeof el === 'string') el = document.querySelector(el);
const flag = this.hasClass(el, className);
if (flag) {
this.removeClass(el, className);
} else {
this.addClass(el, className);
}
return flag;
},
insertAfter(newEl, targetEl) {
const parent = targetEl.parentNode;
if (parent.lastChild === targetEl) {
parent.appendChild(newEl);
} else {
parent.insertBefore(newEl, targetEl.nextSibling);
}
},
// el can be an Element, NodeList or query string
remove(el) {
if (typeof el === 'string') {
[].forEach.call(document.querySelectorAll(el), node => {
node.parentNode.removeChild(node);
});
} else if (el.parentNode) {
// it's an Element
el.parentNode.removeChild(el);
} else if (el instanceof NodeList) {
// it's an array of elements
[].forEach.call(el, node => {
node.parentNode.removeChild(node);
});
} else {
throw new Error('you can only pass Element, array of Elements or query string as argument');
}
},
forceReflow(el) {
el.offsetHeight;
},
getDocumentScrollTop() {
// IE8 used `document.documentElement`
return (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
},
// Set the current vertical position of the scroll bar for document
// Note: do not support fixed position of body
setDocumentScrollTop(value) {
window.scrollTo(0, value);
return value;
},
outerHeight(el) {
return el.offsetHeight;
},
outerHeightWithMargin(el) {
let height = el.offsetHeight;
const style = getComputedStyle(el);
height += (parseFloat(style.marginTop) || 0) + (parseFloat(style.marginBottom) || 0);
return height;
},
outerWidth(el) {
return el.offsetWidth;
},
outerWidthWithMargin(el) {
let width = el.offsetWidth;
const style = getComputedStyle(el);
width += (parseFloat(style.marginLeft) || 0) + (parseFloat(style.marginRight) || 0);
return width;
},
getComputedStyles(el) {
return el.ownerDocument.defaultView.getComputedStyle(el, null);
},
getOffset(el) {
const html = el.ownerDocument.documentElement;
let box = { top: 0, left: 0 };
// If we don't have gBCR, just use 0,0 rather than error
// BlackBerry 5, iOS 3 (original iPhone)
if ( typeof el.getBoundingClientRect !== 'undefined' ) {
box = el.getBoundingClientRect();
}
return {
top: box.top + window.pageYOffset - html.clientTop,
left: box.left + window.pageXOffset - html.clientLeft
};
},
getPosition(el) {
if (!el) {
return {
left: 0,
top: 0
};
}
return {
left: el.offsetLeft,
top: el.offsetTop
};
},
setStyle(node, att, val, style) {
style = style || node.style;
if (style) {
if (val === null || val === '') { // normalize unsetting
val = '';
} else if (!isNaN(Number(val)) && reUnit.test(att)) { // number values may need a unit
val += 'px';
}
if (att === '') {
att = 'cssText';
val = '';
}
style[att] = val;
}
},
setStyles(el, hash) {
const HAS_CSSTEXT_FEATURE = typeof(el.style.cssText) !== 'undefined';
function trim(str) {
return str.replace(/^\s+|\s+$/g, '');
}
let originStyleText;
const originStyleObj = {};
if (!!HAS_CSSTEXT_FEATURE) {
originStyleText = el.style.cssText;
} else {
originStyleText = el.getAttribute('style');
}
originStyleText.split(';').forEach(item => {
if (item.indexOf(':') !== -1) {
const obj = item.split(':');
originStyleObj[trim(obj[0])] = trim(obj[1]);
}
});
const styleObj = {};
Object.keys(hash).forEach(item => {
this.setStyle(el, item, hash[item], styleObj);
});
const mergedStyleObj = Object.assign({}, originStyleObj, styleObj);
const styleText = Object.keys(mergedStyleObj)
.map(item => item + ': ' + mergedStyleObj[item] + ';')
.join(' ');
if (!!HAS_CSSTEXT_FEATURE) {
el.style.cssText = styleText;
} else {
el.setAttribute('style', styleText);
}
},
getStyle(el, att, style) {
style = style || el.style;
let val = '';
if (style) {
val = style[att];
if (val === '') {
val = this.getComputedStyle(el, att);
}
}
return val;
},
// NOTE: Known bug, will return 'auto' if style value is 'auto'
getComputedStyle(el, att) {
const win = el.ownerDocument.defaultView;
// null means not return presudo styles
const computed = win.getComputedStyle(el, null);
return att ? computed[att] : computed;
},
getPageSize() {
let xScroll, yScroll;
if (window.innerHeight && window.scrollMaxY) {
xScroll = window.innerWidth + window.scrollMaxX;
yScroll = window.innerHeight + window.scrollMaxY;
} else {
if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
xScroll = document.body.scrollWidth;
yScroll = document.body.scrollHeight;
} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
xScroll = document.body.offsetWidth;
yScroll = document.body.offsetHeight;
}
}
let windowWidth, windowHeight;
if (self.innerHeight) { // all except Explorer
if (document.documentElement.clientWidth) {
windowWidth = document.documentElement.clientWidth;
} else {
windowWidth = self.innerWidth;
}
windowHeight = self.innerHeight;
} else {
if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else {
if (document.body) { // other Explorers
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}
}
}
let pageHeight, pageWidth;
// for small pages with total height less then height of the viewport
if (yScroll < windowHeight) {
pageHeight = windowHeight;
} else {
pageHeight = yScroll;
}
// for small pages with total width less then width of the viewport
if (xScroll < windowWidth) {
pageWidth = xScroll;
} else {
pageWidth = windowWidth;
}
return {
pageWidth: pageWidth,
pageHeight: pageHeight,
windowWidth: windowWidth,
windowHeight: windowHeight
};
},
get(selector) {
return document.querySelector(selector) || {};
},
getAll(selector) {
return document.querySelectorAll(selector);
},
// selector 可选。字符串值,规定在何处停止对祖先元素进行匹配的选择器表达式。
// filter 可选。字符串值,包含用于匹配元素的选择器表达式。
parentsUntil(el, selector, filter) {
const result = [];
const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
// match start from parent
el = el.parentElement;
while (el && !matchesSelector.call(el, selector)) {
if (!filter) {
result.push(el);
} else {
if (matchesSelector.call(el, filter)) {
result.push(el);
}
}
el = el.parentElement;
}
return result;
},
// 获得匹配选择器的第一个祖先元素,从当前元素开始沿 DOM 树向上
closest(el, selector) {
const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
while (el) {
if (matchesSelector.call(el, selector)) {
return el;
}
el = el.parentElement;
}
return null;
},
// el can be an Element, NodeList or selector
_showHide(el, show ) {
if (typeof el === 'string') el = document.querySelectorAll(el);
const els = (el instanceof NodeList) ? [].slice.call(el) : [el];
let display;
const values = [];
if (els.length === 0) {
return;
}
els.forEach((e, index) => {
if (e.style) {
display = e.style.display;
if (show) {
if (display === 'none') {
values[index] = getAmDisplay(e) || '';
}
} else {
if (display !== 'none') {
values[index] = 'none';
setAmDisplay(e, display);
}
}
}
});
els.forEach((e, index) => {
if ( values[index] !== null ) {
els[index].style.display = values[index];
}
});
},
show(elements) {
this._showHide(elements, true);
},
hide(elements) {
this._showHide(elements, false);
},
toggle(element) {
if (element.style.display === 'none') {
this.show(element);
} else {
this.hide(element);
}
},
/**
* scroll to location with animation
* @param {Number} to to assign the scrollTop value
* @param {Number} duration assign the animate duration
* @return {Null} return null
*/
scrollTo(to = 0, duration = 16) {
if (duration < 0) {
return;
}
const diff = to - this.getDocumentScrollTop();
if (diff === 0) {
return;
}
const perTick = diff / duration * 10;
requestAnimationFrame(() => {
if (Math.abs(perTick) > Math.abs(diff)) {
this.setDocumentScrollTop(this.getDocumentScrollTop() + diff);
return;
}
this.setDocumentScrollTop(this.getDocumentScrollTop() + perTick);
if (diff > 0 && this.getDocumentScrollTop() >= to || diff < 0 && this.getDocumentScrollTop() <= to) {
return;
}
this.scrollTo(to, duration - 16);
});
},
// matches(el, '.my-class'); 这里不能使用伪类选择器
is(el, selector) {
return (el.matches || el.matchesSelector || el.msMatchesSelector || el.mozMatchesSelector || el.webkitMatchesSelector || el.oMatchesSelector).call(el, selector);
},
width(el) {
const styles = this.getComputedStyles(el);
const width = parseFloat(styles.width.indexOf('px') !== -1 ? styles.width : 0);
const boxSizing = styles.boxSizing || 'content-box';
if (boxSizing === 'border-box') {
return width;
}
const borderLeftWidth = parseFloat(styles.borderLeftWidth);
const borderRightWidth = parseFloat(styles.borderRightWidth);
const paddingLeft = parseFloat(styles.paddingLeft);
const paddingRight = parseFloat(styles.paddingRight);
return width - borderRightWidth - borderLeftWidth - paddingLeft - paddingRight;
},
height(el) {
const styles = this.getComputedStyles(el);
const height = parseFloat(styles.height.indexOf('px') !== -1 ? styles.height : 0);
const boxSizing = styles.boxSizing || 'content-box';
if (boxSizing === 'border-box') {
return height;
}
const borderTopWidth = parseFloat(styles.borderTopWidth);
const borderBottomWidth = parseFloat(styles.borderBottomWidth);
const paddingTop = parseFloat(styles.paddingTop);
const paddingBottom = parseFloat(styles.paddingBottom);
return height - borderBottomWidth - borderTopWidth - paddingTop - paddingBottom;
}
};