forked from NativeScript/nativescript-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview-util.ts
More file actions
331 lines (286 loc) · 11.3 KB
/
view-util.ts
File metadata and controls
331 lines (286 loc) · 11.3 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import {isString, isDefined} from "utils/types";
import {View} from "ui/core/view";
import {Placeholder} from "ui/placeholder";
import {ContentView} from 'ui/content-view';
import {LayoutBase} from 'ui/layouts/layout-base';
import {ViewClass, getViewClass, getViewMeta, isKnownView, ViewExtensions, NgView} from './element-registry';
import {getSpecialPropertySetter} from "ui/builder/special-properties";
import {StyleProperty, getPropertyByName, withStyleProperty} from "ui/styling/style-property";
import {ValueSource} from "ui/core/dependency-observable";
import {platformNames, Device} from "platform";
import {rendererLog as traceLog, styleError} from "./trace";
const IOS_PREFX: string = ":ios:";
const ANDROID_PREFX: string = ":android:";
const whiteSpaceSplitter = /\s+/;
export type ViewExtensions = ViewExtensions;
export type NgView = NgView;
export type NgLayoutBase = LayoutBase & ViewExtensions;
export type NgContentView = ContentView & ViewExtensions;
export type BeforeAttachAction = (view: View) => void;
export function isView(view: any): view is NgView {
return view instanceof View;
}
export function isLayout(view: any): view is NgLayoutBase {
return view instanceof LayoutBase;
}
export function isContentView(view: any): view is NgContentView {
return view instanceof ContentView;
}
const propertyMaps: Map<Function, Map<string, string>> = new Map<Function, Map<string, string>>();
export class ViewUtil {
private isIos: boolean;
private isAndroid: boolean;
constructor(device: Device) {
this.isIos = device.os === platformNames.ios;
this.isAndroid = device.os === platformNames.android;
}
public insertChild(parent: any, child: NgView, atIndex = -1) {
if (!parent || child.meta.skipAddToDom) {
return;
}
if (parent.meta && parent.meta.insertChild) {
parent.meta.insertChild(parent, child, atIndex);
} else if (isLayout(parent)) {
if (child.parent === parent) {
let index = (<LayoutBase>parent).getChildIndex(child);
if (index !== -1) {
parent.removeChild(child);
}
}
if (atIndex !== -1) {
parent.insertChild(child, atIndex);
} else {
parent.addChild(child);
}
} else if (isContentView(parent)) {
parent.content = child;
} else if (parent && parent._addChildFromBuilder) {
parent._addChildFromBuilder(child.nodeName, child);
} else {
//throw new Error("Parent can't contain children: " + parent.nodeName + ', ' + parent);
}
}
public removeChild(parent: any, child: NgView) {
if (!parent || child.meta.skipAddToDom) {
return;
}
if (parent.meta && parent.meta.removeChild) {
parent.meta.removeChild(parent, child);
} else if (isLayout(parent)) {
parent.removeChild(child);
} else if (isContentView(parent)) {
if (parent.content === child) {
parent.content = null;
}
} else if (isView(parent)) {
parent._removeView(child);
} else {
//throw new Error('Unknown parent type: ' + parent);
}
}
public getChildIndex(parent: any, child: NgView) {
if (isLayout(parent)) {
return parent.getChildIndex(child);
} else if (isContentView(parent)) {
return child === parent.content ? 0 : -1;
} else {
//throw new Error("Parent can't contain children: " + parent);
}
}
private createAndAttach(name: string, viewClass: ViewClass, parent: NgView, beforeAttach?: BeforeAttachAction): NgView {
const view = <NgView>new viewClass();
view.nodeName = name;
view.meta = getViewMeta(name);
if (beforeAttach) {
beforeAttach(view);
}
if (parent) {
this.insertChild(parent, view);
}
return view;
}
public createView(name: string, parent: NgView, beforeAttach?: BeforeAttachAction): NgView {
if (isKnownView(name)) {
const viewClass = getViewClass(name);
return this.createAndAttach(name, viewClass, parent, beforeAttach);
} else {
return this.createViewContainer(name, parent, beforeAttach);
}
}
public createText(value: string): NgView {
const text = <NgView>new Placeholder();
text.nodeName = "#text";
text.visibility = "collapse";
text.meta = getViewMeta("Placeholder");
return text;
}
public createViewContainer(name: string, parentElement: NgView, beforeAttach: BeforeAttachAction) {
traceLog('Creating view container in:' + parentElement);
const layout = this.createView('ProxyViewContainer', parentElement, beforeAttach);
layout.nodeName = 'ProxyViewContainer';
return layout;
}
public createTemplateAnchor(parentElement: NgView) {
//HACK: Using a ContentView here, so that it creates a native View object
const anchor = this.createAndAttach('template', ContentView, parentElement);
anchor.visibility = "collapse";
anchor.templateParent = parentElement;
return anchor;
}
private isXMLAttribute(name: string): boolean {
switch (name) {
case "style": return true;
case "rows": return true;
case "columns": return true;
case "fontAttributes": return true;
default: return false;
}
}
private platformFilter(attribute: string): string {
let lowered = attribute.toLowerCase();
if (lowered.indexOf(IOS_PREFX) === 0) {
if (this.isIos) {
return attribute.substr(IOS_PREFX.length);
} else {
return null;
}
}
if (lowered.indexOf(ANDROID_PREFX) === 0) {
if (this.isAndroid) {
return attribute.substr(ANDROID_PREFX.length);
} else {
return null;
}
}
return attribute;
}
public setProperty(view: NgView, attributeName: string, value: any): void {
attributeName = this.platformFilter(attributeName);
if (!attributeName) {
return;
}
if (attributeName.indexOf(".") !== -1) {
// Handle nested properties
const properties = attributeName.split(".");
attributeName = properties[properties.length - 1];
let propMap = this.getProperties(view);
let i = 0;
while (i < properties.length - 1 && isDefined(view)) {
let prop = properties[i];
if (propMap.has(prop)) {
prop = propMap.get(prop);
}
view = view[prop];
propMap = this.getProperties(view);
i++;
}
}
if (isDefined(view)) {
this.setPropertyInternal(view, attributeName, value);
}
}
private setPropertyInternal(view: NgView, attributeName: string, value: any): void {
traceLog('Setting attribute: ' + attributeName);
let specialSetter = getSpecialPropertySetter(attributeName);
let propMap = this.getProperties(view);
if (attributeName === "class") {
this.setClasses(view, value);
} else if (this.isXMLAttribute(attributeName)) {
view._applyXmlAttribute(attributeName, value);
} else if (specialSetter) {
specialSetter(view, value);
} else if (propMap.has(attributeName)) {
// We have a lower-upper case mapped property.
let propertyName = propMap.get(attributeName);
view[propertyName] = this.convertValue(value);
} else {
// Unknown attribute value -- just set it to our object as is.
view[attributeName] = this.convertValue(value);
}
}
private convertValue(value: any): any {
if (typeof (value) !== "string" || value === "") {
return value;
}
let valueAsNumber = +value;
if (!isNaN(valueAsNumber)) {
return valueAsNumber;
} else if (value && (value.toLowerCase() === "true" || value.toLowerCase() === "false")) {
return value.toLowerCase() === "true" ? true : false;
} else {
return value;
}
}
private getProperties(instance: any): Map<string, string> {
let type = instance && instance.constructor;
if (!type) {
return new Map<string, string>();
}
if (!propertyMaps.has(type)) {
let propMap = new Map<string, string>();
for (let propName in instance) {
propMap.set(propName.toLowerCase(), propName);
}
propertyMaps.set(type, propMap);
}
return propertyMaps.get(type);
}
private cssClasses(view: NgView) {
if (!view.ngCssClasses) {
view.ngCssClasses = new Map<string, boolean>();
}
return view.ngCssClasses;
}
public addClass(view: NgView, className: string): void {
this.cssClasses(view).set(className, true);
this.syncClasses(view);
}
public removeClass(view: NgView, className: string): void {
this.cssClasses(view).delete(className);
this.syncClasses(view);
}
private setClasses(view: NgView, classesValue: string): void {
let classes = classesValue.split(whiteSpaceSplitter);
this.cssClasses(view).clear();
classes.forEach((className) => this.cssClasses(view).set(className, true));
this.syncClasses(view);
}
private syncClasses(view: NgView): void {
let classValue = (<any>Array).from(this.cssClasses(view).keys()).join(' ');
view.cssClass = classValue;
}
private resolveCssValue(styleValue: string): string {
return styleValue;
}
private setStyleValue(view: NgView, property: StyleProperty, value: any) {
try {
if (value === null) {
view.style._resetValue(property, ValueSource.Local);
}
else {
view.style._setValue(property, value, ValueSource.Local);
}
} catch (ex) {
styleError("Error setting property: " + property.name + " view: " + view + " value: " + value + " " + ex);
}
}
public setStyleProperty(view: NgView, styleName: string, styleValue: string): void {
traceLog("setStyleProperty: " + styleName + " = " + styleValue);
let name = styleName;
let resolvedValue = this.resolveCssValue(styleValue);
withStyleProperty(name, resolvedValue, (property, value) => {
if (isString(property)) {
//Fall back to resolving property by name.
const resolvedProperty = getPropertyByName(name);
if (resolvedProperty) {
this.setStyleValue(view, resolvedProperty, resolvedValue);
} else {
traceLog("Unknown style property: " + styleName);
}
} else {
const resolvedProperty = <StyleProperty>property;
this.setStyleValue(view, resolvedProperty, value);
}
});
}
}