@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.
- package/CHANGELOG.md +16 -0
- package/package.json +1 -1
- 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
|
|
24
|
-
import action from
|
|
25
|
-
import observer from
|
|
26
|
-
import
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
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
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
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
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
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
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
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
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
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;
|