1/* 2 * Responsive Layout helper 3 */ 4ResponsiveHelper = (function($) { 5 // init variables 6 var handlers = [], 7 prevWinWidth, 8 win = $(window), 9 nativeMatchMedia = false; 10 11 // detect match media support 12 if (window.matchMedia) { 13 if (window.Window && window.matchMedia === Window.prototype.matchMedia) { 14 nativeMatchMedia = true; 15 } else if (window.matchMedia.toString().indexOf('native') > -1) { 16 nativeMatchMedia = true; 17 } 18 } 19 20 // prepare resize handler 21 function resizeHandler() { 22 var winWidth = win.width(); 23 if (winWidth !== prevWinWidth) { 24 prevWinWidth = winWidth; 25 26 // loop through range groups 27 $.each(handlers, function(index, rangeObject) { 28 // disable current active area if needed 29 $.each(rangeObject.data, function(property, item) { 30 if (item.currentActive && !matchRange(item.range[0], item.range[1])) { 31 item.currentActive = false; 32 if (typeof item.disableCallback === 'function') { 33 item.disableCallback(); 34 } 35 } 36 }); 37 38 // enable areas that match current width 39 $.each(rangeObject.data, function(property, item) { 40 if (!item.currentActive && matchRange(item.range[0], item.range[1])) { 41 // make callback 42 item.currentActive = true; 43 if (typeof item.enableCallback === 'function') { 44 item.enableCallback(); 45 } 46 } 47 }); 48 }); 49 } 50 } 51 win.bind('load resize orientationchange', resizeHandler); 52 53 // test range 54 function matchRange(r1, r2) { 55 var mediaQueryString = ''; 56 if (r1 > 0) { 57 mediaQueryString += '(min-width: ' + r1 + 'px)'; 58 } 59 if (r2 < Infinity) { 60 mediaQueryString += (mediaQueryString ? ' and ' : '') + '(max-width: ' + r2 + 'px)'; 61 } 62 return matchQuery(mediaQueryString, r1, r2); 63 } 64 65 // media query function 66 function matchQuery(query, r1, r2) { 67 if (window.matchMedia && nativeMatchMedia) { 68 return matchMedia(query).matches; 69 } else if (window.styleMedia) { 70 return styleMedia.matchMedium(query); 71 } else if (window.media) { 72 return media.matchMedium(query); 73 } else { 74 return prevWinWidth >= r1 && prevWinWidth <= r2; 75 } 76 } 77 78 // range parser 79 function parseRange(rangeStr) { 80 var rangeData = rangeStr.split('..'); 81 var x1 = parseInt(rangeData[0], 10) || -Infinity; 82 var x2 = parseInt(rangeData[1], 10) || Infinity; 83 return [x1, x2].sort(function(a, b) { 84 return a - b; 85 }); 86 } 87 88 // export public functions 89 return { 90 addRange: function(ranges) { 91 // parse data and add items to collection 92 var result = { data: {} }; 93 $.each(ranges, function(property, data) { 94 result.data[property] = { 95 range: parseRange(property), 96 enableCallback: data.on, 97 disableCallback: data.off 98 }; 99 }); 100 handlers.push(result); 101 102 // call resizeHandler to recalculate all events 103 prevWinWidth = null; 104 resizeHandler(); 105 } 106 }; 107}(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.