@cocreate/events 1.23.0 → 1.25.0

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/src/index.js CHANGED
@@ -20,448 +20,614 @@
20
20
  // you must obtain a commercial license from CoCreate LLC.
21
21
  // For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
22
22
 
23
- import { queryElements, queryData } from '@cocreate/utils';
24
- import action from '@cocreate/actions';
25
- import observer from '@cocreate/observer';
26
- import '@cocreate/element-prototype';
23
+ import { queryElements, queryData } from "@cocreate/utils";
24
+ import action from "@cocreate/actions";
25
+ import observer from "@cocreate/observer";
26
+ import "@cocreate/element-prototype";
27
27
 
28
28
  const CoCreateEvents = {
29
- elements2: new Map(),
30
- init: function (prefix, events) {
31
- if (prefix && events)
32
- this.initPrefix(prefix, events);
33
- else {
34
- this.initPrefix('toggle', ['click']);
35
- this.initPrefix('click', ['click']);
36
- this.initPrefix('hover', ['mouseover', 'mouseout']);
37
- this.initPrefix('mouseover', ['mouseover']);
38
- this.initPrefix('mouseout', ['mouseout']);
39
- this.initPrefix('input', ['input']);
40
- this.initPrefix('change', ['change']);
41
- this.initPrefix('selected', ['click']);
42
- this.initPrefix('onload', ['onload']);
43
- this.initPrefix('observe', ['observer']);
44
- this.initPrefix('resize', ['onload', 'resize']);
45
- this.initPrefix('localstorage', ['onload', 'observer']);
46
- }
47
-
48
- let customEventEls = document.querySelectorAll('[event-name]')
49
- let names = {}
50
- for (let customEventEl of customEventEls) {
51
- let name = customEventEl.getAttribute('event-name')
52
- name = name.split(',')
53
- for (let i = 0; i < name.length; i++) {
54
- name[i].trim()
55
- if (!names[name]) {
56
- names[name] = name
57
- this.initPrefix(name);
58
- }
59
- }
60
- }
61
-
62
- const self = this
63
- observer.init({
64
- name: 'CoCreateEventName',
65
- observe: ['addedNodes'],
66
- target: `[event-name]`,
67
- callback: function (mutation) {
68
- let name = mutation.target.getAttribute('event-name')
69
- self.initPrefix(name);
70
- }
71
- });
72
-
73
- },
74
-
75
- initPrefix: function (prefix, events) {
76
- const self = this;
77
-
78
- action.init({
79
- name: prefix,
80
- endEvent: `${prefix}End`,
81
- callback: (data) => {
82
- this.__updateElements(data.element, prefix)
83
- }
84
- });
85
-
86
- let selector = `[${prefix}], [${prefix}-key], [${prefix}-attribute], [${prefix}-value], [${prefix}-selector], [${prefix}-closest], [${prefix}-parent], [${prefix}-next], [${prefix}-previous]`
87
-
88
- observer.init({
89
- name: 'CoCreateEventattributes',
90
- observe: ['attributes', 'addedNodes'],
91
- attributeName: [`${prefix}-events`],
92
- target: selector,
93
- callback: function (mutation) {
94
- self.initElements([mutation.target], prefix, events)
95
- }
96
- });
97
-
98
- if (events.includes('observer')) {
99
- observer.init({
100
- name: 'observerAttributes',
101
- observe: ['attributes'],
102
- attributeName: [`${prefix}-key`, `${prefix}-value`, `${prefix}-selector`, `${prefix}-closest`, `${prefix}-parent`, `${prefix}-next`, `${prefix}-previous`],
103
- callback: function (mutation) {
104
- // remove previous observer
105
- self.initElements([mutation.target], prefix, events)
106
- }
107
- });
108
- }
109
-
110
- let elements = document.querySelectorAll(selector)
111
- this.initElements(elements, prefix, events);
112
- },
113
-
114
- elements: new Map(),
115
- initElements: function (elements, prefix, events = []) {
116
- const self = this
117
- for (const el of elements) {
118
- let isEventable = true
119
- let prefixes = this.elements.get(el)
120
- if (!prefixes) {
121
- prefixes = { [prefix]: { events } }
122
- this.elements.set(el, prefixes)
123
- } else if (!prefixes[prefix]) {
124
- prefixes[prefix] = { events }
125
- } else
126
- isEventable = false
127
-
128
- let customEvents = el.getAttribute(`${prefix}-events`)
129
- if (customEvents) {
130
- customEvents = customEvents.split(',')
131
- for (let i = 0; i < customEvents.length; i++)
132
- customEvents[i] = customEvents[i].trim()
133
-
134
- for (let i = 0; i < events.length; i++)
135
- el.removeEventListener(events[i], eventFunction)
136
-
137
- events = customEvents
138
- prefixes[prefix].events = events
139
- isEventable = true
140
- }
141
- if (!events || !isEventable)
142
- continue
143
- if (events.includes('onload'))
144
- this.__updateElements(el, prefix);
145
-
146
- if (events.includes('observer')) {
147
- let target;
148
- for (let attribute of el.attributes) {
149
- if (attribute.name === 'observe-target') {
150
- target = attribute.value
151
- break;
152
- } else if ([`${prefix}-selector`, `${prefix}-closest`, `${prefix}-parent`, `${prefix}-next`, `${prefix}-previous`].includes(attribute.name)) {
153
- target = attribute.value
154
- break;
155
- }
156
- }
157
- if (target)
158
- observer.init({
159
- observe: ['addedNodes'],
160
- target,
161
- callback: function (mutation) {
162
- self.__updateElements(el, prefix, mutation.target);
163
- }
164
- });
165
- }
166
-
167
- if (events.includes('resize')) {
168
- const resizeObserver = new ResizeObserver((entries) => {
169
- const lastEntry = entries[entries.length - 1];
170
- const { width, height } = lastEntry.contentRect;
171
- el.setAttribute(`${prefix}-if-value`, width + 'px')
172
- self.__updateElements(el, prefix)
173
- });
174
- resizeObserver.observe(el);
175
- }
176
-
177
- for (let i = 0; i < events.length; i++) {
178
- if (events[i] !== 'onload' && events[i] !== 'observer' && events[i] !== 'resize') {
179
- el.removeEventListener(events[i], eventFunction)
180
- el.addEventListener(events[i], eventFunction);
181
- }
182
- }
183
-
184
- function eventFunction(event) {
185
- // TODO: apply debounce
186
- // let debounce;
187
- // clearTimeout(debounce);
188
- // debounce = setTimeout(function() {
189
- const target = event.currentTarget
190
- if (target) {
191
- let prefixes = self.elements.get(target)
192
- if (prefixes[prefix].prev === event.type && ['mouseover', 'mouseout'].includes(event.type))
193
- return
194
- else
195
- prefixes[prefix].prev = event.type
196
-
197
- let attribute = target.getAttribute('actions') || ""
198
- if (attribute.includes(prefix))
199
- return;
200
- // if (target.closest(`[actions*="${prefix}"]`))
201
- // return;
202
- self.__updateElements(target, prefix, null, prefixes[prefix].events);
203
-
204
- let selector = `[${prefix}], [${prefix}-key], [${prefix}-value], [${prefix}-selector], [${prefix}-closest], [${prefix}-parent], [${prefix}-next], [${prefix}-previous]`
205
-
206
- let parentElement = target.parentElement;
207
- if (parentElement) {
208
- do {
209
- parentElement = parentElement.closest(selector)
210
- if (parentElement) {
211
- self.__updateElements(parentElement, prefix, null, prefixes[prefix].events);
212
- parentElement = parentElement.parentElement
213
- }
214
- }
215
- while (parentElement)
216
-
217
- }
218
-
219
- }
220
- // }, 500);
221
- }
222
-
223
-
224
- }
225
- },
226
-
227
- __updateElements: async function (element, prefix, target, events) {
228
- const self = this;
229
- // TODO: support empty value when prefix-attribute defined, add and remove the attribute
230
- let targetAttribute = element.getAttribute(`${prefix}-attribute`);
231
- let targetPosition = element.getAttribute(`${prefix}-insert-adjacent`);
232
- // targetAttribute = checkMediaQueries(targetAttribute)
233
- // if (targetAttribute === false)
234
- // return
235
-
236
- let onEvent = element.getAttribute(`${prefix}-on`)
237
- if (onEvent) {
238
- await new Promise((resolve, reject) => {
239
- const handleEvent = () => {
240
- document.removeEventListener(onEvent, handleEvent);
241
- resolve();
242
- };
243
- document.addEventListener(onEvent, handleEvent);
244
- });
245
- }
246
-
247
- let values
248
- if (prefix === 'localstorage') {
249
- let key = element.getAttribute('localstorage-get')
250
- if (key) {
251
- values = localStorage.getItem(key)
252
- } else if (key = element.getAttribute('localstorage-set')) {
253
- values = await element.getValue()
254
- if (values)
255
- localStorage.setItem(key, values)
256
- }
257
-
258
- // if (!key || !values)
259
- if (!key)
260
- return
261
- } else {
262
- values = element.getAttribute(`${prefix}-value`) || element.getAttribute(`${prefix}-if-value`) || element.getAttribute(prefix);
263
- }
264
-
265
- if (values) {
266
- if (typeof values === 'string')
267
- values = values.split(',');
268
- else if (!Array.isArray(values))
269
- values = [values]
270
- } else {
271
- values = await element.getValue()
272
- if (!Array.isArray(values))
273
- values = [values]
274
- }
275
-
276
- if (targetAttribute && !values || values.length === 0)
277
- return;
278
-
279
- let ifCondition = element.getAttribute(`${prefix}-if`);
280
- let elseCondition = element.getAttribute(`${prefix}-else`);
281
-
282
- let ifValue = element.getAttribute(`${prefix}-if-value`)
283
- if (!ifValue && ifValue !== "")
284
- ifValue = await element.getValue() || values //values // await element.getValue()
285
- else if (ifValue || ifValue === "")
286
- ifValue = [ifValue]
287
- else
288
- ifValue = values
289
-
290
- if (!Array.isArray(ifValue))
291
- ifValue = [ifValue]
292
-
293
- //TODO: improved resize toggling of values
294
- // let hasCondition = this.elements2.get(element)
295
- if (ifCondition && evaluateCondition(ifCondition, ifValue)) {
296
-
297
- // if (hasCondition && hasCondition.condition === ifCondition) {
298
- // return
299
- // } else
300
- // this.elements2.set(element, { condition: ifCondition })
301
- } else if (elseCondition && evaluateCondition(elseCondition, ifValue)) {
302
- // if (hasCondition && hasCondition.condition === elseCondition) {
303
- // return
304
- // } else
305
- // this.elements2.set(element, { condition: elseCondition })
306
- } else if (ifCondition || elseCondition) {
307
- return
308
- }
309
-
310
- let targetText = element.getAttribute(`${prefix}-text`);
311
- let targetHtml = element.getAttribute(`${prefix}-html`);
312
- let targetKey = element.getAttribute(`${prefix}-key`);
313
-
314
- let targetGroup = element.getAttribute(`${prefix}-group`);
315
- if (targetGroup) {
316
- document.querySelectorAll(`[${prefix}-group="${targetGroup}"]`).forEach((el) => {
317
- let groupValue = el.getAttribute(`${prefix}-value`) || el.getAttribute(prefix);
318
- let groupValues = groupValue.split(',');
319
- if (!groupValues || groupValues.length == 0) {
320
- return;
321
- }
322
-
323
- groupValues = groupValues.map(x => x.trim());
324
- let groupAttribute = el.getAttribute(`${prefix}-attribute`) || 'class';
325
- let groupPosition = el.getAttribute(`${prefix}-insert-adjacent`);
326
- // groupAttribute = checkMediaQueries(groupAttribute)
327
- // if (!groupAttribute)
328
- // return
329
-
330
- let groupKey = el.getAttribute(`${prefix}-key`)
331
-
332
- // el.removeAttribute(prefix)
333
- self.setValue(prefix, el, groupAttribute, groupValues, groupKey, 'deactivate', events, groupPosition)
334
- let targetElements = queryElements({ el, prefix });
335
- for (let i = 0; i < targetElements.length; i++)
336
- self.setValue(prefix, targetElements[i], groupAttribute, groupValues, groupKey, 'deactivate', events, groupPosition)
337
-
338
- });
339
- }
340
-
341
- // values = values.map(x => x.trim());
342
-
343
- values = values.map(x => {
344
- if (typeof x === 'string')
345
- x = x.trim(); // Update x with the trimmed value
346
- let prop = element.getAttribute(`${prefix}-property`);
347
- if (prop) {
348
- x = `${prop}:${x}`; // Update x with prop if it exists
349
- }
350
- return x; // Return the updated x
351
- });
352
-
353
- let targetElements = queryElements({ element, prefix });
354
- if (targetElements === false)
355
- targetElements = [element]
356
-
357
- for (let i = 0; i < targetElements.length; i++) {
358
- if (!targetAttribute && targetAttribute !== '' && ['click', 'focus', 'blur'].includes(prefix)) {
359
- targetElements[i][prefix]()
360
- } else {
361
- this.setValue(prefix, targetElements[i], targetAttribute, values, targetKey, null, events, targetPosition)
362
- }
363
- }
364
-
365
- document.dispatchEvent(new CustomEvent(`${prefix}End`, {
366
- detail: {}
367
- }))
368
-
369
- },
370
-
371
- setValue: async function (prefix, element, attrName, values, key, deactivate, events, targetPosition) {
372
- if (events && events.includes('mouseout')) {
373
- if (element.matches(':hover')) {
374
- element.addEventListener('mouseout', () => {
375
- this.setValue(prefix, element, attrName, values, key, deactivate, events, targetPosition);
376
- }, { once: true });
377
- return;
378
- }
379
- }
380
-
381
- let attrValues, oldValue;
382
- if (key) {
383
- key = `{{${key}}}`;
384
- // let value = values[0]
385
- replaceKey(prefix, element, key, values)
386
- } else {
387
- if (attrName === 'style') {
388
- if (element.getAttribute(attrName)) {
389
- attrValues = element.getAttribute(attrName).split(';').map(x => x.trim());
390
- attrValues = attrValues.map(function (item, index) {
391
- return item.replace(' ', '');
392
- });
393
- oldValue = values.filter(x => attrValues.includes(x))[0] || '';
394
- }
395
- let newValue = this.__getNextValue(values, oldValue);
396
-
397
- if (oldValue) {
398
- let [property, value] = oldValue.split(":");
399
- element.style[property] = '';
400
- }
401
-
402
- if (newValue != '') {
403
- newValue = newValue.split(';').map(x => x.trim())
404
- for (let style of newValue) {
405
- let [property, value] = style.split(":");
406
- element.style[property] = value;
407
- }
408
- }
409
- } else {
410
- if (attrName === 'value') {
411
- oldValue = await element.getValue()
412
- } else if (attrName === 'text') {
413
- attrValues = element.getAttribute(attrName).split(' ').map(x => x.trim());
414
- oldValue = values.filter(x => attrValues.includes(x))[0] || '';
415
- } else if (attrName === 'html') {
416
- attrValues = element.getAttribute(attrName).split(' ').map(x => x.trim());
417
- oldValue = values.filter(x => attrValues.includes(x))[0] || '';
418
- } else if (element.getAttribute(attrName)) {
419
- attrValues = element.getAttribute(attrName).split(' ').map(x => x.trim());
420
- oldValue = values.filter(x => attrValues.includes(x))[0] || '';
421
- }
422
-
423
- let newValue = this.__getNextValue(values, oldValue);
424
- if (deactivate)
425
- newValue = ""
426
- if (attrName === 'class') {
427
- if (oldValue) {
428
- element.classList.remove(oldValue);
429
- if (values.length === 1) {
430
- return;
431
- }
432
- }
433
-
434
- if (newValue) {
435
- element.classList.add(newValue);
436
- }
437
- } else if (attrName === 'value') {
438
- element.setValue(newValue);
439
- } else if (['$click', '$focus', '$blur'].includes(attrName)) {
440
- element[attrName.substring(1)]()
441
- } else if (attrName) {
442
- element.setAttribute(attrName, newValue);
443
- } else if (targetPosition) {
444
- element.insertAdjacentHTML(targetPosition, newValue);
445
- } else {
446
- if (!attrName && ['click', 'focus', 'blur'].includes(newValue)) {
447
- element[newValue]()
448
- } else if (['click', 'focus', 'blur'].includes(attrName)) {
449
- element[attrName]()
450
- }
451
- }
452
- }
453
- }
454
- },
455
-
456
- __getNextValue: function (values, val) {
457
- let size = values.length;
458
- let nn = values.indexOf(val);
459
- if (nn == -1) {
460
- return values[0];
461
- } else {
462
- return values[(nn + 1) % size];
463
- }
464
- }
29
+ elements2: new Map(),
30
+ init: function (prefix, events) {
31
+ if (prefix && events) this.initPrefix(prefix, events);
32
+ else {
33
+ this.initPrefix("toggle", ["click"]);
34
+ this.initPrefix("click", ["click"]);
35
+ this.initPrefix("hover", ["mouseover", "mouseout"]);
36
+ this.initPrefix("mouseover", ["mouseover"]);
37
+ this.initPrefix("mouseout", ["mouseout"]);
38
+ this.initPrefix("input", ["input"]);
39
+ this.initPrefix("change", ["change"]);
40
+ this.initPrefix("selected", ["click"]);
41
+ this.initPrefix("onload", ["onload"]);
42
+ this.initPrefix("observe", ["observer"]);
43
+ this.initPrefix("resize", ["onload", "resize"]);
44
+ this.initPrefix("localstorage", ["onload", "observer"]);
45
+ }
46
+
47
+ let customEventEls = document.querySelectorAll("[event-name]");
48
+ let names = {};
49
+ for (let customEventEl of customEventEls) {
50
+ let name = customEventEl.getAttribute("event-name");
51
+ name = name.split(",");
52
+ for (let i = 0; i < name.length; i++) {
53
+ name[i].trim();
54
+ if (!names[name]) {
55
+ names[name] = name;
56
+ this.initPrefix(name);
57
+ }
58
+ }
59
+ }
60
+
61
+ const self = this;
62
+ observer.init({
63
+ name: "CoCreateEventName",
64
+ observe: ["addedNodes"],
65
+ selector: `[event-name]`,
66
+ callback: function (mutation) {
67
+ let name = mutation.target.getAttribute("event-name");
68
+ self.initPrefix(name);
69
+ }
70
+ });
71
+ },
72
+
73
+ initPrefix: function (prefix, events) {
74
+ const self = this;
75
+
76
+ action.init({
77
+ name: prefix,
78
+ endEvent: `${prefix}End`,
79
+ callback: (data) => {
80
+ this.__updateElements(
81
+ data.element,
82
+ prefix,
83
+ null,
84
+ null,
85
+ data.params
86
+ );
87
+ }
88
+ });
89
+
90
+ let selector = `[${prefix}], [${prefix}-key], [${prefix}-attribute], [${prefix}-value], [${prefix}-action], [${prefix}-selector], [${prefix}-closest], [${prefix}-parent], [${prefix}-next], [${prefix}-previous]`;
91
+
92
+ observer.init({
93
+ name: "CoCreateEventattributes",
94
+ observe: ["attributes", "addedNodes"],
95
+ attributeName: [`${prefix}-events`],
96
+ selector: selector,
97
+ callback: function (mutation) {
98
+ self.initElements([mutation.target], prefix, events);
99
+ }
100
+ });
101
+
102
+ if (events && events.includes("observer")) {
103
+ observer.init({
104
+ name: "observerAttributes",
105
+ observe: ["attributes"],
106
+ attributeName: [
107
+ `${prefix}-key`,
108
+ `${prefix}-value`,
109
+ `${prefix}-selector`,
110
+ `${prefix}-closest`,
111
+ `${prefix}-parent`,
112
+ `${prefix}-next`,
113
+ `${prefix}-previous`
114
+ ],
115
+ callback: function (mutation) {
116
+ // remove previous observer
117
+ self.initElements([mutation.target], prefix, events);
118
+ }
119
+ });
120
+ }
121
+
122
+ let elements = document.querySelectorAll(selector);
123
+ this.initElements(elements, prefix, events);
124
+ },
125
+
126
+ elements: new Map(),
127
+ initElements: function (elements, prefix, events = []) {
128
+ const self = this;
129
+ for (const el of elements) {
130
+ let isEventable = true;
131
+ let prefixes = this.elements.get(el);
132
+ if (!prefixes) {
133
+ prefixes = { [prefix]: { events } };
134
+ this.elements.set(el, prefixes);
135
+ } else if (!prefixes[prefix]) {
136
+ prefixes[prefix] = { events };
137
+ } else isEventable = false;
138
+
139
+ let customEvents = el.getAttribute(`${prefix}-events`);
140
+ if (customEvents) {
141
+ customEvents = customEvents.split(",");
142
+ for (let i = 0; i < customEvents.length; i++)
143
+ customEvents[i] = customEvents[i].trim();
144
+
145
+ for (let i = 0; i < events.length; i++)
146
+ el.removeEventListener(events[i], eventFunction);
147
+
148
+ events = customEvents;
149
+ prefixes[prefix].events = events;
150
+ isEventable = true;
151
+ }
152
+ if (!events || !isEventable) continue;
153
+ if (events.includes("onload")) this.__updateElements(el, prefix);
154
+
155
+ if (events.includes("observer")) {
156
+ let target;
157
+ for (let attribute of el.attributes) {
158
+ if (attribute.name === "observe-target") {
159
+ target = attribute.value;
160
+ break;
161
+ } else if (
162
+ [
163
+ `${prefix}-selector`,
164
+ `${prefix}-closest`,
165
+ `${prefix}-parent`,
166
+ `${prefix}-next`,
167
+ `${prefix}-previous`
168
+ ].includes(attribute.name)
169
+ ) {
170
+ target = attribute.value;
171
+ break;
172
+ }
173
+ }
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);
212
+ }
213
+
214
+ if (events.includes("resize")) {
215
+ const resizeObserver = new ResizeObserver((entries) => {
216
+ const lastEntry = entries[entries.length - 1];
217
+ const { width, height } = lastEntry.contentRect;
218
+ el.setAttribute(`${prefix}-if-value`, width + "px");
219
+ self.__updateElements(el, prefix);
220
+ });
221
+ resizeObserver.observe(el);
222
+ }
223
+
224
+ for (let i = 0; i < events.length; i++) {
225
+ if (
226
+ events[i] !== "onload" &&
227
+ events[i] !== "observer" &&
228
+ events[i] !== "resize"
229
+ ) {
230
+ el.removeEventListener(events[i], eventFunction);
231
+ el.addEventListener(events[i], eventFunction);
232
+ }
233
+ }
234
+
235
+ function eventFunction(event) {
236
+ // TODO: apply debounce
237
+ // let debounce;
238
+ // clearTimeout(debounce);
239
+ // debounce = setTimeout(function() {
240
+ const target = event.currentTarget;
241
+ if (target) {
242
+ let prefixes = self.elements.get(target);
243
+ if (
244
+ prefixes[prefix].prev === event.type &&
245
+ ["mouseover", "mouseout"].includes(event.type)
246
+ )
247
+ return;
248
+ else prefixes[prefix].prev = event.type;
249
+
250
+ let attribute = target.getAttribute("actions") || "";
251
+ if (attribute.includes(prefix)) return;
252
+ // if (target.closest(`[actions*="${prefix}"]`))
253
+ // return;
254
+ self.__updateElements(
255
+ target,
256
+ prefix,
257
+ null,
258
+ prefixes[prefix].events
259
+ );
260
+
261
+ // let selector = `[${prefix}], [${prefix}-key], [${prefix}-value], [${prefix}-selector], [${prefix}-closest], [${prefix}-parent], [${prefix}-next], [${prefix}-previous]`
262
+
263
+ // let parentElement = target.parentElement;
264
+ // if (parentElement) {
265
+ // do {
266
+ // parentElement = parentElement.closest(selector)
267
+ // if (parentElement) {
268
+ // self.__updateElements(parentElement, prefix, null, prefixes[prefix].events);
269
+ // parentElement = parentElement.parentElement
270
+ // }
271
+ // }
272
+ // while (parentElement)
273
+
274
+ // }
275
+ }
276
+ // }, 500);
277
+ }
278
+ }
279
+ },
280
+
281
+ __updateElements: async function (el, prefix, target, events, params) {
282
+ if (!el.isConnected) return;
283
+
284
+ let elements = [el];
285
+ let targetGroup = el.getAttribute(`${prefix}-group`);
286
+ if (targetGroup) {
287
+ document
288
+ .querySelectorAll(`[${prefix}-group="${targetGroup}"]`)
289
+ .forEach((element) => {
290
+ if (element !== el) elements.push(element);
291
+ });
292
+ }
293
+
294
+ 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
+
306
+ let once = element.getAttribute(`${prefix}-once`);
307
+ if (once || once === "") {
308
+ if (!element.eventsOnce) {
309
+ element.eventsOnce = [prefix];
310
+ } else if (element.eventsOnce.includes(prefix)) {
311
+ continue;
312
+ } else {
313
+ element.eventsOnce.push(prefix);
314
+ }
315
+ }
316
+
317
+ // TODO: support empty value when prefix-attribute defined, add and remove the attribute
318
+ let targetAttribute = element.getAttribute(`${prefix}-attribute`);
319
+ let targetPosition = element.getAttribute(
320
+ `${prefix}-insert-adjacent`
321
+ );
322
+ // targetAttribute = checkMediaQueries(targetAttribute)
323
+ // if (targetAttribute === false)
324
+ // return
325
+
326
+ let onEvent = element.getAttribute(`${prefix}-on`);
327
+ if (onEvent) {
328
+ await new Promise((resolve, reject) => {
329
+ const handleEvent = () => {
330
+ document.removeEventListener(onEvent, handleEvent);
331
+ resolve();
332
+ };
333
+ document.addEventListener(onEvent, handleEvent);
334
+ });
335
+ }
336
+
337
+ let values;
338
+ if (prefix === "localstorage") {
339
+ let key = element.getAttribute("localstorage-get");
340
+ if (key) {
341
+ values = localStorage.getItem(key);
342
+ } else if ((key = element.getAttribute("localstorage-set"))) {
343
+ values = await element.getValue();
344
+ if (values) localStorage.setItem(key, values);
345
+ }
346
+
347
+ // if (!key || !values)
348
+ if (!key) return;
349
+ } else {
350
+ values = element.getAttribute(`${prefix}-value`);
351
+
352
+ if (values === null) {
353
+ let valueElements = queryElements({
354
+ element,
355
+ prefix: `${prefix}-value`
356
+ });
357
+ if (valueElements) {
358
+ let elementValues = [];
359
+ for (let i = 0; i < valueElements.length; i++)
360
+ elementValues.push(valueElements[i].getValue());
361
+
362
+ if (elementValues.length) values = elementValues;
363
+ }
364
+ }
365
+
366
+ if (values === null)
367
+ values = element.getAttribute(`${prefix}-if-value`);
368
+ if (values === null) values = element.getAttribute(prefix);
369
+ }
370
+
371
+ if (values || values === "") {
372
+ if (typeof values === "string") values = values.split(",");
373
+ else if (!Array.isArray(values)) values = [values];
374
+ } else {
375
+ values = await element.getValue();
376
+ if (!Array.isArray(values)) values = [values];
377
+ }
378
+
379
+ if ((targetAttribute && !values) || values.length === 0) return;
380
+
381
+ let ifCondition = element.getAttribute(`${prefix}-if`);
382
+ let elseCondition = element.getAttribute(`${prefix}-else`);
383
+
384
+ let ifValue = element.getAttribute(`${prefix}-if-value`);
385
+ if (!ifValue && ifValue !== "")
386
+ ifValue = (await element.getValue()) || values;
387
+ //values // await element.getValue()
388
+ else if (ifValue || ifValue === "") ifValue = [ifValue];
389
+ else ifValue = values;
390
+
391
+ if (!Array.isArray(ifValue)) ifValue = [ifValue];
392
+
393
+ //TODO: improved resize toggling of values
394
+ // let hasCondition = this.elements2.get(element)
395
+ if (ifCondition && evaluateCondition(ifCondition, ifValue)) {
396
+ // if (hasCondition && hasCondition.condition === ifCondition) {
397
+ // return
398
+ // } else
399
+ // this.elements2.set(element, { condition: ifCondition })
400
+ } else if (
401
+ elseCondition &&
402
+ evaluateCondition(elseCondition, ifValue)
403
+ ) {
404
+ // if (hasCondition && hasCondition.condition === elseCondition) {
405
+ // return
406
+ // } else
407
+ // this.elements2.set(element, { condition: elseCondition })
408
+ } else if (ifCondition || elseCondition) {
409
+ return;
410
+ }
411
+
412
+ let targetText = element.getAttribute(`${prefix}-text`);
413
+ let targetHtml = element.getAttribute(`${prefix}-html`);
414
+ let targetKey = element.getAttribute(`${prefix}-key`);
415
+
416
+ if (prefix === "selected") {
417
+ if (el !== element) element.removeAttribute("selected");
418
+ else element.setAttribute("selected", "");
419
+ }
420
+
421
+ let deactivate = false;
422
+ if (el !== element) deactivate = true;
423
+ // values = values.map(x => x.trim());
424
+
425
+ values = values.map((x) => {
426
+ if (typeof x === "string") x = x.trim(); // Update x with the trimmed value
427
+ let prop = element.getAttribute(`${prefix}-property`);
428
+ if (prop) {
429
+ x = `${prop}:${x}`; // Update x with prop if it exists
430
+ }
431
+ return x; // Return the updated x
432
+ });
433
+
434
+ let targetElements = queryElements({
435
+ element,
436
+ prefix,
437
+ selector: params
438
+ });
439
+ if (targetElements === false) targetElements = [element];
440
+ let action = element.getAttribute(`${prefix}-action`);
441
+ for (let i = 0; i < targetElements.length; i++) {
442
+ if (action) {
443
+ targetElements[i][action]();
444
+ } else if (
445
+ !targetAttribute &&
446
+ targetAttribute !== "" &&
447
+ !targetPosition &&
448
+ ["click", "focus", "blur"].includes(prefix)
449
+ ) {
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
+ )
458
+ targetElements[i][prefix]();
459
+ } else {
460
+ this.setValue(
461
+ prefix,
462
+ targetElements[i],
463
+ targetAttribute,
464
+ values,
465
+ targetKey,
466
+ deactivate,
467
+ events,
468
+ targetPosition
469
+ );
470
+ }
471
+ }
472
+ }
473
+
474
+ document.dispatchEvent(
475
+ new CustomEvent(`${prefix}End`, {
476
+ detail: {}
477
+ })
478
+ );
479
+ },
480
+
481
+ setValue: async function (
482
+ prefix,
483
+ element,
484
+ attrName,
485
+ values,
486
+ key,
487
+ deactivate,
488
+ events,
489
+ targetPosition
490
+ ) {
491
+ if (events && events.includes("mouseout")) {
492
+ if (element.matches(":hover")) {
493
+ element.addEventListener(
494
+ "mouseout",
495
+ () => {
496
+ this.setValue(
497
+ prefix,
498
+ element,
499
+ attrName,
500
+ values,
501
+ key,
502
+ deactivate,
503
+ events,
504
+ targetPosition
505
+ );
506
+ },
507
+ { once: true }
508
+ );
509
+ return;
510
+ }
511
+ }
512
+
513
+ let attrValues, oldValue;
514
+ if (key) {
515
+ key = `{{${key}}}`;
516
+ // let value = values[0]
517
+ replaceKey(prefix, element, key, values);
518
+ } else {
519
+ // TODo: if html in iframe
520
+ let domTextEditor;
521
+ if (element.parentElement) {
522
+ domTextEditor =
523
+ element.parentElement.closest("[contenteditable]");
524
+ }
525
+
526
+ if (attrName === "style") {
527
+ if (element.getAttribute(attrName)) {
528
+ attrValues = element
529
+ .getAttribute(attrName)
530
+ .split(";")
531
+ .map((x) => x.trim());
532
+ attrValues = attrValues.map(function (item, index) {
533
+ return item.replace(" ", "");
534
+ });
535
+ oldValue =
536
+ values.filter((x) => attrValues.includes(x))[0] || "";
537
+ }
538
+ let newValue = this.__getNextValue(values, oldValue);
539
+
540
+ if (oldValue) {
541
+ let [property, value] = oldValue.split(":");
542
+ element.style[property] = "";
543
+ }
544
+
545
+ if (newValue != "") {
546
+ newValue = newValue.split(";").map((x) => x.trim());
547
+ for (let style of newValue) {
548
+ let [property, value] = style.split(":");
549
+ element.style[property] = value;
550
+ }
551
+ }
552
+ } else {
553
+ if (attrName === "value") {
554
+ oldValue = await element.getValue();
555
+ } else if (
556
+ attrName === "text" ||
557
+ attrName === "html" ||
558
+ element.getAttribute(attrName)
559
+ ) {
560
+ attrValues = element
561
+ .getAttribute(attrName)
562
+ .split(" ")
563
+ .map((x) => x.trim());
564
+ oldValue =
565
+ values.filter((x) => attrValues.includes(x))[0] || "";
566
+ }
567
+
568
+ let newValue = this.__getNextValue(values, oldValue);
569
+ if (deactivate) newValue = "";
570
+ if (attrName === "class") {
571
+ if (oldValue) {
572
+ element.classList.remove(oldValue);
573
+ if (values.length === 1) {
574
+ return;
575
+ }
576
+ }
577
+
578
+ if (newValue) {
579
+ element.classList.add(newValue);
580
+ }
581
+ } else if (attrName === "value") {
582
+ element.setValue(newValue);
583
+ } else if (
584
+ ["$click", "$focus", "$blur", "$save", "$read"].includes(
585
+ attrName
586
+ )
587
+ ) {
588
+ element[attrName.substring(1)]();
589
+ } else if (attrName) {
590
+ // TODO: removeAttribute vs setting empty value, how best to define. operator $remove ???
591
+ if (newValue === "" && element.hasAttribute(attrName))
592
+ element.removeAttribute(attrName);
593
+ else element.setAttribute(attrName, newValue);
594
+ } else if (targetPosition) {
595
+ if (domTextEditor) {
596
+ CoCreate.text.insertAdjacentElement({
597
+ domTextEditor,
598
+ position: targetPosition,
599
+ target: element,
600
+ elementValue: newValue
601
+ });
602
+ } else if (targetPosition === "replace") {
603
+ element.insertAdjacentHTML("beforebegin", newValue); // Insert before oldElement
604
+ element.remove(); // Remove oldElement
605
+ } else {
606
+ element.insertAdjacentHTML(targetPosition, newValue);
607
+ }
608
+ } else {
609
+ if (
610
+ !attrName &&
611
+ ["click", "focus", "blur"].includes(newValue)
612
+ ) {
613
+ element[newValue]();
614
+ } else if (["click", "focus", "blur"].includes(attrName)) {
615
+ element[attrName]();
616
+ }
617
+ }
618
+ }
619
+ }
620
+ },
621
+
622
+ __getNextValue: function (values, val) {
623
+ let size = values.length;
624
+ let nn = values.indexOf(val);
625
+ if (nn == -1) {
626
+ return values[0];
627
+ } else {
628
+ return values[(nn + 1) % size];
629
+ }
630
+ }
465
631
  };
