/
mquery.js
183 lines (143 loc) · 4.81 KB
/
mquery.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
/*
* mQuery 0.1.0
* Copyright 2012 Stephen Murphy
*/
var mQuery = function(query, features) {
return new mQuery.Class(query, features);
},
isWebKit = function() {
if (isWebKit.memo) {
return isWebKit.memo;
}
isWebKit.memo = mQuery({WebkitMinDevicePixelRatio: 0}).matches();
return isWebKit.memo;
};
mQuery.Class = function(query, features) {
this._match = null;
this._callback = null;
this._media = mQuery.Lib.query(query, features);
};
mQuery.Class.prototype = {
query: function(query, features) {
if (this._match) { // Match has already occurred
return this;
}
this._media += ', ' + mQuery.Lib.query(query, features);
return this;
},
bind: function(callback) {
this.get();
// Fix open WebKit bug where callbacks aren't fired
// https://bugs.webkit.org/show_bug.cgi?id=75903
if (isWebKit()) {
this._fixWebkitCallback(this.media());
}
this._callback = mQuery.Lib.bind(this, callback);
this._match.addListener(this._callback);
return this;
},
unbind: function() {
this.get();
this._match.removeListener(this._callback);
return this;
},
matches: function() {
this.get();
return this._match.matches;
},
// Evaluate using get()?
media: function() {
return this._media;
},
error: function() {
this.get();
// Invalid queries should be "not all" according to spec. WebKit returns "invalid"
return (this._match.media === 'not all' || this._match.media === 'invalid');
},
get: function() {
if (this._match) {
return this._match;
}
this._match = window.matchMedia(this.media());
return this._match;
},
_fixWebkitCallback: function(selector) {
var style = window.document.createElement('style');
window.document.getElementsByTagName('head')[0].appendChild(style);
if (!window.createPopup) { /* For Safari */
style.appendChild(window.document.createTextNode(''));
}
var s = window.document.styleSheets[document.styleSheets.length - 1];
s.insertRule('@media ' + selector + '{body{}}', s.cssRules.length);
}
};
mQuery.Lib = {
bind: function(scope, fn) {
var args = Array.prototype.slice.call(arguments, 2);
return function () {
return fn.apply(scope, args.concat(Array.prototype.slice.call(arguments)));
};
},
query: function(query, features) {
if (!query) {
return '';
}
if (typeof query === 'object') {
return mQuery.Lib.features(query);
}
var string = '';
if (typeof query === 'string') {
string += query;
}
if (typeof features === 'object') {
string += ' and ' + mQuery.Lib.features(features);
}
return string;
},
// http://dvcs.w3.org/hg/cssom/raw-file/tip/Overview.html#serialize-a-media-query-list
features: function(features) {
var ret = [];
for (var feature in features) {
if (features.hasOwnProperty(feature)) {
ret.push(mQuery.Lib.feature(feature, features[feature]));
}
}
return ret.join(' and ');
},
feature: function(feature, value) {
feature = mQuery.Lib.uncamel(feature);
if (typeof value === 'function') {
value = value(feature);
}
if (value === 0) {
return '(' + feature + ': 0)';
}
return '(' + feature + (value ? ': ' + value : '') + ')';
},
uncamel: function(string) {
return string.replace(/[A-Z]/g, mQuery.Lib.uncamelSub);
},
uncamelSub: function(letter) {
return '-' + letter.toLowerCase();
}
};
/* Modified to check for msMatchMedia.*/
/*! matchMedia() polyfill - Test a CSS media type/query in JS. Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas. Dual MIT/BSD license */
window.matchMedia = window.matchMedia || window.msMatchMedia || (function(doc, undefined){
var bool,
docElem = doc.documentElement,
refNode = docElem.firstElementChild || docElem.firstChild,
// fakeBody required for <FF4 when executed in <head>
fakeBody = doc.createElement('body'),
div = doc.createElement('div');
div.id = 'mq-test-1';
div.style.cssText = "position:absolute;top:-100em";
fakeBody.appendChild(div);
return function(q){
div.innerHTML = '­<style media="'+q+'"> #mq-test-1 { width: 42px; }</style>';
docElem.insertBefore(fakeBody, refNode);
bool = div.offsetWidth == 42;
docElem.removeChild(fakeBody);
return { matches: bool, media: q };
};
})(document);