@cocreate/render 1.8.2 → 1.9.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,25 @@
1
+ # [1.9.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.8.4...v1.9.0) (2022-07-03)
2
+
3
+
4
+ ### Features
5
+
6
+ * 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))
7
+ * 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))
8
+
9
+ ## [1.8.4](https://github.com/CoCreate-app/CoCreate-render/compare/v1.8.3...v1.8.4) (2022-06-19)
10
+
11
+
12
+ ### Bug Fixes
13
+
14
+ * update templates to use placeholder ([9299cce](https://github.com/CoCreate-app/CoCreate-render/commit/9299ccef3864ffe087044a3614aa35799e89f31c))
15
+
16
+ ## [1.8.3](https://github.com/CoCreate-app/CoCreate-render/compare/v1.8.2...v1.8.3) (2022-06-18)
17
+
18
+
19
+ ### Bug Fixes
20
+
21
+ * bump dependencies ([a9db63c](https://github.com/CoCreate-app/CoCreate-render/commit/a9db63c1c00c5df943151e122ffef310c2aa17d7))
22
+
1
23
  ## [1.8.2](https://github.com/CoCreate-app/CoCreate-render/compare/v1.8.1...v1.8.2) (2022-06-12)
2
24
 
3
25
 
@@ -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.2",
3
+ "version": "1.9.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",
@@ -61,8 +61,9 @@
61
61
  "webpack-log": "^3.0.1"
62
62
  },
63
63
  "dependencies": {
64
- "@cocreate/actions": "^1.4.2",
65
- "@cocreate/api": "^1.3.2",
66
- "@cocreate/docs": "^1.3.2"
64
+ "@cocreate/actions": "^1.4.3",
65
+ "@cocreate/api": "^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,6 @@
1
1
  /*globals CustomEvent*/
2
2
  import action from '@cocreate/actions';
3
+ import { queryDocumentSelector } from '@cocreate/utils';
3
4
  // import api from '@cocreate/api';
4
5
 
5
6
  const CoCreateRender = {
@@ -48,7 +49,7 @@ const CoCreateRender = {
48
49
  }
49
50
  },
50
51
 
51
- __replaceValue: function(data, inputValue) {
52
+ __replaceValue: function(data, inputValue, renderKey) {
52
53
  let isPass = false;
53
54
  let self = this;
54
55
  let resultValue = null;
@@ -71,6 +72,9 @@ const CoCreateRender = {
71
72
  isPass = true;
72
73
  inputValue = inputValue.replace(attr, value);
73
74
  }
75
+ if (!isPass && renderKey && attr.includes(`{{${renderKey}.`)) {
76
+ resultValue = '';
77
+ }
74
78
  });
75
79
 
76
80
  if (isPass) {
@@ -82,7 +86,7 @@ const CoCreateRender = {
82
86
 
83
87
  render: function(template, data) {
84
88
  let type = template.getAttribute('render-array') || "data";
85
- const render_key = template.getAttribute('render-key') || type;
89
+ const renderKey = template.getAttribute('render-key') || type;
86
90
  const self = this;
87
91
 
88
92
  // const arrayData = this.__getValueFromObject(data, type);
@@ -118,7 +122,7 @@ const CoCreateRender = {
118
122
  if (!arrayData) {
119
123
  let cloneEl = this.cloneEl(template);
120
124
  cloneEl.classList.add('cloned');
121
- self.setValue([cloneEl], data);
125
+ self.setValue([cloneEl], data, renderKey);
122
126
  template.insertAdjacentHTML('beforebegin', cloneEl.outerHTML);
123
127
  }
124
128
 
@@ -126,9 +130,9 @@ const CoCreateRender = {
126
130
  arrayData.forEach((item) => {
127
131
  let cloneEl = this.cloneEl(template);
128
132
  cloneEl.classList.add('clone_' + type);
129
- let r_data = self.__createObject(item, render_key);
133
+ let r_data = self.__createObject(item, renderKey);
130
134
 
131
- self.setValue([cloneEl], r_data);
135
+ self.setValue([cloneEl], r_data, renderKey);
132
136
  template.insertAdjacentHTML('beforebegin', cloneEl.outerHTML);
133
137
  });
134
138
  }
@@ -143,7 +147,7 @@ const CoCreateRender = {
143
147
  return cloneEl;
144
148
  },
145
149
 
146
- setValue:function(els, data, template){
150
+ setValue:function(els, data, renderKey){
147
151
  if (!data) return;
148
152
  const that = this;
149
153
  Array.from(els).forEach(el => {
@@ -151,15 +155,22 @@ const CoCreateRender = {
151
155
  Array.from(el.attributes).forEach(attr=>{
152
156
  let attr_name = attr.name.toLowerCase();
153
157
  let attrValue = attr.value;
154
- attrValue = that.__replaceValue(data, attrValue);
158
+ attrValue = that.__replaceValue(data, attrValue, renderKey);
155
159
 
156
- if (attrValue) {
160
+ if (attrValue || attrValue == "") {
157
161
  el.setAttribute(attr_name, attrValue);
158
162
  }
159
163
  });
160
164
 
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
+
161
172
  if(el.childNodes.length > 0) {
162
- that.setValue(el.childNodes, data, template);
173
+ that.setValue(el.childNodes, data, renderKey);
163
174
  }
164
175
  if (el.classList.contains('template')) {
165
176
  that.render(el, data);
@@ -167,12 +178,17 @@ const CoCreateRender = {
167
178
  }
168
179
 
169
180
  if (el.nodeType == 3) {
181
+ let valueType = el.parentElement.getAttribute('value-type')
170
182
  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)
183
+ let text = that.__replaceValue(data, textContent, renderKey);
184
+ if (text || text == "") {
185
+ if (valueType == 'text' || valueType == 'string'){
186
+ el.textContent = text;
187
+ } else {
188
+ const newNode = document.createElement('div');
189
+ newNode.innerHTML = text;
190
+ el.replaceWith(...newNode.childNodes)
191
+ }
176
192
  }
177
193
  }
178
194
 
@@ -181,13 +197,13 @@ const CoCreateRender = {
181
197
 
182
198
  data: function({selector, data, elements}) {
183
199
  if (selector) {
184
- let template = document.querySelector(selector);
200
+ let template = queryDocumentSelector(selector);
185
201
  if (!template) return;
186
202
  if (template.classList.contains('template')) {
187
203
  this.render(template, data);
188
204
  }
189
205
  else
190
- this.setValue([template], data, template);
206
+ this.setValue([template], data);
191
207
  } else if (elements) {
192
208
  this.setValue(elements, data);
193
209
  }