@cocreate/elements 1.38.1 → 1.40.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 CHANGED
@@ -1,3 +1,39 @@
1
+ # [1.40.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.39.0...v1.40.0) (2024-12-14)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * allow empty strings and array values ([948a5ec](https://github.com/CoCreate-app/CoCreate-elements/commit/948a5ec60de14dc05df4650cfdf5ff3eae8f4bcd))
7
+ * delete element[i].hasRead in remove function ([6cee7e9](https://github.com/CoCreate-app/CoCreate-elements/commit/6cee7e9ad2ed817c59fb7fe31fbeccd2746d8caf))
8
+ * filter.init('el') ([804c9ad](https://github.com/CoCreate-app/CoCreate-elements/commit/804c9ad2d8c3cb4ad2ceeaf8c11c363268a139ce))
9
+ * getValueFromObject inorder to get values using dotNotation ([b66e466](https://github.com/CoCreate-app/CoCreate-elements/commit/b66e466b3b03ffabc0f65b840c495fc174967075))
10
+ * handling crud updates when render element used ([0216396](https://github.com/CoCreate-app/CoCreate-elements/commit/0216396988d394005282fc85e881054d45d93cfc))
11
+ * moved observer out of loop as only needs to be declared once. ([4941c83](https://github.com/CoCreate-app/CoCreate-elements/commit/4941c837bcbb642f4266932e2e2e99bc4848a27c))
12
+
13
+
14
+ ### Features
15
+
16
+ * attribute observer organization_id and host ([ba4eae9](https://github.com/CoCreate-app/CoCreate-elements/commit/ba4eae9d41cba3bf906c00b484a163ab36d373f7))
17
+ * Attribute refrence to define whether the crud refrence data will be excluded ([b0d3243](https://github.com/CoCreate-app/CoCreate-elements/commit/b0d3243514a910292677e588c0654888b45070eb))
18
+
19
+ # [1.39.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.38.1...v1.39.0) (2024-12-14)
20
+
21
+
22
+ ### Bug Fixes
23
+
24
+ * allow empty strings and array values ([03ccb89](https://github.com/CoCreate-app/CoCreate-elements/commit/03ccb89cc10262a981c3e66ee279be8a49a58b4b))
25
+ * delete element[i].hasRead in remove function ([e549c3d](https://github.com/CoCreate-app/CoCreate-elements/commit/e549c3d70b7b1701e425e1eadb828df681b619c4))
26
+ * filter.init('el') ([571b179](https://github.com/CoCreate-app/CoCreate-elements/commit/571b179b99b6724988b2a65002bc53d5ba005d02))
27
+ * getValueFromObject inorder to get values using dotNotation ([a101064](https://github.com/CoCreate-app/CoCreate-elements/commit/a101064413587e0a95b6423211f3084ad713e76f))
28
+ * handling crud updates when render element used ([88c5781](https://github.com/CoCreate-app/CoCreate-elements/commit/88c5781e7ce02a672e84bed1bf993bbfcd29f83e))
29
+ * moved observer out of loop as only needs to be declared once. ([4f9c63c](https://github.com/CoCreate-app/CoCreate-elements/commit/4f9c63cfdd3d4c6f065e9604535945420ab32b09))
30
+
31
+
32
+ ### Features
33
+
34
+ * attribute observer organization_id and host ([28a9175](https://github.com/CoCreate-app/CoCreate-elements/commit/28a9175aa27f5c66205b350e6a4c14b19f014960))
35
+ * Attribute refrence to define whether the crud refrence data will be excluded ([fb1c35d](https://github.com/CoCreate-app/CoCreate-elements/commit/fb1c35de0ebb2cc484d02f367f6d542266f4b8ed))
36
+
1
37
  ## [1.38.1](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.38.0...v1.38.1) (2024-12-09)
2
38
 
3
39
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/elements",
3
- "version": "1.38.1",
3
+ "version": "1.40.0",
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/form.js CHANGED
@@ -17,17 +17,6 @@ function init(elements) {
17
17
  else if (!Array.isArray(elements)) elements = [elements];
18
18
 
19
19
  for (let element of elements) {
20
- Observer.init({
21
- name: "CoCreateFormElements",
22
- observe: ["addedNodes"],
23
- selector:
24
- "[storage], [database], [array], [index], [object], [key]",
25
- callback: function (mutation) {
26
- if (element == mutation.target.form)
27
- setAttribute(element, [mutation.target]);
28
- }
29
- });
30
-
31
20
  runObjectId(element);
32
21
  setAttribute(element);
33
22
  disableAutoFill(element);
@@ -43,6 +32,8 @@ function init(elements) {
43
32
  /**
44
33
  * @param form
45
34
  */
35
+ // TODO: runObjectId could potentially be removed and handled by element-prototype getAttribute $object_id
36
+ // Somthing to consider is that $object_id is not replaced by an _id causing getAttribute to return a new _id on each get
46
37
  function runObjectId(form) {
47
38
  let elements = Array.from(form.querySelectorAll("[object='ObjectId()']"));
48
39
 
@@ -62,7 +53,7 @@ function runObjectId(form) {
62
53
  function setAttribute(form, elements) {
63
54
  if (!elements)
64
55
  elements = form.querySelectorAll(
65
- "[storage], [database], [array], [index], [object], [key]"
56
+ "[organization_id], [host], [storage], [database], [array], [index], [object], [key]"
66
57
  );
67
58
 
68
59
  for (let attribute of form.attributes) {
@@ -184,6 +175,8 @@ Observer.init({
184
175
  name: "CoCreateForm",
185
176
  observe: ["attributes"],
186
177
  attributeName: getAttributeNames([
178
+ "organization_id",
179
+ "host",
187
180
  "storage",
188
181
  "database",
189
182
  "array",
@@ -195,6 +188,17 @@ Observer.init({
195
188
  mutation.target.tagName === "FORM" && setAttribute(mutation.target)
196
189
  });
197
190
 
191
+ Observer.init({
192
+ name: "CoCreateFormElements",
193
+ observe: ["addedNodes"],
194
+ selector:
195
+ "[organization_id], [host], [storage], [database], [array], [index], [object], [key]",
196
+ callback: function (mutation) {
197
+ if (mutation.target.form)
198
+ setAttribute(mutation.target.form, [mutation.target]);
199
+ }
200
+ });
201
+
198
202
  Action.init({
199
203
  name: "reset",
200
204
  endEvent: "reset",
package/src/index.js CHANGED
@@ -32,7 +32,8 @@ import {
32
32
  sortData,
33
33
  getAttributes,
34
34
  getAttributeNames,
35
- checkValue
35
+ checkValue,
36
+ getValueFromObject
36
37
  } from "@cocreate/utils";
37
38
  import filter from "@cocreate/filter";
38
39
  import { render, renderValue, renderedNodes } from "@cocreate/render";
@@ -150,7 +151,8 @@ async function initElement(el) {
150
151
  // let attributes = ['filter-key', 'filter-search', 'filter-sort-key', 'filter-on', 'filter-limit']
151
152
  // if (el.getFilter || attributes.some(attr => el.hasAttribute(attr))) {
152
153
 
153
- await filter.init();
154
+ await filter.init(el);
155
+
154
156
  if (el.getFilter) {
155
157
  data.$filter = await el.getFilter();
156
158
 
@@ -272,7 +274,7 @@ async function read(element, data, dataKey) {
272
274
  el.hasRead = true;
273
275
  delay.elements.set(el, true);
274
276
  } else {
275
- // console.log('read skipped', el)
277
+ // console.log("read skipped", el);
276
278
  }
277
279
  }
278
280
 
@@ -392,6 +394,7 @@ async function setData(element, data) {
392
394
  console.log("render-json", "");
393
395
  }
394
396
 
397
+ let reference = el.getAttribute("reference");
395
398
  let action = el.getAttribute("actions");
396
399
  if (action && ["database", "array", "object", "key"].includes(action))
397
400
  continue;
@@ -400,7 +403,10 @@ async function setData(element, data) {
400
403
  if (el.getFilter || el.renderValue)
401
404
  await filterData(el, data, type, key);
402
405
  else {
403
- if (!data[type] || !data[type].length) continue;
406
+ if (!data[type] || !data[type].length) {
407
+ if (el.hasAttribute("value-dispatch")) el.setValue("");
408
+ continue;
409
+ }
404
410
 
405
411
  if (key && checkValue(key)) {
406
412
  let value;
@@ -416,8 +422,16 @@ async function setData(element, data) {
416
422
  if ($update) {
417
423
  delete data[type][0].$update;
418
424
  data[type][0] = { ...data[type][0], ...$update };
425
+ } else if (key === "{}") {
426
+ value = data[type][0];
427
+ if (reference === "false") {
428
+ delete data[type][0].$storage;
429
+ delete data[type][0].$database;
430
+ delete data[type][0].$array;
431
+ }
432
+ } else {
433
+ value = CRUD.getValueFromObject(data[type][0], key);
419
434
  }
420
- value = CRUD.getValueFromObject(data[type][0], key);
421
435
  }
422
436
 
423
437
  if (!data[type].length) continue;
@@ -427,6 +441,7 @@ async function setData(element, data) {
427
441
  (isListen === "false" && !data.method.endsWith(".read"))
428
442
  )
429
443
  continue;
444
+
430
445
  // TODO: object.update data returned from server will not include $operator
431
446
  el.setValue(value);
432
447
  }
@@ -445,8 +460,8 @@ async function filterData(element, data, type, key) {
445
460
  operator = key.split(".")[0] || "";
446
461
  }
447
462
 
448
- let property = key.replace(operator + ".", "");
449
-
463
+ let property = key;
464
+ if (operator) property = property.replace(operator + ".", "");
450
465
  if (Array.isArray(data[type])) {
451
466
  let Data = [],
452
467
  isObject;
@@ -462,16 +477,20 @@ async function filterData(element, data, type, key) {
462
477
  doc = { ...doc, ...$update };
463
478
  }
464
479
  }
465
- if (doc[property]) {
466
- if (Array.isArray(doc[property]))
467
- Data.push(...doc[property]);
480
+ let docValue = getValueFromObject(doc, property);
481
+ if (docValue) {
482
+ if (Array.isArray(docValue)) Data.push(...docValue);
468
483
  else {
469
484
  isObject = true;
470
- Data.push(doc[property]);
485
+ Data.push(docValue);
471
486
  }
472
487
  } else continue;
473
488
  }
474
489
 
490
+ if (isObject && Data.length === 1) {
491
+ Data = Data[0];
492
+ }
493
+
475
494
  // if (isObject && Data.length === 1) {
476
495
  // data = { [property]: Data[0] }
477
496
  // } else
@@ -819,14 +838,26 @@ async function getData(form) {
819
838
  let attribute = element.getAttribute("actions");
820
839
  if (attribute.includes("save", "delete")) continue;
821
840
  }
841
+
842
+ if (
843
+ element.matches(
844
+ "[render-selector], [render-closest], [render-parent], [render-next], [render-previous]"
845
+ )
846
+ ) {
847
+ let type = element.getAttribute("type");
848
+ if (type !== "file") continue;
849
+ }
850
+
851
+ let value = await element.getValue();
852
+ if (!value && value !== "") continue;
853
+
822
854
  let Data = { ...data };
823
855
  let dataKey = elements.get(element);
824
- let value = await element.getValue();
825
856
 
826
857
  // console.log(type, value, data)
827
858
  if (!Data[Data.type] && Data.key) {
828
- if (!value) continue;
829
- else if (Array.isArray(value) && !value.length) continue;
859
+ // if (!value && value !== "") continue;
860
+ // else if (Array.isArray(value) && !value.length) continue;
830
861
 
831
862
  Data.method = Data.type + ".create";
832
863
  if (Data.type === "object") {
@@ -870,11 +901,11 @@ async function getData(form) {
870
901
  }
871
902
 
872
903
  if ((Data.type = "object")) {
873
- if (Data.key == "{}") {
874
- if (!value) continue;
875
- else if (Array.isArray(value) && !value.length)
876
- continue;
877
- }
904
+ // if (Data.key == "{}") {
905
+ // if (!value && value !== "") continue;
906
+ // else if (Array.isArray(value) && !value.length)
907
+ // continue;
908
+ // }
878
909
 
879
910
  if (typeof Data[Data.type] === "string")
880
911
  if (Data.key == "{}") {
@@ -901,7 +932,9 @@ async function getData(form) {
901
932
  ...value
902
933
  };
903
934
  else {
904
- Data[Data.type][0][Data.key] = value;
935
+ if (Data[Data.type][0]) {
936
+ Data[Data.type][0][Data.key] = value;
937
+ }
905
938
  if (!Data[Data.type][0]._id)
906
939
  Data.method = Data.type + ".create";
907
940
  }
@@ -1194,6 +1227,7 @@ async function remove(element) {
1194
1227
  )
1195
1228
  element = [element];
1196
1229
  for (let i = 0; i < element.length; i++) {
1230
+ delete element[i].hasRead;
1197
1231
  if (element[i].tagName === "FORM") {
1198
1232
  let form = forms.get(element[i]);
1199
1233
  if (!form) return;
@@ -1438,7 +1472,16 @@ Observer.init({
1438
1472
  Observer.init({
1439
1473
  name: "CoCreateElementsAttributes",
1440
1474
  observe: ["attributes"],
1441
- attributeName: ["storage", "database", "array", "index", "object", "key"],
1475
+ attributeName: [
1476
+ "organization_id",
1477
+ "host",
1478
+ "storage",
1479
+ "database",
1480
+ "array",
1481
+ "index",
1482
+ "object",
1483
+ "key"
1484
+ ],
1442
1485
  // target: selector, // blocks mutations when applied
1443
1486
  callback: function (mutation) {
1444
1487
  let currentValue = mutation.target.getAttribute(mutation.attributeName);
@@ -1462,6 +1505,8 @@ Actions.init([
1462
1505
  name: "delete",
1463
1506
  endEvent: "deleted",
1464
1507
  callback: async (action) => {
1508
+ // TODO: use selector to target elements for deletion if element is apart of render get render get rendering element for more crud detils
1509
+ // this way any selector can be used to target crud deletions not just .selected
1465
1510
  let elements = queryElements({
1466
1511
  element: action.element,
1467
1512
  prefix: "delete"