@cocreate/elements 1.33.0 → 1.33.2

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 +17 -0
  2. package/package.json +1 -1
  3. package/src/index.js +109 -62
package/CHANGELOG.md CHANGED
@@ -1,3 +1,20 @@
1
+ ## [1.33.2](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.33.1...v1.33.2) (2024-01-30)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * !Data.object ([a793613](https://github.com/CoCreate-app/CoCreate-elements/commit/a7936131297f734e39f8a213c09133014f976a5e))
7
+ * data[type]replaced by sortedData if length > 0 ([ff399cb](https://github.com/CoCreate-app/CoCreate-elements/commit/ff399cb6e8ebb77a0a986ed7bc7fe67fc01c2a98))
8
+
9
+ ## [1.33.1](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.33.0...v1.33.1) (2024-01-17)
10
+
11
+
12
+ ### Bug Fixes
13
+
14
+ * improved filter handling ([a207811](https://github.com/CoCreate-app/CoCreate-elements/commit/a207811f82d0ce08d0026e64e4fff8dbaa358327))
15
+ * relocated render-json to setData ([55b65cf](https://github.com/CoCreate-app/CoCreate-elements/commit/55b65cfd3173672a8f2d67c8d6baf681e0ff6c72))
16
+ * update to support new query system ([e005926](https://github.com/CoCreate-app/CoCreate-elements/commit/e005926fd2a9da6f50cb8951dff38cdf35e715a4))
17
+
1
18
  # [1.33.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.32.0...v1.33.0) (2024-01-08)
2
19
 
3
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/elements",
3
- "version": "1.33.0",
3
+ "version": "1.33.2",
4
4
  "description": "Fetch content into any html element by using html5 attributes to query data stored in CoCreate headless CMS. If data is changed it will instantly reflect in html tags as changes are broadcasted in realtime",
5
5
  "keywords": [
6
6
  "elements",
package/src/index.js CHANGED
@@ -81,34 +81,6 @@ async function init(element) {
81
81
  }
82
82
 
83
83
  async function initElement(el) {
84
- if (el.hasAttribute('render-json')) {
85
- // TODO find the json template in the text or attributes
86
- // if found add the node as an element, if the element has crud attributes also add as the element
87
- for (let attribute of el.attributes) {
88
- if (attribute.match(/{{(.*?)}}/)) {
89
- let value = renderValue(attribute, undefined, attribute.value)
90
- if (value)
91
- attribute.value = value
92
- }
93
- }
94
- if (el.innerHTML.match(/{{(.*?)}}/)) {
95
- let value = renderValue(el, undefined, el.innerHTML)
96
- if (value)
97
- attribute.value = value
98
- }
99
-
100
- // if (el.textContent.match(/{{(.*?)}}/)) {
101
- // let value = renderValue(el, undefined, el.textContent)
102
- // if (value)
103
- // attribute.value = value
104
- // }
105
-
106
- // let Data = JSON.parse(match[1]);
107
- // Data.method = object.read
108
- // CRUD.send(Data)
109
-
110
- }
111
-
112
84
  // if (el.closest('.template')) return;
113
85
 
114
86
  let data = getObject(el);
@@ -135,7 +107,7 @@ async function initElement(el) {
135
107
  // if (el.getFilter || attributes.some(attr => el.hasAttribute(attr))) {
136
108
  await filter.init()
137
109
  if (el.getFilter) {
138
- data.$filter = el.getFilter();
110
+ data.$filter = await el.getFilter();
139
111
 
140
112
  el.setFilter = (filter) => {
141
113
  data.$filter = filter
@@ -286,6 +258,31 @@ async function read(element, data, dataKey) {
286
258
  }, 500);
287
259
  }
288
260
 
261
+ function get$in(query, mergedValues = []) {
262
+ for (let key of Object.keys(query)) {
263
+ if (key === '$and' || key === '$or' || key === '$nor') {
264
+ for (let i = 0; i < query[key].length; i++) {
265
+ sort$in(query[key][i], mergedValues)
266
+ }
267
+ } else if (typeof query[key] === 'object' && !Array.isArray(query[key])) {
268
+ if (query[key].$in)
269
+ mergedValues = [...mergedValues, ...query[key].$in];
270
+ }
271
+ }
272
+ return mergedValues
273
+ }
274
+
275
+ function sort$in(data, query) {
276
+ let mergedValues = get$in(query);
277
+
278
+ if (mergedValues.length) {
279
+ const sorted = mergedValues
280
+ .map(name => data.find(career => career.name === name))
281
+ .filter(career => career !== undefined);
282
+ return sorted
283
+ }
284
+ }
285
+
289
286
  async function setData(element, data) {
290
287
  if (!element) {
291
288
  element = getDataElements(data)
@@ -301,28 +298,45 @@ async function setData(element, data) {
301
298
  type = 'object'
302
299
 
303
300
  if (data.$filter && data.$filter.query) {
304
- let mergedValues = [];
305
-
306
- data.$filter.query.forEach(obj => {
307
- if (obj.operator === '$in') {
308
- mergedValues = [...mergedValues, ...obj.value];
309
- }
310
- });
311
-
312
- if (mergedValues.length) {
313
- const sortedType = mergedValues
314
- .map(name => data[type].find(career => career.name === name))
315
- .filter(career => career !== undefined);
316
- data[type] = sortedType
317
- }
301
+ let sortedData = sort$in(data[type], data.$filter.query)
302
+ if (sortedData && sortedData.length)
303
+ data[type] = sortedData
318
304
  }
319
305
 
320
306
  for (let el of element) {
321
307
  // if rendered in server side skip
322
308
  if (el.hasAttribute('rendered'))
323
309
  return el.removeAttribute('rendered');
310
+ if (el.hasAttribute('render-json')) {
311
+ // TODO find the json template in the text or attributes
312
+ // if found add the node as an element, if the element has crud attributes also add as the element
313
+ // for (let attribute of el.attributes) {
314
+ // if (attribute.value.match(/{{(.*?)}}/)) {
315
+ // let value = await renderValue(attribute, data[type][0], attribute.value)
316
+ // if (value)
317
+ // attribute.value = value
318
+ // }
319
+ // }
320
+ // if (el.innerHTML.match(/{{(.*?)}}/)) {
321
+ // let value = renderValue(el, data[type], el.innerHTML)
322
+ // if (value)
323
+ // el.innerHTML = value
324
+ // }
325
+
326
+ // if (el.textContent.match(/{{(.*?)}}/)) {
327
+ // let value = renderValue(el, undefined, el.textContent)
328
+ // if (value)
329
+ // attribute.value = value
330
+ // }
331
+
332
+ // let Data = JSON.parse(match[1]);
333
+ // Data.method = object.read
334
+ // CRUD.send(Data)
335
+ console.log('render-json', '')
324
336
 
325
- if (!data[type])
337
+ }
338
+
339
+ if (!data[type] || !data[type].length)
326
340
  continue;
327
341
 
328
342
  let action = el.getAttribute('actions')
@@ -345,8 +359,17 @@ async function setData(element, data) {
345
359
  }
346
360
 
347
361
  async function filterData(element, data, type, key) {
348
- if (key) {
362
+ let operator = '', value
363
+
364
+ if (key && key !== '$length') {
349
365
  if (!data || !type) return
366
+
367
+ if (key.startsWith('$')) {
368
+ operator = key.split('.')[0] || ''
369
+ }
370
+
371
+ let property = key.replace(operator + '.', '')
372
+
350
373
  if (Array.isArray(data[type])) {
351
374
  let Data = []
352
375
  for (let doc of data[type]) {
@@ -356,27 +379,37 @@ async function filterData(element, data, type, key) {
356
379
  if (_id && doc._id !== _id)
357
380
  return
358
381
  }
359
- if (doc[key]) {
360
- if (Array.isArray(doc[key]))
361
- Data.push(...doc[key])
382
+ if (doc[property]) {
383
+ if (Array.isArray(doc[property]))
384
+ Data.push(...doc[property])
362
385
  else
363
- Data.push(doc[key])
386
+ Data.push(doc[property])
364
387
  } else
365
388
  return
366
389
  }
367
390
  // if (Data.length === 1) {
368
- // data = { [key]: Data[0] }
391
+ // data = { [property]: Data[0] }
369
392
  // } else
370
- data = Data
393
+ if (!operator)
394
+ data = Data
395
+ else if (operator === '$sum') {
396
+ value = Data.reduce((accumulator, currentValue) => {
397
+ return accumulator + currentValue;
398
+ }, 0);
399
+ }
371
400
  } else {
372
- data = { [key]: data[type][key] }
401
+ data = { [property]: data[type][property] }
373
402
  }
374
403
  }
375
404
 
376
- if (element.getFilter && data.method && !data.method.endsWith('.read'))
405
+ if (operator) {
406
+ element.setValue(value);
407
+ } else if (element.getFilter && data.method && !data.method.endsWith('.read'))
377
408
  await checkFilters(element, data, type)
378
409
  else if (element.renderValue) {
379
410
  await element.renderValue(data);
411
+ } else if (key === '$length') {
412
+ element.setValue(data[type].length);
380
413
  } else if (data)
381
414
  element.setValue(data)
382
415
 
@@ -388,6 +421,7 @@ async function filterData(element, data, type, key) {
388
421
 
389
422
  if (!data[type] || !Array.isArray(data[type]) || !data[type].length)
390
423
  return
424
+
391
425
  const evt = new CustomEvent('fetchedData', { bubbles: true });
392
426
  element.dispatchEvent(evt);
393
427
  }
@@ -403,7 +437,7 @@ async function checkFilters(element, data, type) {
403
437
  } else
404
438
  newData = data
405
439
 
406
- let filter = element.getFilter()
440
+ let filter = await element.getFilter()
407
441
  if (filter && filter.query) {
408
442
  for (let i = 0; i < newData.length; i++) {
409
443
  let isMatch = queryData(newData[i], filter.query)
@@ -608,6 +642,8 @@ async function getData(form) {
608
642
  if (Data.type === 'object') {
609
643
  if (typeof Data.object === 'string')
610
644
  Data.object = { _id: Data.object }
645
+ else if (!Data.object)
646
+ Data.object = {}
611
647
 
612
648
  if (Data.key)
613
649
  Data.object[Data.key] = value
@@ -1068,14 +1104,8 @@ async function dndCrudData(element, parent, operator) {
1068
1104
 
1069
1105
  function dndNewData(element, data) {
1070
1106
  let Data = {}
1071
- let query = data.$filter.query
1072
- if (query && query.length) {
1073
- for (let i = 0; i < query.length; i++) {
1074
- if (query.operator === "$eq")
1075
- Data[query.key] = query.value
1076
- if (query.operator === "$ne")
1077
- Data[query.key] = query.value
1078
- }
1107
+ if (data.$filter.query) {
1108
+ dndNewDataUpdate(Data, data.$filter.query)
1079
1109
  }
1080
1110
 
1081
1111
  let sortName, sortDirection
@@ -1105,6 +1135,23 @@ function dndNewData(element, data) {
1105
1135
 
1106
1136
  }
1107
1137
 
1138
+ function dndNewDataUpdate(Data, query) {
1139
+ for (let key of Object.keys(query)) {
1140
+ if (key === '$and' || key === '$or' || key === '$nor') {
1141
+ for (let i = 0; i < query[key].length; i++) {
1142
+ dndNewDataUpdate(Data, query[key][i])
1143
+ }
1144
+ } else if (typeof query[key] === 'object' && !Array.isArray(query[key])) {
1145
+ if (query[key].$eq)
1146
+ Data[key] = query[key].$eq
1147
+ if (query[key].$ne)
1148
+ Data[key] = query[key].$ne
1149
+ } else {
1150
+ Data[key] = query[key]
1151
+ }
1152
+ }
1153
+ }
1154
+
1108
1155
  function dndCrudSend(data, crudType) {
1109
1156
  if (CRUD) {
1110
1157
  let action = data.type;