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.
Files changed (2) hide show
  1. package/be-hive.js +170 -169
  2. 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(node);
61
- }
62
- }
63
- }
64
- }
65
- else if (mutation.type === "attributes") {
66
- //console.log(`The ${mutation.attributeName} attribute was modified.`);
67
- }
68
- }
69
- };
70
- // Create an observer instance linked to the callback function
71
- const observer = new MutationObserver(callback);
72
- observer.observe(rn, config);
73
- this.#mutationObserver = observer;
74
- }
75
- #inspectNewNode(node) {
76
- if (!(node instanceof Element))
77
- return;
78
- const { beEnhanced } = node;
79
- const { registeredBehaviors } = this;
80
- for (const key in registeredBehaviors) {
81
- const registeredBehavior = registeredBehaviors[key];
82
- const { upgrade } = registeredBehavior;
83
- if (!node.matches(upgrade))
84
- continue;
85
- const namespacedName = beEnhanced.getFQName(key);
86
- if (namespacedName === undefined)
87
- continue;
88
- beEnhanced.attachAttr(namespacedName, key);
89
- }
90
- }
91
- // #getPreciseMatch(key: string, node: Element, allowNonNamespaced = true){
92
- // if(allowNonNamespaced && node.matches(`[${key}]`)) return key;
93
- // let testKey = `enh-by-${key}`;
94
- // let test = `[${testKey}]`;
95
- // if(node.matches(test)) return testKey;
96
- // testKey = `data-enh-by-${key}`;
97
- // test = `[${testKey}]`;
98
- // if(node.matches(test)) return testKey;
99
- // return undefined;
100
- // }
101
- #scanForSingleRegisteredBehavior(localName, behaviorKeys) {
102
- const { ifWantsToBe, upgrade } = behaviorKeys;
103
- const attr = `${upgrade}[${localName}],${upgrade}[enh-by-${localName}],${upgrade}[data-enh-by-${localName}]`;
104
- const rn = this.getRootNode();
105
- rn.querySelectorAll(attr).forEach(el => {
106
- const { beEnhanced } = el;
107
- const namespacedName = beEnhanced.getFQName(localName);
108
- if (namespacedName === undefined)
109
- return;
110
- beEnhanced.attachAttr(namespacedName, localName);
111
- });
112
- }
113
- register(parentInstance) {
114
- const parentInstanceLocalName = parentInstance.localName;
115
- if (this.querySelector(parentInstanceLocalName) !== null)
116
- return;
117
- const override = this.overrides[parentInstanceLocalName];
118
- let newInstanceTagName = parentInstanceLocalName;
119
- let newIfWantsToBe = parentInstance.ifWantsToBe;
120
- let newDisabled = parentInstance.disabled;
121
- if (override !== undefined) {
122
- const { ifWantsToBe, block, unblock } = override;
123
- if (block) {
124
- newDisabled = true;
125
- }
126
- else if (unblock) {
127
- newDisabled = false;
128
- }
129
- if (ifWantsToBe) {
130
- newIfWantsToBe = ifWantsToBe;
131
- newInstanceTagName = 'be-' + ifWantsToBe;
132
- }
133
- }
134
- const beSevered = this.hasAttribute('be-severed');
135
- if (beSevered)
136
- newDisabled = true;
137
- const newBehaviorEl = document.createElement('template');
138
- Object.assign(newBehaviorEl.dataset, {
139
- localName: parentInstanceLocalName,
140
- ifWantsToBe: newIfWantsToBe,
141
- upgrade: parentInstance.upgrade,
142
- });
143
- //parentInstanceLocalName
144
- // newBehaviorEl.setAttribute('if-wants-to-be', newIfWantsToBe);
145
- // newBehaviorEl.setAttribute('upgrade', parentInstance.upgrade);
146
- if (newDisabled)
147
- newBehaviorEl.setAttribute('disabled', '');
148
- this.appendChild(newBehaviorEl);
149
- const newRegisteredBehavior = {
150
- ...parentInstance,
151
- ifWantsToBe: newIfWantsToBe,
152
- disabled: newDisabled,
153
- };
154
- this.registeredBehaviors[parentInstanceLocalName] = newRegisteredBehavior;
155
- this.#scanForSingleRegisteredBehavior(parentInstanceLocalName, newRegisteredBehavior);
156
- //console.log({newRegisteredBehavior});
157
- this.dispatchEvent(new CustomEvent('latest-behavior-changed', {
158
- detail: {
159
- value: newRegisteredBehavior,
160
- }
161
- }));
162
- //this.latestBehaviors = [...this.latestBehaviors, newRegisteredBehavior];
163
- //this.#doSweepingScan();
164
- return newBehaviorEl;
165
- }
166
- }
167
- if (!customElements.get('be-hive')) {
168
- customElements.define('be-hive', BeHive);
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.101",
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.27"
29
+ "be-enhanced": "0.0.28"
30
30
  },
31
31
  "devDependencies": {
32
32
  "may-it-serve": "0.0.5"