Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

respond.matchmedia.addListener.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. /*! matchMedia() polyfill addListener/removeListener extension. Author & copyright (c) 2012: Scott Jehl. Dual MIT/BSD license */
  24. (function(w) {
  25. "use strict";
  26. if (w.matchMedia && w.matchMedia("all").addListener) {
  27. return false;
  28. }
  29. var localMatchMedia = w.matchMedia, hasMediaQueries = localMatchMedia("only all").matches, isListening = false, timeoutID = 0, queries = [], handleChange = function(evt) {
  30. w.clearTimeout(timeoutID);
  31. timeoutID = w.setTimeout(function() {
  32. for (var i = 0, il = queries.length; i < il; i++) {
  33. var mql = queries[i].mql, listeners = queries[i].listeners || [], matches = localMatchMedia(mql.media).matches;
  34. if (matches !== mql.matches) {
  35. mql.matches = matches;
  36. for (var j = 0, jl = listeners.length; j < jl; j++) {
  37. listeners[j].call(w, mql);
  38. }
  39. }
  40. }
  41. }, 30);
  42. };
  43. w.matchMedia = function(media) {
  44. var mql = localMatchMedia(media), listeners = [], index = 0;
  45. mql.addListener = function(listener) {
  46. if (!hasMediaQueries) {
  47. return;
  48. }
  49. if (!isListening) {
  50. isListening = true;
  51. w.addEventListener("resize", handleChange, true);
  52. }
  53. if (index === 0) {
  54. index = queries.push({
  55. mql: mql,
  56. listeners: listeners
  57. });
  58. }
  59. listeners.push(listener);
  60. };
  61. mql.removeListener = function(listener) {
  62. for (var i = 0, il = listeners.length; i < il; i++) {
  63. if (listeners[i] === listener) {
  64. listeners.splice(i, 1);
  65. }
  66. }
  67. };
  68. return mql;
  69. };
  70. })(this);
  71. /*! Respond.js v1.4.0: min/max-width media query polyfill. (c) Scott Jehl. MIT Lic. j.mp/respondjs */
  72. (function(w) {
  73. "use strict";
  74. var respond = {};
  75. w.respond = respond;
  76. respond.update = function() {};
  77. var requestQueue = [], xmlHttp = function() {
  78. var xmlhttpmethod = false;
  79. try {
  80. xmlhttpmethod = new w.XMLHttpRequest();
  81. } catch (e) {
  82. xmlhttpmethod = new w.ActiveXObject("Microsoft.XMLHTTP");
  83. }
  84. return function() {
  85. return xmlhttpmethod;
  86. };
  87. }(), ajax = function(url, callback) {
  88. var req = xmlHttp();
  89. if (!req) {
  90. return;
  91. }
  92. req.open("GET", url, true);
  93. req.onreadystatechange = function() {
  94. if (req.readyState !== 4 || req.status !== 200 && req.status !== 304) {
  95. return;
  96. }
  97. callback(req.responseText);
  98. };
  99. if (req.readyState === 4) {
  100. return;
  101. }
  102. req.send(null);
  103. };
  104. respond.ajax = ajax;
  105. respond.queue = requestQueue;
  106. respond.regex = {
  107. media: /@media[^\{]+\{([^\{\}]*\{[^\}\{]*\})+/gi,
  108. keyframes: /@(?:\-(?:o|moz|webkit)\-)?keyframes[^\{]+\{(?:[^\{\}]*\{[^\}\{]*\})+[^\}]*\}/gi,
  109. urls: /(url\()['"]?([^\/\)'"][^:\)'"]+)['"]?(\))/g,
  110. findStyles: /@media *([^\{]+)\{([\S\s]+?)$/,
  111. only: /(only\s+)?([a-zA-Z]+)\s?/,
  112. minw: /\([\s]*min\-width\s*:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/,
  113. maxw: /\([\s]*max\-width\s*:[\s]*([\s]*[0-9\.]+)(px|em)[\s]*\)/
  114. };
  115. respond.mediaQueriesSupported = w.matchMedia && w.matchMedia("only all") !== null && w.matchMedia("only all").matches;
  116. if (respond.mediaQueriesSupported) {
  117. return;
  118. }
  119. 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() {
  120. var ret, div = doc.createElement("div"), body = doc.body, originalHTMLFontSize = docElem.style.fontSize, originalBodyFontSize = body && body.style.fontSize, fakeUsed = false;
  121. div.style.cssText = "position:absolute;font-size:1em;width:1em";
  122. if (!body) {
  123. body = fakeUsed = doc.createElement("body");
  124. body.style.background = "none";
  125. }
  126. docElem.style.fontSize = "100%";
  127. body.style.fontSize = "100%";
  128. body.appendChild(div);
  129. if (fakeUsed) {
  130. docElem.insertBefore(body, docElem.firstChild);
  131. }
  132. ret = div.offsetWidth;
  133. if (fakeUsed) {
  134. docElem.removeChild(body);
  135. } else {
  136. body.removeChild(div);
  137. }
  138. docElem.style.fontSize = originalHTMLFontSize;
  139. if (originalBodyFontSize) {
  140. body.style.fontSize = originalBodyFontSize;
  141. }
  142. ret = eminpx = parseFloat(ret);
  143. return ret;
  144. }, applyMedia = function(fromResize) {
  145. 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();
  146. if (fromResize && lastCall && now - lastCall < resizeThrottle) {
  147. w.clearTimeout(resizeDefer);
  148. resizeDefer = w.setTimeout(applyMedia, resizeThrottle);
  149. return;
  150. } else {
  151. lastCall = now;
  152. }
  153. for (var i in mediastyles) {
  154. if (mediastyles.hasOwnProperty(i)) {
  155. var thisstyle = mediastyles[i], min = thisstyle.minw, max = thisstyle.maxw, minnull = min === null, maxnull = max === null, em = "em";
  156. if (!!min) {
  157. min = parseFloat(min) * (min.indexOf(em) > -1 ? eminpx || getEmValue() : 1);
  158. }
  159. if (!!max) {
  160. max = parseFloat(max) * (max.indexOf(em) > -1 ? eminpx || getEmValue() : 1);
  161. }
  162. if (!thisstyle.hasquery || (!minnull || !maxnull) && (minnull || currWidth >= min) && (maxnull || currWidth <= max)) {
  163. if (!styleBlocks[thisstyle.media]) {
  164. styleBlocks[thisstyle.media] = [];
  165. }
  166. styleBlocks[thisstyle.media].push(rules[thisstyle.rules]);
  167. }
  168. }
  169. }
  170. for (var j in appendedEls) {
  171. if (appendedEls.hasOwnProperty(j)) {
  172. if (appendedEls[j] && appendedEls[j].parentNode === head) {
  173. head.removeChild(appendedEls[j]);
  174. }
  175. }
  176. }
  177. appendedEls.length = 0;
  178. for (var k in styleBlocks) {
  179. if (styleBlocks.hasOwnProperty(k)) {
  180. var ss = doc.createElement("style"), css = styleBlocks[k].join("\n");
  181. ss.type = "text/css";
  182. ss.media = k;
  183. head.insertBefore(ss, lastLink.nextSibling);
  184. if (ss.styleSheet) {
  185. ss.styleSheet.cssText = css;
  186. } else {
  187. ss.appendChild(doc.createTextNode(css));
  188. }
  189. appendedEls.push(ss);
  190. }
  191. }
  192. }, translate = function(styles, href, media) {
  193. var qs = styles.replace(respond.regex.keyframes, "").match(respond.regex.media), ql = qs && qs.length || 0;
  194. href = href.substring(0, href.lastIndexOf("/"));
  195. var repUrls = function(css) {
  196. return css.replace(respond.regex.urls, "$1" + href + "$2$3");
  197. }, useMedia = !ql && media;
  198. if (href.length) {
  199. href += "/";
  200. }
  201. if (useMedia) {
  202. ql = 1;
  203. }
  204. for (var i = 0; i < ql; i++) {
  205. var fullq, thisq, eachq, eql;
  206. if (useMedia) {
  207. fullq = media;
  208. rules.push(repUrls(styles));
  209. } else {
  210. fullq = qs[i].match(respond.regex.findStyles) && RegExp.$1;
  211. rules.push(RegExp.$2 && repUrls(RegExp.$2));
  212. }
  213. eachq = fullq.split(",");
  214. eql = eachq.length;
  215. for (var j = 0; j < eql; j++) {
  216. thisq = eachq[j];
  217. mediastyles.push({
  218. media: thisq.split("(")[0].match(respond.regex.only) && RegExp.$2 || "all",
  219. rules: rules.length - 1,
  220. hasquery: thisq.indexOf("(") > -1,
  221. minw: thisq.match(respond.regex.minw) && parseFloat(RegExp.$1) + (RegExp.$2 || ""),
  222. maxw: thisq.match(respond.regex.maxw) && parseFloat(RegExp.$1) + (RegExp.$2 || "")
  223. });
  224. }
  225. }
  226. applyMedia();
  227. }, makeRequests = function() {
  228. if (requestQueue.length) {
  229. var thisRequest = requestQueue.shift();
  230. ajax(thisRequest.href, function(styles) {
  231. translate(styles, thisRequest.href, thisRequest.media);
  232. parsedSheets[thisRequest.href] = true;
  233. w.setTimeout(function() {
  234. makeRequests();
  235. }, 0);
  236. });
  237. }
  238. }, ripCSS = function() {
  239. for (var i = 0; i < links.length; i++) {
  240. var sheet = links[i], href = sheet.href, media = sheet.media, isCSS = sheet.rel && sheet.rel.toLowerCase() === "stylesheet";
  241. if (!!href && isCSS && !parsedSheets[href]) {
  242. if (sheet.styleSheet && sheet.styleSheet.rawCssText) {
  243. translate(sheet.styleSheet.rawCssText, href, media);
  244. parsedSheets[href] = true;
  245. } else {
  246. if (!/^([a-zA-Z:]*\/\/)/.test(href) && !base || href.replace(RegExp.$1, "").split("/")[0] === w.location.host) {
  247. if (href.substring(0, 2) === "//") {
  248. href = w.location.protocol + href;
  249. }
  250. requestQueue.push({
  251. href: href,
  252. media: media
  253. });
  254. }
  255. }
  256. }
  257. }
  258. makeRequests();
  259. };
  260. ripCSS();
  261. respond.update = ripCSS;
  262. respond.getEmValue = getEmValue;
  263. function callMedia() {
  264. applyMedia(true);
  265. }
  266. if (w.addEventListener) {
  267. w.addEventListener("resize", callMedia, false);
  268. } else if (w.attachEvent) {
  269. w.attachEvent("onresize", callMedia);
  270. }
  271. })(this);