@cocreate/render 1.9.0 → 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.
Files changed (3) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/package.json +1 -1
  3. package/src/index.js +143 -56
package/CHANGELOG.md CHANGED
@@ -1,3 +1,25 @@
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
+
8
+ ## [1.9.2](https://github.com/CoCreate-app/CoCreate-render/compare/v1.9.1...v1.9.2) (2022-07-10)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * use dataOriginal if path includes collection ([e6b9288](https://github.com/CoCreate-app/CoCreate-render/commit/e6b92886bad560fa64e487609c69f37268d87587))
14
+
15
+ ## [1.9.1](https://github.com/CoCreate-app/CoCreate-render/compare/v1.9.0...v1.9.1) (2022-07-08)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * remove test console.log ([e70647f](https://github.com/CoCreate-app/CoCreate-render/commit/e70647f9aaedfab5ecdc4f38e755eb7cf7c564fe))
21
+ * store original data while rendering to have access to root fields eg. collection ([c769747](https://github.com/CoCreate-app/CoCreate-render/commit/c76974721597e1c8d3d8b531a81bf9216b40c731))
22
+
1
23
  # [1.9.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.8.4...v1.9.0) (2022-07-03)
2
24
 
3
25
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/render",
3
- "version": "1.9.0",
3
+ "version": "1.10.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",
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 && path.includes('collection'))
14
+ json = this.dataOriginal
12
15
  let jsonData = json, subpath = path.split('.');
13
16
 
14
17
  for (let i = 0; i < subpath.length; i++) {
@@ -49,25 +52,21 @@ const CoCreateRender = {
49
52
  }
50
53
  },
51
54
 
52
- __replaceValue: function(data, inputValue, renderKey) {
55
+ __replaceValue: function(data, inputValue, renderKey, valueType) {
53
56
  let isPass = false;
54
57
  let self = this;
55
58
  let resultValue = null;
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
- // if (value) {
61
- // if (value && typeof(value) !== "object") {
62
- if (value && !Array.isArray(value)) {
63
- // converts object to string
64
- // if (!Array.isArray(value) && typeof(value) == "object") {
67
+ if (value) {
65
68
  if (typeof(value) == "object") {
66
- let str = '';
67
- for (const [key, val] of Object.entries(value)) {
68
- str += `${key}: ${val}\n`;
69
- }
70
- value = str
69
+ value = this.generateString(value)
71
70
  }
72
71
  isPass = true;
73
72
  inputValue = inputValue.replace(attr, value);
@@ -83,28 +82,36 @@ const CoCreateRender = {
83
82
  }
84
83
  return resultValue;
85
84
  },
85
+
86
+ generateString: function(value, str = ''){
87
+ // let str = '';
88
+ // do {
89
+ let flag = true;
90
+ if (str)
91
+ flag = false;
92
+ for (const [key, val] of Object.entries(value)) {
93
+ if (typeof(val) == "object") {
94
+ str += `${key}: \n`;
95
+ this.generateString(val, str)
96
+ }
97
+ else
98
+ str += `${key}: ${val}\n`;
99
+ }
100
+ // } while()
101
+ if (flag)
102
+ return str
103
+ },
86
104
 
87
105
  render: function(template, data) {
88
106
  let type = template.getAttribute('render-array') || "data";
89
107
  const renderKey = template.getAttribute('render-key') || type;
90
108
  const self = this;
91
109
 
92
- // const arrayData = this.__getValueFromObject(data, type);
93
110
  let arrayData = data;
94
111
 
95
112
  const isRenderObject = template.hasAttribute('render-object');
96
113
  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
-
114
+ const renderObject = template.getAttribute('render-object');
108
115
  type = renderObject || 'data'
109
116
  }
110
117
 
@@ -115,29 +122,52 @@ const CoCreateRender = {
115
122
  arrayData = data[renderArray];
116
123
  type = renderArray || 'data';
117
124
  }
125
+ if (isRenderObject && type) {
126
+ let r_data = self.isRenderObject(arrayData[type], renderKey)
127
+ for (let sdata of r_data) {
128
+ let cloneEl = this.cloneEl(template);
129
+ cloneEl.classList.add('clone_' + type);
130
+ self.setValue([cloneEl], sdata, renderKey);
131
+ template.insertAdjacentElement('beforebegin', cloneEl);
132
+ }
133
+ } else {
118
134
 
119
- if (!Array.isArray(arrayData))
120
- arrayData = this.__getValueFromObject(data, type);
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
- }
135
+ if (!Array.isArray(arrayData))
136
+ arrayData = this.__getValueFromObject(data, type);
128
137
 
129
- if (type && Array.isArray(arrayData)) {
130
- arrayData.forEach((item) => {
138
+ if (!arrayData) {
131
139
  let cloneEl = this.cloneEl(template);
132
- cloneEl.classList.add('clone_' + type);
133
- let r_data = self.__createObject(item, renderKey);
140
+ cloneEl.classList.add('cloned');
141
+ self.setValue([cloneEl], data, renderKey);
142
+ template.insertAdjacentElement('beforebegin', cloneEl);
143
+ }
134
144
 
135
- self.setValue([cloneEl], r_data, renderKey);
136
- template.insertAdjacentHTML('beforebegin', cloneEl.outerHTML);
137
- });
145
+ if(type && Array.isArray(arrayData)) {
146
+ arrayData.forEach((item) => {
147
+ let cloneEl = this.cloneEl(template);
148
+ cloneEl.classList.add('clone_' + type);
149
+ let r_data = self.__createObject(item, renderKey);
150
+ self.setValue([cloneEl], r_data, renderKey);
151
+ template.insertAdjacentElement('beforebegin', cloneEl);
152
+ });
153
+ }
138
154
  }
139
155
  },
140
156
 
157
+ isRenderObject: function(data, renderKey) {
158
+ let array = []
159
+ for (const [key, value] of Object.entries(data)) {
160
+ let type = 'string';
161
+ if (typeof(value) == "object")
162
+ if (Array.isArray(value))
163
+ type = 'array'
164
+ else
165
+ type = 'object'
166
+ array.push({[renderKey]: {key, value, type}})
167
+ }
168
+ return array
169
+ },
170
+
141
171
  cloneEl: function(template) {
142
172
  let cloneEl = template.cloneNode(true);
143
173
  cloneEl.classList.remove('template');
@@ -146,56 +176,110 @@ const CoCreateRender = {
146
176
  cloneEl.setAttribute('templateId', templateId);
147
177
  return cloneEl;
148
178
  },
149
-
150
- setValue:function(els, data, renderKey){
179
+
180
+ setValue: function(els, data, renderKey){
151
181
  if (!data) return;
152
182
  const that = this;
153
183
  Array.from(els).forEach(el => {
184
+
154
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
+
155
196
  Array.from(el.attributes).forEach(attr=>{
156
197
  let attr_name = attr.name.toLowerCase();
157
198
  let attrValue = attr.value;
158
- attrValue = that.__replaceValue(data, attrValue, renderKey);
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);
159
217
 
160
218
  if (attrValue || attrValue == "") {
161
219
  el.setAttribute(attr_name, attrValue);
162
220
  }
163
221
  });
164
222
 
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
- if(el.childNodes.length > 0) {
223
+ if(el.childNodes.length > 0) {
173
224
  that.setValue(el.childNodes, data, renderKey);
174
225
  }
175
- if (el.classList.contains('template')) {
226
+ if (el.classList.contains('template') && !el.hasAttribute('template_id')) {
176
227
  that.render(el, data);
177
228
  }
178
229
  }
179
-
230
+
180
231
  if (el.nodeType == 3) {
181
232
  let valueType = el.parentElement.getAttribute('value-type')
182
- let textContent = el.textContent;
183
- let text = that.__replaceValue(data, textContent, renderKey);
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);
184
254
  if (text || text == "") {
185
255
  if (valueType == 'text' || valueType == 'string'){
186
256
  el.textContent = text;
187
257
  } else {
188
258
  const newNode = document.createElement('div');
189
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)
190
265
  el.replaceWith(...newNode.childNodes)
191
266
  }
192
267
  }
193
268
  }
194
-
195
269
  });
196
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
+ },
197
279
 
280
+ dataOriginal: {},
198
281
  data: function({selector, data, elements}) {
282
+ this.dataOriginal = data;
199
283
  if (selector) {
200
284
  let template = queryDocumentSelector(selector);
201
285
  if (!template) return;
@@ -205,7 +289,10 @@ const CoCreateRender = {
205
289
  else
206
290
  this.setValue([template], data);
207
291
  } else if (elements) {
208
- this.setValue(elements, data);
292
+ if (elements.length == 1 && elements[0].classList.contains('template'))
293
+ this.render(elements[0], data);
294
+ else
295
+ this.setValue(elements, data);
209
296
  }
210
297
 
211
298
  }