be-hive 0.0.101 → 0.0.103
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/be-hive.js +170 -169
- package/package.json +2 -2
package/be-hive.js
CHANGED
|
@@ -1,169 +1,170 @@
|
|
|
1
|
-
import 'be-enhanced/beEnhanced.js';
|
|
2
|
-
export class BeHive extends HTMLElement {
|
|
3
|
-
constructor() {
|
|
4
|
-
super();
|
|
5
|
-
this.registeredBehaviors = {};
|
|
6
|
-
}
|
|
7
|
-
async connectedCallback() {
|
|
8
|
-
this.hidden = true;
|
|
9
|
-
const overridesAttr = this.getAttribute('overrides');
|
|
10
|
-
if (overridesAttr !== null) {
|
|
11
|
-
this.overrides = JSON.parse(overridesAttr);
|
|
12
|
-
}
|
|
13
|
-
else {
|
|
14
|
-
this.overrides = {};
|
|
15
|
-
}
|
|
16
|
-
this.#getInheritedBehaviors();
|
|
17
|
-
this.#addMutationObserver();
|
|
18
|
-
}
|
|
19
|
-
disconnectedCallback() {
|
|
20
|
-
if (this.#mutationObserver !== undefined) {
|
|
21
|
-
this.#mutationObserver.disconnect();
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
#getInheritedBehaviors() {
|
|
25
|
-
const rn = this.getRootNode();
|
|
26
|
-
const host = rn.host;
|
|
27
|
-
if (!host)
|
|
28
|
-
return;
|
|
29
|
-
const parentShadowRealm = host.getRootNode();
|
|
30
|
-
const parentBeHiveInstance = parentShadowRealm.querySelector('be-hive');
|
|
31
|
-
if (parentBeHiveInstance !== null) {
|
|
32
|
-
const { registeredBehaviors } = parentBeHiveInstance;
|
|
33
|
-
for (const key in registeredBehaviors) {
|
|
34
|
-
this.register(registeredBehaviors[key]);
|
|
35
|
-
}
|
|
36
|
-
parentBeHiveInstance.addEventListener('latest-behavior-changed', (e) => {
|
|
37
|
-
this.register(e.detail.value);
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
#mutationObserver;
|
|
42
|
-
#addMutationObserver() {
|
|
43
|
-
const rn = this.getRootNode();
|
|
44
|
-
const config = { attributes: true, childList: true, subtree: true };
|
|
45
|
-
const callback = (mutationList, observer) => {
|
|
46
|
-
for (const mutation of mutationList) {
|
|
47
|
-
if (mutation.type === "childList") {
|
|
48
|
-
//console.log("A child node has been added or removed.");
|
|
49
|
-
for (const node of mutation.addedNodes) {
|
|
50
|
-
this.#inspectNewNode(node);
|
|
51
|
-
}
|
|
52
|
-
for (const node of mutation.removedNodes) {
|
|
53
|
-
const beEnhanced = node.beEnhanced;
|
|
54
|
-
if (beEnhanced === undefined)
|
|
55
|
-
continue;
|
|
56
|
-
for (const key in beEnhanced) {
|
|
57
|
-
const enhancement = beEnhanced[key];
|
|
58
|
-
const detach = enhancement['detach'];
|
|
59
|
-
if (typeof (detach) === 'function') {
|
|
60
|
-
detach(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
observer
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
//
|
|
93
|
-
//
|
|
94
|
-
// let
|
|
95
|
-
//
|
|
96
|
-
//
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
-
// return
|
|
100
|
-
//
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
rn.
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
let
|
|
120
|
-
let
|
|
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
|
-
// newBehaviorEl.setAttribute('
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
this
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
//this
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
1
|
+
import 'be-enhanced/beEnhanced.js';
|
|
2
|
+
export class BeHive extends HTMLElement {
|
|
3
|
+
constructor() {
|
|
4
|
+
super();
|
|
5
|
+
this.registeredBehaviors = {};
|
|
6
|
+
}
|
|
7
|
+
async connectedCallback() {
|
|
8
|
+
this.hidden = true;
|
|
9
|
+
const overridesAttr = this.getAttribute('overrides');
|
|
10
|
+
if (overridesAttr !== null) {
|
|
11
|
+
this.overrides = JSON.parse(overridesAttr);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
this.overrides = {};
|
|
15
|
+
}
|
|
16
|
+
this.#getInheritedBehaviors();
|
|
17
|
+
this.#addMutationObserver();
|
|
18
|
+
}
|
|
19
|
+
disconnectedCallback() {
|
|
20
|
+
if (this.#mutationObserver !== undefined) {
|
|
21
|
+
this.#mutationObserver.disconnect();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
#getInheritedBehaviors() {
|
|
25
|
+
const rn = this.getRootNode();
|
|
26
|
+
const host = rn.host;
|
|
27
|
+
if (!host)
|
|
28
|
+
return;
|
|
29
|
+
const parentShadowRealm = host.getRootNode();
|
|
30
|
+
const parentBeHiveInstance = parentShadowRealm.querySelector('be-hive');
|
|
31
|
+
if (parentBeHiveInstance !== null) {
|
|
32
|
+
const { registeredBehaviors } = parentBeHiveInstance;
|
|
33
|
+
for (const key in registeredBehaviors) {
|
|
34
|
+
this.register(registeredBehaviors[key]);
|
|
35
|
+
}
|
|
36
|
+
parentBeHiveInstance.addEventListener('latest-behavior-changed', (e) => {
|
|
37
|
+
this.register(e.detail.value);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
#mutationObserver;
|
|
42
|
+
#addMutationObserver() {
|
|
43
|
+
const rn = this.getRootNode();
|
|
44
|
+
const config = { attributes: true, childList: true, subtree: true };
|
|
45
|
+
const callback = (mutationList, observer) => {
|
|
46
|
+
for (const mutation of mutationList) {
|
|
47
|
+
if (mutation.type === "childList") {
|
|
48
|
+
//console.log("A child node has been added or removed.");
|
|
49
|
+
for (const node of mutation.addedNodes) {
|
|
50
|
+
this.#inspectNewNode(node);
|
|
51
|
+
}
|
|
52
|
+
for (const node of mutation.removedNodes) {
|
|
53
|
+
const beEnhanced = node.beEnhanced;
|
|
54
|
+
if (beEnhanced === undefined)
|
|
55
|
+
continue;
|
|
56
|
+
for (const key in beEnhanced) {
|
|
57
|
+
const enhancement = beEnhanced[key];
|
|
58
|
+
const detach = enhancement['detach'];
|
|
59
|
+
if (typeof (detach) === 'function') {
|
|
60
|
+
const boundDetach = detach.bind(enhancement);
|
|
61
|
+
boundDetach(node);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
else if (mutation.type === "attributes") {
|
|
67
|
+
//console.log(`The ${mutation.attributeName} attribute was modified.`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
// Create an observer instance linked to the callback function
|
|
72
|
+
const observer = new MutationObserver(callback);
|
|
73
|
+
observer.observe(rn, config);
|
|
74
|
+
this.#mutationObserver = observer;
|
|
75
|
+
}
|
|
76
|
+
#inspectNewNode(node) {
|
|
77
|
+
if (!(node instanceof Element))
|
|
78
|
+
return;
|
|
79
|
+
const { beEnhanced } = node;
|
|
80
|
+
const { registeredBehaviors } = this;
|
|
81
|
+
for (const key in registeredBehaviors) {
|
|
82
|
+
const registeredBehavior = registeredBehaviors[key];
|
|
83
|
+
const { upgrade } = registeredBehavior;
|
|
84
|
+
if (!node.matches(upgrade))
|
|
85
|
+
continue;
|
|
86
|
+
const namespacedName = beEnhanced.getFQName(key);
|
|
87
|
+
if (namespacedName === undefined)
|
|
88
|
+
continue;
|
|
89
|
+
beEnhanced.attachAttr(namespacedName, key);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// #getPreciseMatch(key: string, node: Element, allowNonNamespaced = true){
|
|
93
|
+
// if(allowNonNamespaced && node.matches(`[${key}]`)) return key;
|
|
94
|
+
// let testKey = `enh-by-${key}`;
|
|
95
|
+
// let test = `[${testKey}]`;
|
|
96
|
+
// if(node.matches(test)) return testKey;
|
|
97
|
+
// testKey = `data-enh-by-${key}`;
|
|
98
|
+
// test = `[${testKey}]`;
|
|
99
|
+
// if(node.matches(test)) return testKey;
|
|
100
|
+
// return undefined;
|
|
101
|
+
// }
|
|
102
|
+
#scanForSingleRegisteredBehavior(localName, behaviorKeys) {
|
|
103
|
+
const { ifWantsToBe, upgrade } = behaviorKeys;
|
|
104
|
+
const attr = `${upgrade}[${localName}],${upgrade}[enh-by-${localName}],${upgrade}[data-enh-by-${localName}]`;
|
|
105
|
+
const rn = this.getRootNode();
|
|
106
|
+
rn.querySelectorAll(attr).forEach(el => {
|
|
107
|
+
const { beEnhanced } = el;
|
|
108
|
+
const namespacedName = beEnhanced.getFQName(localName);
|
|
109
|
+
if (namespacedName === undefined)
|
|
110
|
+
return;
|
|
111
|
+
beEnhanced.attachAttr(namespacedName, localName);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
register(parentInstance) {
|
|
115
|
+
const parentInstanceLocalName = parentInstance.localName;
|
|
116
|
+
if (this.querySelector(parentInstanceLocalName) !== null)
|
|
117
|
+
return;
|
|
118
|
+
const override = this.overrides[parentInstanceLocalName];
|
|
119
|
+
let newInstanceTagName = parentInstanceLocalName;
|
|
120
|
+
let newIfWantsToBe = parentInstance.ifWantsToBe;
|
|
121
|
+
let newDisabled = parentInstance.disabled;
|
|
122
|
+
if (override !== undefined) {
|
|
123
|
+
const { ifWantsToBe, block, unblock } = override;
|
|
124
|
+
if (block) {
|
|
125
|
+
newDisabled = true;
|
|
126
|
+
}
|
|
127
|
+
else if (unblock) {
|
|
128
|
+
newDisabled = false;
|
|
129
|
+
}
|
|
130
|
+
if (ifWantsToBe) {
|
|
131
|
+
newIfWantsToBe = ifWantsToBe;
|
|
132
|
+
newInstanceTagName = 'be-' + ifWantsToBe;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const beSevered = this.hasAttribute('be-severed');
|
|
136
|
+
if (beSevered)
|
|
137
|
+
newDisabled = true;
|
|
138
|
+
const newBehaviorEl = document.createElement('template');
|
|
139
|
+
Object.assign(newBehaviorEl.dataset, {
|
|
140
|
+
localName: parentInstanceLocalName,
|
|
141
|
+
ifWantsToBe: newIfWantsToBe,
|
|
142
|
+
upgrade: parentInstance.upgrade,
|
|
143
|
+
});
|
|
144
|
+
//parentInstanceLocalName
|
|
145
|
+
// newBehaviorEl.setAttribute('if-wants-to-be', newIfWantsToBe);
|
|
146
|
+
// newBehaviorEl.setAttribute('upgrade', parentInstance.upgrade);
|
|
147
|
+
if (newDisabled)
|
|
148
|
+
newBehaviorEl.setAttribute('disabled', '');
|
|
149
|
+
this.appendChild(newBehaviorEl);
|
|
150
|
+
const newRegisteredBehavior = {
|
|
151
|
+
...parentInstance,
|
|
152
|
+
ifWantsToBe: newIfWantsToBe,
|
|
153
|
+
disabled: newDisabled,
|
|
154
|
+
};
|
|
155
|
+
this.registeredBehaviors[parentInstanceLocalName] = newRegisteredBehavior;
|
|
156
|
+
this.#scanForSingleRegisteredBehavior(parentInstanceLocalName, newRegisteredBehavior);
|
|
157
|
+
//console.log({newRegisteredBehavior});
|
|
158
|
+
this.dispatchEvent(new CustomEvent('latest-behavior-changed', {
|
|
159
|
+
detail: {
|
|
160
|
+
value: newRegisteredBehavior,
|
|
161
|
+
}
|
|
162
|
+
}));
|
|
163
|
+
//this.latestBehaviors = [...this.latestBehaviors, newRegisteredBehavior];
|
|
164
|
+
//this.#doSweepingScan();
|
|
165
|
+
return newBehaviorEl;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (!customElements.get('be-hive')) {
|
|
169
|
+
customElements.define('be-hive', BeHive);
|
|
170
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "be-hive",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.103",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"web-components",
|
|
6
6
|
"web-component",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"serve": "node node_modules/may-it-serve/serve.js"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"be-enhanced": "0.0.
|
|
29
|
+
"be-enhanced": "0.0.28"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"may-it-serve": "0.0.5"
|