|
| 1 | +/*! matchMedia() polyfill - Test a CSS media type/query in JS. Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas, David Knight. MIT license */ |
| 2 | + |
| 3 | +window.matchMedia || (window.matchMedia = function() { |
| 4 | + "use strict"; |
| 5 | + |
| 6 | + // For browsers that support matchMedium api such as IE 9 and webkit |
| 7 | + var styleMedia = (window.styleMedia || window.media); |
| 8 | + |
| 9 | + // For those that don't support matchMedium |
| 10 | + if (!styleMedia) { |
| 11 | + var style = document.createElement('style'), |
| 12 | + script = document.getElementsByTagName('script')[0], |
| 13 | + info = null; |
| 14 | + |
| 15 | + style.type = 'text/css'; |
| 16 | + style.id = 'matchmediajs-test'; |
| 17 | + |
| 18 | + if (!script) { |
| 19 | + document.head.appendChild(style); |
| 20 | + } else { |
| 21 | + script.parentNode.insertBefore(style, script); |
| 22 | + } |
| 23 | + |
| 24 | + // 'style.currentStyle' is used by IE <= 8 and 'window.getComputedStyle' for all other browsers |
| 25 | + info = ('getComputedStyle' in window) && window.getComputedStyle(style, null) || style.currentStyle; |
| 26 | + |
| 27 | + styleMedia = { |
| 28 | + matchMedium: function(media) { |
| 29 | + var text = '@media ' + media + '{ #matchmediajs-test { width: 1px; } }'; |
| 30 | + |
| 31 | + // 'style.styleSheet' is used by IE <= 8 and 'style.textContent' for all other browsers |
| 32 | + if (style.styleSheet) { |
| 33 | + style.styleSheet.cssText = text; |
| 34 | + } else { |
| 35 | + style.textContent = text; |
| 36 | + } |
| 37 | + |
| 38 | + // Test if media query is true or false |
| 39 | + return info.width === '1px'; |
| 40 | + } |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + return function(media) { |
| 45 | + return { |
| 46 | + matches: styleMedia.matchMedium(media || 'all'), |
| 47 | + media: media || 'all' |
| 48 | + }; |
| 49 | + }; |
| 50 | +}()); |
| 51 | + |
| 52 | +/*! matchMedia() polyfill addListener/removeListener extension. Author & copyright (c) 2012: Scott Jehl. Dual MIT/BSD license */ |
| 53 | +(function() { |
| 54 | + // Bail out for browsers that have addListener support |
| 55 | + if (window.matchMedia && window.matchMedia('all').addListener) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + var localMatchMedia = window.matchMedia, |
| 60 | + hasMediaQueries = localMatchMedia('only all').matches, |
| 61 | + isListening = false, |
| 62 | + timeoutID = 0, // setTimeout for debouncing 'handleChange' |
| 63 | + queries = [], // Contains each 'mql' and associated 'listeners' if 'addListener' is used |
| 64 | + handleChange = function(evt) { |
| 65 | + // Debounce |
| 66 | + clearTimeout(timeoutID); |
| 67 | + |
| 68 | + timeoutID = setTimeout(function() { |
| 69 | + for (var i = 0, il = queries.length; i < il; i++) { |
| 70 | + var mql = queries[i].mql, |
| 71 | + listeners = queries[i].listeners || [], |
| 72 | + matches = localMatchMedia(mql.media).matches; |
| 73 | + |
| 74 | + // Update mql.matches value and call listeners |
| 75 | + // Fire listeners only if transitioning to or from matched state |
| 76 | + if (matches !== mql.matches) { |
| 77 | + mql.matches = matches; |
| 78 | + |
| 79 | + for (var j = 0, jl = listeners.length; j < jl; j++) { |
| 80 | + listeners[j].call(window, mql); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + }, 30); |
| 85 | + }; |
| 86 | + |
| 87 | + window.matchMedia = function(media) { |
| 88 | + var mql = localMatchMedia(media), |
| 89 | + listeners = [], |
| 90 | + index = 0; |
| 91 | + |
| 92 | + mql.addListener = function(listener) { |
| 93 | + // Changes would not occur to css media type so return now (Affects IE <= 8) |
| 94 | + if (!hasMediaQueries) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // Set up 'resize' listener for browsers that support CSS3 media queries (Not for IE <= 8) |
| 99 | + // There should only ever be 1 resize listener running for performance |
| 100 | + if (!isListening) { |
| 101 | + isListening = true; |
| 102 | + window.addEventListener('resize', handleChange, true); |
| 103 | + } |
| 104 | + |
| 105 | + // Push object only if it has not been pushed already |
| 106 | + if (index === 0) { |
| 107 | + index = queries.push({ |
| 108 | + mql: mql, |
| 109 | + listeners: listeners |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + listeners.push(listener); |
| 114 | + }; |
| 115 | + |
| 116 | + mql.removeListener = function(listener) { |
| 117 | + for (var i = 0, il = listeners.length; i < il; i++) { |
| 118 | + if (listeners[i] === listener) { |
| 119 | + listeners.splice(i, 1); |
| 120 | + } |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + return mql; |
| 125 | + }; |
| 126 | +}()); |
| 127 | + |
| 128 | +window.mediaCheck = function(options) { |
| 129 | + var mq; |
| 130 | + |
| 131 | + function mqChange(mq, options) { |
| 132 | + if (mq.matches) { |
| 133 | + if (typeof options.entry === "function") { |
| 134 | + options.entry(); |
| 135 | + } |
| 136 | + } else if (typeof options.exit === "function") { |
| 137 | + options.exit(); |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + mq = window.matchMedia(options.media); |
| 142 | + |
| 143 | + mq.addListener(function() { |
| 144 | + mqChange(mq, options); |
| 145 | + }); |
| 146 | + |
| 147 | + mqChange(mq, options); |
| 148 | +}; |
0 commit comments