Skip to content

Commit 5ae5a49

Browse files
authored
Merge pull request #34021 from andrewbess/revert-removing-match-media
Revert "Removing the MatchMedia"
2 parents fa09109 + 0729064 commit 5ae5a49

File tree

4 files changed

+150
-31
lines changed

4 files changed

+150
-31
lines changed

app/code/Magento/Theme/view/base/requirejs-config.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ var config = {
3434
'domReady': 'requirejs/domReady',
3535
'spectrum': 'jquery/spectrum/spectrum',
3636
'tinycolor': 'jquery/spectrum/tinycolor',
37-
'jquery-ui-modules': 'jquery/ui-modules',
38-
'matchMedia': 'mediaCheck'
37+
'jquery-ui-modules': 'jquery/ui-modules'
3938
},
4039
deps: [
4140
'jquery/jquery-migrate'

app/design/adminhtml/Magento/backend/etc/view.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<item type="file">Lib::jquery/jquery.min.js</item>
5151
<item type="file">Lib::jquery/jquery.parsequery.js</item>
5252
<item type="file">Lib::jquery/jquery-ui.js</item>
53-
<item type="file">Lib::mediaCheck.js</item>
53+
<item type="file">Lib::matchMedia.js</item>
5454
<item type="file">Lib::requirejs/require.js</item>
5555
<item type="file">Lib::requirejs/text.js</item>
5656
<item type="file">Lib::varien/js.js</item>

lib/web/matchMedia.js

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*! matchMedia() polyfill - Test a CSS media type/query in JS. Authors & copyright (c) 2012: Scott Jehl, Paul Irish, Nicholas Zakas, David Knight. MIT license */
2+
3+
window.matchMedia || (window.matchMedia = function() {
4+
"use strict";
5+
6+
// For browsers that support matchMedium api such as IE 9 and webkit
7+
var styleMedia = (window.styleMedia || window.media);
8+
9+
// For those that don't support matchMedium
10+
if (!styleMedia) {
11+
var style = document.createElement('style'),
12+
script = document.getElementsByTagName('script')[0],
13+
info = null;
14+
15+
style.type = 'text/css';
16+
style.id = 'matchmediajs-test';
17+
18+
if (!script) {
19+
document.head.appendChild(style);
20+
} else {
21+
script.parentNode.insertBefore(style, script);
22+
}
23+
24+
// 'style.currentStyle' is used by IE <= 8 and 'window.getComputedStyle' for all other browsers
25+
info = ('getComputedStyle' in window) && window.getComputedStyle(style, null) || style.currentStyle;
26+
27+
styleMedia = {
28+
matchMedium: function(media) {
29+
var text = '@media ' + media + '{ #matchmediajs-test { width: 1px; } }';
30+
31+
// 'style.styleSheet' is used by IE <= 8 and 'style.textContent' for all other browsers
32+
if (style.styleSheet) {
33+
style.styleSheet.cssText = text;
34+
} else {
35+
style.textContent = text;
36+
}
37+
38+
// Test if media query is true or false
39+
return info.width === '1px';
40+
}
41+
};
42+
}
43+
44+
return function(media) {
45+
return {
46+
matches: styleMedia.matchMedium(media || 'all'),
47+
media: media || 'all'
48+
};
49+
};
50+
}());
51+
52+
/*! matchMedia() polyfill addListener/removeListener extension. Author & copyright (c) 2012: Scott Jehl. Dual MIT/BSD license */
53+
(function() {
54+
// Bail out for browsers that have addListener support
55+
if (window.matchMedia && window.matchMedia('all').addListener) {
56+
return false;
57+
}
58+
59+
var localMatchMedia = window.matchMedia,
60+
hasMediaQueries = localMatchMedia('only all').matches,
61+
isListening = false,
62+
timeoutID = 0, // setTimeout for debouncing 'handleChange'
63+
queries = [], // Contains each 'mql' and associated 'listeners' if 'addListener' is used
64+
handleChange = function(evt) {
65+
// Debounce
66+
clearTimeout(timeoutID);
67+
68+
timeoutID = setTimeout(function() {
69+
for (var i = 0, il = queries.length; i < il; i++) {
70+
var mql = queries[i].mql,
71+
listeners = queries[i].listeners || [],
72+
matches = localMatchMedia(mql.media).matches;
73+
74+
// Update mql.matches value and call listeners
75+
// Fire listeners only if transitioning to or from matched state
76+
if (matches !== mql.matches) {
77+
mql.matches = matches;
78+
79+
for (var j = 0, jl = listeners.length; j < jl; j++) {
80+
listeners[j].call(window, mql);
81+
}
82+
}
83+
}
84+
}, 30);
85+
};
86+
87+
window.matchMedia = function(media) {
88+
var mql = localMatchMedia(media),
89+
listeners = [],
90+
index = 0;
91+
92+
mql.addListener = function(listener) {
93+
// Changes would not occur to css media type so return now (Affects IE <= 8)
94+
if (!hasMediaQueries) {
95+
return;
96+
}
97+
98+
// Set up 'resize' listener for browsers that support CSS3 media queries (Not for IE <= 8)
99+
// There should only ever be 1 resize listener running for performance
100+
if (!isListening) {
101+
isListening = true;
102+
window.addEventListener('resize', handleChange, true);
103+
}
104+
105+
// Push object only if it has not been pushed already
106+
if (index === 0) {
107+
index = queries.push({
108+
mql: mql,
109+
listeners: listeners
110+
});
111+
}
112+
113+
listeners.push(listener);
114+
};
115+
116+
mql.removeListener = function(listener) {
117+
for (var i = 0, il = listeners.length; i < il; i++) {
118+
if (listeners[i] === listener) {
119+
listeners.splice(i, 1);
120+
}
121+
}
122+
};
123+
124+
return mql;
125+
};
126+
}());
127+
128+
window.mediaCheck = function(options) {
129+
var mq;
130+
131+
function mqChange(mq, options) {
132+
if (mq.matches) {
133+
if (typeof options.entry === "function") {
134+
options.entry();
135+
}
136+
} else if (typeof options.exit === "function") {
137+
options.exit();
138+
}
139+
};
140+
141+
mq = window.matchMedia(options.media);
142+
143+
mq.addListener(function() {
144+
mqChange(mq, options);
145+
});
146+
147+
mqChange(mq, options);
148+
};

lib/web/mediaCheck.js

-28
This file was deleted.

0 commit comments

Comments
 (0)