-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsingle-spa-angularjs.js
125 lines (103 loc) · 3.06 KB
/
single-spa-angularjs.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
123
124
125
const defaultOpts = {
// required opts
angular: null,
domElementGetter: null,
mainAngularModule: null,
// optional opts
uiRouter: false,
preserveGlobal: false,
elementId: "__single_spa_angular_1",
strictDi: false,
template: undefined,
};
export default function singleSpaAngularJS(userOpts) {
if (typeof userOpts !== "object") {
throw new Error(`single-spa-angularjs requires a configuration object`);
}
const opts = {
...defaultOpts,
...userOpts,
};
if (!opts.angular) {
throw new Error(`single-spa-angularjs must be passed opts.angular`);
}
if (opts.domElementGetter && typeof opts.domElementGetter !== "function") {
throw new Error(
`single-spa-angularjs opts.domElementGetter must be a function`
);
}
if (!opts.mainAngularModule) {
throw new Error(
`single-spa-angularjs must be passed opts.mainAngularModule string`
);
}
// A shared object to store mounted object state
const mountedInstances = {};
return {
bootstrap: bootstrap.bind(null, opts, mountedInstances),
mount: mount.bind(null, opts, mountedInstances),
unmount: unmount.bind(null, opts, mountedInstances),
};
}
function bootstrap(opts) {
return Promise.resolve();
}
function mount(opts, mountedInstances, props = {}) {
return Promise.resolve().then(() => {
window.angular = opts.angular;
const containerEl = getContainerEl(opts, props);
const bootstrapEl = document.createElement("div");
bootstrapEl.id = opts.elementId;
containerEl.appendChild(bootstrapEl);
if (opts.uiRouter) {
const uiViewEl = document.createElement("div");
uiViewEl.setAttribute(
"ui-view",
opts.uiRouter === true ? "" : opts.uiRouter
);
bootstrapEl.appendChild(uiViewEl);
}
if (opts.template) {
bootstrapEl.innerHTML = opts.template;
}
if (opts.strictDi) {
mountedInstances.instance = opts.angular.bootstrap(
bootstrapEl,
[opts.mainAngularModule],
{ strictDi: opts.strictDi }
);
} else {
mountedInstances.instance = opts.angular.bootstrap(bootstrapEl, [
opts.mainAngularModule,
]);
}
mountedInstances.instance.get("$rootScope").singleSpaProps = props;
});
}
function unmount(opts, mountedInstances, props = {}) {
return new Promise((resolve, reject) => {
mountedInstances.instance.get("$rootScope").$destroy();
getContainerEl(opts, props).innerHTML = "";
if (opts.angular === window.angular && !opts.preserveGlobal)
delete window.angular;
setTimeout(resolve);
});
}
function getContainerEl(opts, props) {
let element;
if (opts.domElementGetter) {
element = opts.domElementGetter();
} else {
const htmlId = `single-spa-application:${props.name || props.appName}`;
element = document.getElementById(htmlId);
if (!element) {
element = document.createElement("div");
element.id = htmlId;
document.body.appendChild(element);
}
}
if (!element) {
throw new Error(`domElementGetter did not return a valid dom element`);
}
return element;
}