-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbe-definitive.js
166 lines (166 loc) · 6.27 KB
/
be-definitive.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { BE, propDefaults, propInfo } from 'be-enhanced/BE.js';
import { XE } from 'xtal-element/XE.js';
import { register } from 'be-hive/register.js';
import { TemplMgmt, beTransformed } from 'trans-render/lib/mixins/TemplMgmt.js';
import { toTempl } from 'be-hive/toTempl.js';
export class BeDefinitive extends BE {
static get beConfig() {
return {
parse: false,
};
}
async attach(enhancedElement, enhancementInfo) {
let wcElement = enhancedElement, attrElement = enhancedElement, isLocal = false;
const { localName } = enhancedElement;
const isScript = localName === 'script';
if (localName === 'be-hive' || isScript) {
const { findRealm } = await import('trans-render/lib/findRealm.js');
wcElement = await findRealm(enhancedElement, 'hostish');
attrElement = enhancedElement;
isLocal = true;
}
const templProps = wcElement;
templProps.skipTemplateClone = true;
templProps.clonedTemplate = wcElement.shadowRoot || wcElement;
await super.attach(attrElement, enhancementInfo);
const { fqn } = enhancementInfo;
let params = undefined;
if (attrElement.hasAttribute(fqn)) {
const attrVal = attrElement.getAttribute(fqn).trim();
if (attrVal[0] !== '{' && attrVal[0] !== '[') {
params = {
config: {
tagName: attrVal,
propDefaults: {
shadowRootMode: wcElement.shadowRoot === null ? undefined : 'open',
}
}
};
}
else {
try {
params = JSON.parse(attrVal);
}
catch (e) {
console.error({ fqn, attrVal, e });
this.rejected = true;
return;
}
}
if (isLocal) {
attrElement.removeAttribute(fqn);
}
}
else {
params = {};
}
const { scriptRef } = params;
if (scriptRef || isScript) {
const qry = `#${scriptRef}`;
const scriptElement = (isScript ? enhancedElement :
attrElement.getRootNode().querySelector(qry) || (wcElement.shadowRoot?.querySelector(qry)));
if (!scriptElement) {
throw { qry, message: '404' };
}
import('be-exportable/be-exportable.js');
const beExpAP = await scriptElement.beEnhanced.whenResolved('be-exportable');
await this.setParamsFromScript(enhancedElement, beExpAP.exports, params);
}
params.config = params.config || {};
const config = params.config;
let tagName = config.tagName || wcElement.localName;
if (tagName.indexOf('-') === -1)
tagName = wcElement.id;
config.tagName = tagName;
if (customElements.get(config.tagName))
return;
config.propDefaults = config.propDefaults || {};
//const {propDefaults} = config;
//propDefaults.transform = propDefaults.transform;
config.actions = {
...(config.actions || {}),
...beTransformed,
};
config.propInfo = {
...(config.propInfo || {})
};
await this.register(wcElement, params);
this.resolved = true;
}
async setParamsFromScript(self, exports, params) {
const { complexPropDefaults, mixins, superclass, complexConfig } = params;
if (complexConfig !== undefined) {
const obj = exports[complexConfig];
const config = params.config || {};
Object.assign(config, obj);
params.config = config;
}
if (complexPropDefaults !== undefined) {
for (const key in complexPropDefaults) {
const val = complexPropDefaults[key];
complexPropDefaults[key] = exports[val];
}
}
if (mixins !== undefined) {
for (let i = 0, ii = mixins.length; i < ii; i++) {
const mixin = mixins[i];
mixins[i] = exports[mixin];
}
}
if (superclass !== undefined) {
params.superclass = exports[superclass];
}
//await this.register(self, params);
}
async register(self, params) {
const { config } = params;
const { propDefaults } = config;
const tagName = config.tagName;
const selfDefining = self.localName === tagName;
const fromShadow = selfDefining && self.shadowRoot !== null;
if (fromShadow) {
propDefaults['shadowRootMode'] = self.shadowRoot.mode;
}
const content = fromShadow ? self.shadowRoot : self;
if (selfDefining && content.querySelector('[itemscope]') !== null) {
const { itemize } = await import('./itemize.js');
const props = await itemize(content);
for (const propName in props) {
if (propName in propDefaults)
continue;
propDefaults[propName] = props[propName];
}
//console.log({props});
}
const mainTemplate = await toTempl(self, fromShadow, self);
//TODO: make this a transform plugin?
const adopted = Array.from(mainTemplate.content.querySelectorAll('style[adopt]'));
const styles = adopted.map(s => {
const inner = s.innerHTML;
s.remove();
return inner;
}).join('');
mainTemplate.content.querySelectorAll('[be-gone],[data-be-gone]').forEach(g => g.remove());
params.complexPropDefaults = { ...params.complexPropDefaults, mainTemplate, styles };
params.mixins = [...(params.mixins || []), TemplMgmt];
const { XE } = await import('xtal-element/XE.js');
const ce = new XE(params);
}
}
const tagName = 'be-definitive';
const ifWantsToBe = 'definitive';
const upgrade = '*';
const xe = new XE({
config: {
tagName,
propDefaults: {
...propDefaults,
},
propInfo: {
...propInfo
},
actions: {}
},
superclass: BeDefinitive
});
register(ifWantsToBe, upgrade, tagName);