-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbe-hive.js
163 lines (163 loc) · 6.36 KB
/
be-hive.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
import { Synthesizer } from 'mount-observer/Synthesizer.js';
export { MountObserver } from 'mount-observer/MountObserver.js';
import 'be-enhanced/beEnhanced.js';
import { Enhancers } from 'be-enhanced/beEnhanced.js';
export const defaultObsAttrs = {
hasRootIn: [
{
start: '',
context: 'BuiltIn'
},
{
start: 'enh',
context: 'Both'
},
{
start: 'data-enh',
context: 'Both'
},
],
base: '',
preBaseDelimiter: '-',
preBranchDelimiter: '-',
preLeafDelimiter: '--',
enhancedElementMatches: '*',
enhancedElementInstanceOf: [Element]
};
export const registeredHandlers = new Map();
//export const scopedHandlers = new Map<EMC, ScopedCustomHandlerCluster>();
export function seed(emc) {
if (emc.handlerKey === undefined)
emc.handlerKey = emc.enhPropKey;
emc.top = emc;
const { handlerKey } = emc;
if (!registeredHandlers.has(emc)) {
registeredHandlers.set(emc, new Map());
}
const cluster = registeredHandlers.get(emc);
if (!cluster?.has(handlerKey)) {
cluster.set(handlerKey, new Map());
}
try {
Enhancers.define(emc);
}
catch (e) { }
const mose = document.createElement('script');
const id = `be-hive.${emc.base}`;
mose.id = emc.id = id;
mose.synConfig = emc;
return mose;
}
export class BeHive extends Synthesizer {
activate(mose) {
if (!this.checkIfAllowed(mose))
return;
const { synConfig } = mose;
const mergeWithDefaults = { ...defaultObsAttrs, ...synConfig };
//TODO allow for programmatic adjustments in load event
//this.dispatchEvent(new RegistryEventImpl(mergeWithDefaults));
const { base, block, branches, enhancedElementInstanceOf, enhancedElementMatches, hostInstanceOf, hostMatches, leaves, preBaseDelimiter, preBranchDelimiter, importEnh, preLeafDelimiter, hasRootIn, map, osotas, mapLocalNameTo, ws, mapEnhKeyTo } = mergeWithDefaults;
const mi = {
on: enhancedElementMatches,
whereInstanceOf: enhancedElementInstanceOf,
whereAttr: {
hasRootIn,
hasBase: [preBaseDelimiter, base],
},
observedAttrsWhenMounted: osotas,
};
if (branches !== undefined) {
mi.whereAttr.hasBranchIn = [preBaseDelimiter, branches];
}
if (leaves !== undefined) {
throw 'NI';
}
mose.init = mi;
super.activate(mose);
const mo = mose.observer;
mo.addEventListener('mount', async (e) => {
//TODO: doing this logic asynchronously after pulling in the existing prop
//could result in loss of information
const observedAttrs = await mo.observedAttrs();
const { mountedElement } = e;
const { beEnhanced } = mountedElement;
const enhancementConstructor = await importEnh();
const { enhPropKey, base } = mergeWithDefaults;
if (base !== undefined) {
//TODO: check for data- and enh- and data-enh-
const deferBase = `defer-${base}`;
if (mountedElement.hasAttribute(deferBase)) {
const { wfac } = await import('trans-render/lib/wfac.js');
await wfac(mountedElement, deferBase, (mr, el, attrs) => {
return !el.hasAttribute(deferBase);
});
}
}
const initialPropValues = beEnhanced[enhPropKey] || {};
if (initialPropValues instanceof enhancementConstructor)
return;
const enhancementInstance = new enhancementConstructor();
beEnhanced[enhPropKey] = enhancementInstance;
const initialAttrInfo = mo.readAttrs(mountedElement);
if (map !== undefined) {
for (const attr of initialAttrInfo) {
if (attr.isSOfTAttr)
continue;
const leafIdx = 0;
const { parts, newValue } = attr;
if (newValue === null)
continue;
const { branchIdx } = parts;
const key = `${branchIdx}.${leafIdx}`;
const prop = map[key];
if (prop === undefined)
continue;
switch (typeof prop) {
case 'string':
if (initialPropValues[prop] !== undefined)
continue;
initialPropValues[prop] = newValue;
break;
case 'object':
const { prsObj } = await import('./prsObj.js');
await prsObj(prop, newValue, initialPropValues, attr);
break;
default:
throw 'NI';
}
}
}
if (osotas !== undefined) {
for (const attr of initialAttrInfo) {
if (!attr.isSOfTAttr)
continue;
const { mapsTo, newValue } = attr;
initialPropValues[mapsTo] = newValue;
}
}
if (mapLocalNameTo !== undefined) {
initialPropValues[mapLocalNameTo] = mountedElement.localName;
}
if (mapEnhKeyTo !== undefined) {
initialPropValues[mapEnhKeyTo] = enhPropKey;
}
initialPropValues.customHandlers = registeredHandlers.get(synConfig.top)?.get(enhPropKey);
let filteredWs;
if (ws !== undefined) {
(await import('./e.js')).e(mergeWithDefaults, mountedElement, ws, initialPropValues);
}
//initialPropValues.scopedCustomHandlers = scopedHandlers.get(synConfig.top)?.get(enhPropKey);
await enhancementInstance.attach(mountedElement, {
initialAttrInfo,
initialPropValues,
mountCnfg: mergeWithDefaults,
synConfig,
observedAttrs,
ws: filteredWs
});
});
}
}
if (customElements.get('be-hive') === undefined) {
customElements.define('be-hive', BeHive);
}