@cocreate/render 1.8.3 → 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 CHANGED
@@ -1,3 +1,26 @@
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
+
9
+ # [1.9.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.8.4...v1.9.0) (2022-07-03)
10
+
11
+
12
+ ### Features
13
+
14
+ * added @cocreate/utils queryDocumentSelector to return an element from ann irframe or parent of iframe ([b9bb90f](https://github.com/CoCreate-app/CoCreate-render/commit/b9bb90f9ab72fc24fc71e64a37bcfee73a0c0c9c))
15
+ * support for value-type attribute to define if render will be a string, text, innerHTML. Defaults to innerHTML ([a14fcc2](https://github.com/CoCreate-app/CoCreate-render/commit/a14fcc2ff886df7207c537cca67c3343be2da997))
16
+
17
+ ## [1.8.4](https://github.com/CoCreate-app/CoCreate-render/compare/v1.8.3...v1.8.4) (2022-06-19)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * update templates to use placeholder ([9299cce](https://github.com/CoCreate-app/CoCreate-render/commit/9299ccef3864ffe087044a3614aa35799e89f31c))
23
+
1
24
  ## [1.8.3](https://github.com/CoCreate-app/CoCreate-render/compare/v1.8.2...v1.8.3) (2022-06-18)
2
25
 
3
26
 
@@ -19,8 +19,8 @@
19
19
  </cc-options>
20
20
 
21
21
  <div class="template" template_id="abc1">
22
- <div class="template" render-array="data.current_org" render-key="current_org" value="{{current_org}}">
23
- <h3>{{current_org}}</h3>
22
+ <div class="template" render-array="data.current" render-key="current_org" value="{{current_org.name}}">
23
+ <h3>{{current_org.name}}</h3>
24
24
  </div>
25
25
  </div>
26
26
 
@@ -34,7 +34,8 @@
34
34
  collection : 'dededede',
35
35
  data: [{
36
36
  _id : '123kijfhgfkel45',
37
- current_org: ['test1', 'test2', 'test31', 'test3']
37
+ current_org: ['test1', 'test2', 'test31', 'test3'],
38
+ current: [{name: 'test1w'}, {name: 'test2'}, {name: 'test31'}, {name: 'test3'}]
38
39
  }]
39
40
  }
40
41
 
@@ -11,8 +11,8 @@
11
11
  <body>
12
12
 
13
13
  <div class="template" template_id="abc1">
14
- <div>{{data}}</div>
15
- <h1> {{data.collection}} {{data._id}}</h1>
14
+ <!-- <div>{{data}}</div>
15
+ <h1> {{data.collection}} {{data._id}}</h1> -->
16
16
  <div class="template" render-object="" render-key="one">
17
17
  <h5>{{one.key}}: {{one.value}}</h5>
18
18
  </div>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/render",
3
- "version": "1.8.3",
3
+ "version": "1.9.1",
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",
@@ -63,6 +63,7 @@
63
63
  "dependencies": {
64
64
  "@cocreate/actions": "^1.4.3",
65
65
  "@cocreate/api": "^1.3.3",
66
- "@cocreate/docs": "^1.3.3"
66
+ "@cocreate/docs": "^1.3.3",
67
+ "@cocreate/utils": "^1.8.1"
67
68
  }
68
69
  }
package/src/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  /*globals CustomEvent*/
2
2
  import action from '@cocreate/actions';
3
+ import { queryDocumentSelector } from '@cocreate/utils';
4
+ // import { getValue } from '../../CoCreate-elements/src/getValue';
3
5
  // import api from '@cocreate/api';
4
6
 
5
7
  const CoCreateRender = {
@@ -8,6 +10,8 @@ const CoCreateRender = {
8
10
  try {
9
11
  if(typeof json == 'undefined' || !path)
10
12
  return false;
13
+ if (path.indexOf('.') == -1)
14
+ json = this.dataOriginal
11
15
  let jsonData = json, subpath = path.split('.');
12
16
 
13
17
  for (let i = 0; i < subpath.length; i++) {
@@ -48,29 +52,38 @@ const CoCreateRender = {
48
52
  }
49
53
  },
50
54
 
51
- __replaceValue: function(data, inputValue) {
55
+ __replaceValue: function(data, inputValue, renderKey) {
52
56
  let isPass = false;
53
57
  let self = this;
54
58
  let resultValue = null;
55
59
  let variables = inputValue.match(/{{([A-Za-z0-9_.,\[\]\- ]*)}}/g);
56
60
  if (variables) {
57
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
+
58
66
  let value = self.__getValue(data, attr);
59
67
  // if (value) {
60
68
  // if (value && typeof(value) !== "object") {
61
- if (value && !Array.isArray(value)) {
69
+ // if (value && !Array.isArray(value)) {
70
+ if (value) {
62
71
  // converts object to string
63
72
  // if (!Array.isArray(value) && typeof(value) == "object") {
64
73
  if (typeof(value) == "object") {
65
- let str = '';
66
- for (const [key, val] of Object.entries(value)) {
67
- str += `${key}: ${val}\n`;
68
- }
69
- value = str
74
+ // let str = '';
75
+ // for (const [key, val] of Object.entries(value)) {
76
+ // str += `${key}: ${val}\n`;
77
+ // }
78
+
79
+ value = this.generateString(value)
70
80
  }
71
81
  isPass = true;
72
82
  inputValue = inputValue.replace(attr, value);
73
83
  }
84
+ if (!isPass && renderKey && attr.includes(`{{${renderKey}.`)) {
85
+ resultValue = '';
86
+ }
74
87
  });
75
88
 
76
89
  if (isPass) {
@@ -79,10 +92,29 @@ const CoCreateRender = {
79
92
  }
80
93
  return resultValue;
81
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
+ },
82
114
 
83
115
  render: function(template, data) {
84
116
  let type = template.getAttribute('render-array') || "data";
85
- const render_key = template.getAttribute('render-key') || type;
117
+ const renderKey = template.getAttribute('render-key') || type;
86
118
  const self = this;
87
119
 
88
120
  // const arrayData = this.__getValueFromObject(data, type);
@@ -90,17 +122,7 @@ const CoCreateRender = {
90
122
 
91
123
  const isRenderObject = template.hasAttribute('render-object');
92
124
  if (isRenderObject){
93
- const renderObject = template.getAttribute('render-object');
94
- if (renderObject)
95
- arrayData = data[renderObject];
96
-
97
- let array = []
98
- for (const [key, value] of Object.entries(arrayData)) {
99
- array.push({key: key, value: value})
100
- }
101
- if (array.length > 0)
102
- arrayData = array
103
-
125
+ const renderObject = template.getAttribute('render-object');
104
126
  type = renderObject || 'data'
105
127
  }
106
128
 
@@ -111,29 +133,52 @@ const CoCreateRender = {
111
133
  arrayData = data[renderArray];
112
134
  type = renderArray || 'data';
113
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 {
114
145
 
115
- if (!Array.isArray(arrayData))
116
- arrayData = this.__getValueFromObject(data, type);
117
-
118
- if (!arrayData) {
119
- let cloneEl = this.cloneEl(template);
120
- cloneEl.classList.add('cloned');
121
- self.setValue([cloneEl], data);
122
- template.insertAdjacentHTML('beforebegin', cloneEl.outerHTML);
123
- }
146
+ if (!Array.isArray(arrayData))
147
+ arrayData = this.__getValueFromObject(data, type);
124
148
 
125
- if (type && Array.isArray(arrayData)) {
126
- arrayData.forEach((item) => {
149
+ if (!arrayData) {
127
150
  let cloneEl = this.cloneEl(template);
128
- cloneEl.classList.add('clone_' + type);
129
- let r_data = self.__createObject(item, render_key);
130
-
131
- self.setValue([cloneEl], r_data);
151
+ cloneEl.classList.add('cloned');
152
+ self.setValue([cloneEl], data, renderKey);
132
153
  template.insertAdjacentHTML('beforebegin', cloneEl.outerHTML);
133
- });
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
+ }
134
165
  }
135
166
  },
136
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
+
137
182
  cloneEl: function(template) {
138
183
  let cloneEl = template.cloneNode(true);
139
184
  cloneEl.classList.remove('template');
@@ -142,8 +187,8 @@ const CoCreateRender = {
142
187
  cloneEl.setAttribute('templateId', templateId);
143
188
  return cloneEl;
144
189
  },
145
-
146
- setValue:function(els, data, template){
190
+
191
+ setValue: function(els, data, renderKey){
147
192
  if (!data) return;
148
193
  const that = this;
149
194
  Array.from(els).forEach(el => {
@@ -151,45 +196,61 @@ const CoCreateRender = {
151
196
  Array.from(el.attributes).forEach(attr=>{
152
197
  let attr_name = attr.name.toLowerCase();
153
198
  let attrValue = attr.value;
154
- attrValue = that.__replaceValue(data, attrValue);
199
+ attrValue = that.__replaceValue(data, attrValue, renderKey);
155
200
 
156
- if (attrValue) {
201
+ if (attrValue || attrValue == "") {
157
202
  el.setAttribute(attr_name, attrValue);
158
203
  }
159
204
  });
160
205
 
161
206
  if(el.childNodes.length > 0) {
162
- that.setValue(el.childNodes, data, template);
207
+ that.setValue(el.childNodes, data, renderKey);
163
208
  }
164
- if (el.classList.contains('template')) {
209
+ if (el.classList.contains('template') && !el.hasAttribute('template_id')) {
165
210
  that.render(el, data);
166
211
  }
167
212
  }
168
213
 
169
214
  if (el.nodeType == 3) {
215
+ let valueType = el.parentElement.getAttribute('value-type')
170
216
  let textContent = el.textContent;
171
- let text = that.__replaceValue(data, textContent);
172
- if (text) {
173
- const newNode = document.createElement('div');
174
- newNode.innerHTML = text;
175
- el.replaceWith(...newNode.childNodes)
217
+ let text = that.__replaceValue(data, textContent, renderKey);
218
+ if (text || text == "") {
219
+ if (valueType == 'text' || valueType == 'string'){
220
+ el.textContent = text;
221
+ } else {
222
+ const newNode = document.createElement('div');
223
+ newNode.innerHTML = text;
224
+ el.replaceWith(...newNode.childNodes)
225
+ }
176
226
  }
177
227
  }
178
228
 
179
229
  });
180
230
  },
181
231
 
232
+ dataOriginal: {},
182
233
  data: function({selector, data, elements}) {
234
+ this.dataOriginal = data;
183
235
  if (selector) {
184
- let template = document.querySelector(selector);
236
+ let template = queryDocumentSelector(selector);
185
237
  if (!template) return;
186
238
  if (template.classList.contains('template')) {
187
239
  this.render(template, data);
188
240
  }
189
241
  else
190
- this.setValue([template], data, template);
242
+ this.setValue([template], data);
191
243
  } else if (elements) {
192
- this.setValue(elements, data);
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
+ // }
193
254
  }
194
255
 
195
256
  }