2017-02-22 15:14:49 -08:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
2017-02-24 12:10:19 -08:00
|
|
|
import {AnimationEvent, AnimationTriggerMetadata} from '@angular/animations';
|
2017-02-23 08:51:00 -08:00
|
|
|
import {Injectable, NgZone, RendererFactoryV2, RendererTypeV2, RendererV2} from '@angular/core';
|
2017-02-22 15:14:49 -08:00
|
|
|
|
2017-02-23 08:51:00 -08:00
|
|
|
import {AnimationEngine} from '../animation_engine';
|
2017-02-22 15:14:49 -08:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AnimationRendererFactory implements RendererFactoryV2 {
|
2017-02-23 08:51:00 -08:00
|
|
|
constructor(
|
|
|
|
private delegate: RendererFactoryV2, private _engine: AnimationEngine,
|
|
|
|
private _zone: NgZone) {}
|
2017-02-22 15:14:49 -08:00
|
|
|
|
|
|
|
createRenderer(hostElement: any, type: RendererTypeV2): RendererV2 {
|
|
|
|
let delegate = this.delegate.createRenderer(hostElement, type);
|
2017-02-24 12:10:19 -08:00
|
|
|
if (!hostElement || !type || !type.data || !type.data['animation']) return delegate;
|
|
|
|
|
|
|
|
let animationRenderer = delegate.data['animationRenderer'];
|
|
|
|
if (!animationRenderer) {
|
|
|
|
const namespaceId = type.id;
|
|
|
|
const animationTriggers = type.data['animation'] as AnimationTriggerMetadata[];
|
|
|
|
animationTriggers.forEach(
|
|
|
|
trigger =>
|
|
|
|
this._engine.registerTrigger(trigger, namespaceify(namespaceId, trigger.name)));
|
|
|
|
animationRenderer = new AnimationRenderer(delegate, this._engine, this._zone, namespaceId);
|
|
|
|
delegate.data['animationRenderer'] = animationRenderer;
|
2017-02-22 15:14:49 -08:00
|
|
|
}
|
|
|
|
return animationRenderer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class AnimationRenderer implements RendererV2 {
|
|
|
|
public destroyNode: (node: any) => (void|any) = null;
|
2017-02-23 08:51:00 -08:00
|
|
|
private _flushPromise: Promise<any> = null;
|
2017-02-22 15:14:49 -08:00
|
|
|
|
|
|
|
constructor(
|
2017-02-23 08:51:00 -08:00
|
|
|
public delegate: RendererV2, private _engine: AnimationEngine, private _zone: NgZone,
|
2017-02-24 12:10:19 -08:00
|
|
|
private _namespaceId: string) {
|
2017-02-22 15:14:49 -08:00
|
|
|
this.destroyNode = this.delegate.destroyNode ? (n) => delegate.destroyNode(n) : null;
|
|
|
|
}
|
|
|
|
|
2017-02-24 12:10:19 -08:00
|
|
|
get data() { return this.delegate.data; }
|
|
|
|
|
2017-02-22 15:14:49 -08:00
|
|
|
destroy(): void { this.delegate.destroy(); }
|
|
|
|
|
|
|
|
createElement(name: string, namespace?: string): any {
|
|
|
|
return this.delegate.createElement(name, namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
createComment(value: string): any { return this.delegate.createComment(value); }
|
|
|
|
|
|
|
|
createText(value: string): any { return this.delegate.createText(value); }
|
|
|
|
|
|
|
|
selectRootElement(selectorOrNode: string|any): any {
|
|
|
|
return this.delegate.selectRootElement(selectorOrNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
parentNode(node: any): any { return this.delegate.parentNode(node); }
|
|
|
|
|
|
|
|
nextSibling(node: any): any { return this.delegate.nextSibling(node); }
|
|
|
|
|
|
|
|
setAttribute(el: any, name: string, value: string, namespace?: string): void {
|
|
|
|
this.delegate.setAttribute(el, name, value, namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeAttribute(el: any, name: string, namespace?: string): void {
|
|
|
|
this.delegate.removeAttribute(el, name, namespace);
|
|
|
|
}
|
|
|
|
|
|
|
|
addClass(el: any, name: string): void { this.delegate.addClass(el, name); }
|
|
|
|
|
|
|
|
removeClass(el: any, name: string): void { this.delegate.removeClass(el, name); }
|
|
|
|
|
|
|
|
setStyle(el: any, style: string, value: any, hasVendorPrefix: boolean, hasImportant: boolean):
|
|
|
|
void {
|
|
|
|
this.delegate.setStyle(el, style, value, hasVendorPrefix, hasImportant);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeStyle(el: any, style: string, hasVendorPrefix: boolean): void {
|
|
|
|
this.delegate.removeStyle(el, style, hasVendorPrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
setValue(node: any, value: string): void { this.delegate.setValue(node, value); }
|
|
|
|
|
|
|
|
appendChild(parent: any, newChild: any): void {
|
|
|
|
this._engine.onInsert(newChild, () => this.delegate.appendChild(parent, newChild));
|
2017-02-23 11:34:14 -08:00
|
|
|
this._queueFlush();
|
2017-02-22 15:14:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
insertBefore(parent: any, newChild: any, refChild: any): void {
|
|
|
|
this._engine.onInsert(newChild, () => this.delegate.insertBefore(parent, newChild, refChild));
|
2017-02-23 11:34:14 -08:00
|
|
|
this._queueFlush();
|
2017-02-22 15:14:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
removeChild(parent: any, oldChild: any): void {
|
|
|
|
this._engine.onRemove(oldChild, () => this.delegate.removeChild(parent, oldChild));
|
2017-02-23 08:51:00 -08:00
|
|
|
this._queueFlush();
|
2017-02-22 15:14:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
setProperty(el: any, name: string, value: any): void {
|
|
|
|
if (name.charAt(0) == '@') {
|
2017-02-24 12:10:19 -08:00
|
|
|
this._engine.setProperty(el, namespaceify(this._namespaceId, name.substr(1)), value);
|
2017-02-23 08:51:00 -08:00
|
|
|
this._queueFlush();
|
2017-02-22 15:14:49 -08:00
|
|
|
} else {
|
|
|
|
this.delegate.setProperty(el, name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
listen(target: 'window'|'document'|'body'|any, eventName: string, callback: (event: any) => any):
|
|
|
|
() => void {
|
|
|
|
if (eventName.charAt(0) == '@') {
|
|
|
|
const element = resolveElementFromTarget(target);
|
|
|
|
const [name, phase] = parseTriggerCallbackName(eventName.substr(1));
|
2017-02-23 08:51:00 -08:00
|
|
|
return this._engine.listen(
|
2017-02-24 12:10:19 -08:00
|
|
|
element, namespaceify(this._namespaceId, name), phase, (event: any) => {
|
|
|
|
const e = event as any;
|
|
|
|
if (e.triggerName) {
|
|
|
|
e.triggerName = deNamespaceify(this._namespaceId, e.triggerName);
|
|
|
|
}
|
|
|
|
this._zone.run(() => callback(event));
|
|
|
|
});
|
2017-02-22 15:14:49 -08:00
|
|
|
}
|
|
|
|
return this.delegate.listen(target, eventName, callback);
|
|
|
|
}
|
2017-02-23 08:51:00 -08:00
|
|
|
|
|
|
|
private _queueFlush() {
|
|
|
|
if (!this._flushPromise) {
|
|
|
|
this._zone.runOutsideAngular(() => {
|
|
|
|
this._flushPromise = Promise.resolve(null).then(() => {
|
|
|
|
this._flushPromise = null;
|
|
|
|
this._engine.flush();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-02-22 15:14:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
function resolveElementFromTarget(target: 'window' | 'document' | 'body' | any): any {
|
|
|
|
switch (target) {
|
|
|
|
case 'body':
|
|
|
|
return document.body;
|
|
|
|
case 'document':
|
|
|
|
return document;
|
|
|
|
case 'window':
|
|
|
|
return window;
|
|
|
|
default:
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseTriggerCallbackName(triggerName: string) {
|
|
|
|
const dotIndex = triggerName.indexOf('.');
|
|
|
|
const trigger = triggerName.substring(0, dotIndex);
|
|
|
|
const phase = triggerName.substr(dotIndex + 1);
|
|
|
|
return [trigger, phase];
|
|
|
|
}
|
2017-02-24 12:10:19 -08:00
|
|
|
|
|
|
|
function namespaceify(namespaceId: string, value: string): string {
|
|
|
|
return `${namespaceId}#${value}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function deNamespaceify(namespaceId: string, value: string): string {
|
|
|
|
return value.replace(namespaceId + '#', '');
|
|
|
|
}
|