-
-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathrenderer.ts
More file actions
227 lines (197 loc) · 7.41 KB
/
renderer.ts
File metadata and controls
227 lines (197 loc) · 7.41 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
import { Inject, Injectable, Optional, NgZone, Renderer2, RendererFactory2, RendererType2, RendererStyleFlags2, ViewEncapsulation } from '@angular/core';
import { View, getViewById, profile } from '@nativescript/core';
import { ViewUtil } from './view-util';
import { NgView, InvisibleNode } from './element-registry';
import { NativeScriptDebug } from './trace';
@Injectable()
export class NativeScriptRenderer extends Renderer2 {
data: { [key: string]: any } = Object.create(null);
constructor(private rootView: NgView, private zone: NgZone, private viewUtil: ViewUtil) {
super();
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog('NativeScriptRenderer created');
}
}
@profile
appendChild(parent: NgView, newChild: NgView): void {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.appendChild child: ${newChild} parent: ${parent}`);
}
this.viewUtil.insertChild(parent, newChild);
}
@profile
insertBefore(parent: NgView, newChild: NgView, refChild: NgView): void {
let { previous, next } = refChild instanceof View ? { previous: refChild.previousSibling, next: refChild } : { previous: null, next: null };
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.insertBefore child: ${newChild} ` + `parent: ${parent} previous: ${previous} next: ${next}`);
}
this.viewUtil.insertChild(parent, newChild, previous, next);
}
@profile
removeChild(parent: any, oldChild: NgView): void {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.removeChild child: ${oldChild} parent: ${parent}`);
}
this.viewUtil.removeChild(parent, oldChild);
}
@profile
selectRootElement(selector: string): NgView {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.selectRootElement: ${selector}`);
}
if (selector && selector[0] === '#') {
const result = getViewById(this.rootView, selector.slice(1));
return (result || this.rootView) as NgView;
}
return this.rootView;
}
@profile
parentNode(node: NgView): any {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.parentNode for node: ${node} is ${node.parentNode}`);
}
return node.parentNode;
}
@profile
nextSibling(node: NgView): NgView {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.nextSibling of ${node} is ${node.nextSibling}`);
}
return node.nextSibling;
}
@profile
createComment(_value: any): InvisibleNode {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.createComment ${_value}`);
}
return this.viewUtil.createComment();
}
@profile
createElement(name: any, _namespace: string): NgView {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.createElement: ${name}`);
}
return this.viewUtil.createView(name);
}
@profile
createText(_value: string): InvisibleNode {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.createText ${_value}`);
}
return this.viewUtil.createText();
}
@profile
createViewRoot(hostElement: NgView): NgView {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.createViewRoot ${hostElement.nodeName}`);
}
return hostElement;
}
@profile
projectNodes(parentElement: NgView, nodes: NgView[]): void {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog('NativeScriptRenderer.projectNodes');
}
nodes.forEach((node) => this.viewUtil.insertChild(parentElement, node));
}
@profile
destroy() {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog('NativeScriptRenderer.destroy');
}
// Seems to be called on component dispose only (router outlet)
// TODO: handle this when we resolve routing and navigation.
}
@profile
setAttribute(view: NgView, name: string, value: string, namespace?: string) {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.setAttribute ${view} : ${name} = ${value}, namespace: ${namespace}`);
}
return this.viewUtil.setProperty(view, name, value, namespace);
}
@profile
removeAttribute(_el: NgView, _name: string): void {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.removeAttribute ${_el}: ${_name}`);
}
}
@profile
setProperty(view: any, name: string, value: any) {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.setProperty ${view} : ${name} = ${value}`);
}
return this.viewUtil.setProperty(view, name, value);
}
@profile
addClass(view: NgView, name: string): void {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.addClass ${name}`);
}
this.viewUtil.addClass(view, name);
}
@profile
removeClass(view: NgView, name: string): void {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.removeClass ${name}`);
}
this.viewUtil.removeClass(view, name);
}
@profile
setStyle(view: NgView, styleName: string, value: any, _flags?: RendererStyleFlags2): void {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.setStyle: ${styleName} = ${value}`);
}
this.viewUtil.setStyle(view, styleName, value);
}
@profile
removeStyle(view: NgView, styleName: string, _flags?: RendererStyleFlags2): void {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog('NativeScriptRenderer.removeStyle: ${styleName}');
}
this.viewUtil.removeStyle(view, styleName);
}
// Used only in debug mode to serialize property changes to comment nodes,
// such as <template> placeholders.
@profile
setBindingDebugInfo(renderElement: NgView, propertyName: string, propertyValue: string): void {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.setBindingDebugInfo: ${renderElement}, ${propertyName} = ${propertyValue}`);
}
}
@profile
setElementDebugInfo(renderElement: any, _info: any /*RenderDebugInfo*/): void {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.setElementDebugInfo: ${renderElement}`);
}
}
@profile
invokeElementMethod(_renderElement: NgView, methodName: string, args: Array<any>) {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.invokeElementMethod ${methodName} ${args}`);
}
}
@profile
setValue(_renderNode: any, _value: string) {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.setValue renderNode: ${_renderNode}, value: ${_value}`);
}
}
@profile
listen(renderElement: any, eventName: string, callback: (event: any) => boolean): () => void {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`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);
}
}