-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathonElementResize.coffee
67 lines (64 loc) · 1.88 KB
/
onElementResize.coffee
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
# out: ../onElementResize.js
called = []
disposes = []
fn = () ->
args = arguments
called.push args
return ->
i = called.indexOf args
if i > -1
called.splice i,1
if disposes[i]?
disposes[i]()
disposes.splice i,1
if document? and window?
document.addEventListener "DOMContentLoaded", ->
if window.MutationObserver?
allResizeCbs = []
fn = (el, cb) ->
elheight = el.offsetHeight
elwidth = el.offsetWidth
cbwrapper = ->
if elheight != el.offsetHeight or elwidth != el.offsetWidth
elheight = el.offsetHeight
elwidth = el.offsetWidth
cb.apply(null,arguments)
allResizeCbs.push cbwrapper
return ->
index = allResizeCbs.indexOf cbwrapper
if index > -1
allResizeCbs.splice index,1
callResizeCbs = ->
for cb in allResizeCbs
cb.apply(null,arguments)
require("./_throttledListener")("resize",callResizeCbs)
throttle = require("lodash/throttle")
observer = new MutationObserver throttle(callResizeCbs,66)
observer.observe document.body,
attributes: true
childList: true
characterData: true
subtree: true
else
require "javascript-detect-element-resize"
fn = (el, cb) ->
window.addResizeListener(el,cb)
return -> window.removeResizeListener(el,cb)
for args,i in called
disposes[i] = fn.apply(null, args)
module.exports =
data: ->
resizeCbDisposables: []
methods:
onElementResize: (el,cb) ->
return unless cb?
dispose = fn(el,cb)
@resizeCbDisposables.push dispose
return =>
index = @resizeCbDisposables.indexOf dispose
if index > -1
@resizeCbDisposables.splice index,1
dispose()
beforeDestroy: ->
for resizeCbDisposable in @resizeCbDisposables
resizeCbDisposable()