-
Notifications
You must be signed in to change notification settings - Fork 78
/
NativeElementNode.ts
213 lines (176 loc) · 6.89 KB
/
NativeElementNode.ts
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
import { logger as log, ViewNode, registerElement } from '../basicdom'
import { isAndroid, isIOS, ObservableArray } from '@nativescript/core';
import ElementNode from '../basicdom/ElementNode';
export enum NativeElementPropType {
Value,
Array,
ObservableArray
}
export interface NativeElementPropConfig {
[key: string]: NativeElementPropType
}
function setOnArrayProp(parent: any, value: any, propName: string, index: number, build: (value: any) => any = null) {
log.debug(()=> `setOnArrayProp ${propName} index: ${index}`)
let current = parent[propName];
if (!current || !current.push) {
parent[propName] = build ? build(value) : [value];
} else {
if (current instanceof ObservableArray) {
if (index > -1) {
current.splice(index, 0, value)
} else {
current.push(value);
}
} else {
if (index > -1) {
const newArr = current.slice();
newArr.splice(index, 0, value);
parent[propName] = newArr
} else {
parent[propName] = [...current, value];
}
}
}
}
function removeFromArrayProp(parent: any, value: any, propName: string) {
let current = parent[propName];
if (!current || !current.splice) {
return;
}
let idx = current.indexOf(value);
if (idx < 0) return;
if (current instanceof ObservableArray) {
current.splice(idx, 1);
} else {
const newArr = current.slice()
newArr.splice(idx, 1);
parent[propName] = newArr
}
}
// Implements an ElementNode that wraps a NativeScript object. It uses the object as the source of truth for its attributes
export default class NativeElementNode<T> extends ElementNode {
_nativeElement: T;
propAttribute: string = null;
propConfig: NativeElementPropConfig;
constructor(tagName: string, elementClass: new () => T, setsParentProp: string = null, propConfig: NativeElementPropConfig = {}) {
super(tagName);
this.propConfig = propConfig
this.propAttribute = setsParentProp
this._nativeElement = new elementClass();
(this._nativeElement as any).__SvelteNativeElement__ = this;
log.debug(() => `created ${this} ${this._nativeElement}`)
}
get nativeElement() {
return this._nativeElement
}
set nativeElement(el) {
if (this._nativeElement) {
throw new Error(`Can't overwrite native element.`)
}
this._nativeElement = el
}
getAttribute(fullkey: string) {
let getTarget = this.nativeElement as any;
let keypath = fullkey.split(".");
let resolvedKeys: string[] = [];
while (keypath.length > 0) {
if (!getTarget) return null;
let key = keypath.shift();
resolvedKeys.push(key)
if (keypath.length > 0) {
getTarget = getTarget[key];
} else {
return getTarget[key];
}
}
return null;
}
onInsertedChild(childNode: ViewNode, index: number) {
super.onInsertedChild(childNode, index);
// support for the prop: shorthand for setting parent property to native element
if (!(childNode instanceof NativeElementNode)) return;
let propName = childNode.propAttribute;
if (!propName) return;
//Special case Array and Observable Array keys
if (!this.propConfig[propName] || this.propConfig[propName] == NativeElementPropType.Value) {
this.setAttribute(propName, childNode);
return;
}
//our array index is based on how many items with the same prop attribute come before us
const allPropSetters = this.childNodes.filter(n => n instanceof NativeElementNode && n.propAttribute && n.propAttribute.toLowerCase() == propName.toLowerCase());
const myIndex = allPropSetters.indexOf(childNode)
switch (this.propConfig[propName]) {
case NativeElementPropType.Array:
setOnArrayProp(this.nativeElement, childNode.nativeElement, propName, myIndex)
return;
case NativeElementPropType.ObservableArray:
setOnArrayProp(this.nativeElement, childNode.nativeElement, propName, myIndex, (v) => new ObservableArray(v))
return;
}
}
onRemovedChild(childNode: ViewNode) {
if (!(childNode instanceof NativeElementNode)) return;
let propName = childNode.propAttribute;
if (!propName) return;
//Special case Array and Observable Array keys
switch (this.propConfig[propName]) {
case NativeElementPropType.Array:
case NativeElementPropType.ObservableArray:
removeFromArrayProp(this.nativeElement, childNode.nativeElement, propName)
return;
default:
this.setAttribute(propName, null);
}
super.onRemovedChild(childNode)
}
removeAttribute(fullkey: string) {
// if an attribute is set to null svelte will call removeAttribute
// but we still need to call setAttribute to apply the change on N view
this.setAttribute(fullkey, null);
}
setAttribute(fullkey: string, value: any) {
const nv = this.nativeElement as any
let setTarget = nv;
// normalize key
if (__ANDROID__) {
if (fullkey.startsWith('android:')) {
fullkey = fullkey.substring(8);
}
if (fullkey.startsWith('ios:')) {
return;
}
}
if (__IOS__) {
if (fullkey.startsWith('ios:')) {
fullkey = fullkey.substring(4);
}
if (fullkey.startsWith('android:')) {
return;
}
}
if (fullkey.startsWith("prop:")) {
this.propAttribute = fullkey.substring(5);
return;
}
//we might be getting an element from a propertyNode eg page.actionBar, unwrap
if (value instanceof NativeElementNode) {
value = value.nativeElement
}
let keypath = fullkey.split(".");
let resolvedKeys: string[] = [];
while (keypath.length > 0) {
if (!setTarget) return;
let key = keypath.shift();
resolvedKeys.push(key)
if (keypath.length > 0) {
setTarget = setTarget[key];
} else {
log.debug(() => `setAttr value ${this} ${resolvedKeys.join(".")} ${value}`)
setTarget[key] = value
}
}
}
}
export function registerNativeConfigElement<T>(elementName: string, resolver: () => new () => T, parentProp: string = null, propConfig: NativeElementPropConfig = {}) {
registerElement(elementName, () => new NativeElementNode(elementName, resolver(), parentProp, propConfig));
}