@cocreate/events 1.24.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.
Files changed (3) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/package.json +1 -1
  3. package/src/index.js +784 -622
package/src/index.js CHANGED
@@ -20,465 +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
- selector: `[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
- selector: selector,
93
- callback: function (mutation) {
94
- self.initElements([mutation.target], prefix, events)
95
- }
96
- });
97
-
98
- if (events && 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
- selector: 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 (el, prefix, target, events) {
228
- const self = this;
229
- let elements = [el]
230
- let targetGroup = el.getAttribute(`${prefix}-group`);
231
- if (targetGroup) {
232
- document.querySelectorAll(`[${prefix}-group="${targetGroup}"]`).forEach((element) => {
233
- if (element !== el)
234
- elements.push(element)
235
- })
236
- }
237
-
238
- for (let element of elements) {
239
- // TODO: support empty value when prefix-attribute defined, add and remove the attribute
240
- let targetAttribute = element.getAttribute(`${prefix}-attribute`);
241
- let targetPosition = element.getAttribute(`${prefix}-insert-adjacent`);
242
- // targetAttribute = checkMediaQueries(targetAttribute)
243
- // if (targetAttribute === false)
244
- // return
245
-
246
- let onEvent = element.getAttribute(`${prefix}-on`)
247
- if (onEvent) {
248
- await new Promise((resolve, reject) => {
249
- const handleEvent = () => {
250
- document.removeEventListener(onEvent, handleEvent);
251
- resolve();
252
- };
253
- document.addEventListener(onEvent, handleEvent);
254
- });
255
- }
256
-
257
- let values
258
- if (prefix === 'localstorage') {
259
- let key = element.getAttribute('localstorage-get')
260
- if (key) {
261
- values = localStorage.getItem(key)
262
- } else if (key = element.getAttribute('localstorage-set')) {
263
- values = await element.getValue()
264
- if (values)
265
- localStorage.setItem(key, values)
266
- }
267
-
268
- // if (!key || !values)
269
- if (!key)
270
- return
271
- } else {
272
-
273
- values = element.getAttribute(`${prefix}-value`)
274
-
275
- if (values === null) {
276
- let valueElements = queryElements({ element, prefix: `${prefix}-value` });
277
- if (valueElements) {
278
- let elementValues = []
279
- for (let i = 0; i < valueElements.length; i++)
280
- elementValues.push(valueElements[i].getValue())
281
-
282
- if (elementValues.length)
283
- values = elementValues
284
- }
285
- }
286
-
287
- if (values === null)
288
- values = element.getAttribute(`${prefix}-if-value`)
289
- if (values === null)
290
- values = element.getAttribute(prefix);
291
-
292
- }
293
-
294
- if (values || values === '') {
295
- if (typeof values === 'string')
296
- values = values.split(',');
297
- else if (!Array.isArray(values))
298
- values = [values]
299
- } else {
300
- values = await element.getValue()
301
- if (!Array.isArray(values))
302
- values = [values]
303
- }
304
-
305
- if (targetAttribute && !values || values.length === 0)
306
- return;
307
-
308
- let ifCondition = element.getAttribute(`${prefix}-if`);
309
- let elseCondition = element.getAttribute(`${prefix}-else`);
310
-
311
- let ifValue = element.getAttribute(`${prefix}-if-value`)
312
- if (!ifValue && ifValue !== "")
313
- ifValue = await element.getValue() || values //values // await element.getValue()
314
- else if (ifValue || ifValue === "")
315
- ifValue = [ifValue]
316
- else
317
- ifValue = values
318
-
319
- if (!Array.isArray(ifValue))
320
- ifValue = [ifValue]
321
-
322
- //TODO: improved resize toggling of values
323
- // let hasCondition = this.elements2.get(element)
324
- if (ifCondition && evaluateCondition(ifCondition, ifValue)) {
325
-
326
- // if (hasCondition && hasCondition.condition === ifCondition) {
327
- // return
328
- // } else
329
- // this.elements2.set(element, { condition: ifCondition })
330
- } else if (elseCondition && evaluateCondition(elseCondition, ifValue)) {
331
- // if (hasCondition && hasCondition.condition === elseCondition) {
332
- // return
333
- // } else
334
- // this.elements2.set(element, { condition: elseCondition })
335
- } else if (ifCondition || elseCondition) {
336
- return
337
- }
338
-
339
- let targetText = element.getAttribute(`${prefix}-text`);
340
- let targetHtml = element.getAttribute(`${prefix}-html`);
341
- let targetKey = element.getAttribute(`${prefix}-key`);
342
-
343
- if (prefix === 'selected') {
344
- if (el !== element)
345
- element.removeAttribute('selected')
346
- else
347
- element.setAttribute('selected', '')
348
- }
349
-
350
- let deactivate = false
351
- if (el !== element)
352
- deactivate = true
353
- // values = values.map(x => x.trim());
354
-
355
- values = values.map(x => {
356
- if (typeof x === 'string')
357
- x = x.trim(); // Update x with the trimmed value
358
- let prop = element.getAttribute(`${prefix}-property`);
359
- if (prop) {
360
- x = `${prop}:${x}`; // Update x with prop if it exists
361
- }
362
- return x; // Return the updated x
363
- });
364
-
365
- let targetElements = queryElements({ element, prefix });
366
- if (targetElements === false)
367
- targetElements = [element]
368
- let action = element.getAttribute(`${prefix}-action`)
369
- for (let i = 0; i < targetElements.length; i++) {
370
- if (action) {
371
- targetElements[i][action]()
372
- } else if (!targetAttribute && targetAttribute !== '' && ['click', 'focus', 'blur'].includes(prefix)) {
373
- targetElements[i][prefix]()
374
- } else {
375
- this.setValue(prefix, targetElements[i], targetAttribute, values, targetKey, deactivate, events, targetPosition)
376
- }
377
- }
378
- }
379
-
380
- document.dispatchEvent(new CustomEvent(`${prefix}End`, {
381
- detail: {}
382
- }))
383
-
384
- },
385
-
386
- setValue: async function (prefix, element, attrName, values, key, deactivate, events, targetPosition) {
387
- if (events && events.includes('mouseout')) {
388
- if (element.matches(':hover')) {
389
- element.addEventListener('mouseout', () => {
390
- this.setValue(prefix, element, attrName, values, key, deactivate, events, targetPosition);
391
- }, { once: true });
392
- return;
393
- }
394
- }
395
-
396
- let attrValues, oldValue;
397
- if (key) {
398
- key = `{{${key}}}`;
399
- // let value = values[0]
400
- replaceKey(prefix, element, key, values)
401
- } else {
402
- if (attrName === 'style') {
403
- if (element.getAttribute(attrName)) {
404
- attrValues = element.getAttribute(attrName).split(';').map(x => x.trim());
405
- attrValues = attrValues.map(function (item, index) {
406
- return item.replace(' ', '');
407
- });
408
- oldValue = values.filter(x => attrValues.includes(x))[0] || '';
409
- }
410
- let newValue = this.__getNextValue(values, oldValue);
411
-
412
- if (oldValue) {
413
- let [property, value] = oldValue.split(":");
414
- element.style[property] = '';
415
- }
416
-
417
- if (newValue != '') {
418
- newValue = newValue.split(';').map(x => x.trim())
419
- for (let style of newValue) {
420
- let [property, value] = style.split(":");
421
- element.style[property] = value;
422
- }
423
- }
424
- } else {
425
- if (attrName === 'value') {
426
- oldValue = await element.getValue()
427
- } else if (attrName === 'text' || attrName === 'html' || element.getAttribute(attrName)) {
428
- attrValues = element.getAttribute(attrName).split(' ').map(x => x.trim());
429
- oldValue = values.filter(x => attrValues.includes(x))[0] || '';
430
- }
431
-
432
- let newValue = this.__getNextValue(values, oldValue);
433
- if (deactivate)
434
- newValue = ""
435
- if (attrName === 'class') {
436
- if (oldValue) {
437
- element.classList.remove(oldValue);
438
- if (values.length === 1) {
439
- return;
440
- }
441
- }
442
-
443
- if (newValue) {
444
- element.classList.add(newValue);
445
- }
446
- } else if (attrName === 'value') {
447
- element.setValue(newValue);
448
- } else if (['$click', '$focus', '$blur', '$save', '$read'].includes(attrName)) {
449
- element[attrName.substring(1)]()
450
- } else if (attrName) {
451
- // TODO: removeAttribute vs setting empty value, how best to define. operator $remove ???
452
- if (newValue === '' && element.hasAttribute(attrName))
453
- element.removeAttribute(attrName);
454
- else
455
- element.setAttribute(attrName, newValue);
456
- } else if (targetPosition) {
457
- if (targetPosition === 'replace') {
458
- element.insertAdjacentHTML('beforebegin', newValue); // Insert before oldElement
459
- element.remove(); // Remove oldElement
460
- } else
461
- element.insertAdjacentHTML(targetPosition, newValue);
462
- } else {
463
- if (!attrName && ['click', 'focus', 'blur'].includes(newValue)) {
464
- element[newValue]()
465
- } else if (['click', 'focus', 'blur'].includes(attrName)) {
466
- element[attrName]()
467
- }
468
- }
469
- }
470
- }
471
- },
472
-
473
- __getNextValue: function (values, val) {
474
- let size = values.length;
475
- let nn = values.indexOf(val);
476
- if (nn == -1) {
477
- return values[0];
478
- } else {
479
- return values[(nn + 1) % size];
480
- }
481
- }
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
+ }
482
631
  };
483
632
 
484
633
  const eventElements = new Map();
@@ -486,180 +635,193 @@ const eventKeys = new Map();
486
635
  const elementAttributes = new Map();
487
636
 
488
637
  function replaceKey(prefix, element, key, values) {
489
- let currentValue = key
490
-
491
- if (eventElements.has(element)) {
492
- currentValue = eventElements.get(element).get(key);
493
- }
494
- let value = CoCreateEvents.__getNextValue(values, currentValue);
495
- eventKeys.set(key, value)
496
- eventElements.set(element, eventKeys)
497
-
498
- let attributes = elementAttributes.get(element)
499
- if (!attributes) {
500
- attributes = {}
501
- elementAttributes.set(element, attributes)
502
- }
503
-
504
-
505
- for (let attribute of element.attributes) {
506
- let newName = attribute.name;
507
- let newValue = attribute.value;
508
- let attrName = attribute.name;
509
- let attrValue = attribute.value;
510
- let setAttr = false;
511
-
512
- if (attrName === 'clone-data')
513
- console.log('clone-data')
514
- if (attrValue.includes(currentValue) || attributes[attrName] && attributes[attrName].valueKeys && attributes[attrName].valueKeys[key]) {
515
- if (!attributes[attrName])
516
- attributes[attrName] = { originalValue: attrValue }
517
- else if (!attributes[attrName].originalValue)
518
- attributes[attrName].originalValue = attrValue
519
- if (!attributes[attrName].valueKeys)
520
- attributes[attrName].valueKeys = { [key]: value }
521
- else
522
- attributes[attrName].valueKeys[key] = value;
523
-
524
- newValue = attributes[attrName].originalValue
525
- for (let valueKey of Object.keys(attributes[attrName].valueKeys)) {
526
- const valueKeyRegex = new RegExp(valueKey, "g");
527
- newValue = newValue.replace(valueKeyRegex, value);
528
- }
529
- setAttr = true;
530
- }
531
- // if (attrName.includes(currentValue) || attributes[attrName] && attributes[attrName].nameKeys && attributes[attrName].nameKeys[key]){
532
- // if (!attributes[attrName])
533
- // attributes[attrName] = { originalName: attrName }
534
- // else if (!attributes[attrName].originalName)
535
- // attributes[attrName].originalName = attrName
536
-
537
- // if (!attributes[attrName].nameKeys)
538
- // attributes[attrName].nameKeys = {[key]: value}
539
-
540
- // newName = attributes[attrName].originalName
541
- // for (let nameKey of Object.keys(attributes[attrName].nameKeys)) {
542
- // let r
543
- // if (attrName === nameKey)
544
- // r = nameKey
545
- // else
546
- // r = attributes[attrName].nameKeys[nameKey]
547
-
548
- // const nameKeyRegex = new RegExp(r, "g");
549
- // newName = newName.replace(nameKeyRegex, value);
550
- // }
551
-
552
- // attributes[attrName].nameKeys[key] = value;
553
-
554
-
555
- // element.removeAttribute(attrName);
556
- // setAttr = true;
557
-
558
- // }
559
- if (setAttr)
560
- element.setAttribute(newName, newValue);
561
- }
562
- let html = element.innerHTML;
563
- if (html.indexOf(currentValue) !== -1 || element.htmlKeys && element.htmlKeys[key]) {
564
- if (!element.originalHtml)
565
- element.originalHtml = html
566
- if (!element.htmlKeys)
567
- element.htmlKeys = { [key]: value }
568
- else
569
- element.htmlKeys[key] = value;
570
-
571
- let newHTML = element.originalHtml
572
- if (newHTML) {
573
- for (let elementKey of Object.keys(element.htmlKeys)) {
574
- const htmlKeyRegex = new RegExp(elementKey, "g");
575
- newHTML = newHTML.replace(htmlKeyRegex, value);
576
- }
577
-
578
- element.innerHTML = newHTML;
579
- }
580
- }
581
-
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
+ }
582
730
  }
583
731
 
584
732
  function evaluateCondition(condition, values) {
585
- let orConditions = condition.split(/\s*\|\|\s*/);
586
-
587
- if (orConditions.length > 1) {
588
- return orConditions.some(cond => checkCondition(cond, values));
589
- } else {
590
- let andConditions = condition.split(/\s*&&\s*/);
591
- return andConditions.every(cond => checkCondition(cond, values));
592
- }
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
+ }
593
741
  }
594
742
 
595
743
  function checkCondition(condition, value) {
596
- let isNegated = condition.startsWith('!');
597
- if (isNegated) {
598
- condition = condition.substring(1);
599
- }
600
- const operatorMatch = condition.match(/(<=|>=|<|>)(.+)/);
601
- if (operatorMatch)
602
- condition = operatorMatch[2].trim()
603
-
604
- let parse = true
605
- if (condition === 'true') {
606
- return !!value[0];
607
- } else if (condition === 'false') {
608
- return !value[0];
609
- } else if (condition === '[]' && typeof value[0] === 'string') {
610
- parse = false
611
- }
612
-
613
-
614
-
615
- // TODO: why parse updated conditin to boolean false
616
- if (parse && condition !== 'false')
617
- condition = parseCondition(condition);
618
-
619
- let result;
620
-
621
- if (condition === '$template') {
622
- result = (/{{\s*([\w\W]+)\s*}}/g.test(value[0]))
623
- } else if (Array.isArray(value) && !(typeof condition === 'object')) {
624
- if (operatorMatch) {
625
- condition = parseFloat(condition)
626
- result = value.some(v => {
627
- v = parseFloat(v)
628
- switch (operatorMatch[1]) {
629
- case '<': return v < condition;
630
- case '>': return v > condition;
631
- case '<=': return v <= condition;
632
- case '>=': return v >= condition;
633
- default: return false;
634
- }
635
- });
636
- } else
637
- result = value.includes(condition);
638
- } else if (Array.isArray(value)) {
639
- // TODO: handle comparing array to array, vs querying the ayya items for a match
640
- result = queryData(value, condition)
641
- } else if (typeof value === 'object' && value !== null && typeof parsedCond === 'object') {
642
- result = queryData(value, condition)
643
- } else {
644
- result = value === condition;
645
- }
646
-
647
- result = isNegated ? !result : result;
648
- return result
649
-
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;
650
808
  }
651
809
 
652
- function parseCondition(condition) {
653
- try {
654
- // Attempt to parse the condition as JSON
655
- let parsedJson = JSON.parse(condition);
656
- return parsedJson;
657
- } catch (e) {
658
- // If JSON parsing fails, check if the condition is a number
659
- return !isNaN(condition) && condition.trim() !== '' ? Number(condition) : condition;
660
- }
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;
661
823
  }
662
824
 
663
825
  CoCreateEvents.init();
664
826
 
665
- export default CoCreateEvents;
827
+ export default CoCreateEvents;