zent
Version:
一套前端设计语言和基于React的实现
48 lines (47 loc) • 1.58 kB
JavaScript
import { MediaQuery } from './MediaQuery';
var MediaQueryDispatch = (function () {
function MediaQueryDispatch() {
if (!window.matchMedia) {
throw new Error('matchMedia not present, legacy browsers require a polyfill');
}
this.queries = {};
this.browserIsIncapable = !window.matchMedia('only all').matches;
}
MediaQueryDispatch.prototype.register = function (q, options, shouldDegrade) {
if (shouldDegrade === void 0) { shouldDegrade = false; }
var queries = this.queries;
var isUnconditional = shouldDegrade && this.browserIsIncapable;
if (!queries[q]) {
queries[q] = new MediaQuery(q, isUnconditional);
}
if (typeof options === 'function') {
options = { match: options };
}
if (!Array.isArray(options)) {
options = [options];
}
var query = queries[q];
options.forEach(function (handler) {
if (typeof handler === 'function') {
handler = { match: handler };
}
query.addHandler(handler);
});
return this;
};
MediaQueryDispatch.prototype.unregister = function (q, handler) {
var query = this.queries[q];
if (query) {
if (handler) {
query.removeHandler(handler);
}
else {
query.clear();
delete this.queries[q];
}
}
return this;
};
return MediaQueryDispatch;
}());
export { MediaQueryDispatch };