Skip to content

Commit 90c0bd9

Browse files
committed
fix: update attribute selectors from "state_id" to "state-id" for consistency
1 parent f224808 commit 90c0bd9

1 file changed

Lines changed: 22 additions & 47 deletions

File tree

src/index.js

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ import uid from "@cocreate/uuid";
55
import localStorage from "@cocreate/local-storage";
66

77
function init() {
8-
let elements = document.querySelectorAll("[state_id]");
8+
let elements = document.querySelectorAll("[state-id]");
99
initElements(elements);
1010
window.addEventListener("storage", function (e) {
1111
if (e.key == "state") {
1212
elements = document.querySelectorAll(
13-
'[state_id]:not([state-onstoragechange="false"])'
13+
'[state-id]:not([state-onstoragechange="false"])'
1414
);
1515
initElements(elements);
1616
}
1717
});
1818
document.addEventListener("click", function (e) {
19-
const target = e.target.closest("[state_to]");
19+
const target = e.target.closest("[state-to]");
2020
if (target) {
2121
if (target.closest('[actions*="state"]')) return;
2222
stateAttributes(target);
@@ -29,7 +29,7 @@ function initElements(elements) {
2929
}
3030

3131
function initElement(element) {
32-
let state_id = element.getAttribute("state_id");
32+
let state_id = element.getAttribute("state-id");
3333
if (!state_id) return;
3434

3535
let statedAttributes = localStorage.getItem("state");
@@ -61,27 +61,6 @@ function _setAttributeValues(el, attrValues) {
6161

6262
for (const key of Object.keys(attrValues)) {
6363
_setAttributeValue(el, key, attrValues[key], isOverwrite);
64-
_setAttributeValue(el, `state-${key}`, attrValues[key], isOverwrite);
65-
if (key == "array" || key == "object" || key == "name") {
66-
_setAttributeValue(
67-
el,
68-
`fetch-${key}`,
69-
attrValues[key],
70-
isOverwrite
71-
);
72-
_setAttributeValue(
73-
el,
74-
`state-fetch-${key}`,
75-
attrValues[key],
76-
isOverwrite
77-
);
78-
}
79-
if (key == "template") {
80-
_setAttributeValue(el, "template_id", attrValues[key], isOverwrite);
81-
}
82-
if (key == "template_id") {
83-
_setAttributeValue(el, "template", attrValues[key], isOverwrite);
84-
}
8564
}
8665
}
8766

@@ -130,12 +109,12 @@ async function stateAttributes(element) {
130109
// if (form) {
131110
// elements = form.querySelectorAll("[state_to]");
132111
// } else {
133-
if (element.hasAttribute("state_to")) elements.push(element);
134-
let nestedElements = element.querySelectorAll("[state_to]");
112+
if (element.hasAttribute("state-to")) elements.push(element);
113+
let nestedElements = element.querySelectorAll("[state-to]");
135114
elements.push(...nestedElements);
136115
let current = element;
137116
while (current) {
138-
if (current.hasAttribute("state_to") && !elements.includes(current)) {
117+
if (current.hasAttribute("state-to") && !elements.includes(current)) {
139118
elements.push(current);
140119
}
141120
current = current.parentElement;
@@ -158,7 +137,7 @@ async function stateAttributes(element) {
158137
break;
159138
}
160139

161-
let state_to = elements[i].getAttribute("state_to");
140+
let state_to = elements[i].getAttribute("state-to");
162141
Object.assign(statedAttributes, { [`${state_to}`]: attrValues });
163142
_getStateId(attrValues, state_to);
164143
}
@@ -187,33 +166,29 @@ async function _getAttributeValues(element) {
187166
let attributeValues = {};
188167
let attributes = element.attributes;
189168
for (let attribute of attributes) {
190-
if (attribute.name.startsWith("state-")) {
191-
if (attribute.value == "$uid")
192-
Object.assign(attributeValues, {
193-
[`${attribute.name.substring(5)}`]: uid.generate(6)
194-
});
195-
else if (attribute.name == "state-value" && !attribute.value)
196-
Object.assign(attributeValues, {
197-
value: await element.getValue()
198-
});
199-
else
200-
Object.assign(attributeValues, {
201-
[`${attribute.name.substring(6)}`]: attribute.value
202-
});
169+
if (attribute.name.startsWith("state.")) {
170+
const key = attribute.name.slice(6);
171+
if (attribute.value == "$uid") {
172+
Object.assign(attributeValues, { [key]: uid.generate(6) });
173+
} else if (key === "value" && !attribute.value) {
174+
Object.assign(attributeValues, { value: await element.getValue() });
175+
} else {
176+
Object.assign(attributeValues, { [key]: attribute.value });
177+
}
203178
}
204179
}
205180
if (
206181
element.value !== undefined &&
207-
!element.hasAttribute("state-value") &&
208-
element.getvalue
182+
!element.hasAttribute("state.value") &&
183+
element.getValue
209184
)
210185
Object.assign(attributeValues, { value: await element.getValue() });
211186

212187
return attributeValues;
213188
}
214189

215190
function _getStateId(attrValues, state_to) {
216-
const elements = document.querySelectorAll(`[state_id="${state_to}"]`);
191+
const elements = document.querySelectorAll(`[state-id="${state_to}"]`);
217192
for (let element of elements) _setAttributeValues(element, attrValues);
218193
}
219194

@@ -239,7 +214,7 @@ window.addEventListener("popstate", function (event) {
239214
if (event.state) {
240215
if (event.state.statedAttributes) {
241216
localStorage.setItem("state", event.state.statedAttributes);
242-
let elements = document.querySelectorAll("[state_id]");
217+
let elements = document.querySelectorAll("[state-id]");
243218
initElements(elements);
244219
}
245220

@@ -257,7 +232,7 @@ window.addEventListener("popstate", function (event) {
257232
Observer.init({
258233
name: "CoCreateState",
259234
types: ["addedNodes"],
260-
selector: "[state_id]",
235+
selector: "[state-id]",
261236
callback: function (mutation) {
262237
initElement(mutation.target);
263238
}

0 commit comments

Comments
 (0)