|
1bba387
Scott Jehl authored
|
||
| 1 | /*! matchMedia() polyfill addListener/removeListener extension. Author & copyright (c) 2012: Scott Jehl. Dual MIT/BSD license */ | |
| 2 | (function(){ | |
|
0903df1
knightdr authored
|
||
| 3 | // Bail out for browsers that have addListener support | |
| 4 | if (window.matchMedia && window.matchMedia('all').addListener) { | |
| 5 | return false; | |
| 6 | } | |
| 7 | ||
| 8 | var localMatchMedia = window.matchMedia, | |
| 9 | hasMediaQueries = localMatchMedia('only all').matches, | |
| 10 | isListening = false, | |
| 11 | timeoutID = 0, // setTimeout for debouncing 'handleChange' | |
| 12 | queries = [], // Contains each 'mql' and associated 'listeners' if 'addListener' is used | |
| 13 | handleChange = function(evt) { | |
| 14 | // Debounce | |
| 15 | clearTimeout(timeoutID); | |
|
83ca799
knightdr authored
|
||
| 16 | ||
|
0903df1
knightdr authored
|
||
| 17 | timeoutID = setTimeout(function() { | |
| 18 | for (var i = 0, il = queries.length; i < il; i++) { | |
| 19 | var mql = queries[i].mql, | |
| 20 | listeners = queries[i].listeners || [], | |
| 21 | matches = localMatchMedia(mql.media).matches; | |
|
83ca799
knightdr authored
|
||
| 22 | ||
|
0903df1
knightdr authored
|
||
| 23 | // Update mql.matches value and call listeners | |
| 24 | // Fire listeners only if transitioning to or from matched state | |
| 25 | if (matches !== mql.matches) { | |
| 26 | mql.matches = matches; | |
|
83ca799
knightdr authored
|
||
| 27 | ||
|
0903df1
knightdr authored
|
||
| 28 | for (var j = 0, jl = listeners.length; j < jl; j++) { | |
| 29 | listeners[j].call(window, mql); | |
|
83ca799
knightdr authored
|
||
| 30 | } | |
| 31 | } | |
|
0903df1
knightdr authored
|
||
| 32 | } | |
| 33 | }, 30); | |
| 34 | }; | |
|
83ca799
knightdr authored
|
||
| 35 | ||
|
0903df1
knightdr authored
|
||
| 36 | window.matchMedia = function(media) { | |
| 37 | var mql = localMatchMedia(media), | |
| 38 | listeners = [], | |
| 39 | index = 0; | |
|
83ca799
knightdr authored
|
||
| 40 | ||
|
0903df1
knightdr authored
|
||
| 41 | mql.addListener = function(listener) { | |
| 42 | // Changes would not occur to css media type so return now (Affects IE <= 8) | |
| 43 | if (!hasMediaQueries) { | |
| 44 | return; | |
| 45 | } | |
|
83ca799
knightdr authored
|
||
| 46 | ||
|
0903df1
knightdr authored
|
||
| 47 | // Set up 'resize' listener for browsers that support CSS3 media queries (Not for IE <= 8) | |
| 48 | // There should only ever be 1 resize listener running for performance | |
| 49 | if (!isListening) { | |
| 50 | isListening = true; | |
| 51 | window.addEventListener('resize', handleChange, true); | |
| 52 | } | |
|
83ca799
knightdr authored
|
||
| 53 | ||
|
0903df1
knightdr authored
|
||
| 54 | // Push object only if it has not been pushed already | |
| 55 | if (index === 0) { | |
| 56 | index = queries.push({ | |
| 57 | mql : mql, | |
| 58 | listeners : listeners | |
| 59 | }); | |
| 60 | } | |
|
83ca799
knightdr authored
|
||
| 61 | ||
|
0903df1
knightdr authored
|
||
| 62 | listeners.push(listener); | |
| 63 | }; | |
|
83ca799
knightdr authored
|
||
| 64 | ||
|
0903df1
knightdr authored
|
||
| 65 | mql.removeListener = function(listener) { | |
| 66 | for (var i = 0, il = listeners.length; i < il; i++){ | |
| 67 | if (listeners[i] === listener){ | |
| 68 | listeners.splice(i, 1); | |
|
83ca799
knightdr authored
|
||
| 69 | } | |
|
0903df1
knightdr authored
|
||
| 70 | } | |
|
83ca799
knightdr authored
|
||
| 71 | }; | |
|
0903df1
knightdr authored
|
||
| 72 | ||
| 73 | return mql; | |
| 74 | }; | |
| 75 | }()); |