forked from scottjehl/Respond
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrespond.src.js
245 lines (224 loc) · 7.16 KB
/
respond.src.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
/*
* respond.js - A small and fast polyfill for min/max-width CSS3 Media Queries
* Copyright 2011, Scott Jehl, scottjehl.com
* Dual licensed under the MIT or GPL Version 2 licenses.
* Usage: Check out the readme file or github.com/scottjehl/respond
*/
(function( win, mqSupported ){
//exposed namespace
win.respond = {};
//define update even in native-mq-supporting browsers, to avoid errors
respond.update = function(){};
//if media queries are supported, exit here
if( mqSupported ){ return; }
//define vars
var doc = win.document,
docElem = doc.documentElement,
mediastyles = [],
parsedSheets = [],
resizeThrottle = 0,
head = doc.getElementsByTagName( "head" )[0] || docElem,
links = head.getElementsByTagName( "link" ),
//loop stylesheets, send text content to translateQueries
ripCSS = function(){
var sheets = doc.styleSheets,
sl = sheets.length;
for( var i = 0; i < sl; i++ ){
var sheet = sheets[ i ],
href = sheet.href,
parsed = false;
//only links plz
if( !!href ){
//prevent re-parsing when ripCSS is re-called
for( var i in parsedSheets ){
if( parsedSheets[ i ] === href ){
parsed = true;
}
}
if( !parsed ){
ajax( href, function( styles ){
translateQueries( styles, href );
parsedSheets.push( href );
} );
}
}
}
},
//find media blocks in css text, convert to style blocks
translateQueries = function( styles, href ){
var qs = styles.match(/@media .*{([\S\s]+?)(?=\}\/\*\/mediaquery\*\/)/gmi),
ql = qs && qs.length || 0,
href = href.substring( 0, href.lastIndexOf( "/" )) + "/";
for( var i = 0; i < ql; i++ ){
var fullq = qs[ i ].match(/(@media |,\s?)(.*)\{([\S\s]+?)$/) && RegExp.$2,
eachq = fullq.split(","),
rules = RegExp.$3;
for( var j = 0; j < eachq.length; j++ ){
var thisq = eachq[ j ],
type = thisq.match(/(only )?([a-z]+)(\sand)?/) && RegExp.$2,
minw = thisq.match(/\(min\-width:\s?(\s?[0-9]+)px\s?\)/) && RegExp.$1,
maxw = thisq.match(/\(max\-width:\s?(\s?[0-9]+)px\s?\)/) && RegExp.$1;
//only translate queries that have a type + a min or a max width
if( type ){
var ss = doc.createElement( "style" ),
placehold = doc.createTextNode( "" ),
minw = parseFloat( minw ),
maxw = parseFloat( maxw ),
//replace relative URLs with stylesheet's base path
//hat tip: css3mediaqueries lib for regexp gotcha
rules = rules.replace( /(url\()['"]?([^\/\)'"][^:\)'"]+)['"]?(\))/g, "$1" + href + "$2$3" );
//must set type for IE
ss.type = "text/css";
ss.media = type;
if ( ss.styleSheet ){
ss.styleSheet.cssText = rules;
}
else {
ss.appendChild( doc.createTextNode( rules ) );
}
//append in order to maintain cascade
var lastSS = mediastyles.length && mediastyles[ mediastyles.length-1 ].ss || links[ links.length-1 ];
if( head.lastchild == lastSS ) {
head.appendChild( ss );
} else {
head.insertBefore( ss, lastSS.nextSibling );
}
mediastyles.push( {
"ss" : ss,
"minw" : minw,
"maxw" : maxw,
"placehold" : placehold
} );
}
}
applyMedia();
}
},
lastCall,
//enable/disable style blocks based on win width
applyMedia = function( fromResize ){
//throttle resize calls
var now = (new Date()).getTime();
if( fromResize && lastCall && now - lastCall < resizeThrottle ){
return;
}
else {
lastCall = now;
}
var name = "clientWidth",
docElemProp = docElem[ name ],
currWidth = doc.compatMode === "CSS1Compat" && docElemProp || doc.body[ name ] || docElemProp,
//loop whole array or just one item
loopStyles = fromResize ? mediastyles : [ mediastyles[ mediastyles.length-1 ] ];
for( var i in loopStyles ){
var thisstyle = loopStyles[ i ];
if( !thisstyle.minw && !thisstyle.maxq ||
( !thisstyle.minw || thisstyle.minw && currWidth >= thisstyle.minw ) &&
(!thisstyle.maxw || thisstyle.maxw && currWidth <= thisstyle.maxw ) ){
if( thisstyle.placehold.parentNode === head ){
head.insertBefore( thisstyle.ss, thisstyle.placehold );
head.removeChild( thisstyle.placehold );
}
}
else {
if( thisstyle.ss.parentNode === head ){
head.insertBefore( thisstyle.placehold, thisstyle.ss );
head.removeChild( thisstyle.ss );
}
}
}
},
//tweaked Ajax functions from Quirksmode
ajax = function( url, callback ) {
var req = xmlHttp();
if (!req){
return;
}
req.open( "GET", url, true );
req.onreadystatechange = function () {
if ( req.readyState != 4 || req.status != 200 && req.status != 304 ){
return;
}
callback(req.responseText);
}
if (req.readyState == 4){
return;
}
req.send();
},
//define ajax obj
xmlHttp = (function() {
var xmlhttpmethod = false,
attempts = [
function(){ return new ActiveXObject("Microsoft.XMLHTTP") },
function(){ return new ActiveXObject("Msxml3.XMLHTTP") },
function(){ return new ActiveXObject("Msxml2.XMLHTTP") },
function(){ return new XMLHttpRequest() }
],
al = attempts.length;
while( al-- ){
try {
xmlhttpmethod = attempts[ al ]();
}
catch(e) {
continue;
}
break;
}
return function(){
return xmlhttpmethod;
};
})();
//translate CSS
ripCSS();
//expose update for re-running respond later on
respond.update = ripCSS;
//adjust on resize
function callMedia(){
applyMedia( true );
}
if( win.addEventListener ){
win.addEventListener( "resize", callMedia, false );
}
else if( win.attachEvent ){
win.attachEvent( "onresize", callMedia );
}
})(
this,
(function( win ){
//cond. comm. IE check by James Padolsey
var ie = (function(undef){
var v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while(div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->', all[0]);
return v > 4 ? v : undef;
}());
//for speed, flag browsers with window.matchMedia support and IE 9 as supported
if( win.matchMedia || ie && ie >= 9 ){ return true; }
//flag IE 8 and under as false - no test needed
if( ie && ie <= 8){ return false; }
//otherwise proceed with test
var doc = win.document,
docElem = doc.documentElement,
fb = doc.createElement( "body" ),
div = doc.createElement( "div" ),
se = doc.createElement( "style" ),
cssrule = "@media only all { #qtest { position: absolute; } }";
div.setAttribute( "id", "qtest" );
se.type = "text/css";
fb.appendChild( div );
if ( se.styleSheet ){
se.styleSheet.cssText = cssrule;
}
else {
se.appendChild(doc.createTextNode(cssrule));
}
docElem.insertBefore( fb, docElem.firstChild );
docElem.insertBefore( se, fb );
support = ( win.getComputedStyle ? win.getComputedStyle( div,null ) : div.currentStyle )["position"] == "absolute";
docElem.removeChild( fb );
docElem.removeChild( se );
return support;
})( this )
);