-
Notifications
You must be signed in to change notification settings - Fork 45
/
helpers.ts
92 lines (83 loc) · 2.59 KB
/
helpers.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
// Hot Module Replacement
function identity(val?: any) {
return val;
}
export function bootloader(main: any, before?: any, after?: any): void {
if (typeof main === 'object') {
const _main = main.main;
before = main.before;
after = main.after;
main = _main;
}
before = before || identity;
after = after || identity;
const readyState = document.readyState;
function __domReadyHandler() {
document.removeEventListener('DOMContentLoaded', __domReadyHandler);
after(main(before(readyState)));
}
switch (readyState) {
case 'loading':
document.addEventListener('DOMContentLoaded', __domReadyHandler);
break;
case 'interactive':
case 'complete':
default:
after(main(before(readyState)));
}
}
// create new host elements and remove the old elements
export function createNewHosts(cmps: any) {
const components = Array.prototype.map.call(cmps, function(componentNode: any) {
const newNode = document.createElement(componentNode.tagName);
const parentNode = componentNode.parentNode;
const currentDisplay = newNode.style.display;
newNode.style.display = 'none';
parentNode.insertBefore(newNode, componentNode);
function removeOldHost() {
newNode.style.display = currentDisplay;
try {
parentNode.removeChild(componentNode);
} catch (e) {}
}
return removeOldHost;
});
return function removeOldHosts() {
components.forEach((removeOldHost: any) => removeOldHost());
};
}
// remove old styles
export function removeNgStyles() {
const docHead = document.head;
const _styles = docHead.querySelectorAll('style');
const styles = Array.prototype.slice.call(_styles);
styles
.filter((style: any) => style.innerText.indexOf('_ng') !== -1)
.map((el: any) => docHead.removeChild(el));
}
// get input values
export function getInputValues() {
const _inputs = document.querySelectorAll('input');
const inputs = Array.prototype.slice.call(_inputs);
return inputs.map((input: any) => input.value);
}
// set input values
export function setInputValues(_inputs: any) {
const inputs = document.querySelectorAll('input');
if (_inputs && inputs.length === _inputs.length) {
_inputs.forEach((value: any, i: number) => {
const el: any = inputs[i];
el.value = value;
el.dispatchEvent(new CustomEvent('input', {detail: el.value}));
});
// clear array
_inputs.length = 0;
}
}
// get/set input values
export function createInputTransfer() {
const _inputs = getInputValues();
return function restoreInputValues() {
return setInputValues(_inputs);
};
}