-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mount.js
149 lines (149 loc) · 4.52 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { O } from './froop/O.js';
export class Mount extends O {
static mntCfgMxn = {
propInfo: {
clonedTemplate: {
type: 'Object',
ro: true,
},
hydrated: {
type: 'Boolean',
ro: true,
},
deferHydration: {
type: 'Boolean',
parse: true,
attrName: 'defer-hydration'
}
},
actions: {
cloneMT: {
ifAllOf: 'csr'
},
initCSRXform: {
ifAllOf: ['clonedTemplate', 'xform'],
ifNoneOf: ['deferHydration'],
},
mountClone: {
ifAllOf: ['clonedTemplate', 'hydrated'],
},
initSSRXform: {
ifAllOf: ['xform'],
ifNoneOf: ['csr', 'deferHydration'],
},
onNoXForm: {
ifNoneOf: ['xform']
}
}
};
#root;
#csr = false;
get csr() {
return (this.config.assumeCSR && !this.hasAttribute('no-csr')) || this.#csr || this.hasAttribute('csr');
}
get config() {
return this.constructor.config;
}
// get mntConfig(){
// return (<any>this.constructor).mntCfgMxn as OConfig;
// }
get xform() {
return this.config.xform;
}
constructor() {
super();
const { config } = this;
const { shadowRootInit, styles } = config;
if (shadowRootInit) {
if (this.shadowRoot === null) {
this.attachShadow(shadowRootInit);
this.#csr = true;
}
else if (!styles) {
const declarativeStyles = Array.from(this.shadowRoot.querySelectorAll('style[adopt]'));
config.styles = declarativeStyles.map(x => x.innerHTML);
}
this.#root = this.shadowRoot;
let stringStyles;
if (typeof (styles) === 'string') {
stringStyles = [styles];
}
else if (Array.isArray(styles) && styles.length > 0 && typeof (styles[0]) === 'string') {
stringStyles = styles;
}
else if (typeof (styles) === undefined) {
stringStyles = [''];
}
if (stringStyles !== undefined) {
stringStyles = stringStyles.map(x => x.replace("<style>", "").replace("</style>", ""));
stringStyles[0] += String.raw `
* {
--attrs-to-reflect: initial;
}
`;
const CSSStyleSheets = [];
for (const stringSyleSheet of stringStyles) {
const newSheet = new CSSStyleSheet();
newSheet.replaceSync(stringSyleSheet);
CSSStyleSheets.push(newSheet);
}
config.styles = CSSStyleSheets;
}
if (this.#csr) {
let compiledStyles = config.styles;
if (compiledStyles !== undefined) {
this.shadowRoot.adoptedStyleSheets = [...compiledStyles];
}
}
}
else {
this.#root = this;
}
}
cloneMT(self) {
const { config } = this;
let { mainTemplate } = config;
if (typeof mainTemplate === 'string') {
const templ = document.createElement('template');
templ.innerHTML = mainTemplate;
config.mainTemplate = templ;
mainTemplate = templ;
}
const clonedTemplate = mainTemplate.content.cloneNode(true);
return {
clonedTemplate
};
}
async initCSRXform(self) {
const { clonedTemplate, xform, propagator } = self;
const { Transform } = await import('./Transform.js');
await Transform(clonedTemplate, this, xform, {
propagator,
propagatorIsReady: true,
});
return {
hydrated: true,
};
}
mountClone(self) {
const { clonedTemplate } = self;
this.#root.appendChild(clonedTemplate);
return {};
}
async initSSRXform(self) {
const root = self.#root;
const { xform, propagator } = self;
const { Transform } = await import('./Transform.js');
await Transform(root, this, xform, {
propagator
});
return {
hydrated: true
};
}
async onNoXForm(self) {
return {
hydrated: true,
};
}
}