respond.src.js 8.6 KB

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