Skip to content

Commit 0eb5fc9

Browse files
committed
feat: Add <eventname>-delay to delay event execution and validate DOM presence
- Introduced `<eventname>-delay` to delay the execution of an event and verify if the element that fired the event is still present in the DOM. - Added `<eventname>-observe` to define observer types (e.g., `attributes`, `addedNodes`, `removedNodes`, `characterData`, `childList`). - Added `<eventname>-attributes` to specify attributes to observe, separated by commas.
1 parent d696c35 commit 0eb5fc9

File tree

1 file changed

+89
-21
lines changed

1 file changed

+89
-21
lines changed

src/index.js

Lines changed: 89 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ const CoCreateEvents = {
7777
name: prefix,
7878
endEvent: `${prefix}End`,
7979
callback: (data) => {
80-
this.__updateElements(data.element, prefix);
80+
this.__updateElements(
81+
data.element,
82+
prefix,
83+
null,
84+
null,
85+
data.params
86+
);
8187
}
8288
});
8389

@@ -165,14 +171,44 @@ const CoCreateEvents = {
165171
break;
166172
}
167173
}
168-
if (target)
169-
observer.init({
170-
observe: ["addedNodes"],
171-
selector: target,
172-
callback: function (mutation) {
173-
self.__updateElements(el, prefix, mutation.target);
174-
}
175-
});
174+
175+
let attributeName = (
176+
el.getAttribute(`${prefix}-attributes`) || ""
177+
)
178+
.split(/\s*,\s*/)
179+
.filter((item) => item);
180+
181+
let observeAttribute = (
182+
el.getAttribute(`${prefix}-observe`) || ""
183+
)
184+
.split(/\s*,\s*/)
185+
.filter((item) => item);
186+
187+
let observerConfig = {
188+
observe: observeAttribute,
189+
callback: function (mutation) {
190+
self.__updateElements(el, prefix, mutation.target);
191+
}
192+
};
193+
194+
if (target) {
195+
if (
196+
target &&
197+
!observeAttribute.length &&
198+
!attributeName.length
199+
) {
200+
observerConfig.observe.push("addedNodes");
201+
}
202+
observerConfig.target = target;
203+
}
204+
205+
if (attributeName.length) {
206+
observerConfig.observe.push("attributes");
207+
observerConfig.attributeName = attributeName;
208+
}
209+
210+
if (observerConfig.observe.length)
211+
observer.init(observerConfig);
176212
}
177213

178214
if (events.includes("resize")) {
@@ -242,8 +278,9 @@ const CoCreateEvents = {
242278
}
243279
},
244280

245-
__updateElements: async function (el, prefix, target, events) {
281+
__updateElements: async function (el, prefix, target, events, params) {
246282
if (!el.isConnected) return;
283+
247284
let elements = [el];
248285
let targetGroup = el.getAttribute(`${prefix}-group`);
249286
if (targetGroup) {
@@ -255,6 +292,17 @@ const CoCreateEvents = {
255292
}
256293

257294
for (let element of elements) {
295+
let delay = element.getAttribute(`${prefix}-delay`);
296+
297+
if (delay) {
298+
delay = parseInt(delay, 10) || 0;
299+
if (delay > 0) {
300+
await new Promise((resolve) => setTimeout(resolve, delay));
301+
}
302+
}
303+
304+
if (!element.isConnected) return;
305+
258306
let once = element.getAttribute(`${prefix}-once`);
259307
if (once || once === "") {
260308
if (!element.eventsOnce) {
@@ -383,7 +431,11 @@ const CoCreateEvents = {
383431
return x; // Return the updated x
384432
});
385433

386-
let targetElements = queryElements({ element, prefix });
434+
let targetElements = queryElements({
435+
element,
436+
prefix,
437+
selector: params
438+
});
387439
if (targetElements === false) targetElements = [element];
388440
let action = element.getAttribute(`${prefix}-action`);
389441
for (let i = 0; i < targetElements.length; i++) {
@@ -395,7 +447,14 @@ const CoCreateEvents = {
395447
!targetPosition &&
396448
["click", "focus", "blur"].includes(prefix)
397449
) {
398-
if (element.hasAttribute(prefix))
450+
// TODO: click causes an infinite loop if targetElement[i] is a child of element do to event propactaion
451+
if (
452+
(targetElements[i] === element &&
453+
element.hasAttribute(prefix)) ||
454+
(params &&
455+
targetElements[i] !== element &&
456+
!element.contains(targetElements[i]))
457+
)
399458
targetElements[i][prefix]();
400459
} else {
401460
this.setValue(
@@ -694,12 +753,19 @@ function checkCondition(condition, value) {
694753
return !!value[0];
695754
} else if (condition === "false") {
696755
return !value[0];
697-
} else if (condition === "[]" && typeof value[0] === "string") {
698-
parse = false;
699756
}
757+
// else if (condition === "[]" && typeof value[0] === "string") {
758+
// parse = false;
759+
// }
700760

701761
// TODO: why parse updated conditin to boolean false
702-
if (parse && condition !== "false") condition = parseCondition(condition);
762+
// if (parse && condition !== "false") condition = parseCondition(condition);
763+
764+
if (typeof value[0] === "number") {
765+
condition = parseNumberCondition(condition);
766+
} else if (typeof value[0] === "object" && typeof condition === "string") {
767+
condition = parseJsonCondition(condition);
768+
}
703769

704770
let result;
705771

@@ -725,7 +791,7 @@ function checkCondition(condition, value) {
725791
});
726792
} else result = value.includes(condition);
727793
} else if (Array.isArray(value)) {
728-
// TODO: handle comparing array to array, vs querying the ayya items for a match
794+
// TODO: handle comparing array to array, vs querying the array items for a match
729795
result = queryData(value, condition);
730796
} else if (
731797
typeof value === "object" &&
@@ -741,18 +807,20 @@ function checkCondition(condition, value) {
741807
return result;
742808
}
743809

744-
function parseCondition(condition) {
810+
function parseJsonCondition(condition) {
745811
try {
746812
// Attempt to parse the condition as JSON
747813
let parsedJson = JSON.parse(condition);
748814
return parsedJson;
749815
} catch (e) {
750-
// If JSON parsing fails, check if the condition is a number
751-
return !isNaN(condition) && condition.trim() !== ""
752-
? Number(condition)
753-
: condition;
816+
return condition;
754817
}
755818
}
819+
function parseNumberCondition(condition) {
820+
return !isNaN(condition) && condition.trim() !== ""
821+
? Number(condition)
822+
: condition;
823+
}
756824

757825
CoCreateEvents.init();
758826

0 commit comments

Comments
 (0)