| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
(function(w) { |
| 9 |
"use strict"; |
| 10 |
w.matchMedia = w.matchMedia || function(doc, undefined) { |
| 11 |
var bool, docElem = doc.documentElement, refNode = docElem.firstElementChild || docElem.firstChild, fakeBody = doc.createElement("body"), div = doc.createElement("div"); |
| 12 |
div.id = "mq-test-1"; |
| 13 |
div.style.cssText = "position:absolute;top:-100em"; |
| 14 |
fakeBody.style.background = "none"; |
| 15 |
fakeBody.appendChild(div); |
| 16 |
return function(q) { |
| 17 |
div.innerHTML = '­<style media="' + q + '"> #mq-test-1 { width: 42px; }</style>'; |
| 18 |
docElem.insertBefore(fakeBody, refNode); |
| 19 |
bool = div.offsetWidth === 42; |
| 20 |
docElem.removeChild(fakeBody); |
| 21 |
return { |
| 22 |
matches: bool, |
| 23 |
media: q |
| 24 |
}; |
| 25 |
}; |
| 26 |
}(w.document); |
| 27 |
})(this); |
| 28 |
|
| 29 |
(function(w) { |
| 30 |
"use strict"; |
| 31 |
var respond = {}; |
| 32 |
w.respond = respond; |
| 33 |
respond.update = function() {}; |
| 34 |
var requestQueue = [], xmlHttp = function() { |
| 35 |
var xmlhttpmethod = false; |
| 36 |
try { |
| 37 |
xmlhttpmethod = new w.XMLHttpRequest(); |
| 38 |
} catch (e) { |
| 39 |
xmlhttpmethod = new w.ActiveXObject("Microsoft.XMLHTTP"); |
| 40 |
} |
| 41 |
return function() { |
| 42 |
return xmlhttpmethod; |
| 43 |
}; |
| 44 |
}(), ajax = function(url, callback) { |
| 45 |
var req = xmlHttp(); |
| 46 |
if (!req) { |
| 47 |
return; |
| 48 |
} |
| 49 |
req.open("GET", url, true); |
| 50 |
req.onreadystatechange = function() { |
| 51 |
if (req.readyState !== 4 || req.status !== 200 && req.status !== 304) { |
| 52 |
return; |
| 53 |
} |
| 54 |
callback(req.responseText); |
| 55 |
}; |
| 56 |
if (req.readyState === 4) { |
| 57 |
return; |
| 58 |
} |
| 59 |
req.send(null); |
| 60 |
}, isUnsupportedMediaQuery = function(query) { |
| 61 |
return query.replace(respond.regex.minmaxwh, "").match(respond.regex.other); |
| 62 |
}; |
| 63 |
respond.ajax = ajax; |
| 64 |
respond.queue = requestQueue; |
| 65 |
respond.unsupportedmq = isUnsupportedMediaQuery; |
| 66 |
respond.regex = { |
| 67 |
media: /@media[^\{]+\{([^\{\}]*\{[^\}\{]*\})+/gi, |
| 68 |
keyframes: /@(?:\-(?:o|moz|webkit)\-)?keyframes[^\{]+\{(?:[^\{\}]*\{[^\}\{]*\})+[^\}]*\}/gi, |
| 69 |
comments: /\/\*[^*]*\*+([^/][^*]*\*+)*\//gi, |
| 70 |
urls: /(url\()['"]?([^\/\)'"][^:\)'"]+)['"]?(\))/g, |
| 71 |
findStyles: /@media *([^\{]+)\{([\S\s]+?)$/, |
| 72 |
only: /(only\s+)?([a-zA-Z]+)\s?/, |
| 73 |
minw: /\(\s*min\-width\s*:\s*(\s*[0-9\.]+)(px|em)\s*\)/, |
| 74 |
maxw: /\(\s*max\-width\s*:\s*(\s*[0-9\.]+)(px|em)\s*\)/, |
| 75 |
minmaxwh: /\(\s*m(in|ax)\-(height|width)\s*:\s*(\s*[0-9\.]+)(px|em)\s*\)/gi, |
| 76 |
other: /\([^\)]*\)/g |
| 77 |
}; |
| 78 |
respond.mediaQueriesSupported = w.matchMedia && w.matchMedia("only all") !== null && w.matchMedia("only all").matches; |
| 79 |
if (respond.mediaQueriesSupported) { |
| 80 |
return; |
| 81 |
} |
| 82 |
var doc = w.document, docElem = doc.documentElement, mediastyles = [], rules = [], appendedEls = [], parsedSheets = {}, resizeThrottle = 30, head = doc.getElementsByTagName("head")[0] || docElem, base = doc.getElementsByTagName("base")[0], links = head.getElementsByTagName("link"), lastCall, resizeDefer, eminpx, getEmValue = function() { |
| 83 |
var ret, div = doc.createElement("div"), body = doc.body, originalHTMLFontSize = docElem.style.fontSize, originalBodyFontSize = body && body.style.fontSize, fakeUsed = false; |
| 84 |
div.style.cssText = "position:absolute;font-size:1em;width:1em"; |
| 85 |
if (!body) { |
| 86 |
body = fakeUsed = doc.createElement("body"); |
| 87 |
body.style.background = "none"; |
| 88 |
} |
| 89 |
docElem.style.fontSize = "100%"; |
| 90 |
body.style.fontSize = "100%"; |
| 91 |
body.appendChild(div); |
| 92 |
if (fakeUsed) { |
| 93 |
docElem.insertBefore(body, docElem.firstChild); |
| 94 |
} |
| 95 |
ret = div.offsetWidth; |
| 96 |
if (fakeUsed) { |
| 97 |
docElem.removeChild(body); |
| 98 |
} else { |
| 99 |
body.removeChild(div); |
| 100 |
} |
| 101 |
docElem.style.fontSize = originalHTMLFontSize; |
| 102 |
if (originalBodyFontSize) { |
| 103 |
body.style.fontSize = originalBodyFontSize; |
| 104 |
} |
| 105 |
ret = eminpx = parseFloat(ret); |
| 106 |
return ret; |
| 107 |
}, applyMedia = function(fromResize) { |
| 108 |
var name = "clientWidth", docElemProp = docElem[name], currWidth = doc.compatMode === "CSS1Compat" && docElemProp || doc.body[name] || docElemProp, styleBlocks = {}, lastLink = links[links.length - 1], now = new Date().getTime(); |
| 109 |
if (fromResize && lastCall && now - lastCall < resizeThrottle) { |
| 110 |
w.clearTimeout(resizeDefer); |
| 111 |
resizeDefer = w.setTimeout(applyMedia, resizeThrottle); |
| 112 |
return; |
| 113 |
} else { |
| 114 |
lastCall = now; |
| 115 |
} |
| 116 |
for (var i in mediastyles) { |
| 117 |
if (mediastyles.hasOwnProperty(i)) { |
| 118 |
var thisstyle = mediastyles[i], min = thisstyle.minw, max = thisstyle.maxw, minnull = min === null, maxnull = max === null, em = "em"; |
| 119 |
if (!!min) { |
| 120 |
min = parseFloat(min) * (min.indexOf(em) > -1 ? eminpx || getEmValue() : 1); |
| 121 |
} |
| 122 |
if (!!max) { |
| 123 |
max = parseFloat(max) * (max.indexOf(em) > -1 ? eminpx || getEmValue() : 1); |
| 124 |
} |
| 125 |
if (!thisstyle.hasquery || (!minnull || !maxnull) && (minnull || currWidth >= min) && (maxnull || currWidth <= max)) { |
| 126 |
if (!styleBlocks[thisstyle.media]) { |
| 127 |
styleBlocks[thisstyle.media] = []; |
| 128 |
} |
| 129 |
styleBlocks[thisstyle.media].push(rules[thisstyle.rules]); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
for (var j in appendedEls) { |
| 134 |
if (appendedEls.hasOwnProperty(j)) { |
| 135 |
if (appendedEls[j] && appendedEls[j].parentNode === head) { |
| 136 |
head.removeChild(appendedEls[j]); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
appendedEls.length = 0; |
| 141 |
for (var k in styleBlocks) { |
| 142 |
if (styleBlocks.hasOwnProperty(k)) { |
| 143 |
var ss = doc.createElement("style"), css = styleBlocks[k].join("\n"); |
| 144 |
ss.type = "text/css"; |
| 145 |
ss.media = k; |
| 146 |
head.insertBefore(ss, lastLink.nextSibling); |
| 147 |
if (ss.styleSheet) { |
| 148 |
ss.styleSheet.cssText = css; |
| 149 |
} else { |
| 150 |
ss.appendChild(doc.createTextNode(css)); |
| 151 |
} |
| 152 |
appendedEls.push(ss); |
| 153 |
} |
| 154 |
} |
| 155 |
}, translate = function(styles, href, media) { |
| 156 |
var qs = styles.replace(respond.regex.comments, "").replace(respond.regex.keyframes, "").match(respond.regex.media), ql = qs && qs.length || 0; |
| 157 |
href = href.substring(0, href.lastIndexOf("/")); |
| 158 |
var repUrls = function(css) { |
| 159 |
return css.replace(respond.regex.urls, "$1" + href + "$2$3"); |
| 160 |
}, useMedia = !ql && media; |
| 161 |
if (href.length) { |
| 162 |
href += "/"; |
| 163 |
} |
| 164 |
if (useMedia) { |
| 165 |
ql = 1; |
| 166 |
} |
| 167 |
for (var i = 0; i < ql; i++) { |
| 168 |
var fullq, thisq, eachq, eql; |
| 169 |
if (useMedia) { |
| 170 |
fullq = media; |
| 171 |
rules.push(repUrls(styles)); |
| 172 |
} else { |
| 173 |
fullq = qs[i].match(respond.regex.findStyles) && RegExp.$1; |
| 174 |
rules.push(RegExp.$2 && repUrls(RegExp.$2)); |
| 175 |
} |
| 176 |
eachq = fullq.split(","); |
| 177 |
eql = eachq.length; |
| 178 |
for (var j = 0; j < eql; j++) { |
| 179 |
thisq = eachq[j]; |
| 180 |
if (isUnsupportedMediaQuery(thisq)) { |
| 181 |
continue; |
| 182 |
} |
| 183 |
mediastyles.push({ |
| 184 |
media: thisq.split("(")[0].match(respond.regex.only) && RegExp.$2 || "all", |
| 185 |
rules: rules.length - 1, |
| 186 |
hasquery: thisq.indexOf("(") > -1, |
| 187 |
minw: thisq.match(respond.regex.minw) && parseFloat(RegExp.$1) + (RegExp.$2 || ""), |
| 188 |
maxw: thisq.match(respond.regex.maxw) && parseFloat(RegExp.$1) + (RegExp.$2 || "") |
| 189 |
}); |
| 190 |
} |
| 191 |
} |
| 192 |
applyMedia(); |
| 193 |
}, makeRequests = function() { |
| 194 |
if (requestQueue.length) { |
| 195 |
var thisRequest = requestQueue.shift(); |
| 196 |
ajax(thisRequest.href, function(styles) { |
| 197 |
translate(styles, thisRequest.href, thisRequest.media); |
| 198 |
parsedSheets[thisRequest.href] = true; |
| 199 |
w.setTimeout(function() { |
| 200 |
makeRequests(); |
| 201 |
}, 0); |
| 202 |
}); |
| 203 |
} |
| 204 |
}, ripCSS = function() { |
| 205 |
for (var i = 0; i < links.length; i++) { |
| 206 |
var sheet = links[i], href = sheet.href, media = sheet.media, isCSS = sheet.rel && sheet.rel.toLowerCase() === "stylesheet"; |
| 207 |
if (!!href && isCSS && !parsedSheets[href]) { |
| 208 |
if (sheet.styleSheet && sheet.styleSheet.rawCssText) { |
| 209 |
translate(sheet.styleSheet.rawCssText, href, media); |
| 210 |
parsedSheets[href] = true; |
| 211 |
} else { |
| 212 |
if (!/^([a-zA-Z:]*\/\/)/.test(href) && !base || href.replace(RegExp.$1, "").split("/")[0] === w.location.host) { |
| 213 |
if (href.substring(0, 2) === "//") { |
| 214 |
href = w.location.protocol + href; |
| 215 |
} |
| 216 |
requestQueue.push({ |
| 217 |
href: href, |
| 218 |
media: media |
| 219 |
}); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
makeRequests(); |
| 225 |
}; |
| 226 |
ripCSS(); |
| 227 |
respond.update = ripCSS; |
| 228 |
respond.getEmValue = getEmValue; |
| 229 |
function callMedia() { |
| 230 |
applyMedia(true); |
| 231 |
} |
| 232 |
if (w.addEventListener) { |
| 233 |
w.addEventListener("resize", callMedia, false); |
| 234 |
} else if (w.attachEvent) { |
| 235 |
w.attachEvent("onresize", callMedia); |
| 236 |
} |
| 237 |
})(this); |