@cocreate/elements 1.35.8 → 1.36.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 CHANGED
@@ -1,3 +1,26 @@
1
+ ## [1.36.1](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.36.0...v1.36.1) (2024-07-09)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * handleing of key="{}" where value is an array ([c86ffd9](https://github.com/CoCreate-app/CoCreate-elements/commit/c86ffd9697839611e8fb10a3f76e0a9bff9892cb))
7
+ * objectId() habndleing to add _id to element ([b2bb7b4](https://github.com/CoCreate-app/CoCreate-elements/commit/b2bb7b423ea2316be0087506fa094a3a8e8cce09))
8
+ * removed testing log ([1b4f38e](https://github.com/CoCreate-app/CoCreate-elements/commit/1b4f38e0cfc1b764dd695dc3288e74f6cb15b676))
9
+ * set flag rendered to prevent loop caused by fireListeners and sync ([3dd1267](https://github.com/CoCreate-app/CoCreate-elements/commit/3dd1267784617c1a666b032fc427671f0cf07375))
10
+ * use continue in for loop and continue if !data$storage ([32020f1](https://github.com/CoCreate-app/CoCreate-elements/commit/32020f17fecc6a578ff58b9fd1dec50a6f611b11))
11
+
12
+ # [1.36.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.35.8...v1.36.0) (2024-06-30)
13
+
14
+
15
+ ### Bug Fixes
16
+
17
+ * re init form elements ([7b28c19](https://github.com/CoCreate-app/CoCreate-elements/commit/7b28c19fe31bed297b930d113c98cfd9f913b0fe))
18
+
19
+
20
+ ### Features
21
+
22
+ * form.setValue() will set value in elements if value is object else it if string will fallback elementPrototype setVlaue() ([e76df88](https://github.com/CoCreate-app/CoCreate-elements/commit/e76df88fb1755aeffa8f15587d304e2df6497801))
23
+
1
24
  ## [1.35.8](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.35.7...v1.35.8) (2024-06-23)
2
25
 
3
26
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/elements",
3
- "version": "1.35.8",
3
+ "version": "1.36.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/form.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import Observer from '@cocreate/observer';
2
2
  import { getAttributeNames, ObjectId } from '@cocreate/utils';
3
3
  import Action from '@cocreate/actions';
4
- import '@cocreate/element-prototype';
4
+ import elementPrototype from '@cocreate/element-prototype';
5
5
 
6
6
 
7
7
  /**
@@ -33,6 +33,7 @@ function init(elements) {
33
33
  runObjectId(element);
34
34
  setAttribute(element);
35
35
  disableAutoFill(element);
36
+ setValue(element)
36
37
  element.addEventListener('submit', function (event) {
37
38
  if (!element.hasAttribute('action')) {
38
39
  event.preventDefault();
@@ -45,20 +46,17 @@ function init(elements) {
45
46
  * @param form
46
47
  */
47
48
  function runObjectId(form) {
48
- let elements = form.querySelectorAll("[object='ObjectId()']")
49
- let arrays = []
50
- // Add a array to the array list
49
+ let elements = Array.from(form.querySelectorAll("[object='ObjectId()']"));
50
+
51
+ if (form.getAttribute('object') === "ObjectId()") {
52
+ elements.push(form);
53
+ }
51
54
  for (let i = 0; i < elements.length; i++) {
52
- elements[i].setAttribute('object', '')
53
55
  let array = elements[i].getAttribute('array')
54
- // Add a array to the array list.
55
- if (array && !arrays.includes(array))
56
- arrays.push(array)
57
- }
58
- // Sets the object id for each array in the array.
59
- for (let i = 0; i < arrays.length; i++) {
60
- // TODO: needs access to setTypeValue
61
- setTypeValue(form, { array: arrays[i], object: [{ _id: ObjectId().toString() }] });
56
+ if (!array)
57
+ continue
58
+ elements[i].setAttribute('object', ObjectId().toString())
59
+
62
60
  }
63
61
  }
64
62
 
@@ -134,6 +132,23 @@ function reset(form) {
134
132
  }));
135
133
  }
136
134
 
135
+ function setValue(form) {
136
+ form.setValue = (value) => {
137
+ if (typeof value !== "object")
138
+ elementPrototype.setValue(form, value);
139
+ else {
140
+ const inputs = form.querySelectorAll('[name], [key]');
141
+ inputs.forEach(element => {
142
+ const key = element.getAttribute('key') || element.getAttribute('name');
143
+ if (value[key]) {
144
+ element.setValue(value[key]);
145
+ }
146
+ });
147
+
148
+ }
149
+ }
150
+ }
151
+
137
152
  Observer.init({
138
153
  name: 'CoCreateForm',
139
154
  observe: ['addedNodes'],
package/src/index.js CHANGED
@@ -53,7 +53,7 @@ const debounce = new Map();
53
53
  * any of these CRUD attributes: "storage", "database", "array", "render-json".
54
54
  */
55
55
  async function init(element) {
56
- if (element && !(element instanceof HTMLCollection) && !Array.isArray(element))
56
+ if (element && !(element instanceof HTMLCollection) && !(element instanceof NodeList) && !Array.isArray(element))
57
57
  element = [element]
58
58
  else if (!element) {
59
59
  element = document.querySelectorAll(selector)
@@ -416,11 +416,13 @@ async function filterData(element, data, type, key) {
416
416
  if (Array.isArray(data[type])) {
417
417
  let Data = [], isObject
418
418
  for (let doc of data[type]) {
419
+ if (!doc.$storage)
420
+ continue
419
421
  // TODO: should have been handled by getDataElements()
420
422
  if (type === 'object') {
421
423
  let _id = element.getAttribute('object')
422
424
  if (_id && doc._id !== _id)
423
- return
425
+ continue
424
426
  let $update = doc.$update
425
427
  if ($update) {
426
428
  delete doc.$update
@@ -436,7 +438,7 @@ async function filterData(element, data, type, key) {
436
438
  Data.push(doc[property])
437
439
  }
438
440
  } else
439
- return
441
+ continue
440
442
  }
441
443
 
442
444
  // if (isObject && Data.length === 1) {
@@ -716,6 +718,12 @@ function getDataKeys(data) {
716
718
  async function getData(form) {
717
719
  let dataKeys = new Map()
718
720
  let formObject = forms.get(form)
721
+ if (!formObject) {
722
+ let elements = form.querySelectorAll(selector)
723
+ await init(elements)
724
+ return await getData(form)
725
+ }
726
+
719
727
  for (let type of formObject.types.values()) {
720
728
  for (let [element, data] of type.entries()) {
721
729
  if (!element.hasAttribute('key') || element.getAttribute('save') === 'false')
@@ -773,10 +781,21 @@ async function getData(form) {
773
781
  }
774
782
 
775
783
  if (Data.type = 'object') {
784
+ if (Data.key == '{}') {
785
+ if (!value)
786
+ continue
787
+ else if (Array.isArray(value) && !value.length)
788
+ continue
789
+ }
790
+
776
791
  if (typeof Data[Data.type] === 'string')
777
- if (Data.key == '{}')
778
- Data[Data.type] = { _id: Data[Data.type], ...value }
779
- else
792
+ if (Data.key == '{}') {
793
+ // TODO: handle if array length more than 1
794
+ if (Array.isArray(value))
795
+ Data[Data.type] = { _id: Data[Data.type], ...value[0] }
796
+ else
797
+ Data[Data.type] = { _id: Data[Data.type], ...value }
798
+ } else
780
799
  Data[Data.type] = { _id: Data[Data.type], [Data.key]: value }
781
800
  else if (Array.isArray(Data[Data.type]))
782
801
  if (Data.key == '{}')
@@ -1124,6 +1143,11 @@ function initSocket() {
1124
1143
  CRUD.listen(action, function (data) {
1125
1144
  if (data.resolved && data.status === 'received' && CRUD.socket.clientId === data.clientId)
1126
1145
  return
1146
+ if (data.rendered)
1147
+ return
1148
+
1149
+ data.rendered = true
1150
+
1127
1151
  setData(null, data);
1128
1152
  });
1129
1153
  }
@@ -1317,6 +1341,8 @@ Observer.init({
1317
1341
  let currentValue = mutation.target.getAttribute(mutation.attributeName)
1318
1342
  if (currentValue !== mutation.oldValue) {
1319
1343
  remove(mutation.target)
1344
+ if (mutation.target.tagName === 'FORM')
1345
+ return
1320
1346
  init([mutation.target])
1321
1347
  }
1322
1348
  }