@cocreate/events 1.18.0 → 1.19.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 +12 -0
  2. package/package.json +1 -1
  3. package/src/index.js +127 -13
package/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # [1.19.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.18.0...v1.19.0) (2024-01-30)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * prefix-if-value to containg resize observer value ([fc561e7](https://github.com/CoCreate-app/CoCreate-events/commit/fc561e73cbcbbb28d0dc8126ce124742f48e9a4b))
7
+
8
+
9
+ ### Features
10
+
11
+ * handle resize observer event and if and else attributes conditions ([233e370](https://github.com/CoCreate-app/CoCreate-events/commit/233e3700741f0f4792559b2ff6ecd6719a5321c0))
12
+
1
13
  # [1.18.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.17.0...v1.18.0) (2024-01-17)
2
14
 
3
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/events",
3
- "version": "1.18.0",
3
+ "version": "1.19.0",
4
4
  "description": "A simple events component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "events",
package/src/index.js CHANGED
@@ -20,13 +20,13 @@
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, checkMediaQueries } from '@cocreate/utils';
23
+ import { queryElements, checkMediaQueries, queryData } from '@cocreate/utils';
24
24
  import action from '@cocreate/actions';
25
25
  import observer from '@cocreate/observer';
26
26
  import '@cocreate/element-prototype';
27
27
 
28
28
  const CoCreateEvents = {
29
-
29
+ elements2: new Map(),
30
30
  init: function (prefix, events) {
31
31
  if (prefix && events)
32
32
  this.initPrefix(prefix, events);
@@ -41,6 +41,7 @@ const CoCreateEvents = {
41
41
  this.initPrefix('selected', ['click']);
42
42
  this.initPrefix('onload', ['onload']);
43
43
  this.initPrefix('observe', ['observer']);
44
+ this.initPrefix('resize', ['onload', 'resize']);
44
45
  this.initPrefix('localstorage', ['onload', 'observer']);
45
46
  }
46
47
 
@@ -160,8 +161,18 @@ const CoCreateEvents = {
160
161
  });
161
162
  }
162
163
 
164
+ if (events.includes('resize')) {
165
+ const resizeObserver = new ResizeObserver((entries) => {
166
+ const lastEntry = entries[entries.length - 1];
167
+ const { width, height } = lastEntry.contentRect;
168
+ el.setAttribute(`${prefix}-if-value`, width + 'px')
169
+ self.__updateElements(el, prefix)
170
+ });
171
+ resizeObserver.observe(el);
172
+ }
173
+
163
174
  for (let i = 0; i < events.length; i++) {
164
- if (events[i] !== 'onload' && events[i] !== 'observer') {
175
+ if (events[i] !== 'onload' && events[i] !== 'observer' && events[i] !== 'resize') {
165
176
  el.removeEventListener(events[i], eventFunction)
166
177
  el.addEventListener(events[i], eventFunction);
167
178
  }
@@ -213,6 +224,10 @@ const CoCreateEvents = {
213
224
  __updateElements: async function (element, prefix, target) {
214
225
  const self = this;
215
226
  // TODO: support empty value when prefix-attribute defined, add and remove the attribute
227
+ let targetAttribute = element.getAttribute(`${prefix}-attribute`);
228
+ // targetAttribute = checkMediaQueries(targetAttribute)
229
+ // if (targetAttribute === false)
230
+ // return
216
231
 
217
232
  let values
218
233
  if (prefix === 'localstorage') {
@@ -226,8 +241,9 @@ const CoCreateEvents = {
226
241
  }
227
242
  if (!key || !values)
228
243
  return
229
- } else
230
- values = element.getAttribute(`${prefix}-value`) || element.getAttribute(prefix);
244
+ } else {
245
+ values = element.getAttribute(`${prefix}-value`) || element.getAttribute(`${prefix}-if-value`) || element.getAttribute(prefix);
246
+ }
231
247
  if (values)
232
248
  values = values.split(',');
233
249
  else {
@@ -236,13 +252,33 @@ const CoCreateEvents = {
236
252
  values = [values]
237
253
  }
238
254
 
239
- if (!values || values.length == 0)
255
+ if (targetAttribute && !values || values.length === 0)
240
256
  return;
241
257
 
242
- let targetAttribute = element.getAttribute(`${prefix}-attribute`);
243
- // targetAttribute = checkMediaQueries(targetAttribute)
244
- // if (targetAttribute === false)
245
- // return
258
+ let ifCondition = element.getAttribute(`${prefix}-if`);
259
+ let elseCondition = element.getAttribute(`${prefix}-else`);
260
+
261
+ let ifValue = element.getAttribute(`${prefix}-if-value`)
262
+ if (ifValue || ifValue === "")
263
+ ifValue = [ifValue]
264
+ else
265
+ ifValue = values
266
+
267
+ //TODO: improved resize toggling of values
268
+ // let hasCondition = this.elements2.get(element)
269
+ if (ifCondition && evaluateCondition(ifCondition, ifValue)) {
270
+ // if (hasCondition && hasCondition.condition === ifCondition) {
271
+ // return
272
+ // } else
273
+ // this.elements2.set(element, { condition: ifCondition })
274
+ } else if (elseCondition && evaluateCondition(elseCondition, ifValue)) {
275
+ // if (hasCondition && hasCondition.condition === elseCondition) {
276
+ // return
277
+ // } else
278
+ // this.elements2.set(element, { condition: elseCondition })
279
+ } else if (ifCondition || elseCondition) {
280
+ return
281
+ }
246
282
 
247
283
  let targetText = element.getAttribute(`${prefix}-text`);
248
284
  let targetHtml = element.getAttribute(`${prefix}-html`);
@@ -274,7 +310,16 @@ const CoCreateEvents = {
274
310
  });
275
311
  }
276
312
 
277
- values = values.map(x => x.trim());
313
+ // values = values.map(x => x.trim());
314
+
315
+ values = values.map(x => {
316
+ x = x.trim(); // Update x with the trimmed value
317
+ let prop = element.getAttribute(`${prefix}-property`);
318
+ if (prop) {
319
+ x = `${prop}:${x}`; // Update x with prop if it exists
320
+ }
321
+ return x; // Return the updated x
322
+ });
278
323
 
279
324
  let targetElements = queryElements({ element, prefix });
280
325
  if (targetElements === false)
@@ -339,7 +384,6 @@ const CoCreateEvents = {
339
384
  let newValue = this.__getNextValue(values, oldValue);
340
385
  if (deactivate)
341
386
  newValue = ""
342
-
343
387
  if (attrName === 'class') {
344
388
  if (oldValue) {
345
389
  element.classList.remove(oldValue);
@@ -353,9 +397,19 @@ const CoCreateEvents = {
353
397
  }
354
398
  } else if (attrName === 'value') {
355
399
  element.setValue(newValue);
356
- } else {
400
+ } else if (['$click', '$focus', '$blur'].includes(attrName)) {
401
+ element[attrName.substring(1)]()
402
+ } else if (attrName) {
357
403
  element.setAttribute(attrName, newValue);
404
+ } else {
405
+ if (!attrName && ['click', 'focus', 'blur'].includes(newValue)) {
406
+ element[newValue]()
407
+ } else if (['click', 'focus', 'blur'].includes(attrName)) {
408
+ element[attrName]()
409
+ }
410
+
358
411
  }
412
+
359
413
  }
360
414
  }
361
415
  },
@@ -471,6 +525,66 @@ function replaceKey(prefix, element, key, values) {
471
525
 
472
526
  }
473
527
 
528
+ function evaluateCondition(condition, values) {
529
+ let orConditions = condition.split(/\s*\|\|\s*/);
530
+
531
+ if (orConditions.length > 1) {
532
+ return orConditions.some(cond => checkCondition(cond, values));
533
+ } else {
534
+ let andConditions = condition.split(/\s*&&\s*/);
535
+ return andConditions.every(cond => checkCondition(cond, values));
536
+ }
537
+ }
538
+
539
+ function checkCondition(condition, value) {
540
+ let isNegated = condition.startsWith('!');
541
+ if (isNegated) {
542
+ condition = condition.substring(1);
543
+ }
544
+ const operatorMatch = condition.match(/(<=|>=|<|>)(.+)/);
545
+ if (operatorMatch)
546
+ condition = operatorMatch[2].trim()
547
+ condition = parseCondition(condition);
548
+ let result;
549
+
550
+ if (Array.isArray(value) && !(typeof condition === 'object')) {
551
+ if (operatorMatch) {
552
+ condition = parseFloat(condition)
553
+ result = value.some(v => {
554
+ v = parseFloat(v)
555
+ switch (operatorMatch[1]) {
556
+ case '<': return v < condition;
557
+ case '>': return v > condition;
558
+ case '<=': return v <= condition;
559
+ case '>=': return v >= condition;
560
+ default: return false;
561
+ }
562
+ });
563
+ } else
564
+ result = value.includes(condition);
565
+ } else if (Array.isArray(value)) {
566
+ // TODO: handle comparing array to array, vs querying the ayya items for a match
567
+ result = queryData(value, condition)
568
+ } else if (typeof value === 'object' && value !== null && typeof parsedCond === 'object') {
569
+ result = queryData(value, condition)
570
+ } else {
571
+ result = value === condition;
572
+ }
573
+
574
+ return isNegated ? !result : result;
575
+ }
576
+
577
+ function parseCondition(condition) {
578
+ try {
579
+ // Attempt to parse the condition as JSON
580
+ let parsedJson = JSON.parse(condition);
581
+ return parsedJson;
582
+ } catch (e) {
583
+ // If JSON parsing fails, check if the condition is a number
584
+ return !isNaN(condition) && condition.trim() !== '' ? Number(condition) : condition;
585
+ }
586
+ }
587
+
474
588
  CoCreateEvents.init();
475
589
 
476
590
  export default CoCreateEvents;