UNPKG

md2

Version:

Angular2 based Material Design components, directives and services are Accordion, Autocomplete, Chips(Tags), Collapse, Colorpicker, Data Table, Datepicker, Dialog(Modal), Menu, Multiselect, Select, Tabs, Tags(Chips), Toast and Tooltip.

138 lines 6.98 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; import { Injectable, NgZone, Optional, SkipSelf } from '@angular/core'; import { Platform } from '../../platform/index'; import { Subject } from 'rxjs/Subject'; import { Observable } from 'rxjs/Observable'; import { Subscription } from 'rxjs/Subscription'; import 'rxjs/add/observable/fromEvent'; import 'rxjs/add/observable/merge'; import 'rxjs/add/operator/auditTime'; /** Time in ms to throttle the scrolling events by default. */ export var DEFAULT_SCROLL_TIME = 20; /** * Service contained all registered Scrollable references and emits an event when any one of the * Scrollable references emit a scrolled event. */ var ScrollDispatcher = (function () { function ScrollDispatcher(_ngZone, _platform) { this._ngZone = _ngZone; this._platform = _platform; /** Subject for notifying that a registered scrollable reference element has been scrolled. */ this._scrolled = new Subject(); /** Keeps track of the global `scroll` and `resize` subscriptions. */ this._globalSubscription = null; /** Keeps track of the amount of subscriptions to `scrolled`. Used for cleaning up afterwards. */ this._scrolledCount = 0; /** * Map of all the scrollable references that are registered with the service and their * scroll event subscriptions. */ this.scrollableReferences = new Map(); } /** * Registers a Scrollable with the service and listens for its scrolled events. When the * scrollable is scrolled, the service emits the event in its scrolled observable. * @param scrollable Scrollable instance to be registered. */ ScrollDispatcher.prototype.register = function (scrollable) { var _this = this; var scrollSubscription = scrollable.elementScrolled().subscribe(function () { return _this._notify(); }); this.scrollableReferences.set(scrollable, scrollSubscription); }; /** * Deregisters a Scrollable reference and unsubscribes from its scroll event observable. * @param scrollable Scrollable instance to be deregistered. */ ScrollDispatcher.prototype.deregister = function (scrollable) { if (this.scrollableReferences.has(scrollable)) { this.scrollableReferences.get(scrollable).unsubscribe(); this.scrollableReferences.delete(scrollable); } }; /** * Subscribes to an observable that emits an event whenever any of the registered Scrollable * references (or window, document, or body) fire a scrolled event. Can provide a time in ms * to override the default "throttle" time. */ ScrollDispatcher.prototype.scrolled = function (auditTimeInMs, callback) { var _this = this; if (auditTimeInMs === void 0) { auditTimeInMs = DEFAULT_SCROLL_TIME; } // Scroll events can only happen on the browser, so do nothing if we're not on the browser. if (!this._platform.isBrowser) { return Subscription.EMPTY; } // In the case of a 0ms delay, use an observable without auditTime // since it does add a perceptible delay in processing overhead. var observable = auditTimeInMs > 0 ? this._scrolled.asObservable().auditTime(auditTimeInMs) : this._scrolled.asObservable(); this._scrolledCount++; if (!this._globalSubscription) { this._globalSubscription = this._ngZone.runOutsideAngular(function () { return Observable.merge(Observable.fromEvent(window.document, 'scroll'), Observable.fromEvent(window, 'resize')).subscribe(function () { return _this._notify(); }); }); } // Note that we need to do the subscribing from here, in order to be able to remove // the global event listeners once there are no more subscriptions. var subscription = observable.subscribe(callback); subscription.add(function () { _this._scrolledCount--; if (_this._globalSubscription && !_this.scrollableReferences.size && !_this._scrolledCount) { _this._globalSubscription.unsubscribe(); _this._globalSubscription = null; } }); return subscription; }; /** Returns all registered Scrollables that contain the provided element. */ ScrollDispatcher.prototype.getScrollContainers = function (elementRef) { var _this = this; var scrollingContainers = []; this.scrollableReferences.forEach(function (_subscription, scrollable) { if (_this.scrollableContainsElement(scrollable, elementRef)) { scrollingContainers.push(scrollable); } }); return scrollingContainers; }; /** Returns true if the element is contained within the provided Scrollable. */ ScrollDispatcher.prototype.scrollableContainsElement = function (scrollable, elementRef) { var element = elementRef.nativeElement; var scrollableElement = scrollable.getElementRef().nativeElement; // Traverse through the element parents until we reach null, checking if any of the elements // are the scrollable's element. do { if (element == scrollableElement) { return true; } } while (element = element.parentElement); }; /** Sends a notification that a scroll event has been fired. */ ScrollDispatcher.prototype._notify = function () { this._scrolled.next(); }; return ScrollDispatcher; }()); ScrollDispatcher = __decorate([ Injectable(), __metadata("design:paramtypes", [NgZone, Platform]) ], ScrollDispatcher); export { ScrollDispatcher }; export function SCROLL_DISPATCHER_PROVIDER_FACTORY(parentDispatcher, ngZone, platform) { return parentDispatcher || new ScrollDispatcher(ngZone, platform); } export var SCROLL_DISPATCHER_PROVIDER = { // If there is already a ScrollDispatcher available, use that. Otherwise, provide a new one. provide: ScrollDispatcher, deps: [[new Optional(), new SkipSelf(), ScrollDispatcher], NgZone, Platform], useFactory: SCROLL_DISPATCHER_PROVIDER_FACTORY }; //# sourceMappingURL=scroll-dispatcher.js.map