@cocreate/render 1.9.2 → 1.10.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 +7 -0
- package/package.json +1 -1
- package/src/index.js +69 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [1.10.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.9.2...v1.10.0) (2022-07-14)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* renderMap function to apply map containing placeholder to node object in order to render new values ([7afffa0](https://github.com/CoCreate-app/CoCreate-render/commit/7afffa07b2cc1dd562e96d1ee558433d3f6d043a))
|
|
7
|
+
|
|
1
8
|
## [1.9.2](https://github.com/CoCreate-app/CoCreate-render/compare/v1.9.1...v1.9.2) (2022-07-10)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -52,7 +52,7 @@ const CoCreateRender = {
|
|
|
52
52
|
}
|
|
53
53
|
},
|
|
54
54
|
|
|
55
|
-
__replaceValue: function(data, inputValue, renderKey) {
|
|
55
|
+
__replaceValue: function(data, inputValue, renderKey, valueType) {
|
|
56
56
|
let isPass = false;
|
|
57
57
|
let self = this;
|
|
58
58
|
let resultValue = null;
|
|
@@ -128,7 +128,7 @@ const CoCreateRender = {
|
|
|
128
128
|
let cloneEl = this.cloneEl(template);
|
|
129
129
|
cloneEl.classList.add('clone_' + type);
|
|
130
130
|
self.setValue([cloneEl], sdata, renderKey);
|
|
131
|
-
template.
|
|
131
|
+
template.insertAdjacentElement('beforebegin', cloneEl);
|
|
132
132
|
}
|
|
133
133
|
} else {
|
|
134
134
|
|
|
@@ -139,7 +139,7 @@ const CoCreateRender = {
|
|
|
139
139
|
let cloneEl = this.cloneEl(template);
|
|
140
140
|
cloneEl.classList.add('cloned');
|
|
141
141
|
self.setValue([cloneEl], data, renderKey);
|
|
142
|
-
template.
|
|
142
|
+
template.insertAdjacentElement('beforebegin', cloneEl);
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
if(type && Array.isArray(arrayData)) {
|
|
@@ -148,7 +148,7 @@ const CoCreateRender = {
|
|
|
148
148
|
cloneEl.classList.add('clone_' + type);
|
|
149
149
|
let r_data = self.__createObject(item, renderKey);
|
|
150
150
|
self.setValue([cloneEl], r_data, renderKey);
|
|
151
|
-
template.
|
|
151
|
+
template.insertAdjacentElement('beforebegin', cloneEl);
|
|
152
152
|
});
|
|
153
153
|
}
|
|
154
154
|
}
|
|
@@ -181,42 +181,101 @@ const CoCreateRender = {
|
|
|
181
181
|
if (!data) return;
|
|
182
182
|
const that = this;
|
|
183
183
|
Array.from(els).forEach(el => {
|
|
184
|
+
|
|
184
185
|
if (el.nodeType == 1) {
|
|
186
|
+
if (el.renderMap) {
|
|
187
|
+
let placeholder = el.renderMap.get(el)
|
|
188
|
+
if (placeholder){
|
|
189
|
+
el.innerHTML = placeholder.placeholder
|
|
190
|
+
renderKey = placeholder.renderKey
|
|
191
|
+
if (data[renderKey][0])
|
|
192
|
+
data = {[renderKey]: data[renderKey][0]}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
185
196
|
Array.from(el.attributes).forEach(attr=>{
|
|
186
197
|
let attr_name = attr.name.toLowerCase();
|
|
187
198
|
let attrValue = attr.value;
|
|
188
|
-
|
|
199
|
+
|
|
200
|
+
// get placeholder
|
|
201
|
+
let placeholder
|
|
202
|
+
if (attr.renderMap)
|
|
203
|
+
placeholder = attr.renderMap.get(attr)
|
|
204
|
+
else
|
|
205
|
+
that.renderMap(attr, attr.value, renderKey)
|
|
206
|
+
|
|
207
|
+
if(placeholder){
|
|
208
|
+
attrValue = placeholder.placeholder;
|
|
209
|
+
renderKey = placeholder.renderKey
|
|
210
|
+
let updateData = data;
|
|
211
|
+
if (data[renderKey][0])
|
|
212
|
+
updateData = {[renderKey]: data[renderKey][0]}
|
|
213
|
+
attrValue = that.__replaceValue(updateData, attrValue, renderKey);
|
|
214
|
+
}
|
|
215
|
+
else
|
|
216
|
+
attrValue = that.__replaceValue(data, attrValue, renderKey);
|
|
189
217
|
|
|
190
218
|
if (attrValue || attrValue == "") {
|
|
191
219
|
el.setAttribute(attr_name, attrValue);
|
|
192
220
|
}
|
|
193
221
|
});
|
|
194
222
|
|
|
195
|
-
if(el.childNodes.length > 0) {
|
|
223
|
+
if(el.childNodes.length > 0) {
|
|
196
224
|
that.setValue(el.childNodes, data, renderKey);
|
|
197
225
|
}
|
|
198
226
|
if (el.classList.contains('template') && !el.hasAttribute('template_id')) {
|
|
199
227
|
that.render(el, data);
|
|
200
228
|
}
|
|
201
229
|
}
|
|
202
|
-
|
|
230
|
+
|
|
203
231
|
if (el.nodeType == 3) {
|
|
204
232
|
let valueType = el.parentElement.getAttribute('value-type')
|
|
205
|
-
|
|
206
|
-
|
|
233
|
+
|
|
234
|
+
// get placeholder
|
|
235
|
+
let textContent, placeholder, text;
|
|
236
|
+
if (el.renderMap)
|
|
237
|
+
placeholder = el.renderMap.get(el)
|
|
238
|
+
if (placeholder) {
|
|
239
|
+
textContent = placeholder.placeholder
|
|
240
|
+
renderKey = placeholder.renderKey
|
|
241
|
+
let updateData = data;
|
|
242
|
+
if (data[renderKey][0])
|
|
243
|
+
updateData = {[renderKey]: data[renderKey][0]}
|
|
244
|
+
text = that.__replaceValue(updateData, textContent, renderKey, valueType);
|
|
245
|
+
}
|
|
246
|
+
if (!text) {
|
|
247
|
+
textContent = el.textContent;
|
|
248
|
+
that.renderMap(el, textContent, renderKey)
|
|
249
|
+
text = that.__replaceValue(data, textContent, renderKey, valueType);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// let textContent = el.textContent;
|
|
253
|
+
// let text = that.__replaceValue(data, textContent, renderKey, valueType);
|
|
207
254
|
if (text || text == "") {
|
|
208
255
|
if (valueType == 'text' || valueType == 'string'){
|
|
209
256
|
el.textContent = text;
|
|
210
257
|
} else {
|
|
211
258
|
const newNode = document.createElement('div');
|
|
212
259
|
newNode.innerHTML = text;
|
|
260
|
+
let parentElement = el.parentElement
|
|
261
|
+
if (!parentElement.renderMap) {
|
|
262
|
+
that.renderMap(parentElement, textContent, renderKey)
|
|
263
|
+
} else if (!parentElement['renderMap'].has(parentElement))
|
|
264
|
+
that.renderMap(parentElement, textContent, renderKey)
|
|
213
265
|
el.replaceWith(...newNode.childNodes)
|
|
214
266
|
}
|
|
215
267
|
}
|
|
216
268
|
}
|
|
217
|
-
|
|
218
269
|
});
|
|
219
270
|
},
|
|
271
|
+
|
|
272
|
+
renderMap: function(node, placeholder, renderKey) {
|
|
273
|
+
if (!node.renderMap) {
|
|
274
|
+
node['renderMap'] = new Map();
|
|
275
|
+
}
|
|
276
|
+
if (placeholder && renderKey)
|
|
277
|
+
node['renderMap'].set(node, {placeholder, renderKey})
|
|
278
|
+
},
|
|
220
279
|
|
|
221
280
|
dataOriginal: {},
|
|
222
281
|
data: function({selector, data, elements}) {
|