@cocreate/render 1.19.2 → 1.21.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,28 @@
1
+ # [1.21.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.20.0...v1.21.0) (2023-01-10)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * bump dependencies ([115c000](https://github.com/CoCreate-app/CoCreate-render/commit/115c0005e3b4a0e4fc2cbc7c2dc80083bfa7b135))
7
+
8
+
9
+ ### Features
10
+
11
+ * run cocreate.pass prior to rendering in dom ([968b82b](https://github.com/CoCreate-app/CoCreate-render/commit/968b82b9fa159e1196729555ebf40228cca92017))
12
+
13
+ # [1.20.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.19.2...v1.20.0) (2023-01-09)
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * bump dependnecies ([0c049d8](https://github.com/CoCreate-app/CoCreate-render/commit/0c049d806a515842f2cf0928073f1cae14868ea2))
19
+ * replaceValue send el for possible retrieval of data from parent templates ([902391a](https://github.com/CoCreate-app/CoCreate-render/commit/902391a0efa8b9491c2f13326eb93799516d803d))
20
+
21
+
22
+ ### Features
23
+
24
+ * if key and value could not be found check in parent template ([b275832](https://github.com/CoCreate-app/CoCreate-render/commit/b27583278c5ee5ddda2697fa68deaded5f53374d))
25
+
1
26
  ## [1.19.2](https://github.com/CoCreate-app/CoCreate-render/compare/v1.19.1...v1.19.2) (2023-01-06)
2
27
 
3
28
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/render",
3
- "version": "1.19.2",
3
+ "version": "1.21.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.52",
64
+ "@cocreate/api": "^1.6.3",
65
+ "@cocreate/docs": "^1.5.6",
66
+ "@cocreate/element-prototype": "^1.4.0",
67
+ "@cocreate/observer": "^1.5.56",
68
+ "@cocreate/utils": "^1.17.19"
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.renderedData)
28
+ value = getValueFromObject(parentTemplate.renderedData, 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.renderedData)
232
+ el.renderedData = {...data}
233
+ else if (Object.keys(el.renderedData)[0] == Object.keys(data)[0]) {
234
+ el.renderedData = {...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,38 +270,48 @@ 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);
281
301
  }
282
302
  });
283
303
 
304
+ if (CoCreate.pass) {
305
+ if (el.hasAttribute('[pass_id]'))
306
+ CoCreate.pass.initElement(el)
307
+ }
308
+
284
309
  if (el.childNodes.length > 0) {
285
310
  that.setValue(el.childNodes, updateData || data, renderArray, renderKey);
286
311
  }
287
312
  if ((el.tagName == 'TEMPLATE' || el.hasAttribute('template') || el.classList.contains('template')) && !el.hasAttribute('template_id')) {
288
- that.render(el, data);
313
+ if (el.getAttribute('render-object') || el.getAttribute('render-array'))
314
+ that.render(el, data);
289
315
  }
290
316
 
291
317
  }
@@ -307,13 +333,13 @@ const CoCreateRender = {
307
333
  updateData = data[renderArray]
308
334
  if (renderKey)
309
335
  updateData = {[renderKey]: updateData}
310
- text = that.__replaceValue(updateData, textContent, renderKey, valueType);
336
+ text = that.__replaceValue(updateData, textContent, renderKey, el);
311
337
  }
312
338
  if (!placeholder && !text) {
313
339
  textContent = el.textContent;
314
340
  if (!el.renderMap)
315
341
  that.renderMap(el, textContent, renderArray, renderKey)
316
- text = that.__replaceValue(data, textContent, renderKey, valueType);
342
+ text = that.__replaceValue(data, textContent, renderKey, el);
317
343
  }
318
344
 
319
345
  if (text || text == "") {
@@ -402,15 +428,15 @@ observer.init({
402
428
  if (parentElement) {
403
429
  let el = element
404
430
  let parentKeys = [];
405
- let renderData = new Map();
431
+ let renderedData = new Map();
406
432
  do {
407
433
  let data;
408
434
  el = el.parentElement
409
435
  if (el) {
410
436
  if (el.hasAttribute('render-clone'))
411
- data = el.renderData
437
+ data = el.renderedData
412
438
  if (data) {
413
- renderData.set(data, '')
439
+ renderedData.set(data, '')
414
440
  }
415
441
  let parentKey = el.getAttribute('parentKey')
416
442
  if (parentKey && parentKey != null) {
@@ -450,7 +476,7 @@ observer.init({
450
476
 
451
477
  element = parentElement.firstElementChild
452
478
  let obj = {}
453
- let array = Array.from(renderData.keys())
479
+ let array = Array.from(renderedData.keys())
454
480
  for (let data of array.reverse()){
455
481
  obj = {...obj, ...data}
456
482
  }