-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.js
100 lines (91 loc) · 2.91 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
function resizeListener(e) {
var win = e.target || e.srcElement
if (win.__resizeRAF__) {
cancelAnimationFrame(win.__resizeRAF__)
}
win.__resizeRAF__ = requestAnimationFrame(function () {
var trigger = win.__resizeTrigger__
var listeners = trigger && trigger.__resizeListeners__
if (listeners) {
listeners.forEach(function (fn) {
fn.call(trigger, e)
})
}
})
}
var _exports = function exports(element, fn) {
var window = this
var document = window.document
var isIE
var attachEvent = document.attachEvent
if (typeof navigator !== "undefined") {
isIE =
navigator.userAgent.match(/Trident/) || navigator.userAgent.match(/Edge/)
}
function objectLoad() {
this.contentDocument.defaultView.__resizeTrigger__ = this.__resizeElement__
this.contentDocument.defaultView.addEventListener("resize", resizeListener)
}
if (!element.__resizeListeners__) {
element.__resizeListeners__ = []
if (attachEvent) {
element.__resizeTrigger__ = element
element.attachEvent("onresize", resizeListener)
} else {
if (getComputedStyle(element).position === "static") {
element.style.position = "relative"
}
var obj = (element.__resizeTrigger__ = document.createElement("object"))
obj.setAttribute(
"style",
"position: absolute; top: 0; left: 0; height: 100%; width: 100%; pointer-events: none; z-index: -1; opacity: 0;"
)
obj.setAttribute("class", "resize-sensor")
// prevent <object> from stealing keyboard focus
obj.setAttribute("tabindex", "-1")
// prevent screenreaders to see this object
obj.setAttribute("title", "")
obj.__resizeElement__ = element
obj.onload = objectLoad
obj.type = "text/html"
if (isIE) {
element.appendChild(obj)
}
obj.data = "about:blank"
if (!isIE) {
element.appendChild(obj)
}
}
}
element.__resizeListeners__.push(fn)
}
module.exports =
typeof window === "undefined" ? _exports : _exports.bind(window)
module.exports.unbind = function (element, fn) {
var attachEvent = document.attachEvent
var listeners = element.__resizeListeners__ || []
if (fn) {
var index = listeners.indexOf(fn)
if (index !== -1) {
listeners.splice(index, 1)
}
} else {
listeners = element.__resizeListeners__ = []
}
if (!listeners.length) {
if (attachEvent) {
element.detachEvent("onresize", resizeListener)
} else if (element.__resizeTrigger__) {
var contentDocument = element.__resizeTrigger__.contentDocument
var defaultView = contentDocument && contentDocument.defaultView
if (defaultView) {
defaultView.removeEventListener("resize", resizeListener)
delete defaultView.__resizeTrigger__
}
element.__resizeTrigger__ = !element.removeChild(
element.__resizeTrigger__
)
}
delete element.__resizeListeners__
}
}