-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathangular2.js
122 lines (99 loc) · 3.53 KB
/
angular2.js
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
/* @flow */
/* eslint new-cap: 0 */
import { replaceObject } from 'belter/src';
import type { ComponentDriverType } from '../component';
import { CONTEXT } from '../constants';
type Angular2Injection = {||};
type Angular2Component = {||};
type Angular2Module = {| annotations : Object, name : string |};
type Angular2 = {|
Component : ({| selector : string, template : string, inputs : $ReadOnlyArray<string> |}) => {|
Class : ({| constructor : $ReadOnlyArray<Angular2Injection | Function>, ngOnInit : () => void, ngDoCheck : () => void |}) => Angular2Component
|},
NgModule : ({| declarations : $ReadOnlyArray<*>, exports : $ReadOnlyArray<*> |}) => {|
Class : ({| constructor : () => void |}) => Angular2Module
|},
ElementRef : Angular2Injection,
NgZone : Angular2Injection,
Inject : Function
|};
const equals = (obj1, obj2) => {
const checked = {};
for (const key in obj1) {
if (obj1.hasOwnProperty(key)) {
checked[key] = true;
if (obj1[key] !== obj2[key]) {
return false;
}
}
}
for (const key in obj2) {
if (!checked[key]) {
return false;
}
}
return true;
};
export const angular2 : ComponentDriverType<*, Angular2, Angular2Module, *, *> = {
register: (tag, propsDef, init, { Component : AngularComponent, NgModule, ElementRef, NgZone, Inject }) => {
class ComponentInstance {
elementRef : Object;
internalProps : Object;
parent : Object;
props : Object;
zone : Object;
_props : Object;
static annotations : $ReadOnlyArray<*>;
static parameters : $ReadOnlyArray<*>;
constructor (elementRef, zone) {
this._props = {};
this.elementRef = elementRef;
this.zone = zone;
}
getProps () : Object {
return replaceObject({ ...this.internalProps, ...this.props }, item => {
if (typeof item === 'function') {
const { zone } = this;
return function angular2Wrapped() : void {
// $FlowFixMe
return zone.run(() => item.apply(this, arguments));
};
}
return item;
});
}
ngOnInit() {
const targetElement = this.elementRef.nativeElement;
this.parent = init(this.getProps());
this.parent.render(targetElement, CONTEXT.IFRAME);
}
ngDoCheck() {
if (this.parent && !equals(this._props, this.props)) {
this._props = { ...this.props };
this.parent.updateProps(this.getProps());
}
}
}
ComponentInstance.parameters = [
[ new Inject(ElementRef) ],
[ new Inject(NgZone) ]
];
ComponentInstance.annotations = [
new AngularComponent({
selector: tag,
template: `<div></div>`,
inputs: [ 'props' ]
})
];
class ModuleInstance {
static annotations : $ReadOnlyArray<*>;
}
ModuleInstance.annotations = [
new NgModule({
declarations: [ ComponentInstance ],
exports: [ ComponentInstance ]
})
];
return ModuleInstance;
}
};