-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmount.js
82 lines (73 loc) · 2.02 KB
/
mount.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
import { createElement } from "react";
import { render } from "react-dom";
import { withErrorBoundary } from "react-error-boundary";
import ErrorBoundary from "../components/alert/alert";
import { setToken } from "./token";
import Store from "../components/store";
import { persistor } from "./store";
/**
* We look for containers and corresponding applications.
* Thereafter we mount them if a corresponding container and application can be found.
*
* @param {HTMLElement} context - The HTML element you want to search for app containers in.
*/
function mount(context) {
if (!context) return;
const appContainers = context.querySelectorAll("[data-ddb-app]");
function mountApp(container) {
const appName = container?.dataset?.ddbApp;
const app = window.ddbReact?.apps?.[appName];
// Ensure that the application exists and that the container isn't already populated.
const isValidMount = app && !container.innerHTML;
if (isValidMount) {
render(
createElement(
Store,
{},
createElement(withErrorBoundary(app, ErrorBoundary), {
...container.dataset
})
),
container
);
}
}
appContainers.forEach(mountApp);
}
/**
* If you want to remove all ddb apps in a certain context.
*
* @param {HTMLElement} context - The HTML element you want to search for app containers in.
*/
function unmount(context) {
if (!context) return;
const appContainers = context.querySelectorAll("[data-ddb-app]");
function unMountApp(container) {
const appContainerToUnmount = container;
appContainerToUnmount.innerHTML = "";
}
appContainers.forEach(unMountApp);
}
/**
* Resets any stored state of all components.
*
* @returns {Promise<any>}
*/
function reset() {
return persistor.purge();
}
function init() {
const initial = {
apps: {},
setToken,
mount,
unmount,
reset
};
window.ddbReact = {
...(window.ddbReact || {}),
...initial
};
}
// Inject into the global namespace for third party access.
init();