466
632
 
467
633
  const eventElements = new Map();
@@ -469,178 +635,193 @@ const eventKeys = new Map();
469
635
  const elementAttributes = new Map();
470
636
 
471
637
  function replaceKey(prefix, element, key, values) {
472
- let currentValue = key
473
-
474
- if (eventElements.has(element)) {
475
- currentValue = eventElements.get(element).get(key);
476
- }
477
- let value = CoCreateEvents.__getNextValue(values, currentValue);
478
- eventKeys.set(key, value)
479
- eventElements.set(element, eventKeys)
480
-
481
- let attributes = elementAttributes.get(element)
482
- if (!attributes) {
483
- attributes = {}
484
- elementAttributes.set(element, attributes)
485
- }
486
-
487
-
488
- for (let attribute of element.attributes) {
489
- let newName = attribute.name;
490
- let newValue = attribute.value;
491
- let attrName = attribute.name;
492
- let attrValue = attribute.value;
493
- let setAttr = false;
494
-
495
- if (attrName === 'clone-data')
496
- console.log('clone-data')
497
- if (attrValue.includes(currentValue) || attributes[attrName] && attributes[attrName].valueKeys && attributes[attrName].valueKeys[key]) {
498
- if (!attributes[attrName])
499
- attributes[attrName] = { originalValue: attrValue }
500
- else if (!attributes[attrName].originalValue)
501
- attributes[attrName].originalValue = attrValue
502
- if (!attributes[attrName].valueKeys)
503
- attributes[attrName].valueKeys = { [key]: value }
504
- else
505
- attributes[attrName].valueKeys[key] = value;
506
-
507
- newValue = attributes[attrName].originalValue
508
- for (let valueKey of Object.keys(attributes[attrName].valueKeys)) {
509
- const valueKeyRegex = new RegExp(valueKey, "g");
510
- newValue = newValue.replace(valueKeyRegex, value);
511
- }
512
- setAttr = true;
513
- }
514
- // if (attrName.includes(currentValue) || attributes[attrName] && attributes[attrName].nameKeys && attributes[attrName].nameKeys[key]){
515
- // if (!attributes[attrName])
516
- // attributes[attrName] = { originalName: attrName }
517
- // else if (!attributes[attrName].originalName)
518
- // attributes[attrName].originalName = attrName
519
-
520
- // if (!attributes[attrName].nameKeys)
521
- // attributes[attrName].nameKeys = {[key]: value}
522
-
523
- // newName = attributes[attrName].originalName
524
- // for (let nameKey of Object.keys(attributes[attrName].nameKeys)) {
525
- // let r
526
- // if (attrName === nameKey)
527
- // r = nameKey
528
- // else
529
- // r = attributes[attrName].nameKeys[nameKey]
530
-
531
- // const nameKeyRegex = new RegExp(r, "g");
532
- // newName = newName.replace(nameKeyRegex, value);
533
- // }
534
-
535
- // attributes[attrName].nameKeys[key] = value;
536
-
537
-
538
- // element.removeAttribute(attrName);
539
- // setAttr = true;
540
-
541
- // }
542
- if (setAttr)
543
- element.setAttribute(newName, newValue);
544
- }
545
- let html = element.innerHTML;
546
- if (html.indexOf(currentValue) !== -1 || element.htmlKeys && element.htmlKeys[key]) {
547
- if (!element.originalHtml)
548
- element.originalHtml = html
549
- if (!element.htmlKeys)
550
- element.htmlKeys = { [key]: value }
551
- else
552
- element.htmlKeys[key] = value;
553
-
554
- let newHTML = element.originalHtml
555
- if (newHTML) {
556
- for (let elementKey of Object.keys(element.htmlKeys)) {
557
- const htmlKeyRegex = new RegExp(elementKey, "g");
558
- newHTML = newHTML.replace(htmlKeyRegex, value);
559
- }
560
-
561
- element.innerHTML = newHTML;
562
- }
563
- }
564
-
638
+ let currentValue = key;
639
+
640
+ if (eventElements.has(element)) {
641
+ currentValue = eventElements.get(element).get(key);
642
+ }
643
+ let value = CoCreateEvents.__getNextValue(values, currentValue);
644
+ eventKeys.set(key, value);
645
+ eventElements.set(element, eventKeys);
646
+
647
+ let attributes = elementAttributes.get(element);
648
+ if (!attributes) {
649
+ attributes = {};
650
+ elementAttributes.set(element, attributes);
651
+ }
652
+
653
+ for (let attribute of element.attributes) {
654
+ let newName = attribute.name;
655
+ let newValue = attribute.value;
656
+ let attrName = attribute.name;
657
+ let attrValue = attribute.value;
658
+ let setAttr = false;
659
+
660
+ if (attrName === "clone-data") console.log("clone-data");
661
+ if (
662
+ attrValue.includes(currentValue) ||
663
+ (attributes[attrName] &&
664
+ attributes[attrName].valueKeys &&
665
+ attributes[attrName].valueKeys[key])
666
+ ) {
667
+ if (!attributes[attrName])
668
+ attributes[attrName] = { originalValue: attrValue };
669
+ else if (!attributes[attrName].originalValue)
670
+ attributes[attrName].originalValue = attrValue;
671
+ if (!attributes[attrName].valueKeys)
672
+ attributes[attrName].valueKeys = { [key]: value };
673
+ else attributes[attrName].valueKeys[key] = value;
674
+
675
+ newValue = attributes[attrName].originalValue;
676
+ for (let valueKey of Object.keys(attributes[attrName].valueKeys)) {
677
+ const valueKeyRegex = new RegExp(valueKey, "g");
678
+ newValue = newValue.replace(valueKeyRegex, value);
679
+ }
680
+ setAttr = true;
681
+ }
682
+ // if (attrName.includes(currentValue) || attributes[attrName] && attributes[attrName].nameKeys && attributes[attrName].nameKeys[key]){
683
+ // if (!attributes[attrName])
684
+ // attributes[attrName] = { originalName: attrName }
685
+ // else if (!attributes[attrName].originalName)
686
+ // attributes[attrName].originalName = attrName
687
+
688
+ // if (!attributes[attrName].nameKeys)
689
+ // attributes[attrName].nameKeys = {[key]: value}
690
+
691
+ // newName = attributes[attrName].originalName
692
+ // for (let nameKey of Object.keys(attributes[attrName].nameKeys)) {
693
+ // let r
694
+ // if (attrName === nameKey)
695
+ // r = nameKey
696
+ // else
697
+ // r = attributes[attrName].nameKeys[nameKey]
698
+
699
+ // const nameKeyRegex = new RegExp(r, "g");
700
+ // newName = newName.replace(nameKeyRegex, value);
701
+ // }
702
+
703
+ // attributes[attrName].nameKeys[key] = value;
704
+
705
+ // element.removeAttribute(attrName);
706
+ // setAttr = true;
707
+
708
+ // }
709
+ if (setAttr) element.setAttribute(newName, newValue);
710
+ }
711
+ let html = element.innerHTML;
712
+ if (
713
+ html.indexOf(currentValue) !== -1 ||
714
+ (element.htmlKeys && element.htmlKeys[key])
715
+ ) {
716
+ if (!element.originalHtml) element.originalHtml = html;
717
+ if (!element.htmlKeys) element.htmlKeys = { [key]: value };
718
+ else element.htmlKeys[key] = value;
719
+
720
+ let newHTML = element.originalHtml;
721
+ if (newHTML) {
722
+ for (let elementKey of Object.keys(element.htmlKeys)) {
723
+ const htmlKeyRegex = new RegExp(elementKey, "g");
724
+ newHTML = newHTML.replace(htmlKeyRegex, value);
725
+ }
726
+
727
+ element.innerHTML = newHTML;
728
+ }
729
+ }
565
730
  }
