-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
51 lines (38 loc) · 1.12 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
'use strict';
var Emitter = require('component-emitter');
var debounce = require('debounce');
var resize = module.exports;
// Make resize an event emitter
Emitter(resize);
resize.debounceDelay = 50;
resize.onGlobalResize = function() {
this.width = window.innerWidth;
this.height = window.innerHeight;
this.halfWidth = 0.5 * this.width;
this.halfHeight = 0.5 * this.height;
this.applyResize();
if (this.debounceDelay > 0) {
this.debounceResize();
} else {
this.applyResizeDebounce();
}
};
resize.applyResizeDebounce = function() {
this.emit('resizeDebounce');
};
resize.applyResize = function() {
this.emit('resize');
};
resize.addListener = function(listener, noDebounce) {
this.on(noDebounce ? 'resize' : 'resizeDebounce', listener);
};
resize.removeListener = function(listener) {
if (listener) {
this.off('resize', listener);
this.off('resizeDebounce', listener);
}
};
resize.debounceResize = debounce(resize.applyResizeDebounce.bind(resize), resize.debounceDelay);
// first call to init values
resize.onGlobalResize();
window.addEventListener('resize', resize.onGlobalResize.bind(resize));