Issue #2207629 by droplet, tarekdj, Devin Carlson, rpayanm: Update matchMedia library to latest release

8.0.x
Nathaniel Catchpole 2014-12-19 10:10:06 +00:00
parent dfad7c651b
commit 37e7d708c9
5 changed files with 14 additions and 141 deletions

View File

@ -0,0 +1 @@
(function(){if(window.matchMedia&&window.matchMedia("all").addListener){return false}var e=window.matchMedia,i=e("only all").matches,n=false,t=0,a=[],r=function(i){clearTimeout(t);t=setTimeout(function(){for(var i=0,n=a.length;i<n;i++){var t=a[i].mql,r=a[i].listeners||[],o=e(t.media).matches;if(o!==t.matches){t.matches=o;for(var s=0,l=r.length;s<l;s++){r[s].call(window,t)}}}},30)};window.matchMedia=function(t){var o=e(t),s=[],l=0;o.addListener=function(e){if(!i){return}if(!n){n=true;window.addEventListener("resize",r,true)}if(l===0){l=a.push({mql:o,listeners:s})}s.push(e)};o.removeListener=function(e){for(var i=0,n=s.length;i<n;i++){if(s[i]===e){s.splice(i,1)}}};return o}})();

View File

@ -0,0 +1 @@
window.matchMedia||(window.matchMedia=function(){"use strict";var e=window.styleMedia||window.media;if(!e){var t=document.createElement("style"),i=document.getElementsByTagName("script")[0],n=null;t.type="text/css";t.id="matchmediajs-test";i.parentNode.insertBefore(t,i);n="getComputedStyle"in window&&window.getComputedStyle(t,null)||t.currentStyle;e={matchMedium:function(e){var i="@media "+e+"{ #matchmediajs-test { width: 1px; } }";if(t.styleSheet){t.styleSheet.cssText=i}else{t.textContent=i}return n.width==="1px"}}}return function(t){return{matches:e.matchMedium(t||"all"),media:t||"all"}}}());

View File

@ -752,17 +752,21 @@ jquery.ui.widget:
matchmedia:
remote: https://github.com/paulirish/matchMedia.js
# @todo Contribute upstream and replace with upstream version.
# @see http://drupal.org/node/1815602
version: VERSION
license:
version: &matchmedia_version 0.2.0
license: &matchmedia_license
name: MIT
url: https://github.com/paulirish/matchMedia.js/blob/0.1.0/LICENSE.txt
url: https://github.com/paulirish/matchMedia.js/blob/0.2.0/LICENSE.txt
gpl-compatible: true
js:
misc/matchmedia.js: {}
assets/vendor/matchMedia/matchMedia.min.js: { minified: true }
matchmedia.addListener:
version: *matchmedia_version
license: *matchmedia_license
js:
assets/vendor/matchMedia/matchMedia.addListener.min.js: { minified: true }
dependencies:
- core/drupal.debounce
- core/matchmedia
modernizr:
remote: https://github.com/Modernizr/Modernizr

View File

@ -1,134 +0,0 @@
/**
* Polyfill the behavior of window.matchMedia.
*
* @see http://dev.w3.org/csswg/cssom-view/#widl-Window-matchMedia-MediaQueryList-DOMString-query
*
* Test whether a CSS media type or media query applies. Register listeners
* to MediaQueryList objects.
*
* Adapted from https://github.com/paulirish/matchMedia.js with the addition
* of addListener and removeListener. The polyfill referenced above uses
* polling to trigger registered listeners on matchMedia tests.
* This polyfill triggers tests on window resize and orientationchange.
*/
window.matchMedia = window.matchMedia || (function (doc, window, Drupal) {
"use strict";
var docElem = doc.documentElement;
var refNode = docElem.firstElementChild || docElem.firstChild;
// fakeBody required for <FF4 when executed in <head>.
var fakeBody = doc.createElement("body");
var div = doc.createElement("div");
div.id = "mq-test-1";
div.style.cssText = "position:absolute;top:-100em";
fakeBody.style.background = "none";
fakeBody.appendChild(div);
/**
* A replacement for the native MediaQueryList object.
*
* @param {String} q
* A media query e.g. "screen" or "screen and (min-width: 28em)".
*/
function MediaQueryList(q) {
this.media = q;
this.matches = false;
this.check.call(this);
}
/**
* Polyfill the addListener and removeListener methods.
*/
MediaQueryList.prototype = {
listeners: [],
/**
* Perform the media query application check.
*/
check: function () {
var isApplied;
div.innerHTML = "&shy;<style media=\"" + this.media + "\"> #mq-test-1 {width: 42px;}</style>";
docElem.insertBefore(fakeBody, refNode);
isApplied = div.offsetWidth === 42;
docElem.removeChild(fakeBody);
this.matches = isApplied;
},
/**
* Polyfill the addListener method of the MediaQueryList object.
*
* @param {Function} callback
* The callback to be invoked when the media query is applicable.
*
* @return {Object MediaQueryList}
* A MediaQueryList object that indicates whether the registered media
* query applies. The matches property is true when the media query
* applies and false when not. The original media query is referenced in
* the media property.
*/
addListener: function (callback) {
var handler = (function (mql, debounced) {
return function () {
// Only execute the callback if the state has changed.
var oldstate = mql.matches;
mql.check();
if (oldstate !== mql.matches) {
debounced.call(mql, mql);
}
};
}(this, Drupal.debounce(callback, 250)));
this.listeners.push({
'callback': callback,
'handler': handler
});
// Associate the handler to the resize and orientationchange events.
if ('addEventListener' in window) {
window.addEventListener('resize', handler);
window.addEventListener('orientationchange', handler);
}
else if ('attachEvent' in window) {
window.attachEvent('onresize', handler);
window.attachEvent('onorientationchange', handler);
}
},
/**
* Polyfill the removeListener method of the MediaQueryList object.
*
* @param {Function} callback
* The callback to be removed from the set of listeners.
*/
removeListener: function (callback) {
for (var i = 0, listeners = this.listeners; i < listeners.length; i++) {
if (listeners[i].callback === callback) {
// Disassociate the handler to the resize and orientationchange events.
if ('removeEventListener' in window) {
window.removeEventListener('resize', listeners[i].handler);
window.removeEventListener('orientationchange', listeners[i].handler);
}
else if ('detachEvent' in window) {
window.detachEvent('onresize', listeners[i].handler);
window.detachEvent('onorientationchange', listeners[i].handler);
}
listeners.splice(i, 1);
}
}
}
};
/**
* Return a MediaQueryList.
*
* @param {String} q
* A media query e.g. "screen" or "screen and (min-width: 28em)". The media
* query is checked for applicability before the object is returned.
*/
return function (q) {
// Build a new MediaQueryList object with the result of the check.
return new MediaQueryList(q);
};
}(document, window, Drupal));

View File

@ -25,6 +25,7 @@ toolbar:
- core/drupal.announce
- core/backbone
- core/matchmedia
- core/matchmedia.addListener
- core/jquery.once
- core/drupal.displace
- toolbar/toolbar.menu