/
AtomXFControl.ts
80 lines (65 loc) · 2.42 KB
/
AtomXFControl.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
import { AtomBinder } from "../../core/AtomBinder";
import { AtomBridge, BaseElementBridge } from "../../core/AtomBridge";
import { AtomComponent } from "../../core/AtomComponent";
import Bind from "../../core/Bind";
import { IAtomElement } from "../../core/types";
import XNode from "../../core/XNode";
import { NavigationService } from "../../services/NavigationService";
declare var UMD: any;
UMD.defaultApp = "@web-atoms/core/dist/xf/XFApp";
UMD.viewPrefix = "xf";
AtomBridge.platform = "xf";
export class AtomXFControl extends AtomComponent<IAtomElement, AtomXFControl> {
public get parent(): AtomXFControl {
return AtomBridge.instance.atomParent(this.element, true) as any;
}
public atomParent(e: IAtomElement): AtomXFControl {
return AtomBridge.instance.atomParent(e, false) as any;
}
public append(element: any): AtomXFControl {
this.element.appendChild(element.element || element);
return this;
}
public dispose(e?: IAtomElement): void {
const el = this.element;
super.dispose(e);
AtomBridge.instance.dispose(el);
}
public invokeEvent(event: { type: string, detail?: any }): void {
(AtomBridge.instance as any).invokeEvent(this.element, event.type, event);
}
public staticResource(name: string) {
return (AtomBridge.instance as any).getStaticResource(this.element, name);
}
public loadXaml(text: string) {
(AtomBridge.instance as any).loadXaml(this.element, text);
}
protected setElementValue(element: any, name: string, value: any): void {
if (/^event/.test(name)) {
this.bindEvent(element, name.substr(5), async () => {
try {
const p = value() as Promise<any>;
if (p) {
await p;
}
} catch (e) {
if (/cancelled/i.test(e)) {
return;
}
const nav: NavigationService = this.app.resolve(NavigationService);
await nav.alert(e, "Error");
}
});
return;
}
AtomBridge.instance.setValue(element, name, value);
}
}
declare var bridge;
bridge.controlFactory = AtomXFControl;
// add custom event...
declare var global;
global.CustomEvent = function(type: string, { detail }) {
this.type = type;
this.detail = detail;
};