-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidth-watcher.js
More file actions
211 lines (183 loc) · 6.32 KB
/
Copy pathwidth-watcher.js
File metadata and controls
211 lines (183 loc) · 6.32 KB
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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else {
root.WidthWatcher = factory();
}
}(this, function () {
'use strict';
return function() {
var mediaQueries = {}, wrappedListeners = {};
/**
* Adds breakpoints
*/
function add() {
var i, breakpoints = getIntArray(arguments);
for (i = 0; i < breakpoints.length; i++) {
if (typeof breakpoints[i] !== 'number' || breakpoints[i] <= 0) {
throw new Error('WidthWatcher only accepts unsigned integers');
}
if (typeof mediaQueries[breakpoints[i]] !== 'undefined') {
continue;
}
mediaQueries[breakpoints[i]] = {
"max": matchMedia('screen and (max-width: ' + breakpoints[i] + 'px)'),
"min": matchMedia('screen and (min-width: ' + breakpoints[i] + 'px)')
};
}
}
/**
* Converts arguments to plain array
* @param args
* @returns {Array}
*/
function getIntArray(args) {
var i, retval = [];
for (i = 0; i < args.length; i++) {
if (Object.prototype.toString.call(args[i]) === '[object Array]') {
retval = retval.concat(args[i]);
} else {
retval.push(args[i]);
}
}
return retval;
}
/**
* Remove breakpoint
* @param breakpoint
*/
function remove(breakpoint) {
if (mediaQueries.hasOwnProperty(breakpoint)) {
// TODO: remove
}
}
/**
* Adds and removes listeners to breakpoints
* @param fn
* @param breakpoint
* @param direction
* @param type
*/
function toggleListener(fn, breakpoint, direction, type) {
var i;
type = type || 'add';
if (typeof fn !== 'object') {
fn = {
fn: fn,
direction: direction,
breakpoint: breakpoint
};
}
console.log(wrapListener(fn.fn) == wrapListener(fn.fn));
for (i in mediaQueries) {
if (!mediaQueries.hasOwnProperty(i)) continue;
if (typeof fn.breakpoint == 'undefined' || fn.breakpoint === parseInt(i)) {
if (typeof fn.direction == 'undefined' || fn.direction == 'up' || fn.direction == 'increase') {
if (type === 'add') {
mediaQueries[i].min.addListener(wrapListener(fn.fn));
} else {
mediaQueries[i].min.removeListener(wrapListener(fn.fn));
}
}
if (typeof fn.direction == 'undefined' || fn.direction == 'down' || fn.direction == 'decrease') {
if (type === 'add') {
mediaQueries[i].max.addListener(wrapListener(fn.fn));
} else {
mediaQueries[i].max.removeListener(wrapListener(fn.fn));
}
}
}
}
}
/**
* Adds listener
* @param fn
* @param breakpoint
* @param direction
*/
function addListener(fn, breakpoint, direction) {
toggleListener(fn, breakpoint, direction, 'add');
}
/**
* Removes listener
* @param fn
* @param breakpoint
* @param direction
*/
function removeListener(fn, breakpoint, direction) {
toggleListener(fn, breakpoint, direction, 'remove');
}
/**
* Wrap listeners so they will ne executed only on match
* @param fn
* @returns {Function}
*/
function wrapListener(fn) {
// cache wrapped listeners to correctly remove them if needed
if (typeof wrappedListeners[fn] === 'undefined') {
wrappedListeners[fn] = function (mq) {
if (!mq.matches) return;
fn(customMediaQuery(mq));
};
}
return wrappedListeners[fn];
}
/**
* Shorthand for adding increase listener
* @param fn
* @param breakpoint
*/
function addIncreaseListener(fn, breakpoint) {
addListener({
fn: fn,
breakpoint: breakpoint,
direction: 'increase'
});
}
/**
* Shorthand for adding decrease listener
* @param fn
* @param breakpoint
*/
function addDecreaseListener(fn, breakpoint) {
addListener({
fn: fn,
breakpoint: breakpoint,
direction: 'decrease'
});
}
/**
* Returns array of added breakpoints
* @returns {Array}
*/
function getBreakpoints() {
var retval = [];
for (var i in mediaQueries) {
if (!mediaQueries.hasOwnProperty(i)) continue;
retval.push(parseInt(i));
}
return retval;
}
/**
* Decorates native mediaQuery object
* @param mediaQuery
* @returns {*}
*/
function customMediaQuery(mediaQuery) {
mediaQuery.getValue = function () {
var match = mediaQuery.media.match(/\d+/);
return match ? match[0] : false;
};
return mediaQuery;
}
return {
add: add,
remove: remove,
addListener: addListener,
addDecreaseListener: addDecreaseListener,
addIncreaseListener: addIncreaseListener,
removeListener: removeListener,
getBreakpoints: getBreakpoints
};
};
}));