forked from harvesthq/harvey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harvey.js
387 lines (295 loc) · 10.6 KB
/
harvey.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Generated by CoffeeScript 1.3.1
/*
Harvey stateManager — Copyright (c) 2012 Joschka Kintscher
*/
(function() {
var State, _mediaQueryList,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
this.Harvey = (function() {
Harvey.name = 'Harvey';
function Harvey() {}
Harvey.states = {};
Harvey.queries = [];
/*
Creates a new State object for the given media query using the passed hash
of callbacks and stores it in @states. The passed hash may contain up to
three callbacks. See documentation of the State class for more information.
*/
Harvey.attach = function(mediaQuery, callbacks) {
var state;
if (!this.states.hasOwnProperty(mediaQuery)) {
this.states[mediaQuery] = [];
this._add_css_for(mediaQuery);
}
state = new State(mediaQuery, callbacks != null ? callbacks.setup : void 0, callbacks != null ? callbacks.on : void 0, callbacks != null ? callbacks.off : void 0);
this.states[mediaQuery].push(state);
if (__indexOf.call(this.queries, mediaQuery) < 0) {
this._watch_query(mediaQuery);
}
if (this._window_matchmedia(mediaQuery).matches) {
this._update_states([state], true);
}
return state;
};
/*
Removes a given State object from the @states hash.
@param object state A valid state object
*/
Harvey.detach = function(state) {
var i, s, _i, _len, _ref, _results;
_ref = this.states[state.condition];
_results = [];
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
s = _ref[i];
if (state === s) {
_results.push(this.states[s.condition][i] = void 0);
} else {
_results.push(void 0);
}
}
return _results;
};
/*
Create a new matchMediaListener for the passed media query.
@param string mediaQuery A valid CSS media query to watch
*/
Harvey._watch_query = function(mediaQuery) {
var _this = this;
this.queries.push(mediaQuery);
return this._window_matchmedia(mediaQuery).addListener(function(mql) {
return _this._update_states(_this.states[mediaQuery], mql.matches);
});
};
/*
Activates/Deactivates every State object in the passed list.
@param array states A list of State objects to update
@param boolean active Whether to activate or deactivate the given states
*/
Harvey._update_states = function(states, active) {
var state, _i, _len, _results;
_results = [];
for (_i = 0, _len = states.length; _i < _len; _i++) {
state = states[_i];
if (active) {
_results.push(state.activate());
} else {
_results.push(state.deactivate());
}
}
return _results;
};
/*
BEWARE: You're at the edge of the map, mate. Here there be monsters!
------------------------------------------------------------------------------------
Private methods to fix and polyfill the matchMedia interface for several engines
* Inspired by Nicholas C. Zakas' article on the different problems with matchMedia
http://www.nczonline.net/blog/2012/01/19/css-media-queries-in-javascript-part-2/
* Implementing a modified coffeescript version of Paul Irish's matchMedia.js polyfill
https://github.com/paulirish/matchMedia.js
*/
/*
[FIX] for Firefox/Gecko browsers that lose reference to the
MediaQueryList object unless it's being stored for runtime.
*/
Harvey._mediaList = {};
/*
@param string mediaQuery A valid CSS media query to monitor for updates
@Return object MediaQueryList Depending on the browser and matchMedia support either a native
mediaQueryList object or an instance of _mediaQueryList
*/
Harvey._window_matchmedia = function(mediaQuery) {
if (window.matchMedia) {
if (!(mediaQuery in this._mediaList)) {
this._mediaList[mediaQuery] = window.matchMedia(mediaQuery);
}
return this._mediaList[mediaQuery];
}
/*
[POLYFILL] for all browsers that don't support matchMedia() at all (CSS media query support
is still mandatory though).
*/
if (!this._listening) {
this._listen();
}
if (!(mediaQuery in this._mediaList)) {
this._mediaList[mediaQuery] = new _mediaQueryList(mediaQuery);
}
return this._mediaList[mediaQuery];
};
/*
Add resize and orientationChange event listeners to the window element
to monitor updates to the viewport
*/
Harvey._listen = function() {
var evt,
_this = this;
evt = window.addEventListener || window.attachEvent;
evt('resize', function() {
var mediaList, mediaQuery, _ref, _results;
_ref = _this._mediaList;
_results = [];
for (mediaQuery in _ref) {
mediaList = _ref[mediaQuery];
_results.push(mediaList._process());
}
return _results;
});
evt('orientationChange', function() {
var mediaList, mediaQuery, _ref, _results;
_ref = _this._mediaList;
_results = [];
for (mediaQuery in _ref) {
mediaList = _ref[mediaQuery];
_results.push(mediaList._process());
}
return _results;
});
return this._listening = true;
};
/*
[FIX] for Webkit engines that only trigger the MediaQueryListListener
when there is at least one CSS selector for the respective media query
@param string MediaQuery The media query to inject CSS for
*/
Harvey._add_css_for = function(mediaQuery) {
if (!this.style) {
this.style = document.createElement('style');
document.getElementsByTagName('head')[0].appendChild(this.style);
}
return this.style.appendChild(document.createTextNode("@media " + mediaQuery + " {.harvey-test{}}"));
};
return Harvey;
})();
/*
A State allows to execute a set of callbacks for the given valid CSS media query.
Callbacks are executed in the context of their state object to allow access to the
corresponding media query of the State.
States are not exposed to the global namespace. They can be used by calling the
static Harvey.attach() and Harvey.detach() methods.
*/
State = (function() {
State.name = 'State';
State.prototype.active = false;
State.prototype.is_setup = false;
/*
Creates a new State object
@param string condition The media query to check for
@param function setup Called the first time `condition` becomes valid
@param function on Called every time `condition` becomes valid
@param function off Called every time `condition` becomes invalid
*/
function State(condition, setup, on, off) {
this.condition = condition;
this.setup = setup;
this.on = on;
this.off = off;
}
/*
Activate this State object if it is currently deactivated. Also perform all
set up tasks if this is the first time the State is activated
*/
State.prototype.activate = function() {
if (this.active) {
return;
}
if (!this.is_setup) {
if (typeof this.setup === "function") {
this.setup();
}
this.is_setup = true;
}
if (typeof this.on === "function") {
this.on();
}
return this.active = true;
};
/*
Deactive this State object if it is currently active
*/
State.prototype.deactivate = function() {
if (!this.active) {
return;
}
if (typeof this.off === "function") {
this.off();
}
return this.active = false;
};
return State;
})();
/*
[FIX] simple implemenation of the matchMedia interface to mimic the native
matchMedia interface behaviour to work as a polyfill for Harvey
*/
_mediaQueryList = (function() {
_mediaQueryList.name = '_mediaQueryList';
/*
Creates a new _mediaQueryList object
@param string media A valid CSS media query
*/
function _mediaQueryList(media) {
this.media = media;
this._listeners = [];
this.matches = this._matches();
}
/*
Add a new listener to this mediaQueryList that will be called every time
the media query becomes valid
*/
_mediaQueryList.prototype.addListener = function(listener) {
this._listeners.push(listener);
return;
};
/*
Evaluate the media query of this mediaQueryList object and notify
all registered listeners if the state has changed
*/
_mediaQueryList.prototype._process = function() {
var callback, current, _i, _len, _ref, _results;
current = this._matches();
if (this.matches === current) {
return;
}
this.matches = current;
_ref = this._listeners;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
callback = _ref[_i];
_results.push(callback(this));
}
return _results;
};
/*
Check whether the media query is currently valid
*/
_mediaQueryList.prototype._matches = function() {
if (!this._tester) {
this._get_tester();
}
this._tester.innerHTML = '­<style media="' + this.media + '">#harvey-mq-test{width:42px;}</style>';
this._tester.removeChild(this._tester.firstChild);
return this._tester.offsetWidth === 42;
};
/*
Retrieve the element to test the media query on from the DOM or create
it if it has not been injected into the page yet
*/
_mediaQueryList.prototype._get_tester = function() {
this._tester = document.getElementById('harvey-mq-test');
if (!this._tester) {
return this._build_tester();
}
};
/*
Create a new div with a unique id, move it outsite of the viewport and inject it into the DOM.
This element will be used to check whether the registered media query is currently valid.
*/
_mediaQueryList.prototype._build_tester = function() {
this._tester = document.createElement('div');
this._tester.id = 'harvey-mq-test';
this._tester.style.cssText = 'position:absolute;top:-100em';
return document.body.insertBefore(this._tester, document.body.firstChild);
};
return _mediaQueryList;
})();
}).call(this);