@cocreate/elements 1.17.20 → 1.17.22
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 +15 -0
- package/CoCreate.config.js +1 -1
- package/package.json +7 -9
- package/src/index.js +158 -158
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
## [1.17.22](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.17.21...v1.17.22) (2023-06-11)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* renamed db to storage ([f79fd3d](https://github.com/CoCreate-app/CoCreate-elements/commit/f79fd3d19551342fa81dd53c5f37c2a60d85c270))
|
|
7
|
+
* renamed hosts to host. the value can be a string or an array of strings ([a6b114b](https://github.com/CoCreate-app/CoCreate-elements/commit/a6b114b273dbb69410f7b0cadf0f7a743b74891c))
|
|
8
|
+
|
|
9
|
+
## [1.17.21](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.17.20...v1.17.21) (2023-06-10)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
* Update dependencies versions for [@cocreate](https://github.com/cocreate) libraries ([af714a9](https://github.com/CoCreate-app/CoCreate-elements/commit/af714a9397fb4e6f306b2548aeb6083b35dab07d))
|
|
15
|
+
|
|
1
16
|
## [1.17.20](https://github.com/CoCreate-app/CoCreate-elements/compare/v1.17.19...v1.17.20) (2023-06-10)
|
|
2
17
|
|
|
3
18
|
|
package/CoCreate.config.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/elements",
|
|
3
|
-
"version": "1.17.
|
|
3
|
+
"version": "1.17.22",
|
|
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",
|
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
"start": "npx webpack --config webpack.config.js",
|
|
28
28
|
"build": "NODE_ENV=production npx webpack --config webpack.config.js",
|
|
29
29
|
"dev": "npx webpack --config webpack.config.js --watch",
|
|
30
|
-
"docs": "node ./node_modules/@cocreate/docs/src/index.js",
|
|
31
30
|
"postinstall": "node ./node_modules/@cocreate/cli/check-coc.js"
|
|
32
31
|
},
|
|
33
32
|
"repository": {
|
|
@@ -60,12 +59,11 @@
|
|
|
60
59
|
"webpack-log": "^3.0.1"
|
|
61
60
|
},
|
|
62
61
|
"dependencies": {
|
|
63
|
-
"@cocreate/actions": "^1.8.
|
|
64
|
-
"@cocreate/crud-client": "^1.21.
|
|
65
|
-
"@cocreate/
|
|
66
|
-
"@cocreate/
|
|
67
|
-
"@cocreate/
|
|
68
|
-
"@cocreate/
|
|
69
|
-
"@cocreate/utils": "^1.21.3"
|
|
62
|
+
"@cocreate/actions": "^1.8.19",
|
|
63
|
+
"@cocreate/crud-client": "^1.21.11",
|
|
64
|
+
"@cocreate/element-prototype": "^1.8.17",
|
|
65
|
+
"@cocreate/filter": "^1.19.17",
|
|
66
|
+
"@cocreate/observer": "^1.8.6",
|
|
67
|
+
"@cocreate/utils": "^1.21.4"
|
|
70
68
|
}
|
|
71
69
|
}
|
package/src/index.js
CHANGED
|
@@ -11,196 +11,196 @@ const initializing = new Map();
|
|
|
11
11
|
const els = new Map();
|
|
12
12
|
|
|
13
13
|
function init() {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
let elements = document.querySelectorAll(selector);
|
|
15
|
+
__initSocket();
|
|
16
|
+
initElements(elements);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
function initElements(elements) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
20
|
+
let documents = new Map();
|
|
21
|
+
// let els = []
|
|
22
|
+
for (let element of elements) {
|
|
23
|
+
let doc = initElement(element);
|
|
24
|
+
if (doc) {
|
|
25
|
+
let key = JSON.stringify(doc)
|
|
26
|
+
let item = documents.get(key);
|
|
27
|
+
if (item)
|
|
28
|
+
doc.elements = [...item.elements, element]
|
|
29
|
+
else
|
|
30
|
+
doc.elements = [element]
|
|
31
|
+
|
|
32
|
+
// let collection = doc.collection;
|
|
33
|
+
// let document_id = doc.document_id;
|
|
34
|
+
// let filter = doc.filter;
|
|
35
|
+
let initialize = initializing.get(element)
|
|
36
|
+
if (!initialize) {
|
|
37
|
+
initializing.set(element, key);
|
|
38
|
+
documents.set(key, doc);
|
|
39
|
+
// els.set(key, doc);
|
|
40
|
+
// els.push(element)
|
|
41
|
+
} else {
|
|
42
|
+
// if (document_id && initialize.document_id != document_id || filter && JSON.stringify(initialize.filter) != JSON.stringify(filter)){
|
|
43
|
+
if (initialize != key) {
|
|
44
|
+
initializing.set(element, key);
|
|
45
|
+
documents.set(key, doc);
|
|
46
|
+
// els.set(key, doc);
|
|
47
|
+
// els.push(element)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
read(documents);
|
|
54
54
|
}
|
|
55
|
-
|
|
55
|
+
|
|
56
56
|
function initElement(el) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
57
|
+
__initEvents(el);
|
|
58
|
+
|
|
59
|
+
if (el.closest('.template')) return;
|
|
60
|
+
|
|
61
|
+
// if rendered in server side skip
|
|
62
|
+
if (el.hasAttribute('rendered')) {
|
|
63
|
+
el.removeAttribute('rendered');
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const { collection, document_id, name, isRead } = crud.getAttributes(el);
|
|
68
|
+
if (!collection || !name) return;
|
|
69
|
+
|
|
70
|
+
if (document_id)
|
|
71
|
+
if (!document_id.match(/^[0-9a-fA-F]{24}$/)) return;
|
|
72
|
+
|
|
73
|
+
let filter
|
|
74
|
+
if (el.hasAttribute('filter_id') || el.hasAttribute('filter-name') || el.hasAttribute('filter-sort-name')) {
|
|
75
|
+
filter = ccfilter.init(el, "filter_id");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!crud.checkValue(collection) || !crud.checkValue(name)) return;
|
|
79
|
+
|
|
80
|
+
if (isRead == 'false') return;
|
|
81
|
+
if (!document_id && !filter) return;
|
|
82
|
+
|
|
83
|
+
return { collection, document_id, filter };
|
|
84
84
|
}
|
|
85
|
-
|
|
85
|
+
|
|
86
86
|
async function read(documents) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
87
|
+
if (documents && documents.size > 0) {
|
|
88
|
+
for (let [key, { collection, document_id, filter, elements }] of documents) {
|
|
89
|
+
documents.delete(key);
|
|
90
|
+
|
|
91
|
+
let data = { collection }
|
|
92
|
+
if (document_id)
|
|
93
|
+
data.document = { _id: document_id }
|
|
94
|
+
if (filter)
|
|
95
|
+
data.filter = filter
|
|
96
|
+
|
|
97
|
+
let response = await crud.readDocument(data);
|
|
98
|
+
setData(elements, response);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
101
|
}
|
|
102
|
-
|
|
102
|
+
|
|
103
103
|
function setData(elements, data) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
104
|
+
if (!data.document || !data.document[0]) return;
|
|
105
|
+
let key = getKey(data)
|
|
106
|
+
if (!elements) {
|
|
107
|
+
elements = els.get(key)
|
|
108
|
+
} else {
|
|
109
|
+
let eles = els.get(key)
|
|
110
|
+
if (eles && eles.length)
|
|
111
|
+
els.set(key, [...eles, ...elements])
|
|
112
|
+
else
|
|
113
|
+
els.set(key, elements)
|
|
114
|
+
}
|
|
115
|
+
if (!elements || elements.length == 0)
|
|
116
|
+
return
|
|
117
|
+
|
|
118
|
+
for (let el of elements) {
|
|
119
|
+
const { collection, document_id, name, isRead, isUpdate, isCrdt } = crud.getAttributes(el);
|
|
120
|
+
|
|
121
|
+
if (el.hasAttribute('actions') || el.tagName === 'DIV' && !el.classList.contains('domEditor')) continue;
|
|
122
|
+
if (!name || isRead == "false" || isUpdate == "false" || isCrdt == "true") continue;
|
|
123
|
+
|
|
124
|
+
if (data.document[0]['collection'] == collection && data.document[0]['_id'] == document_id) {
|
|
125
|
+
let value = crud.getValueFromObject(data.document[0], name);
|
|
126
|
+
el.setValue(value);
|
|
127
|
+
initializing.delete(el)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
function getKey(data) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
133
|
+
let key = {};
|
|
134
|
+
let attributes = ["storage", "database", "collection", "index", "document"]
|
|
135
|
+
|
|
136
|
+
for (let attribute of attributes) {
|
|
137
|
+
let value = data[attribute]
|
|
138
|
+
if (value) {
|
|
139
|
+
if (attribute == 'document')
|
|
140
|
+
key[attribute] = value
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
key.document = data.document[0]._id
|
|
144
|
+
return JSON.stringify(key)
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
async function save(element) {
|
|
148
|
-
|
|
149
|
-
|
|
148
|
+
let value = element.getValue();
|
|
149
|
+
crud.save(element, value);
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
function __initSocket() {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
153
|
+
crud.listen('updateDocument', function (data) {
|
|
154
|
+
setData(null, data);
|
|
155
|
+
});
|
|
156
156
|
}
|
|
157
157
|
|
|
158
158
|
const elementEvents = new Map();
|
|
159
159
|
function __initEvents(el) {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
160
|
+
if (!elementEvents.has(el)) {
|
|
161
|
+
if (el.tagName == 'INPUT' || el.tagName == 'TEXTAREA' || el.tagName == 'SELECT' || el.hasAttribute('contenteditable')) {
|
|
162
|
+
el.addEventListener('input', function (e) {
|
|
163
|
+
const { document_id, name, isRealtime, isCrdt } = crud.getAttributes(el);
|
|
164
|
+
if (isCrdt == "true" && document_id && document_id != 'pending' || isRealtime == "false" || name == "_id") return;
|
|
165
|
+
if (document_id && e.detail && e.detail.skip == true) return;
|
|
166
|
+
save(el);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
elementEvents.set(el, true)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
observer.init({
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
175
|
+
name: 'CoCreateElementsChildList',
|
|
176
|
+
observe: ['childList'],
|
|
177
|
+
target: selector,
|
|
178
|
+
callback: function (mutation) {
|
|
179
|
+
initElements(mutation.addedNodes);
|
|
180
|
+
}
|
|
181
181
|
});
|
|
182
182
|
|
|
183
183
|
observer.init({
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
184
|
+
name: 'CoCreateElementsAttributes',
|
|
185
|
+
observe: ['attributes'],
|
|
186
|
+
attributeName: crud.getAttributeNames(['collection', 'document_id', 'name']),
|
|
187
|
+
target: selector,
|
|
188
|
+
callback: function (mutation) {
|
|
189
|
+
let { collection, document_id, name } = crud.getAttributes(mutation.target);
|
|
190
|
+
if (collection && document_id && name)
|
|
191
|
+
initElements([mutation.target]);
|
|
192
|
+
}
|
|
193
193
|
});
|
|
194
194
|
|
|
195
195
|
action.init({
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
196
|
+
name: "saveHtml",
|
|
197
|
+
endEvent: "changed-element",
|
|
198
|
+
callback: (btn, data) => {
|
|
199
|
+
let form = btn.closet('form');
|
|
200
|
+
save(form);
|
|
201
|
+
},
|
|
202
202
|
});
|
|
203
203
|
|
|
204
204
|
init();
|
|
205
205
|
|
|
206
|
-
export default {initElements, initElement, save};
|
|
206
|
+
export default { initElements, initElement, save };
|