@cocreate/elements 1.8.4 → 1.11.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 +29 -0
- package/package.json +3 -2
- package/src/getValue.js +32 -12
- package/src/index.js +15 -19
- package/src/setValue.js +27 -11
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
# [1.11.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.10.0...v1.11.0) (2022-07-25)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* if mutation detected only get value for mutated element ([f0681e3](https://github.com/CoCreate-app/CoCreate-elements/commit/f0681e380f59870f0f8ad34eea9ee5b1c3fbde8c))
|
|
7
|
+
* remved isFlat ([d85e120](https://github.com/CoCreate-app/CoCreate-elements/commit/d85e1204647b41efbe2755fd1bf1ade9926d670f))
|
|
8
|
+
* supports getting a value from any element ([2ec2f51](https://github.com/CoCreate-app/CoCreate-elements/commit/2ec2f5156d7bcb15a091ea424c0ab7bb5de200eb))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* crud.getObjectValueByPath to return value using name containing a dotNotation string ([d158084](https://github.com/CoCreate-app/CoCreate-elements/commit/d15808445001609db85100a1389784739e6d891f))
|
|
14
|
+
|
|
15
|
+
# [1.10.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.9.0...v1.10.0) (2022-07-08)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* value-type support and if element name attribute = _id do not save ([164d12f](https://github.com/CoCreate-app/CoCreate-elements/commit/164d12f40a84d85d4114df3b8efe51b9cf6a4e11))
|
|
21
|
+
|
|
22
|
+
# [1.9.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.8.4...v1.9.0) (2022-07-03)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* setValue can target element in iframe or parent of iframe using @cocreate/utils queryDocumentSelectorAll ([1447236](https://github.com/CoCreate-app/CoCreate-elements/commit/1447236b86507336a9c3db6452f0760790cabd59))
|
|
28
|
+
* value-type attribute to define if value is an object ([9649b6a](https://github.com/CoCreate-app/CoCreate-elements/commit/9649b6acdbe5e97f5418df5244025a359433891c))
|
|
29
|
+
|
|
1
30
|
## [1.8.4](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.8.3...v1.8.4) (2022-06-18)
|
|
2
31
|
|
|
3
32
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/elements",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.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",
|
|
@@ -64,6 +64,7 @@
|
|
|
64
64
|
"@cocreate/actions": "^1.4.3",
|
|
65
65
|
"@cocreate/crud-client": "^1.7.1",
|
|
66
66
|
"@cocreate/docs": "^1.3.3",
|
|
67
|
-
"@cocreate/observer": "^1.5.3"
|
|
67
|
+
"@cocreate/observer": "^1.5.3",
|
|
68
|
+
"@cocreate/utils": "^1.8.1"
|
|
68
69
|
}
|
|
69
70
|
}
|
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
|
|
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
|
|
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(valueEl);
|
|
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
|
@@ -79,18 +79,25 @@ function setData(elements, data) {
|
|
|
79
79
|
let selector = `[collection='${collection}'][document_id='${document_id}']:not(cocreate-select, link)`;
|
|
80
80
|
elements = document.querySelectorAll(selector);
|
|
81
81
|
}
|
|
82
|
-
let encodeData = crud.encodeObject(data.data);
|
|
83
82
|
|
|
84
83
|
elements.forEach((el) => {
|
|
85
84
|
const { collection, document_id, name, isRead, isUpdate, isCrdt } = crud.getAttr(el);
|
|
86
85
|
if (el.hasAttribute('actions')) return;
|
|
87
86
|
if (isRead == "false" || isUpdate == "false" || isCrdt == "true") return;
|
|
88
|
-
// let isEditable = el.getAttribute('contenteditable');
|
|
89
87
|
|
|
90
|
-
// if (data['collection'] == collection && data['document_id'] == document_id && !isEditable) {
|
|
91
88
|
if (data['collection'] == collection && data['document_id'] == document_id) {
|
|
92
|
-
|
|
93
|
-
|
|
89
|
+
let value;
|
|
90
|
+
let valueType = el.getAttribute('value-type');
|
|
91
|
+
if(valueType == 'object' || valueType == 'json'){
|
|
92
|
+
// if (name == 'data')
|
|
93
|
+
// value = JSON.stringify(data[name])
|
|
94
|
+
// else
|
|
95
|
+
value = JSON.stringify(data.data[name])
|
|
96
|
+
value = decodeURIComponent(value)
|
|
97
|
+
} else
|
|
98
|
+
value = crud.getObjectValueByPath(data.data, name);
|
|
99
|
+
|
|
100
|
+
setValue(el, value);
|
|
94
101
|
|
|
95
102
|
isRendered = true;
|
|
96
103
|
}
|
|
@@ -113,10 +120,7 @@ function setData(elements, data) {
|
|
|
113
120
|
|
|
114
121
|
async function save(element) {
|
|
115
122
|
let value = getValue(element);
|
|
116
|
-
|
|
117
|
-
// if (element.tagName == "INPUT" && element.type === "checkbox" || element.tagName == "SELECT" && element.hasAttribute('multiple'))
|
|
118
|
-
// isFlat = true;
|
|
119
|
-
await crud.save(element, value, isFlat);
|
|
123
|
+
await crud.save(element, value);
|
|
120
124
|
}
|
|
121
125
|
|
|
122
126
|
function __initSocket() {
|
|
@@ -130,20 +134,12 @@ function __initEvents(el) {
|
|
|
130
134
|
if (!elementEvents.has(el)) {
|
|
131
135
|
if (el.tagName == 'INPUT' || el.tagName == 'TEXTAREA' || el.tagName == 'SELECT' || el.hasAttribute('contenteditable')) {
|
|
132
136
|
el.addEventListener('input', function(e) {
|
|
133
|
-
const {document_id, isRealtime, isCrdt} = crud.getAttr(el);
|
|
134
|
-
if (isCrdt == "true" && document_id || isRealtime == "false") return;
|
|
137
|
+
const {document_id, name, isRealtime, isCrdt} = crud.getAttr(el);
|
|
138
|
+
if (isCrdt == "true" && document_id || isRealtime == "false" || name == "_id") return;
|
|
135
139
|
if (document_id && e.detail && e.detail.skip == true) return;
|
|
136
140
|
save(el);
|
|
137
141
|
});
|
|
138
142
|
|
|
139
|
-
// el.addEventListener('change', function(e) {
|
|
140
|
-
// if (el.tagName == 'SELECT') {
|
|
141
|
-
// const {isRealtime, isSave, isUpdate} = crud.getAttr(el);
|
|
142
|
-
// if (isRealtime == "false" || isSave == "false" || isUpdate == "false") return;
|
|
143
|
-
// if (e.detail && e.detail.skip == true) return;
|
|
144
|
-
// save(el);
|
|
145
|
-
// }
|
|
146
|
-
// });
|
|
147
143
|
elementEvents.set(el, true)
|
|
148
144
|
}
|
|
149
145
|
}
|
package/src/setValue.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import observer from '@cocreate/observer';
|
|
2
2
|
import crud from '@cocreate/crud-client';
|
|
3
|
-
import {
|
|
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
|
-
|
|
36
|
+
|
|
37
|
+
let value
|
|
38
|
+
if(element.getValue)
|
|
39
|
+
value = element.getValue(element);
|
|
35
40
|
if (!value) return;
|
|
41
|
+
|
|
36
42
|
let selector = element.getAttribute('set-value');
|
|
37
43
|
if(!selector) return;
|
|
38
|
-
let elements
|
|
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))
|
|
@@ -73,7 +83,8 @@ function setValueByFind(element){
|
|
|
73
83
|
|
|
74
84
|
var setValue = (el, value) => {
|
|
75
85
|
if (value === null || value === undefined) return;
|
|
76
|
-
|
|
86
|
+
let valueType = el.getAttribute('value-type');
|
|
87
|
+
|
|
77
88
|
if (el.tagName == 'INPUT' || el.tagName == 'TEXTAREA' || el.tagName == 'SELECT') {
|
|
78
89
|
const {isCrdt} = crud.getAttr(el);
|
|
79
90
|
if (isCrdt == "true" || el.type === 'file') return;
|
|
@@ -115,7 +126,7 @@ var setValue = (el, value) => {
|
|
|
115
126
|
el.setAttribute("value", value);
|
|
116
127
|
}
|
|
117
128
|
|
|
118
|
-
if (el.classList.contains('domEditor')) {
|
|
129
|
+
if (el.classList.contains('domEditor') || el.hasAttribute('get-value') || el.hasAttribute('get-value-closest')) {
|
|
119
130
|
if (el.getAttribute('data-domEditor') == "replace") {
|
|
120
131
|
let newElement = document.createElement("div");
|
|
121
132
|
newElement.innerHTML = value;
|
|
@@ -130,7 +141,10 @@ var setValue = (el, value) => {
|
|
|
130
141
|
}
|
|
131
142
|
}
|
|
132
143
|
else {
|
|
133
|
-
|
|
144
|
+
if (valueType == 'string' || valueType == 'text')
|
|
145
|
+
el.textContent = value;
|
|
146
|
+
else
|
|
147
|
+
el.innerHTML = value;
|
|
134
148
|
}
|
|
135
149
|
}
|
|
136
150
|
}
|
|
@@ -140,8 +154,10 @@ var setValue = (el, value) => {
|
|
|
140
154
|
}
|
|
141
155
|
else {
|
|
142
156
|
if (el.hasAttribute('contenteditable') && el == document.activeElement) return;
|
|
143
|
-
|
|
144
|
-
|
|
157
|
+
if (valueType == 'string' || valueType == 'text')
|
|
158
|
+
el.textContent = value;
|
|
159
|
+
else
|
|
160
|
+
el.innerHTML = value;
|
|
145
161
|
if (el.hasAttribute("value")) {
|
|
146
162
|
el.setAttribute("value", value);
|
|
147
163
|
}
|
|
@@ -209,7 +225,7 @@ function dispatchEvents(el) {
|
|
|
209
225
|
observer.init({
|
|
210
226
|
name: 'set-value',
|
|
211
227
|
observe: ['addedNodes'],
|
|
212
|
-
target: '[
|
|
228
|
+
target: '[get-value], [get-value-closest]',
|
|
213
229
|
callback: function(mutation) {
|
|
214
230
|
initElement(mutation.target);
|
|
215
231
|
}
|