-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmediaCheck.js
352 lines (305 loc) · 7.85 KB
/
mediaCheck.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
(function() {
'use strict';
angular
.module('mediaCheck', []); // module setter
angular
.module('mediaCheck') // module getter
.factory('mediaCheck', mediaCheck); // factory
// inject dependencies
mediaCheck.$inject = ['$window', '$timeout'];
/**
* mediaCheck factory function
*
* @param $window
* @param $timeout
* @returns {object}
*/
function mediaCheck($window, $timeout) {
var hasMatchMedia = $window.matchMedia !== undefined && !!$window.matchMedia('!').addListener;
/**
* Wrap handlers in $timeout
* to prevent $digest errors
*
* @param fn {function}
* @param mq {object} matchMedia query
* @private
*/
function _timeoutFn(fn, mq) {
if (typeof fn === 'function') {
$timeout(function() {
fn(mq);
});
}
}
/**
* Convert ems to px
*
* @param value {number}
* @returns {number}
* @private
*/
function _convertEmToPx(value) {
var emElement = document.createElement('div');
emElement.style.width = '1em';
emElement.style.position = 'absolute';
document.body.appendChild(emElement);
document.body.removeChild(emElement);
return value * emElement.offsetWidth;
}
/**
* Get pixel value
*
* @param width {number}
* @param unit {string}
* @returns {string}
* @private
*/
function _getPXValue(width, unit) {
var value;
value = void 0;
switch (unit) {
case 'em':
value = _convertEmToPx(width);
break;
default:
value = width;
}
return value;
}
/**
* MediaCheck constructor object
*
* @type {{buildInstance: buildInstance, matchCurrent: matchCurrent}}
*/
var MediaCheck = {
buildInstance: buildInstance,
matchCurrent: matchCurrent
};
/**
* Create instance
*
* @param options {object}
*/
function buildInstance(options) {
this.options = options;
this.matchMap = {};
// local variables
var setup = this.options;
var matchMap = this.matchMap;
var media = setup['media'];
var optionsIsArr = Object.prototype.toString.call(media) === '[object Array]';
var $scope = setup['scope'];
var debounce = setup['debounce'];
var debounceSpeed = !!debounce || debounce === 0 ? debounce : 150;
var $win = angular.element($window);
/**
* Setup mediaquery
*
* @param options {object}
* @returns {*}
* @private
*/
function _setupMQ(options) {
// general
var query = options['mq'];
// media query changing functions
var enterFn = options.enter;
var exitFn = options.exit;
var changeFn = options.change;
/**
* matchMedia supported
* Use matchMedia to listen for breakpoint changes
*
* @returns {*} create listener
*/
function _mmSupported() {
var mq = $window.matchMedia(query);
/**
* Check for matches
* Run functions appropriately
*
* @param mq {object} MediaQueryList object
*/
function _mqChange(mq) {
if (mq.matches) {
_timeoutFn(enterFn, mq);
} else {
_timeoutFn(exitFn, mq);
}
_timeoutFn(changeFn, mq);
}
/**
* Set up matchCurrent (matchMedia supported)
* Run proper function for current breakpoint
* Run _mqChange on-demand
*/
function matchCurrent() {
_mqChange(mq);
}
// matchMap function mapping for matchCurrent()
matchMap[query] = matchCurrent;
/**
* Create listener for media query changes
* Bind to orientation change
* Unbind on $scope destruction
*/
function _createListener() {
/**
* Create matchMedia listener when matchMedia is supported
*
* @returns {*} _mqChange function
*/
function _mqListListener() {
return _mqChange(mq)
}
mq.addListener(_mqListListener);
// bind to the orientationchange event and fire _mqChange
$win.bind('orientationchange', _mqListListener);
// cleanup listeners when $scope is $destroyed
if ($scope) {
$scope.$on('$destroy', function() {
mq.removeListener(_mqListListener);
$win.unbind('orientationchange', _mqListListener);
matchMap = {};
});
}
return _mqChange(mq);
}
return _createListener();
}
/**
* matchMedia not supported
* Use window resize to listen for breakpoint changes
*
* @returns {*} create listener
*/
function _mmNotSupported() {
var breakpoints = {};
var debounceResize;
/**
* Check for matches
* Run functions appropriately
*
* @param mq {object}
* @returns {*} set breakpoint matching boolean
*/
function _mqChange(mq) {
if (mq.matches) {
if (!!breakpoints[query] === false) {
_timeoutFn(enterFn, mq);
}
} else {
if (breakpoints[query] === true || breakpoints[query] == null) {
_timeoutFn(exitFn, mq);
}
}
if ((mq.matches && (!breakpoints[query]) || (!mq.matches && (breakpoints[query] === true || breakpoints[query] == null)))) {
_timeoutFn(changeFn, mq);
}
return breakpoints[query] = mq.matches;
}
breakpoints[query] = null;
/**
* Create matchMedia listener when matchMedia not supported
*
* @returns {*} _mqChange function
*/
function _mmListener() {
var parts = query.match(/\((.*)-.*:\s*([\d\.]*)(.*)\)/);
var constraint = parts[1];
var value = _getPXValue(parseInt(parts[2], 10), parts[3]);
var fakeMatchMedia = { media: query };
var windowWidth = $window.innerWidth || document.documentElement.clientWidth;
fakeMatchMedia.matches = constraint === 'max' && value > windowWidth || constraint === 'min' && value < windowWidth;
return _mqChange(fakeMatchMedia);
}
/**
* Set up matchCurrent (matchMedia not supported)
* Run proper function for current breakpoint
*/
function matchCurrent() {
var mq = {
media: query,
matches: breakpoints[query]
};
if (breakpoints[query]) {
_timeoutFn(enterFn, mq);
} else {
_timeoutFn(exitFn, mq);
}
_timeoutFn(changeFn, mq);
}
// matchMap function mapping for matchCurrent()
matchMap[query] = matchCurrent;
/**
* Window resized
*/
function _fakeMatchMediaResize() {
$timeout.cancel(debounceResize);
debounceResize = $timeout(_mmListener, debounceSpeed);
}
$win.bind('resize', _fakeMatchMediaResize);
if ($scope) {
$scope.$on('$destroy', function() {
$win.unbind('resize', _fakeMatchMediaResize);
matchMap = {};
});
}
return _mmListener();
}
if (hasMatchMedia) {
// Modern browser supports matchMedia
_mmSupported();
} else {
// Browser does not support matchMedia (<=IE9, IE Mobile)
_mmNotSupported();
}
}
// Run setup function
if (optionsIsArr) {
for (var i = 0; i < media.length; i++) {
var optionsItem = media[i];
if (typeof optionsItem === 'object') {
_setupMQ(optionsItem);
}
}
} else if (!optionsIsArr && typeof media === 'object') {
_setupMQ(media);
}
}
/**
* matchCurrent() method
*
* @param matchQuery {string} optional
*/
function matchCurrent(matchQuery) {
var matchMap = this.matchMap;
if (matchQuery) {
if (typeof matchMap[matchQuery] === 'function') {
matchMap[matchQuery]();
} else {
throw new Error('Requested mediaquery not found in mediaCheck');
}
} else {
angular.forEach(matchMap, function(value, key) {
value();
});
}
}
/**
* Return constructor to assign and use
*
* @param options {object} options
* @returns {object} MediaCheck
*/
function init(options) {
var MC = Object.create(MediaCheck);
MC.buildInstance(options);
return MC;
}
// CALLABLE MEMBERS
return {
init: init
};
}
})();