@angular/flex-layout
Version:
Angular 2 Flexbox Layout
104 lines • 4.28 kB
JavaScript
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { BreakPointRegistry } from './breakpoints/break-point-registry';
import { MatchMedia } from './match-media';
import { mergeAlias } from '../utils/add-alias';
/**
* MediaMonitor uses the MatchMedia service to observe mediaQuery changes (both activations and
* deactivations). These changes are are published as MediaChange notifications.
*
* Note: all notifications will be performed within the
* ng Zone to trigger change detections and component updates.
*
* It is the MediaMonitor that:
* - auto registers all known breakpoints
* - injects alias information into each raw MediaChange event
* - provides accessor to the currently active BreakPoint
* - publish list of overlapping BreakPoint(s); used by ResponsiveActivation
*/
export var MediaMonitor = (function () {
function MediaMonitor(_breakpoints, _matchMedia) {
this._breakpoints = _breakpoints;
this._matchMedia = _matchMedia;
this._registerBreakpoints();
}
Object.defineProperty(MediaMonitor.prototype, "breakpoints", {
/**
* Read-only accessor to the list of breakpoints configured in the BreakPointRegistry provider
*/
get: function () {
return this._breakpoints.items.slice();
},
enumerable: true,
configurable: true
});
Object.defineProperty(MediaMonitor.prototype, "activeOverlaps", {
get: function () {
var _this = this;
var items = this._breakpoints.overlappings.reverse();
return items.filter(function (bp) {
return _this._matchMedia.isActive(bp.mediaQuery);
});
},
enumerable: true,
configurable: true
});
Object.defineProperty(MediaMonitor.prototype, "active", {
get: function () {
var _this = this;
var found = null, items = this.breakpoints.reverse();
items.forEach(function (bp) {
if (bp.alias !== '') {
if (!found && _this._matchMedia.isActive(bp.mediaQuery)) {
found = bp;
}
}
});
var first = this.breakpoints[0];
return found || (this._matchMedia.isActive(first.mediaQuery) ? first : null);
},
enumerable: true,
configurable: true
});
/**
* For the specified mediaQuery alias, is the mediaQuery range active?
*/
MediaMonitor.prototype.isActive = function (alias) {
var bp = this._breakpoints.findByAlias(alias) || this._breakpoints.findByQuery(alias);
return this._matchMedia.isActive(bp ? bp.mediaQuery : alias);
};
/**
* External observers can watch for all (or a specific) mql changes.
* If specific breakpoint is observed, only return *activated* events
* otherwise return all events for BOTH activated + deactivated changes.
*/
MediaMonitor.prototype.observe = function (alias) {
var bp = this._breakpoints.findByAlias(alias) || this._breakpoints.findByQuery(alias);
var hasAlias = function (change) { return (bp ? change.mqAlias !== "" : true); };
// Note: the raw MediaChange events [from MatchMedia] do not contain important alias information
return this._matchMedia
.observe(bp ? bp.mediaQuery : alias)
.map(function (change) { return mergeAlias(change, bp); })
.filter(hasAlias);
};
/**
* Immediate calls to matchMedia() to establish listeners
* and prepare for immediate subscription notifications
*/
MediaMonitor.prototype._registerBreakpoints = function () {
var _this = this;
this._breakpoints.items.forEach(function (bp) {
_this._matchMedia.registerQuery(bp.mediaQuery);
});
};
MediaMonitor.decorators = [
{ type: Injectable },
];
/** @nocollapse */
MediaMonitor.ctorParameters = function () { return [
{ type: BreakPointRegistry, },
{ type: MatchMedia, },
]; };
return MediaMonitor;
}());
//# sourceMappingURL=/usr/local/google/home/tinagao/WebstormProjects/caretaker/flex-layout/src/lib/media-query/media-monitor.js.map