-
Notifications
You must be signed in to change notification settings - Fork 0
/
m.js
164 lines (132 loc) · 3.29 KB
/
m.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
/**
*
__
/\ \
|::\ \
|:|:\ \
__|:|\:\ \
/::::|_\:\__\
\:\~~\ \/__/
\:\ \
\:\ \
\:\__\
\/__/
M
Tiny ES6 module template for progressive enhancement.
import M from '@superstructure.net/m';
export default class MyModule extends M {
constructor(mediaQuery) {
super(mediaQuery);
}
onInit() {}
onResize(viewport, isUIResize) {}
onDestroy() {}
}
*
*
*/
export default class M {
constructor(mediaQuery = 'screen') {
this._name = this.constructor.name.toLowerCase();
this._mediaQuery = mediaQuery;
this._mediaQueryList = window.matchMedia(mediaQuery);
this._resizeDelay = 500;
this._resizeDebounce = null;
this._runResizeHook = false;
this._viewport = {
prevWidth: undefined,
prevHeight: undefined,
width: undefined,
height: undefined
};
this._initiated = false;
this._onResize();
this._onMediaQueryChange();
this._bindEvents();
}
selector(role) {
if (!role) {
return;
}
return '[data-' + this._name + '-role~="' + role + '"]';
}
/**
* Internal functions
*/
_init() {
this._initiated = true;
document.documentElement.classList.add('initiated--' + this._name);
if (this.onInit) {
this.onInit();
}
}
_destroy() {
if (this.onDestroy) {
this.onDestroy();
}
document.documentElement.classList.remove('initiated--' + this._name);
this._initiated = false;
}
_bindEvents() {
/**
* MediaQueryList listener
*/
if (this._mediaQueryList.addEventListener) {
this._mediaQueryList.addEventListener('change', () => {
this._handleMediaQueryChange();
});
} else if (this._mediaQueryList.addListener) {
this._mediaQueryList.addListener(() => {
this._handleMediaQueryChange();
});
}
/**
* Resize event
*/
window.addEventListener('resize', () => {
this._handleResize();
});
}
_handleMediaQueryChange() {
this._runResizeHook = false;
this._onMediaQueryChange();
}
_handleResize() {
clearTimeout(this._resizeDebounce);
this._resizeDebounce = setTimeout(() => {
this._onResize();
}, this._resizeDelay);
}
_onMediaQueryChange() {
if (this._mediaQueryList.matches) {
if (!this._initiated) {
this._init();
}
} else {
if (this._initiated) {
this._destroy();
}
}
}
_onResize() {
this._viewport.prevWidth = this._viewport.width || window.innerWidth;
this._viewport.prevHeight = this._viewport.height || window.innerHeight;
this._viewport.width = window.innerWidth;
this._viewport.height = window.innerHeight;
// calculate 100vh
const _div = document.createElement('div');
_div.style.height = '100vh';
document.body.appendChild(_div);
this._viewport.height100vh = _div.clientHeight;
document.body.removeChild(_div);
if (this._runResizeHook && this._initiated && this.onResize) {
this.onResize(
this._viewport,
this._viewport.width < 840 &&
this._viewport.width === this._viewport.prevWidth &&
Math.abs(this._viewport.height - this._viewport.prevHeight) < 120
);
}
this._runResizeHook = true;
}
}