@cocreate/elements 1.9.0 → 1.11.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 +28 -0
- package/package.json +1 -1
- package/src/fetchSrc.js +1 -1
- package/src/getValue.js +32 -12
- package/src/index.js +7 -20
- package/src/setValue.js +26 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
## [1.11.1](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.11.0...v1.11.1) (2022-08-01)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* fetchSrc - ignores link tags ([e266a95](https://github.com/CoCreate-app/CoCreate-elements/commit/e266a954ab15783e5feeff0f32045ff7b6c8ea91))
|
|
7
|
+
|
|
8
|
+
# [1.11.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.10.0...v1.11.0) (2022-07-25)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* if mutation detected only get value for mutated element ([f0681e3](https://github.com/CoCreate-app/CoCreate-elements/commit/f0681e380f59870f0f8ad34eea9ee5b1c3fbde8c))
|
|
14
|
+
* remved isFlat ([d85e120](https://github.com/CoCreate-app/CoCreate-elements/commit/d85e1204647b41efbe2755fd1bf1ade9926d670f))
|
|
15
|
+
* supports getting a value from any element ([2ec2f51](https://github.com/CoCreate-app/CoCreate-elements/commit/2ec2f5156d7bcb15a091ea424c0ab7bb5de200eb))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* crud.getObjectValueByPath to return value using name containing a dotNotation string ([d158084](https://github.com/CoCreate-app/CoCreate-elements/commit/d15808445001609db85100a1389784739e6d891f))
|
|
21
|
+
|
|
22
|
+
# [1.10.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.9.0...v1.10.0) (2022-07-08)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* value-type support and if element name attribute = _id do not save ([164d12f](https://github.com/CoCreate-app/CoCreate-elements/commit/164d12f40a84d85d4114df3b8efe51b9cf6a4e11))
|
|
28
|
+
|
|
1
29
|
# [1.9.0](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.8.4...v1.9.0) (2022-07-03)
|
|
2
30
|
|
|
3
31
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/elements",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.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/fetchSrc.js
CHANGED
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,15 +79,12 @@ 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
89
|
let value;
|
|
93
90
|
let valueType = el.getAttribute('value-type');
|
|
@@ -95,11 +92,12 @@ function setData(elements, data) {
|
|
|
95
92
|
// if (name == 'data')
|
|
96
93
|
// value = JSON.stringify(data[name])
|
|
97
94
|
// else
|
|
98
|
-
|
|
95
|
+
value = JSON.stringify(data.data[name])
|
|
99
96
|
value = decodeURIComponent(value)
|
|
100
97
|
} else
|
|
101
|
-
value =
|
|
102
|
-
|
|
98
|
+
value = crud.getObjectValueByPath(data.data, name);
|
|
99
|
+
|
|
100
|
+
setValue(el, value);
|
|
103
101
|
|
|
104
102
|
isRendered = true;
|
|
105
103
|
}
|
|
@@ -122,10 +120,7 @@ function setData(elements, data) {
|
|
|
122
120
|
|
|
123
121
|
async function save(element) {
|
|
124
122
|
let value = getValue(element);
|
|
125
|
-
|
|
126
|
-
// if (element.tagName == "INPUT" && element.type === "checkbox" || element.tagName == "SELECT" && element.hasAttribute('multiple'))
|
|
127
|
-
// isFlat = true;
|
|
128
|
-
await crud.save(element, value, isFlat);
|
|
123
|
+
await crud.save(element, value);
|
|
129
124
|
}
|
|
130
125
|
|
|
131
126
|
function __initSocket() {
|
|
@@ -139,20 +134,12 @@ function __initEvents(el) {
|
|
|
139
134
|
if (!elementEvents.has(el)) {
|
|
140
135
|
if (el.tagName == 'INPUT' || el.tagName == 'TEXTAREA' || el.tagName == 'SELECT' || el.hasAttribute('contenteditable')) {
|
|
141
136
|
el.addEventListener('input', function(e) {
|
|
142
|
-
const {document_id, isRealtime, isCrdt} = crud.getAttr(el);
|
|
143
|
-
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;
|
|
144
139
|
if (document_id && e.detail && e.detail.skip == true) return;
|
|
145
140
|
save(el);
|
|
146
141
|
});
|
|
147
142
|
|
|
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
143
|
elementEvents.set(el, true)
|
|
157
144
|
}
|
|
158
145
|
}
|
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
|
-
|
|
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
|
}
|