@cocreate/events 1.17.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 +19 -0
  2. package/package.json +1 -1
  3. package/src/index.js +133 -14
package/CHANGELOG.md CHANGED
@@ -1,3 +1,22 @@
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
+
13
+ # [1.18.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.17.0...v1.18.0) (2024-01-17)
14
+
15
+
16
+ ### Features
17
+
18
+ * handle localstorage-set attribute ([663ce65](https://github.com/CoCreate-app/CoCreate-events/commit/663ce65227b3223a35576fd8618652ab21f344ca))
19
+
1
20
  # [1.17.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.16.0...v1.17.0) (2023-11-25)
2
21
 
3
22
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/events",
3
- "version": "1.17.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,16 +224,26 @@ 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') {
219
234
  let key = element.getAttribute('localstorage-get')
220
- if (key)
235
+ if (key) {
221
236
  values = localStorage.getItem(key)
237
+ } else if (key = element.getAttribute('localstorage-set')) {
238
+ values = await element.getValue()
239
+ if (values)
240
+ localStorage.setItem(key, values)
241
+ }
222
242
  if (!key || !values)
223
243
  return
224
- } else
225
- 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
+ }
226
247
  if (values)
227
248
  values = values.split(',');
228
249
  else {
@@ -231,13 +252,33 @@ const CoCreateEvents = {
231
252
  values = [values]
232
253
  }
233
254
 
234
- if (!values || values.length == 0)
255
+ if (targetAttribute && !values || values.length === 0)
235
256
  return;
236
257
 
237
- let targetAttribute = element.getAttribute(`${prefix}-attribute`);
238
- // targetAttribute = checkMediaQueries(targetAttribute)
239
- // if (targetAttribute === false)
240
- // 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
+ }
241
282
 
242
283
  let targetText = element.getAttribute(`${prefix}-text`);
243
284
  let targetHtml = element.getAttribute(`${prefix}-html`);
@@ -269,7 +310,16 @@ const CoCreateEvents = {
269
310
  });
270
311
  }
271
312
 
272
- 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
+ });
273
323
 
274
324
  let targetElements = queryElements({ element, prefix });
275
325
  if (targetElements === false)
@@ -334,7 +384,6 @@ const CoCreateEvents = {
334
384
  let newValue = this.__getNextValue(values, oldValue);
335
385
  if (deactivate)
336
386
  newValue = ""
337
-
338
387
  if (attrName === 'class') {
339
388
  if (oldValue) {
340
389
  element.classList.remove(oldValue);
@@ -348,9 +397,19 @@ const CoCreateEvents = {
348
397
  }
349
398
  } else if (attrName === 'value') {
350
399
  element.setValue(newValue);
351
- } else {
400
+ } else if (['$click', '$focus', '$blur'].includes(attrName)) {
401
+ element[attrName.substring(1)]()
402
+ } else if (attrName) {
352
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
+
353
411
  }
412
+
354
413
  }
355
414
  }
356
415
  },
@@ -466,6 +525,66 @@ function replaceKey(prefix, element, key, values) {
466
525
 
467
526
  }
468
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
+
469
588
  CoCreateEvents.init();
470
589
 
471
590
  export default CoCreateEvents;