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