@cocreate/render 1.9.0 → 1.9.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 +8 -0
- package/package.json +1 -1
- package/src/index.js +89 -44
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
## [1.9.1](https://github.com/CoCreate-app/CoCreate-render/compare/v1.9.0...v1.9.1) (2022-07-08)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* remove test console.log ([e70647f](https://github.com/CoCreate-app/CoCreate-render/commit/e70647f9aaedfab5ecdc4f38e755eb7cf7c564fe))
|
|
7
|
+
* store original data while rendering to have access to root fields eg. collection ([c769747](https://github.com/CoCreate-app/CoCreate-render/commit/c76974721597e1c8d3d8b531a81bf9216b40c731))
|
|
8
|
+
|
|
1
9
|
# [1.9.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.8.4...v1.9.0) (2022-07-03)
|
|
2
10
|
|
|
3
11
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/*globals CustomEvent*/
|
|
2
2
|
import action from '@cocreate/actions';
|
|
3
3
|
import { queryDocumentSelector } from '@cocreate/utils';
|
|
4
|
+
// import { getValue } from '../../CoCreate-elements/src/getValue';
|
|
4
5
|
// import api from '@cocreate/api';
|
|
5
6
|
|
|
6
7
|
const CoCreateRender = {
|
|
@@ -9,6 +10,8 @@ const CoCreateRender = {
|
|
|
9
10
|
try {
|
|
10
11
|
if(typeof json == 'undefined' || !path)
|
|
11
12
|
return false;
|
|
13
|
+
if (path.indexOf('.') == -1)
|
|
14
|
+
json = this.dataOriginal
|
|
12
15
|
let jsonData = json, subpath = path.split('.');
|
|
13
16
|
|
|
14
17
|
for (let i = 0; i < subpath.length; i++) {
|
|
@@ -56,18 +59,24 @@ const CoCreateRender = {
|
|
|
56
59
|
let variables = inputValue.match(/{{([A-Za-z0-9_.,\[\]\- ]*)}}/g);
|
|
57
60
|
if (variables) {
|
|
58
61
|
variables.forEach((attr) => {
|
|
62
|
+
if (attr.includes(`collection`))
|
|
63
|
+
if( this.dataOriginal[renderKey] && this.dataOriginal[renderKey]['collection'] && attr.includes(`{{${renderKey}.`))
|
|
64
|
+
data[renderKey]['collection'] = this.dataOriginal[renderKey]['collection']
|
|
65
|
+
|
|
59
66
|
let value = self.__getValue(data, attr);
|
|
60
67
|
// if (value) {
|
|
61
68
|
// if (value && typeof(value) !== "object") {
|
|
62
|
-
if (value && !Array.isArray(value)) {
|
|
69
|
+
// if (value && !Array.isArray(value)) {
|
|
70
|
+
if (value) {
|
|
63
71
|
// converts object to string
|
|
64
72
|
// if (!Array.isArray(value) && typeof(value) == "object") {
|
|
65
73
|
if (typeof(value) == "object") {
|
|
66
|
-
let str = '';
|
|
67
|
-
for (const [key, val] of Object.entries(value)) {
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
|
|
74
|
+
// let str = '';
|
|
75
|
+
// for (const [key, val] of Object.entries(value)) {
|
|
76
|
+
// str += `${key}: ${val}\n`;
|
|
77
|
+
// }
|
|
78
|
+
|
|
79
|
+
value = this.generateString(value)
|
|
71
80
|
}
|
|
72
81
|
isPass = true;
|
|
73
82
|
inputValue = inputValue.replace(attr, value);
|
|
@@ -83,6 +92,25 @@ const CoCreateRender = {
|
|
|
83
92
|
}
|
|
84
93
|
return resultValue;
|
|
85
94
|
},
|
|
95
|
+
|
|
96
|
+
generateString: function(value, str = ''){
|
|
97
|
+
// let str = '';
|
|
98
|
+
// do {
|
|
99
|
+
let flag = true;
|
|
100
|
+
if (str)
|
|
101
|
+
flag = false;
|
|
102
|
+
for (const [key, val] of Object.entries(value)) {
|
|
103
|
+
if (typeof(val) == "object") {
|
|
104
|
+
str += `${key}: \n`;
|
|
105
|
+
this.generateString(val, str)
|
|
106
|
+
}
|
|
107
|
+
else
|
|
108
|
+
str += `${key}: ${val}\n`;
|
|
109
|
+
}
|
|
110
|
+
// } while()
|
|
111
|
+
if (flag)
|
|
112
|
+
return str
|
|
113
|
+
},
|
|
86
114
|
|
|
87
115
|
render: function(template, data) {
|
|
88
116
|
let type = template.getAttribute('render-array') || "data";
|
|
@@ -94,17 +122,7 @@ const CoCreateRender = {
|
|
|
94
122
|
|
|
95
123
|
const isRenderObject = template.hasAttribute('render-object');
|
|
96
124
|
if (isRenderObject){
|
|
97
|
-
const renderObject = template.getAttribute('render-object');
|
|
98
|
-
if (renderObject)
|
|
99
|
-
arrayData = data[renderObject];
|
|
100
|
-
|
|
101
|
-
let array = []
|
|
102
|
-
for (const [key, value] of Object.entries(arrayData)) {
|
|
103
|
-
array.push({key: key, value: value})
|
|
104
|
-
}
|
|
105
|
-
if (array.length > 0)
|
|
106
|
-
arrayData = array
|
|
107
|
-
|
|
125
|
+
const renderObject = template.getAttribute('render-object');
|
|
108
126
|
type = renderObject || 'data'
|
|
109
127
|
}
|
|
110
128
|
|
|
@@ -115,29 +133,52 @@ const CoCreateRender = {
|
|
|
115
133
|
arrayData = data[renderArray];
|
|
116
134
|
type = renderArray || 'data';
|
|
117
135
|
}
|
|
136
|
+
if (isRenderObject && type) {
|
|
137
|
+
let r_data = self.isRenderObject(arrayData[type], renderKey)
|
|
138
|
+
for (let sdata of r_data) {
|
|
139
|
+
let cloneEl = this.cloneEl(template);
|
|
140
|
+
cloneEl.classList.add('clone_' + type);
|
|
141
|
+
self.setValue([cloneEl], sdata, renderKey);
|
|
142
|
+
template.insertAdjacentHTML('beforebegin', cloneEl.outerHTML);
|
|
143
|
+
}
|
|
144
|
+
} else {
|
|
118
145
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (!arrayData) {
|
|
123
|
-
let cloneEl = this.cloneEl(template);
|
|
124
|
-
cloneEl.classList.add('cloned');
|
|
125
|
-
self.setValue([cloneEl], data, renderKey);
|
|
126
|
-
template.insertAdjacentHTML('beforebegin', cloneEl.outerHTML);
|
|
127
|
-
}
|
|
146
|
+
if (!Array.isArray(arrayData))
|
|
147
|
+
arrayData = this.__getValueFromObject(data, type);
|
|
128
148
|
|
|
129
|
-
|
|
130
|
-
arrayData.forEach((item) => {
|
|
149
|
+
if (!arrayData) {
|
|
131
150
|
let cloneEl = this.cloneEl(template);
|
|
132
|
-
cloneEl.classList.add('
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
self.setValue([cloneEl], r_data, renderKey);
|
|
151
|
+
cloneEl.classList.add('cloned');
|
|
152
|
+
self.setValue([cloneEl], data, renderKey);
|
|
136
153
|
template.insertAdjacentHTML('beforebegin', cloneEl.outerHTML);
|
|
137
|
-
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if(type && Array.isArray(arrayData)) {
|
|
157
|
+
arrayData.forEach((item) => {
|
|
158
|
+
let cloneEl = this.cloneEl(template);
|
|
159
|
+
cloneEl.classList.add('clone_' + type);
|
|
160
|
+
let r_data = self.__createObject(item, renderKey);
|
|
161
|
+
self.setValue([cloneEl], r_data, renderKey);
|
|
162
|
+
template.insertAdjacentHTML('beforebegin', cloneEl.outerHTML);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
138
165
|
}
|
|
139
166
|
},
|
|
140
167
|
|
|
168
|
+
isRenderObject: function(data, renderKey) {
|
|
169
|
+
let array = []
|
|
170
|
+
for (const [key, value] of Object.entries(data)) {
|
|
171
|
+
let type = 'string';
|
|
172
|
+
if (typeof(value) == "object")
|
|
173
|
+
if (Array.isArray(value))
|
|
174
|
+
type = 'array'
|
|
175
|
+
else
|
|
176
|
+
type = 'object'
|
|
177
|
+
array.push({[renderKey]: {key, value, type}})
|
|
178
|
+
}
|
|
179
|
+
return array
|
|
180
|
+
},
|
|
181
|
+
|
|
141
182
|
cloneEl: function(template) {
|
|
142
183
|
let cloneEl = template.cloneNode(true);
|
|
143
184
|
cloneEl.classList.remove('template');
|
|
@@ -146,8 +187,8 @@ const CoCreateRender = {
|
|
|
146
187
|
cloneEl.setAttribute('templateId', templateId);
|
|
147
188
|
return cloneEl;
|
|
148
189
|
},
|
|
149
|
-
|
|
150
|
-
setValue:function(els, data, renderKey){
|
|
190
|
+
|
|
191
|
+
setValue: function(els, data, renderKey){
|
|
151
192
|
if (!data) return;
|
|
152
193
|
const that = this;
|
|
153
194
|
Array.from(els).forEach(el => {
|
|
@@ -162,17 +203,10 @@ const CoCreateRender = {
|
|
|
162
203
|
}
|
|
163
204
|
});
|
|
164
205
|
|
|
165
|
-
// if (el.classList.contains('template')) {
|
|
166
|
-
// that.render(el, data);
|
|
167
|
-
// }
|
|
168
|
-
// if(el.childNodes.length > 0) {
|
|
169
|
-
// that.setValue(el.childNodes, data, renderKey);
|
|
170
|
-
// }
|
|
171
|
-
|
|
172
206
|
if(el.childNodes.length > 0) {
|
|
173
207
|
that.setValue(el.childNodes, data, renderKey);
|
|
174
208
|
}
|
|
175
|
-
if (el.classList.contains('template')) {
|
|
209
|
+
if (el.classList.contains('template') && !el.hasAttribute('template_id')) {
|
|
176
210
|
that.render(el, data);
|
|
177
211
|
}
|
|
178
212
|
}
|
|
@@ -195,7 +229,9 @@ const CoCreateRender = {
|
|
|
195
229
|
});
|
|
196
230
|
},
|
|
197
231
|
|
|
232
|
+
dataOriginal: {},
|
|
198
233
|
data: function({selector, data, elements}) {
|
|
234
|
+
this.dataOriginal = data;
|
|
199
235
|
if (selector) {
|
|
200
236
|
let template = queryDocumentSelector(selector);
|
|
201
237
|
if (!template) return;
|
|
@@ -205,7 +241,16 @@ const CoCreateRender = {
|
|
|
205
241
|
else
|
|
206
242
|
this.setValue([template], data);
|
|
207
243
|
} else if (elements) {
|
|
208
|
-
|
|
244
|
+
if (elements.length == 1 && elements[0].classList.contains('template'))
|
|
245
|
+
this.render(elements[0], data);
|
|
246
|
+
else
|
|
247
|
+
this.setValue(elements, data);
|
|
248
|
+
// for (let element of elements) {
|
|
249
|
+
// if (element.classList.contains('template'))
|
|
250
|
+
// this.render(element, data);
|
|
251
|
+
// else
|
|
252
|
+
// this.setValue([element], data);
|
|
253
|
+
// }
|
|
209
254
|
}
|
|
210
255
|
|
|
211
256
|
}
|