@cocreate/elements 1.10.0 → 1.12.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,31 @@
1
+ # [1.12.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.11.1...v1.12.0) (2022-08-31)
2
+
3
+
4
+ ### Features
5
+
6
+ * add getValue() and setValue() to HTMLElement.prototype ([1fde509](https://github.com/CoCreate-app/CoCreate-elements/commit/1fde50918b279bb49103bf7bea1f24861615a0a9))
7
+
8
+ ## [1.11.1](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.11.0...v1.11.1) (2022-08-01)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * fetchSrc - ignores link tags ([e266a95](https://github.com/CoCreate-app/CoCreate-elements/commit/e266a954ab15783e5feeff0f32045ff7b6c8ea91))
14
+
15
+ # [1.11.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.10.0...v1.11.0) (2022-07-25)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * if mutation detected only get value for mutated element ([f0681e3](https://github.com/CoCreate-app/CoCreate-elements/commit/f0681e380f59870f0f8ad34eea9ee5b1c3fbde8c))
21
+ * remved isFlat ([d85e120](https://github.com/CoCreate-app/CoCreate-elements/commit/d85e1204647b41efbe2755fd1bf1ade9926d670f))
22
+ * supports getting a value from any element ([2ec2f51](https://github.com/CoCreate-app/CoCreate-elements/commit/2ec2f5156d7bcb15a091ea424c0ab7bb5de200eb))
23
+
24
+
25
+ ### Features
26
+
27
+ * crud.getObjectValueByPath to return value using name containing a dotNotation string ([d158084](https://github.com/CoCreate-app/CoCreate-elements/commit/d15808445001609db85100a1389784739e6d891f))
28
+
1
29
  # [1.10.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.9.0...v1.10.0) (2022-07-08)
2
30
 
3
31
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/elements",
3
- "version": "1.10.0",
3
+ "version": "1.12.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",
@@ -1,51 +1,30 @@
1
- let v = el.getValue()
1
+ import { setValue } from './setValue';
2
+ import { getValue } from './getValue';
2
3
 
3
- HTMLElement.prototype.getValue = function() {
4
- this.style.display = 'none';
5
- };
6
-
7
- HTMLInputElement.prototype.getValue = function() {
8
- let value = this.value;
4
+ HTMLElement.prototype.getValue = function() {
5
+ let value = getValue(this)
6
+ return value;
7
+ };
9
8
 
10
- let prefix = this.getAttribute('value-prefix') || "";
11
- let suffix = this.getAttribute('value-suffix') || "";
9
+ HTMLElement.prototype.setValue = function(value) {
10
+ setValue(this, value)
11
+ };
12
12
 
13
- if (this.type === "checkbox") {
14
- let el_name = this.getAttribute('name');
15
- let checkboxs = document.querySelectorAll(`input[type=checkbox][name='${el_name}']`);
16
- if (checkboxs.length > 1) {
17
- value = [];
18
- checkboxs.forEach(el => {
19
- if (el.checked) value.push(el.value);
20
- });
21
- }
22
- else {
23
- value = this.checked;
24
- }
25
- }
26
- else if (this.type === "number") {
27
- value = Number(value);
28
- }
29
- else if (this.type === "password") {
30
- value = this.__encryptPassword(value);
31
- }
13
+ HTMLInputElement.prototype.getValue = function() {
14
+ let value = getValue(this)
15
+ return value;
16
+ };
32
17
 
33
- if (typeof value == "string") {
34
- value = prefix + value + suffix;
35
- }
18
+ HTMLInputElement.prototype.setValue = function(value) {
19
+ setValue(this, value)
20
+ };
36
21
 
37
- if (this.tagName == "SELECT" && this.hasAttribute('multiple')) {
38
- let options = this.selectedOptions;
39
- value = [];
40
- for (let i = 0; i < options.length; i++) {
41
- value.push(options[i].value);
42
- }
43
- }
22
+ HTMLHeadingElement.prototype.getValue = function() {
23
+ let value = getValue(this)
24
+ return value;
25
+ };
26
+
27
+ HTMLHeadingElement.prototype.setValue = function(value) {
28
+ setValue(this, value)
29
+ };
44
30
 
45
- return value;
46
- };
47
-
48
- HTMLHeadingElement.prototype.getValue = function() {
49
- this.style.display = 'none';
50
- };
51
-
package/src/fetchSrc.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import observer from '@cocreate/observer';
2
2
 
3
- const selector = '[src]:not(img, video, script, input, iframe, frame)'
3
+ const selector = '[src]:not(img, video, script, input, iframe, frame, link)'
4
4
  const initializing = new Map();
5
5
 
6
6
 
package/src/getValue.js CHANGED
@@ -4,7 +4,7 @@ import { setValue } from './setValue';
4
4
  const valueEls = new Map();
5
5
 
6
6
  function initGetValues() {
7
- var elements = document.querySelectorAll('[get-value]');
7
+ var elements = document.querySelectorAll('[get-value], [get-value-closest]');
8
8
  initElements(elements);
9
9
  }
10
10
 
@@ -15,15 +15,19 @@ function initElements(elements) {
15
15
  setValueByFind(valueEl);
16
16
  }
17
17
 
18
- function initElement(element) {
19
- let selector = element.getAttribute('get-value');
18
+ function initElement(element, mutation) {
19
+ let selector = element.getAttribute('get-value') || element.getAttribute('get-value-closest');
20
20
  if(!selector) return;
21
21
  if(/{{\s*([\w\W]+)\s*}}/g.test(selector)) return;
22
22
 
23
- let valueEl = document.querySelector(selector);
23
+ let valueEl
24
+ if (element.hasAttribute('get-value-closest'))
25
+ valueEl = element.closest(selector);
26
+ else
27
+ valueEl = document.querySelector(selector);
24
28
  if(!valueEl) return;
25
29
 
26
- initEvents(valueEl, element);
30
+ initEvents(valueEl, element, mutation);
27
31
 
28
32
  element.dispatchEvent(new Event("input", {
29
33
  "bubbles": true
@@ -31,7 +35,7 @@ function initElement(element) {
31
35
 
32
36
  }
33
37
 
34
- function initEvents(valueEl, element){
38
+ function initEvents(valueEl, element, mutation){
35
39
  if (!valueEls.has(valueEl)) {
36
40
  valueEls.set(valueEl, [element]);
37
41
 
@@ -41,17 +45,30 @@ function initEvents(valueEl, element){
41
45
 
42
46
  valueEl.addEventListener('updated_by_fetch', (e) => {
43
47
  setValueByFind(e.target);
44
- });
48
+ });
45
49
  }
46
50
  else
47
51
  valueEls.get(valueEl).push(element);
52
+
53
+ // ToDo: check to see if creates any loops or un wanted save by input event
54
+ if (mutation)
55
+ mutation = [element]
56
+ setValueByFind(valueEl, mutation)
48
57
  }
49
58
 
50
- function setValueByFind(valueEl) {
51
- let value = valueEl.getValue(valueEl);
59
+ function setValueByFind(valueEl, mutation) {
60
+ let value;
61
+ // todo can be removed if all elements have getValue using prototype
62
+ if (valueEl.hasAttribute('value'))
63
+ value = valueEl.getAttribute('value');
64
+ else if(valueEl.getValue)
65
+ value = valueEl.getValue();
66
+ else
67
+ value = getValue(valueEl);
52
68
  if (!value) return;
53
- let elements = valueEls.get(valueEl);
69
+ let elements = mutation || valueEls.get(valueEl);
54
70
 
71
+ if (elements)
55
72
  for(let element of elements){
56
73
  let key = element.getAttribute('get-value-key');
57
74
  if (key){
@@ -121,6 +138,9 @@ var getValue = (element) => {
121
138
  else if (element.tagName === 'IFRAME') {
122
139
  value = element.srcdoc;
123
140
  }
141
+ else if (element.hasAttribute('value')){
142
+ value = element.getAttribute('value');
143
+ }
124
144
  else {
125
145
  value = element.innerHTML;
126
146
  }
@@ -138,9 +158,9 @@ function __encryptPassword(str) {
138
158
  observer.init({
139
159
  name: 'get-value',
140
160
  observe: ['addedNodes'],
141
- target: '[get-value]',
161
+ target: '[get-value], [get-value-closest]',
142
162
  callback: function(mutation) {
143
- initElement(mutation.target);
163
+ initElement(mutation.target, true);
144
164
  }
145
165
  });
146
166
 
package/src/index.js CHANGED
@@ -4,6 +4,7 @@ import crud from '@cocreate/crud-client';
4
4
  import { initSetValues, setValue } from './setValue';
5
5
  import { initGetValues, getValue } from './getValue';
6
6
  import './fetchSrc';
7
+ import './HTMLElement';
7
8
 
8
9
  const selector = "[collection][document_id][name]:not(cocreate-select, link), input, textarea, select, [contenteditable]";
9
10
  const initializing = new Map();
@@ -33,11 +34,6 @@ function initElements(elements) {
33
34
  }
34
35
 
35
36
  function initElement(el) {
36
- if (el.tagName != 'DIV' || el.classList.contains('domEditor') || el.hasAttribute('contenteditable')){
37
- el.getValue = getValue;
38
- el.setValue = setValue;
39
- }
40
-
41
37
  __initEvents(el);
42
38
 
43
39
  if (el.closest('.template')) return;
@@ -79,15 +75,12 @@ function setData(elements, data) {
79
75
  let selector = `[collection='${collection}'][document_id='${document_id}']:not(cocreate-select, link)`;
80
76
  elements = document.querySelectorAll(selector);
81
77
  }
82
- let encodeData = crud.encodeObject(data.data);
83
78
 
84
79
  elements.forEach((el) => {
85
80
  const { collection, document_id, name, isRead, isUpdate, isCrdt } = crud.getAttr(el);
86
81
  if (el.hasAttribute('actions')) return;
87
82
  if (isRead == "false" || isUpdate == "false" || isCrdt == "true") return;
88
- // let isEditable = el.getAttribute('contenteditable');
89
83
 
90
- // if (data['collection'] == collection && data['document_id'] == document_id && !isEditable) {
91
84
  if (data['collection'] == collection && data['document_id'] == document_id) {
92
85
  let value;
93
86
  let valueType = el.getAttribute('value-type');
@@ -98,8 +91,9 @@ function setData(elements, data) {
98
91
  value = JSON.stringify(data.data[name])
99
92
  value = decodeURIComponent(value)
100
93
  } else
101
- value = encodeData[name];
102
- setValue(el, value);
94
+ value = crud.getObjectValueByPath(data.data, name);
95
+
96
+ el.setValue(value);
103
97
 
104
98
  isRendered = true;
105
99
  }
@@ -122,10 +116,7 @@ function setData(elements, data) {
122
116
 
123
117
  async function save(element) {
124
118
  let value = getValue(element);
125
- let isFlat = false;
126
- // if (element.tagName == "INPUT" && element.type === "checkbox" || element.tagName == "SELECT" && element.hasAttribute('multiple'))
127
- // isFlat = true;
128
- await crud.save(element, value, isFlat);
119
+ await crud.save(element, value);
129
120
  }
130
121
 
131
122
  function __initSocket() {
@@ -145,14 +136,6 @@ function __initEvents(el) {
145
136
  save(el);
146
137
  });
147
138
 
148
- // el.addEventListener('change', function(e) {
149
- // if (el.tagName == 'SELECT') {
150
- // const {isRealtime, isSave, isUpdate} = crud.getAttr(el);
151
- // if (isRealtime == "false" || isSave == "false" || isUpdate == "false") return;
152
- // if (e.detail && e.detail.skip == true) return;
153
- // save(el);
154
- // }
155
- // });
156
139
  elementEvents.set(el, true)
157
140
  }
158
141
  }
package/src/setValue.js CHANGED
@@ -3,7 +3,7 @@ import crud from '@cocreate/crud-client';
3
3
  import { queryDocumentSelectorAll } from '@cocreate/utils';
4
4
 
5
5
  function initSetValues() {
6
- var elements = document.querySelectorAll('[set-value]');
6
+ var elements = document.querySelectorAll('[set-value], [set-value-closest]');
7
7
  initElements(elements);
8
8
  }
9
9
 
@@ -24,6 +24,8 @@ function initEvents(element){
24
24
  element.addEventListener('updated_by_fetch', (e) => {
25
25
  setValueByFind(e.target);
26
26
  });
27
+
28
+ setValueByFind(element);
27
29
  }
28
30
 
29
31
  const setValueMap = new Map();
@@ -31,12 +33,20 @@ function setValueByFind(element){
31
33
  let key = element.getAttribute('set-value-key');
32
34
  if (key)
33
35
  key = `{{${key}}}`;
34
- let value = element.getValue(element);
36
+
37
+ let value
38
+ if(element.getValue)
39
+ value = element.getValue();
35
40
  if (!value) return;
41
+
36
42
  let selector = element.getAttribute('set-value');
37
43
  if(!selector) return;
38
- let elements = queryDocumentSelectorAll(selector);
39
-
44
+ let elements;
45
+ if (element.hasAttribute('set-value-closest'))
46
+ elements = [element.closest(selector)];
47
+ else
48
+ elements = queryDocumentSelectorAll(selector);
49
+
40
50
  for(let element of elements){
41
51
  if (key){
42
52
  if (setValueMap.has(element))
@@ -116,7 +126,7 @@ var setValue = (el, value) => {
116
126
  el.setAttribute("value", value);
117
127
  }
118
128
 
119
- if (el.classList.contains('domEditor')) {
129
+ if (el.classList.contains('domEditor') || el.hasAttribute('get-value') || el.hasAttribute('get-value-closest')) {
120
130
  if (el.getAttribute('data-domEditor') == "replace") {
121
131
  let newElement = document.createElement("div");
122
132
  newElement.innerHTML = value;
@@ -215,7 +225,7 @@ function dispatchEvents(el) {
215
225
  observer.init({
216
226
  name: 'set-value',
217
227
  observe: ['addedNodes'],
218
- target: '[set-value]',
228
+ target: '[get-value], [get-value-closest]',
219
229
  callback: function(mutation) {
220
230
  initElement(mutation.target);
221
231
  }