forked from NativeScript/nativescript-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.ts
More file actions
241 lines (203 loc) · 9.29 KB
/
renderer.ts
File metadata and controls
241 lines (203 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import {Inject, Injectable, Optional, NgZone} from '@angular/core';
import {
Renderer,
RootRenderer,
RenderComponentType,
RenderDebugInfo
} from '@angular/core/src/render/api';
import { AnimationKeyframe } from '@angular/core/src/animation/animation_keyframe';
import { AnimationPlayer } from '@angular/core/src/animation/animation_player';
import { AnimationStyles } from '@angular/core/src/animation/animation_styles';
import { AnimationDriver } from '@angular/core/src/animation/animation_driver';
import {APP_ROOT_VIEW, DEVICE} from "./platform-providers";
import {isBlank} from '@angular/core/src/facade/lang';
import {CONTENT_ATTR} from '@angular/platform-browser/src/dom/dom_renderer';
import {View} from "ui/core/view";
import * as application from "application";
import {topmost} from 'ui/frame';
import {Page} from 'ui/page';
import {ViewUtil, NgView} from "./view-util";
import {rendererLog as traceLog} from "./trace";
import {escapeRegexSymbols} from "utils/utils";
import { Device } from "platform";
@Injectable()
export class NativeScriptRootRenderer implements RootRenderer {
private _viewUtil: ViewUtil;
constructor(
@Optional() @Inject(APP_ROOT_VIEW) private _rootView: View,
@Inject(DEVICE) device: Device,
private _animationDriver: AnimationDriver,
private _zone: NgZone) {
this._viewUtil = new ViewUtil(device);
}
private _registeredComponents: Map<string, NativeScriptRenderer> = new Map<string, NativeScriptRenderer>();
public get rootView(): View {
if (!this._rootView) {
this._rootView = topmost().currentPage;
}
return this._rootView;
}
public get page(): Page {
return <Page>this.rootView.page;
}
public get viewUtil(): ViewUtil {
return this._viewUtil;
}
renderComponent(componentProto: RenderComponentType): Renderer {
let renderer = this._registeredComponents.get(componentProto.id);
if (isBlank(renderer)) {
renderer = new NativeScriptRenderer(this, componentProto, this._animationDriver, this._zone);
this._registeredComponents.set(componentProto.id, renderer);
}
return renderer;
}
}
@Injectable()
export class NativeScriptRenderer extends Renderer {
private componentProtoId: string;
private hasComponentStyles: boolean;
private get viewUtil(): ViewUtil {
return this.rootRenderer.viewUtil;
}
constructor(
private rootRenderer: NativeScriptRootRenderer,
private componentProto: RenderComponentType,
private animationDriver: AnimationDriver,
private zone: NgZone) {
super();
let stylesLength = componentProto.styles.length;
this.componentProtoId = componentProto.id;
for (let i = 0; i < stylesLength; i++) {
this.hasComponentStyles = true;
let cssString = componentProto.styles[i] + "";
const realCSS = this.replaceNgAttribute(cssString, this.componentProtoId);
application.addCss(realCSS);
}
traceLog('NativeScriptRenderer created');
}
private attrReplacer = new RegExp(escapeRegexSymbols(CONTENT_ATTR), "g");
private attrSanitizer = /-/g;
private replaceNgAttribute(input: string, componentId: string): string {
return input.replace(this.attrReplacer,
"_ng_content_" + componentId.replace(this.attrSanitizer, "_"));
}
renderComponent(componentProto: RenderComponentType): Renderer {
return this.rootRenderer.renderComponent(componentProto);
}
selectRootElement(selector: string): NgView {
traceLog('selectRootElement: ' + selector);
const rootView = <NgView><any>this.rootRenderer.rootView;
rootView.nodeName = 'ROOT';
return rootView;
}
createViewRoot(hostElement: NgView): NgView {
traceLog('CREATE VIEW ROOT: ' + hostElement.nodeName);
return hostElement;
}
projectNodes(parentElement: NgView, nodes: NgView[]): void {
traceLog('NativeScriptRenderer.projectNodes');
nodes.forEach((node) => {
this.viewUtil.insertChild(parentElement, node);
});
}
attachViewAfter(anchorNode: NgView, viewRootNodes: NgView[]) {
traceLog('NativeScriptRenderer.attachViewAfter: ' + anchorNode.nodeName + ' ' + anchorNode);
const parent = (<NgView>anchorNode.parent || anchorNode.templateParent);
const insertPosition = this.viewUtil.getChildIndex(parent, anchorNode);
viewRootNodes.forEach((node, index) => {
const childIndex = insertPosition + index + 1;
this.viewUtil.insertChild(parent, node, childIndex);
});
}
detachView(viewRootNodes: NgView[]) {
traceLog('NativeScriptRenderer.detachView');
for (let i = 0; i < viewRootNodes.length; i++) {
let node = viewRootNodes[i];
this.viewUtil.removeChild(<NgView>node.parent, node);
}
}
public destroyView(hostElement: NgView, viewAllNodes: NgView[]) {
traceLog("NativeScriptRenderer.destroyView");
// Seems to be called on component dispose only (router outlet)
//TODO: handle this when we resolve routing and navigation.
}
setElementProperty(renderElement: NgView, propertyName: string, propertyValue: any) {
traceLog("NativeScriptRenderer.setElementProperty " + renderElement + ': ' + propertyName + " = " + propertyValue);
this.viewUtil.setProperty(renderElement, propertyName, propertyValue);
}
setElementAttribute(renderElement: NgView, attributeName: string, attributeValue: string) {
traceLog("NativeScriptRenderer.setElementAttribute " + renderElement + ': ' + attributeName + " = " + attributeValue);
return this.setElementProperty(renderElement, attributeName, attributeValue);
}
setElementClass(renderElement: NgView, className: string, isAdd: boolean): void {
traceLog("NativeScriptRenderer.setElementClass " + className + " - " + isAdd);
if (isAdd) {
this.viewUtil.addClass(renderElement, className);
} else {
this.viewUtil.removeClass(renderElement, className);
}
}
setElementStyle(renderElement: NgView, styleName: string, styleValue: string): void {
this.viewUtil.setStyleProperty(renderElement, styleName, styleValue);
}
/**
* Used only in debug mode to serialize property changes to comment nodes,
* such as <template> placeholders.
*/
setBindingDebugInfo(renderElement: NgView, propertyName: string, propertyValue: string): void {
traceLog('NativeScriptRenderer.setBindingDebugInfo: ' + renderElement + ', ' + propertyName + ' = ' + propertyValue);
}
setElementDebugInfo(renderElement: any, info: RenderDebugInfo): void {
traceLog('NativeScriptRenderer.setElementDebugInfo: ' + renderElement);
}
/**
* Calls a method on an element.
*/
invokeElementMethod(renderElement: NgView, methodName: string, args: Array<any>) {
traceLog("NativeScriptRenderer.invokeElementMethod " + methodName + " " + args);
}
setText(renderNode: any, text: string) {
traceLog("NativeScriptRenderer.setText");
}
public createTemplateAnchor(parentElement: NgView): NgView {
traceLog('NativeScriptRenderer.createTemplateAnchor');
return this.viewUtil.createTemplateAnchor(parentElement);
}
public createElement(parentElement: NgView, name: string): NgView {
traceLog('NativeScriptRenderer.createElement: ' + name + ' parent: ' + parentElement + ', ' + (parentElement ? parentElement.nodeName : 'null'));
return this.viewUtil.createView(name, parentElement, (view) => {
// Set an attribute to the view to scope component-specific css.
// The property name is pre-generated by Angular.
if (this.hasComponentStyles) {
const cssAttribute = this.replaceNgAttribute(CONTENT_ATTR, this.componentProtoId);
view[cssAttribute] = true;
}
});
}
public createText(parentElement: NgView, value: string): NgView {
traceLog('NativeScriptRenderer.createText');
return this.viewUtil.createText(value);
}
public listen(renderElement: NgView, eventName: string, callback: Function): Function {
traceLog('NativeScriptRenderer.listen: ' + eventName);
// Explicitly wrap in zone
let zonedCallback = (...args) => {
this.zone.run(() => {
callback.apply(undefined, args);
});
};
renderElement.on(eventName, zonedCallback);
if (eventName === View.loadedEvent && renderElement.isLoaded) {
const notifyData = { eventName: View.loadedEvent, object: renderElement };
zonedCallback(notifyData);
}
return () => renderElement.off(eventName, zonedCallback);
}
public listenGlobal(target: string, eventName: string, callback: Function): Function {
throw new Error('NativeScriptRenderer.listenGlobal() - Not implemented.');
}
public animate(element: any, startingStyles: AnimationStyles, keyframes: AnimationKeyframe[], duration: number, delay: number, easing: string): AnimationPlayer {
let player = this.animationDriver.animate(element, startingStyles, keyframes, duration, delay, easing);
return player;
}
}