Skip to content

Commit ae6b58a

Browse files
committed
fix: improve event handling and element manipulation in CoCreateEvents
1 parent bb8a01a commit ae6b58a

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

src/index.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ import "@cocreate/element-prototype";
2828
const CoCreateEvents = {
2929
elements2: new Map(),
3030
init: function (prefix, events) {
31-
if (prefix && events) this.initPrefix(prefix, events);
32-
else {
31+
if (prefix && events) {
32+
this.initPrefix(prefix, events);
33+
} else {
3334
this.initPrefix("toggle", ["click"]);
3435
this.initPrefix("click", ["click"]);
3536
this.initPrefix("hover", ["mouseover", "mouseout"]);
@@ -385,11 +386,9 @@ const CoCreateEvents = {
385386
let elements = [el];
386387
let targetGroup = el.getAttribute(`${prefix}-group`);
387388
if (targetGroup) {
388-
document
389-
.querySelectorAll(`[${prefix}-group="${targetGroup}"]`)
390-
.forEach((element) => {
391-
if (element !== el) elements.push(element);
392-
});
389+
elements = document.querySelectorAll(
390+
`[${prefix}-group="${targetGroup}"]`
391+
);
393392
}
394393

395394
for (let element of elements) {
@@ -583,8 +582,11 @@ const CoCreateEvents = {
583582
if (el !== element) {
584583
deactivate = true;
585584
}
586-
// values = values.map(x => x.trim());
585+
if (events && events.includes("click")) {
586+
// TODO: handling of attributes that represent states aria-expanded, aria-selected etc.
587+
}
587588

589+
// values = values.map(x => x.trim());
588590
values = values.map((x) => {
589591
if (typeof x === "string") x = x.trim(); // Update x with the trimmed value
590592
let prop = element.getAttribute(`${prefix}-property`);
@@ -604,10 +606,14 @@ const CoCreateEvents = {
604606
targetElements = [element];
605607
}
606608

607-
let action = element.getAttribute(`${prefix}-action`);
609+
let action =
610+
element.getAttribute(`${prefix}-action`) ||
611+
element.getAttribute(`${prefix}-method`);
608612
for (let i = 0; i < targetElements.length; i++) {
609613
if (action) {
610-
targetElements[i][action]();
614+
if (targetElements[i][action]) {
615+
targetElements[i][action]();
616+
}
611617
} else if (
612618
!targetAttribute &&
613619
targetAttribute !== "" &&
@@ -736,14 +742,18 @@ const CoCreateEvents = {
736742
if (deactivate) newValue = "";
737743
if (attrName === "class") {
738744
if (oldValue) {
739-
element.classList.remove(oldValue);
745+
oldValue.split(" ").forEach((className) => {
746+
element.classList.remove(className);
747+
});
740748
if (values.length === 1) {
741749
return;
742750
}
743751
}
744752

745753
if (newValue) {
746-
element.classList.add(newValue);
754+
newValue.split(" ").forEach((className) => {
755+
element.classList.add(className);
756+
});
747757
}
748758
} else if (attrName === "value") {
749759
element.setValue(newValue, false);
@@ -755,9 +765,11 @@ const CoCreateEvents = {
755765
element[attrName.substring(1)]();
756766
} else if (attrName) {
757767
// TODO: removeAttribute vs setting empty value, how best to define. operator $remove ???
758-
if (newValue === "" && element.hasAttribute(attrName))
768+
if (newValue === "" && element.hasAttribute(attrName)) {
759769
element.removeAttribute(attrName);
760-
else element.setAttribute(attrName, newValue);
770+
} else {
771+
element.setAttribute(attrName, newValue);
772+
}
761773
} else if (targetPosition) {
762774
if (domTextEditor) {
763775
CoCreate.text.insertAdjacentElement({

0 commit comments

Comments
 (0)