@cocreate/elements 1.33.0 → 1.33.1
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 +9 -0
- package/package.json +1 -1
- package/src/index.js +106 -61
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
## [1.33.1](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.33.0...v1.33.1) (2024-01-17)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* improved filter handling ([a207811](https://github.com/CoCreate-app/CoCreate-elements/commit/a207811f82d0ce08d0026e64e4fff8dbaa358327))
|
|
7
|
+
* relocated render-json to setData ([55b65cf](https://github.com/CoCreate-app/CoCreate-elements/commit/55b65cfd3173672a8f2d67c8d6baf681e0ff6c72))
|
|
8
|
+
* update to support new query system ([e005926](https://github.com/CoCreate-app/CoCreate-elements/commit/e005926fd2a9da6f50cb8951dff38cdf35e715a4))
|
|
9
|
+
|
|
1
10
|
# [1.33.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.32.0...v1.33.0) (2024-01-08)
|
|
2
11
|
|
|
3
12
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/elements",
|
|
3
|
-
"version": "1.33.
|
|
3
|
+
"version": "1.33.1",
|
|
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,26 +298,43 @@ async function setData(element, data) {
|
|
|
301
298
|
type = 'object'
|
|
302
299
|
|
|
303
300
|
if (data.$filter && data.$filter.query) {
|
|
304
|
-
let
|
|
305
|
-
|
|
306
|
-
|
|
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)
|
|
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', '')
|
|
336
|
+
|
|
337
|
+
}
|
|
324
338
|
|
|
325
339
|
if (!data[type])
|
|
326
340
|
continue;
|
|
@@ -345,8 +359,17 @@ async function setData(element, data) {
|
|
|
345
359
|
}
|
|
346
360
|
|
|
347
361
|
async function filterData(element, data, type, key) {
|
|
348
|
-
|
|
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[
|
|
360
|
-
if (Array.isArray(doc[
|
|
361
|
-
Data.push(...doc[
|
|
382
|
+
if (doc[property]) {
|
|
383
|
+
if (Array.isArray(doc[property]))
|
|
384
|
+
Data.push(...doc[property])
|
|
362
385
|
else
|
|
363
|
-
Data.push(doc[
|
|
386
|
+
Data.push(doc[property])
|
|
364
387
|
} else
|
|
365
388
|
return
|
|
366
389
|
}
|
|
367
390
|
// if (Data.length === 1) {
|
|
368
|
-
// data = { [
|
|
391
|
+
// data = { [property]: Data[0] }
|
|
369
392
|
// } else
|
|
370
|
-
|
|
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 = { [
|
|
401
|
+
data = { [property]: data[type][property] }
|
|
373
402
|
}
|
|
374
403
|
}
|
|
375
404
|
|
|
376
|
-
if (
|
|
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)
|
|
@@ -1068,14 +1102,8 @@ async function dndCrudData(element, parent, operator) {
|
|
|
1068
1102
|
|
|
1069
1103
|
function dndNewData(element, data) {
|
|
1070
1104
|
let Data = {}
|
|
1071
|
-
|
|
1072
|
-
|
|
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
|
-
}
|
|
1105
|
+
if (data.$filter.query) {
|
|
1106
|
+
dndNewDataUpdate(Data, data.$filter.query)
|
|
1079
1107
|
}
|
|
1080
1108
|
|
|
1081
1109
|
let sortName, sortDirection
|
|
@@ -1105,6 +1133,23 @@ function dndNewData(element, data) {
|
|
|
1105
1133
|
|
|
1106
1134
|
}
|
|
1107
1135
|
|
|
1136
|
+
function dndNewDataUpdate(Data, query) {
|
|
1137
|
+
for (let key of Object.keys(query)) {
|
|
1138
|
+
if (key === '$and' || key === '$or' || key === '$nor') {
|
|
1139
|
+
for (let i = 0; i < query[key].length; i++) {
|
|
1140
|
+
dndNewDataUpdate(Data, query[key][i])
|
|
1141
|
+
}
|
|
1142
|
+
} else if (typeof query[key] === 'object' && !Array.isArray(query[key])) {
|
|
1143
|
+
if (query[key].$eq)
|
|
1144
|
+
Data[key] = query[key].$eq
|
|
1145
|
+
if (query[key].$ne)
|
|
1146
|
+
Data[key] = query[key].$ne
|
|
1147
|
+
} else {
|
|
1148
|
+
Data[key] = query[key]
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1108
1153
|
function dndCrudSend(data, crudType) {
|
|
1109
1154
|
if (CRUD) {
|
|
1110
1155
|
let action = data.type;
|