root / drupal7 / sites / all / themes / adaptivetheme / at_core / scripts / matchMedia.addListener.js @ 74f6bef0
1 | 74f6bef0 | Assos Assos | /*! matchMedia() polyfill addListener/removeListener extension. Author & copyright (c) 2012: Scott Jehl. Dual MIT/BSD license */
|
---|---|---|---|
2 | (function(){
|
||
3 | // monkeypatch unsupported addListener/removeListener with polling
|
||
4 | if( !window.matchMedia( "" ).addListener ){ |
||
5 | var oldMM = window.matchMedia;
|
||
6 | |||
7 | window.matchMedia = function( q ){ |
||
8 | var ret = oldMM( q ),
|
||
9 | listeners = [], |
||
10 | last = false,
|
||
11 | timer, |
||
12 | check = function(){ |
||
13 | var list = oldMM( q ),
|
||
14 | unmatchToMatch = list.matches && !last, |
||
15 | matchToUnmatch = !list.matches && last; |
||
16 | |||
17 | //fire callbacks only if transitioning to or from matched state
|
||
18 | if( unmatchToMatch || matchToUnmatch ){
|
||
19 | for( var i =0, il = listeners.length; i< il; i++ ){ |
||
20 | listeners[ i ].call( ret, list ); |
||
21 | } |
||
22 | } |
||
23 | last = list.matches; |
||
24 | }; |
||
25 | |||
26 | ret.addListener = function( cb ){ |
||
27 | listeners.push( cb ); |
||
28 | if( !timer ){
|
||
29 | timer = setInterval( check, 1000 );
|
||
30 | } |
||
31 | }; |
||
32 | |||
33 | ret.removeListener = function( cb ){ |
||
34 | for( var i =0, il = listeners.length; i< il; i++ ){ |
||
35 | if( listeners[ i ] === cb ){
|
||
36 | listeners.splice( i, 1 );
|
||
37 | } |
||
38 | } |
||
39 | if( !listeners.length && timer ){
|
||
40 | clearInterval( timer ); |
||
41 | } |
||
42 | }; |
||
43 | |||
44 | return ret;
|
||
45 | }; |
||
46 | } |
||
47 | }()); |