@mschop/alpine-web-components 1.0.4 → 1.0.6
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/AlpineWebComponent.ts +12 -13
- package/package.json +1 -1
package/AlpineWebComponent.ts
CHANGED
|
@@ -24,7 +24,7 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
24
24
|
protected abstract template: string
|
|
25
25
|
protected styles: () => string = () => ``
|
|
26
26
|
protected attributeCasts: AttributeCasts = {};
|
|
27
|
-
|
|
27
|
+
protected alpineInitHandler = () => this.init()
|
|
28
28
|
|
|
29
29
|
constructor() {
|
|
30
30
|
super()
|
|
@@ -61,24 +61,23 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
61
61
|
this.state.attributes = {};
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (oldValue === castedNewValue) {
|
|
64
|
+
if (oldValue === newValue) {
|
|
67
65
|
return;
|
|
68
66
|
}
|
|
69
67
|
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
const castedNewValue = this.castFromAttribute(name, newValue);
|
|
69
|
+
|
|
70
|
+
if (this.state.attributes[name] !== castedNewValue) {
|
|
71
|
+
this.state.attributes[name] = castedNewValue;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
this.state.attributes[name] = castedNewValue;
|
|
75
74
|
}
|
|
76
75
|
|
|
77
|
-
|
|
76
|
+
protected loadTemplate() {
|
|
78
77
|
this.getRoot().innerHTML = this.template;
|
|
79
78
|
}
|
|
80
79
|
|
|
81
|
-
|
|
80
|
+
protected bootState() {
|
|
82
81
|
let state = this.state;
|
|
83
82
|
|
|
84
83
|
// @ts-ignore
|
|
@@ -93,14 +92,14 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
93
92
|
this.state = Alpine.reactive(state);
|
|
94
93
|
}
|
|
95
94
|
|
|
96
|
-
|
|
95
|
+
protected bootAlpine() {
|
|
97
96
|
// @ts-ignore
|
|
98
97
|
Alpine.addScopeToNode(this.getRoot(), this.state)
|
|
99
98
|
// @ts-ignore
|
|
100
99
|
Alpine.initTree(this.getRoot())
|
|
101
100
|
}
|
|
102
101
|
|
|
103
|
-
|
|
102
|
+
protected bootAttributes(): void {
|
|
104
103
|
// @ts-ignore
|
|
105
104
|
this.constructor.observedAttributes?.forEach((key: string) => {
|
|
106
105
|
const value = this.getAttribute(key);
|
|
@@ -119,11 +118,11 @@ abstract class AlpineWebComponent extends HTMLElement {
|
|
|
119
118
|
})
|
|
120
119
|
}
|
|
121
120
|
|
|
122
|
-
|
|
121
|
+
protected castToAttribute(name: string, value: any): null|string {
|
|
123
122
|
return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name].toAttribute(value);
|
|
124
123
|
}
|
|
125
124
|
|
|
126
|
-
|
|
125
|
+
protected castFromAttribute(name: string, value: null|string): any {
|
|
127
126
|
return this.attributeCasts[name] === undefined ? value : this.attributeCasts[name].fromAttribute(value);
|
|
128
127
|
}
|
|
129
128
|
|