@cocreate/render 1.19.2 → 1.20.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 CHANGED
@@ -1,3 +1,16 @@
1
+ # [1.20.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.19.2...v1.20.0) (2023-01-09)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * bump dependnecies ([0c049d8](https://github.com/CoCreate-app/CoCreate-render/commit/0c049d806a515842f2cf0928073f1cae14868ea2))
7
+ * replaceValue send el for possible retrieval of data from parent templates ([902391a](https://github.com/CoCreate-app/CoCreate-render/commit/902391a0efa8b9491c2f13326eb93799516d803d))
8
+
9
+
10
+ ### Features
11
+
12
+ * if key and value could not be found check in parent template ([b275832](https://github.com/CoCreate-app/CoCreate-render/commit/b27583278c5ee5ddda2697fa68deaded5f53374d))
13
+
1
14
  ## [1.19.2](https://github.com/CoCreate-app/CoCreate-render/compare/v1.19.1...v1.19.2) (2023-01-06)
2
15
 
3
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/render",
3
- "version": "1.19.2",
3
+ "version": "1.20.0",
4
4
  "description": "A simple render component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "render",
@@ -60,11 +60,11 @@
60
60
  "webpack-log": "^3.0.1"
61
61
  },
62
62
  "dependencies": {
63
- "@cocreate/actions": "^1.5.50",
64
- "@cocreate/api": "^1.6.1",
65
- "@cocreate/docs": "^1.5.4",
66
- "@cocreate/element-prototype": "^1.2.25",
67
- "@cocreate/observer": "^1.5.54",
68
- "@cocreate/utils": "^1.17.16"
63
+ "@cocreate/actions": "^1.5.51",
64
+ "@cocreate/api": "^1.6.2",
65
+ "@cocreate/docs": "^1.5.5",
66
+ "@cocreate/element-prototype": "^1.2.27",
67
+ "@cocreate/observer": "^1.5.55",
68
+ "@cocreate/utils": "^1.17.18"
69
69
  }
70
70
  }
package/src/index.js CHANGED
@@ -9,10 +9,31 @@ import './index.css';
9
9
 
10
10
  const CoCreateRender = {
11
11
 
12
- __getValue: function(data, attrValue) {
12
+ __getValue: function(data, attrValue, el) {
13
13
  let result = /{{\s*([\w\W]+)\s*}}/g.exec(attrValue);
14
14
  if (result) {
15
- return getValueFromObject(data, result[1].trim());
15
+ let value = getValueFromObject(data, result[1].trim());
16
+ if (!value) {
17
+ let parentTemplate
18
+ if (el && el.nodeType == 1) {
19
+ if (el.hasAttribute('templateid') && el.parentElement)
20
+ parentTemplate = el.parentElement.closest('[templateid]')
21
+ else
22
+ parentTemplate = el.closest('[templateid]')
23
+ }
24
+
25
+ if (parentTemplate) {
26
+ do {
27
+ if (parentTemplate.renderData)
28
+ value = getValueFromObject(parentTemplate.renderData, result[1].trim());
29
+ if (!value && parentTemplate.parentElement)
30
+ parentTemplate = parentTemplate.parentElement.closest('[templateid]')
31
+ else
32
+ parentTemplate = ""
33
+ } while (!value && parentTemplate)
34
+ }
35
+ }
36
+ return value;
16
37
  }
17
38
  return false;
18
39
  },
@@ -35,39 +56,24 @@ const CoCreateRender = {
35
56
  }
36
57
  },
37
58
 
38
- __replaceValue: function(data, inputValue, renderKey, valueType) {
39
- let isPass = false;
40
- let self = this;
41
- let resultValue = null;
42
- let variables = inputValue.match(/{{([A-Za-z0-9_.,\[\]\- ]*)}}/g);
43
- if (variables) {
44
- variables.forEach((attr) => {
45
- let value;
46
-
47
- // if (attr == "{{data._id}}" || attr == `{{${renderKey}._id}}` || attr == "{{document_id}}" || attr == "{{document._id}}")
48
- // value = this.document_id;
49
- // else if (attr == "{{collection}" || attr == `{{${renderKey}.collection}}`)
50
- // value = this.collection;
51
- // else
52
- value = self.__getValue(data, attr);
59
+ __replaceValue: function(data, inputValue, renderKey, el) {
60
+ let outputValue = null;
61
+ let placeholders = inputValue.match(/{{([A-Za-z0-9_.,\[\]\- ]*)}}/g);
62
+ if (placeholders) {
63
+ for (let placeholder of placeholders) {
64
+ let value = this.__getValue(data, placeholder, el);
53
65
 
54
66
  if (value) {
55
- if (typeof(value) == "object") {
67
+ if (typeof(value) == "object")
56
68
  value = this.generateString(value)
57
- }
58
- isPass = true;
59
- inputValue = inputValue.replace(attr, value);
60
- }
61
- if (!isPass && renderKey && attr.includes(`{{${renderKey}.`)) {
62
- resultValue = '';
69
+
70
+ outputValue = inputValue.replace(placeholder, value);
71
+ } else if (renderKey && placeholder.includes(`{{${renderKey}.`)) {
72
+ outputValue = '';
63
73
  }
64
- });
65
-
66
- if (isPass) {
67
- resultValue = inputValue;
68
74
  }
69
75
  }
70
- return resultValue;
76
+ return outputValue;
71
77
  },
72
78
 
73
79
  generateString: function(value, str = ''){
@@ -212,7 +218,6 @@ const CoCreateRender = {
212
218
  document_id: '',
213
219
  setValue: function(els, data, renderArray, renderKey){
214
220
  if (!data) return;
215
-
216
221
  let isRenderKey
217
222
  if (data.renderKey)
218
223
  isRenderKey = true
@@ -223,22 +228,33 @@ const CoCreateRender = {
223
228
 
224
229
  if (el.nodeType == 1) {
225
230
  if (el.hasAttribute('render-clone')) {
226
- el.renderData = {...data}
231
+ if (!el.renderData)
232
+ el.renderData = {...data}
233
+ else if (Object.keys(el.renderData)[0] == Object.keys(data)[0]) {
234
+ el.renderData = {...data}
235
+ }
236
+
227
237
  }
238
+
228
239
  if (el.renderMap && !isRenderKey) {
229
240
  let placeholder = el.renderMap.get(el)
230
241
  if (placeholder){
231
- let valueType = el.getAttribute('value-type')
232
242
  renderKey = placeholder.renderKey
233
243
  renderArray = placeholder.renderArray
234
244
  if (renderArray && Array.isArray(data[renderArray]))
235
245
  updateData = data[renderArray][0]
236
- else
246
+ else if (data[renderArray])
237
247
  updateData = data[renderArray]
238
- if (renderKey)
239
- updateData = {[renderKey]: updateData}
248
+
249
+ if (renderKey) {
250
+ if (updateData)
251
+ updateData = {[renderKey]: updateData}
252
+ else
253
+ updateData = {[renderKey]: data}
254
+ }
255
+
240
256
  let textContent = placeholder.placeholder
241
- let text = that.__replaceValue(updateData, textContent, renderKey, valueType);
257
+ let text = that.__replaceValue(updateData, textContent, renderKey, el);
242
258
  if (text && text != el.renderedValue)
243
259
  el.innerHTML = placeholder.placeholder
244
260
  }
@@ -254,27 +270,31 @@ const CoCreateRender = {
254
270
  that.renderMap(attr, attr.value, renderArray, renderKey)
255
271
 
256
272
  if (placeholder && !isRenderKey){
257
- let updateData = data;
258
- // let oldValue = attrValue
259
273
  let temp = placeholder.placeholder;
260
- renderKey = placeholder.renderKey
261
- renderArray = placeholder.renderArray
262
- if (renderArray && Array.isArray(data[renderArray]))
263
- updateData = data[renderArray][0]
264
- else
265
- updateData = data[renderArray]
266
- if (renderKey && updateData)
267
- updateData = {[renderKey]: updateData}
268
-
269
- attrValue = that.__replaceValue(updateData, temp, renderKey);
274
+ // let updateData
275
+ // renderKey = placeholder.renderKey
276
+ // renderArray = placeholder.renderArray
277
+ // if (renderArray && Array.isArray(data[renderArray]))
278
+ // updateData = data[renderArray][0]
279
+ // else if (data[renderArray])
280
+ // updateData = data[renderArray]
281
+
282
+ // if (renderKey) {
283
+ // if (updateData)
284
+ // updateData = {[renderKey]: updateData}
285
+ // else
286
+ // updateData = {[renderKey]: data}
287
+ // }
288
+
289
+ attrValue = that.__replaceValue(updateData, temp, renderKey, el);
270
290
  if (attrValue == attr.value)
271
291
  attrValue = undefined
272
292
  }
273
293
  else
274
- attrValue = that.__replaceValue(data, attrValue, renderKey);
294
+ attrValue = that.__replaceValue(data, attrValue, renderKey, el);
275
295
 
276
296
  // ToDo: support attibute name replace if has {{}}
277
- // attr_name = that.__replaceValue(data, attr_name, renderKey);
297
+ // attr_name = that.__replaceValue(data, attr_name, renderKey, el);
278
298
 
279
299
  if (attrValue || attrValue == "") {
280
300
  el.setAttribute(attr_name, attrValue);
@@ -285,7 +305,8 @@ const CoCreateRender = {
285
305
  that.setValue(el.childNodes, updateData || data, renderArray, renderKey);
286
306
  }
287
307
  if ((el.tagName == 'TEMPLATE' || el.hasAttribute('template') || el.classList.contains('template')) && !el.hasAttribute('template_id')) {
288
- that.render(el, data);
308
+ if (el.getAttribute('render-object') || el.getAttribute('render-array'))
309
+ that.render(el, data);
289
310
  }
290
311
 
291
312
  }
@@ -307,13 +328,13 @@ const CoCreateRender = {
307
328
  updateData = data[renderArray]
308
329
  if (renderKey)
309
330
  updateData = {[renderKey]: updateData}
310
- text = that.__replaceValue(updateData, textContent, renderKey, valueType);
331
+ text = that.__replaceValue(updateData, textContent, renderKey, el);
311
332
  }
312
333
  if (!placeholder && !text) {
313
334
  textContent = el.textContent;
314
335
  if (!el.renderMap)
315
336
  that.renderMap(el, textContent, renderArray, renderKey)
316
- text = that.__replaceValue(data, textContent, renderKey, valueType);
337
+ text = that.__replaceValue(data, textContent, renderKey, el);
317
338
  }
318
339
 
319
340
  if (text || text == "") {