From 0479ff52397f29d841e29736c75a7e1b52421c68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Scott=20Gonz=C3=A1lez?= Date: Fri, 17 Sep 2010 14:19:53 -0400 Subject: [PATCH] Refactored $.media and moved to jQuery.mobile.support.js --- js/jQuery.mobile.js | 34 ---------------------------------- js/jQuery.mobile.support.js | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 34 deletions(-) diff --git a/js/jQuery.mobile.js b/js/jQuery.mobile.js index d2540ee9..1d18e109 100644 --- a/js/jQuery.mobile.js +++ b/js/jQuery.mobile.js @@ -27,40 +27,6 @@ backBtnText = "Back", urlStack = [location.hash.replace(/^#/,'')]; - /* - add some core behavior,events - */ - - //test whether a CSS media type or query applies (adapted from work by Scott Jehl & Paul Irish: http://gist.github.com/557891) - $.media = (function(){ - /* - note: once support improves, try window.matchMedia here - if ( window.matchMedia ){ - //use native support if available - return window.matchMedia; - } - */ - var cache = {}, - testDiv = $('
'), - fakeBody = $('').append(testDiv); - return function(q){ - if (cache[q] === undefined) { - var styleBlock = $(''); - var cssrule = '@media '+q+' { #jq-mediatest { position: absolute; } }'; - if (styleBlock[0].styleSheet){ - styleBlock[0].styleSheet.cssText = cssrule; - } - else { - styleBlock.text(cssrule); - } - $html.prepend(fakeBody).prepend(styleBlock); - cache[q] = ((window.getComputedStyle ? window.getComputedStyle(testDiv[0],null) : testDiv[0].currentStyle)['position'] == 'absolute'); - fakeBody.add(styleBlock).remove(); - } - return cache[q]; - }; - })(); - //hide Address bar function hideBrowserChrome(){ $.event.special.scrollstart.enabled = false; diff --git a/js/jQuery.mobile.support.js b/js/jQuery.mobile.support.js index 8951d357..1fd37d63 100644 --- a/js/jQuery.mobile.support.js +++ b/js/jQuery.mobile.support.js @@ -42,3 +42,23 @@ $.extend( $.support, { fakeBody.remove(); })(); + +// test whether a CSS media type or query applies +$.media = (function() { + // TODO: use window.matchMedia once at least one UA implements it + var cache = {}, + testDiv = $( "
" ), + fakeBody = $( "" ).append( testDiv ); + + return function( query ) { + if ( !( query in cache ) ) { + var styleBlock = $( "" ); + $html.prepend( fakeBody ).prepend( styleBlock ); + cache[ query ] = testDiv.css( "position" ) === "absolute"; + fakeBody.add( styleBlock ).remove(); + } + return cache[ query ]; + }; +})();