/*! * devextreme-angular-test * Version: 17.2.8 * Build date: Mon Feb 05 2018 * * Copyright (c) 2012 - 2018 Developer Express Inc. ALL RIGHTS RESERVED * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file in the root of the project for details. * * https://github.com/DevExpress/devextreme-angular */ import { EventEmitter, NgZone, Injectable } from '@angular/core'; import { DxComponent } from './component'; import * as eventsEngine from 'devextreme/events/core/events_engine'; const dxToNgEventNames = {}; interface IEventSubscription { handler: any; unsubscribe: () => void; } export class NgEventsStrategy { private subscriptions: { [key: string]: IEventSubscription[] } = {}; constructor(private component: DxComponent, private ngZone: NgZone) { } hasEvent(name: string) { return this.ngZone.run(() => { return this.getEmitter(name).observers.length; }); } fireEvent(name, args) { this.ngZone.run(() => { this.getEmitter(name).next(args && args[0]); }); } on(name, handler) { let eventSubscriptions = this.subscriptions[name] || [], subcription = this.getEmitter(name).subscribe(handler.bind(this.component.instance)), unsubscribe = subcription.unsubscribe.bind(subcription); eventSubscriptions.push({ handler, unsubscribe }); this.subscriptions[name] = eventSubscriptions; } off(name, handler) { let eventSubscriptions = this.subscriptions[name] || []; if (handler) { eventSubscriptions.some((subscription, i) => { if (subscription.handler === handler) { subscription.unsubscribe(); eventSubscriptions.splice(i, 1); return true; } }); } else { eventSubscriptions.forEach(subscription => { subscription.unsubscribe(); }); eventSubscriptions.splice(0, eventSubscriptions.length); } } dispose() {} private getEmitter(eventName: string): EventEmitter { let ngEventName = dxToNgEventNames[eventName]; if (!this.component[ngEventName]) { this.component[ngEventName] = new EventEmitter(); } return this.component[ngEventName]; } } export class EmitterHelper { strategy: NgEventsStrategy; constructor(ngZone: NgZone, public component: DxComponent) { this.strategy = new NgEventsStrategy(component, ngZone); } fireNgEvent(eventName: string, eventArgs: any) { let emitter = this.component[eventName]; if (emitter) { emitter.next(eventArgs && eventArgs[0]); } } createEmitter(ngEventName: string, dxEventName: string) { this.component[ngEventName] = new EventEmitter(); if (dxEventName) { dxToNgEventNames[dxEventName] = ngEventName; } } } @Injectable() export class EventsRegistrator { constructor(ngZone: NgZone) { eventsEngine.set({ on: function(...args) { ngZone.runOutsideAngular(() => { this.callBase.apply(this, args); }); } }); } }