566
731
 
567
732
  function evaluateCondition(condition, values) {
568
- let orConditions = condition.split(/\s*\|\|\s*/);
569
-
570
- if (orConditions.length > 1) {
571
- return orConditions.some(cond => checkCondition(cond, values));
572
- } else {
573
- let andConditions = condition.split(/\s*&&\s*/);
574
- return andConditions.every(cond => checkCondition(cond, values));
575
- }
733
+ let orConditions = condition.split(/\s*\|\|\s*/);
734
+
735
+ if (orConditions.length > 1) {
736
+ return orConditions.some((cond) => checkCondition(cond, values));
737
+ } else {
738
+ let andConditions = condition.split(/\s*&&\s*/);
739
+ return andConditions.every((cond) => checkCondition(cond, values));
740
+ }
576
741
  }
577
742
 
578
743
  function checkCondition(condition, value) {
579
- let isNegated = condition.startsWith('!');
580
- if (isNegated) {
581
- condition = condition.substring(1);
582
- }
583
- const operatorMatch = condition.match(/(<=|>=|<|>)(.+)/);
584
- if (operatorMatch)
585
- condition = operatorMatch[2].trim()
586
-
587
- let parse = true
588
- if (condition === 'true') {
589
- return !!value[0];
590
- } else if (condition === 'false') {
591
- return !value[0];
592
- } else if (condition === '[]' && typeof value[0] === 'string') {
593
- parse = false
594
- }
595
-
596
-
597
-
598
- // TODO: why parse updated conditin to boolean false
599
- if (parse && condition !== 'false')
600
- condition = parseCondition(condition);
601
-
602
- let result;
603
-
604
- if (condition === '$template') {
605
- result = (/{{\s*([\w\W]+)\s*}}/g.test(value[0]))
606
- } else if (Array.isArray(value) && !(typeof condition === 'object')) {
607
- if (operatorMatch) {
608
- condition = parseFloat(condition)
609
- result = value.some(v => {
610
- v = parseFloat(v)
611
- switch (operatorMatch[1]) {
612
- case '<': return v < condition;
613
- case '>': return v > condition;
614
- case '<=': return v <= condition;
615
- case '>=': return v >= condition;
616
- default: return false;
617
- }
618
- });
619
- } else
620
- result = value.includes(condition);
621
- } else if (Array.isArray(value)) {
622
- // TODO: handle comparing array to array, vs querying the ayya items for a match
623
- result = queryData(value, condition)
624
- } else if (typeof value === 'object' && value !== null && typeof parsedCond === 'object') {
625
- result = queryData(value, condition)
626
- } else {
627
- result = value === condition;
628
- }
629
-
630
- return isNegated ? !result : result;
744
+ let isNegated = condition.startsWith("!");
745
+ if (isNegated) {
746
+ condition = condition.substring(1);
747
+ }
748
+ const operatorMatch = condition.match(/(<=|>=|<|>)(.+)/);
749
+ if (operatorMatch) condition = operatorMatch[2].trim();
750
+
751
+ let parse = true;
752
+ if (condition === "true") {
753
+ return !!value[0];
754
+ } else if (condition === "false") {
755
+ return !value[0];
756
+ }
757
+ // else if (condition === "[]" && typeof value[0] === "string") {
758
+ // parse = false;
759
+ // }
760
+
761
+ // TODO: why parse updated conditin to boolean false
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
+ }
769
+
770
+ let result;
771
+
772
+ if (condition === "$template") {
773
+ result = /{{\s*([\w\W]+)\s*}}/g.test(value[0]);
774
+ } else if (Array.isArray(value) && !(typeof condition === "object")) {
775
+ if (operatorMatch) {
776
+ condition = parseFloat(condition);
777
+ result = value.some((v) => {
778
+ v = parseFloat(v);
779
+ switch (operatorMatch[1]) {
780
+ case "<":
781
+ return v < condition;
782
+ case ">":
783
+ return v > condition;
784
+ case "<=":
785
+ return v <= condition;
786
+ case ">=":
787
+ return v >= condition;
788
+ default:
789
+ return false;
790
+ }
791
+ });
792
+ } else result = value.includes(condition);
793
+ } else if (Array.isArray(value)) {
794
+ // TODO: handle comparing array to array, vs querying the array items for a match
795
+ result = queryData(value, condition);
796
+ } else if (
797
+ typeof value === "object" &&
798
+ value !== null &&
799
+ typeof parsedCond === "object"
800
+ ) {
801
+ result = queryData(value, condition);
802
+ } else {
803
+ result = value === condition;
804
+ }
805
+
806
+ result = isNegated ? !result : result;
807
+ return result;
631
808
  }
632
809
 
633
- function parseCondition(condition) {
634
- try {
635
- // Attempt to parse the condition as JSON
636
- let parsedJson = JSON.parse(condition);
637
- return parsedJson;
638
- } catch (e) {
639
- // If JSON parsing fails, check if the condition is a number
640
- return !isNaN(condition) && condition.trim() !== '' ? Number(condition) : condition;
641
- }
810
+ function parseJsonCondition(condition) {
811
+ try {
812
+ // Attempt to parse the condition as JSON
813
+ let parsedJson = JSON.parse(condition);
814
+ return parsedJson;
815
+ } catch (e) {
816
+ return condition;
817
+ }
818
+ }
819
+ function parseNumberCondition(condition) {
820
+ return !isNaN(condition) && condition.trim() !== ""
821
+ ? Number(condition)
822
+ : condition;
642
823
  }
643
824
 
644
825
  CoCreateEvents.init();
645
826
 
646
- export default CoCreateEvents;
827
+ export default CoCreateEvents;