responsiveObserve.js
1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _extends from "@babel/runtime/helpers/extends";
export var responsiveArray = ['xxl', 'xl', 'lg', 'md', 'sm', 'xs'];
export var responsiveMap = {
xs: '(max-width: 575px)',
sm: '(min-width: 576px)',
md: '(min-width: 768px)',
lg: '(min-width: 992px)',
xl: '(min-width: 1200px)',
xxl: '(min-width: 1600px)'
};
var subscribers = new Map();
var subUid = -1;
var screens = {};
var responsiveObserve = {
matchHandlers: {},
dispatch: function dispatch(pointMap) {
screens = pointMap;
subscribers.forEach(function (func) {
return func(screens);
});
return subscribers.size >= 1;
},
subscribe: function subscribe(func) {
if (!subscribers.size) this.register();
subUid += 1;
subscribers.set(subUid, func);
func(screens);
return subUid;
},
unsubscribe: function unsubscribe(token) {
subscribers["delete"](token);
if (!subscribers.size) this.unregister();
},
unregister: function unregister() {
var _this = this;
Object.keys(responsiveMap).forEach(function (screen) {
var matchMediaQuery = responsiveMap[screen];
var handler = _this.matchHandlers[matchMediaQuery];
handler === null || handler === void 0 ? void 0 : handler.mql.removeListener(handler === null || handler === void 0 ? void 0 : handler.listener);
});
subscribers.clear();
},
register: function register() {
var _this2 = this;
Object.keys(responsiveMap).forEach(function (screen) {
var matchMediaQuery = responsiveMap[screen];
var listener = function listener(_ref) {
var matches = _ref.matches;
_this2.dispatch(_extends(_extends({}, screens), _defineProperty({}, screen, matches)));
};
var mql = window.matchMedia(matchMediaQuery);
mql.addListener(listener);
_this2.matchHandlers[matchMediaQuery] = {
mql: mql,
listener: listener
};
listener(mql);
});
}
};
export default responsiveObserve;