@cocreate/render 1.8.4 → 1.9.2
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 +23 -0
- package/demo/render.array.html +4 -3
- package/demo/render.object.html +2 -2
- package/package.json +3 -2
- package/src/index.js +87 -46
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
+
## [1.9.2](https://github.com/CoCreate-app/CoCreate-render/compare/v1.9.1...v1.9.2) (2022-07-10)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* use dataOriginal if path includes collection ([e6b9288](https://github.com/CoCreate-app/CoCreate-render/commit/e6b92886bad560fa64e487609c69f37268d87587))
|
|
7
|
+
|
|
8
|
+
## [1.9.1](https://github.com/CoCreate-app/CoCreate-render/compare/v1.9.0...v1.9.1) (2022-07-08)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* remove test console.log ([e70647f](https://github.com/CoCreate-app/CoCreate-render/commit/e70647f9aaedfab5ecdc4f38e755eb7cf7c564fe))
|
|
14
|
+
* store original data while rendering to have access to root fields eg. collection ([c769747](https://github.com/CoCreate-app/CoCreate-render/commit/c76974721597e1c8d3d8b531a81bf9216b40c731))
|
|
15
|
+
|
|
16
|
+
# [1.9.0](https://github.com/CoCreate-app/CoCreate-render/compare/v1.8.4...v1.9.0) (2022-07-03)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Features
|
|
20
|
+
|
|
21
|
+
* 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))
|
|
22
|
+
* 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))
|
|
23
|
+
|
|
1
24
|
## [1.8.4](https://github.com/CoCreate-app/CoCreate-render/compare/v1.8.3...v1.8.4) (2022-06-19)
|
|
2
25
|
|
|
3
26
|
|
package/demo/render.array.html
CHANGED
|
@@ -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.
|
|
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
|
|
package/demo/render.object.html
CHANGED
|
@@ -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.
|
|
3
|
+
"version": "1.9.2",
|
|
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 && path.includes('collection'))
|
|
14
|
+
json = this.dataOriginal
|
|
11
15
|
let jsonData = json, subpath = path.split('.');
|
|
12
16
|
|
|
13
17
|
for (let i = 0; i < subpath.length; i++) {
|
|
@@ -55,18 +59,14 @@ const CoCreateRender = {
|
|
|
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
|
-
|
|
60
|
-
// if (value && typeof(value) !== "object") {
|
|
61
|
-
if (value && !Array.isArray(value)) {
|
|
62
|
-
// converts object to string
|
|
63
|
-
// if (!Array.isArray(value) && typeof(value) == "object") {
|
|
67
|
+
if (value) {
|
|
64
68
|
if (typeof(value) == "object") {
|
|
65
|
-
|
|
66
|
-
for (const [key, val] of Object.entries(value)) {
|
|
67
|
-
str += `${key}: ${val}\n`;
|
|
68
|
-
}
|
|
69
|
-
value = str
|
|
69
|
+
value = this.generateString(value)
|
|
70
70
|
}
|
|
71
71
|
isPass = true;
|
|
72
72
|
inputValue = inputValue.replace(attr, value);
|
|
@@ -82,28 +82,36 @@ const CoCreateRender = {
|
|
|
82
82
|
}
|
|
83
83
|
return resultValue;
|
|
84
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
|
+
},
|
|
85
104
|
|
|
86
105
|
render: function(template, data) {
|
|
87
106
|
let type = template.getAttribute('render-array') || "data";
|
|
88
107
|
const renderKey = template.getAttribute('render-key') || type;
|
|
89
108
|
const self = this;
|
|
90
109
|
|
|
91
|
-
// const arrayData = this.__getValueFromObject(data, type);
|
|
92
110
|
let arrayData = data;
|
|
93
111
|
|
|
94
112
|
const isRenderObject = template.hasAttribute('render-object');
|
|
95
113
|
if (isRenderObject){
|
|
96
|
-
const renderObject = template.getAttribute('render-object');
|
|
97
|
-
if (renderObject)
|
|
98
|
-
arrayData = data[renderObject];
|
|
99
|
-
|
|
100
|
-
let array = []
|
|
101
|
-
for (const [key, value] of Object.entries(arrayData)) {
|
|
102
|
-
array.push({key: key, value: value})
|
|
103
|
-
}
|
|
104
|
-
if (array.length > 0)
|
|
105
|
-
arrayData = array
|
|
106
|
-
|
|
114
|
+
const renderObject = template.getAttribute('render-object');
|
|
107
115
|
type = renderObject || 'data'
|
|
108
116
|
}
|
|
109
117
|
|
|
@@ -114,29 +122,52 @@ const CoCreateRender = {
|
|
|
114
122
|
arrayData = data[renderArray];
|
|
115
123
|
type = renderArray || 'data';
|
|
116
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.insertAdjacentHTML('beforebegin', cloneEl.outerHTML);
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
117
134
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (!arrayData) {
|
|
122
|
-
let cloneEl = this.cloneEl(template);
|
|
123
|
-
cloneEl.classList.add('cloned');
|
|
124
|
-
self.setValue([cloneEl], data, renderKey);
|
|
125
|
-
template.insertAdjacentHTML('beforebegin', cloneEl.outerHTML);
|
|
126
|
-
}
|
|
135
|
+
if (!Array.isArray(arrayData))
|
|
136
|
+
arrayData = this.__getValueFromObject(data, type);
|
|
127
137
|
|
|
128
|
-
|
|
129
|
-
arrayData.forEach((item) => {
|
|
138
|
+
if (!arrayData) {
|
|
130
139
|
let cloneEl = this.cloneEl(template);
|
|
131
|
-
cloneEl.classList.add('
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
self.setValue([cloneEl], r_data, renderKey);
|
|
140
|
+
cloneEl.classList.add('cloned');
|
|
141
|
+
self.setValue([cloneEl], data, renderKey);
|
|
135
142
|
template.insertAdjacentHTML('beforebegin', cloneEl.outerHTML);
|
|
136
|
-
}
|
|
143
|
+
}
|
|
144
|
+
|
|
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.insertAdjacentHTML('beforebegin', cloneEl.outerHTML);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
137
154
|
}
|
|
138
155
|
},
|
|
139
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
|
+
|
|
140
171
|
cloneEl: function(template) {
|
|
141
172
|
let cloneEl = template.cloneNode(true);
|
|
142
173
|
cloneEl.classList.remove('template');
|
|
@@ -145,8 +176,8 @@ const CoCreateRender = {
|
|
|
145
176
|
cloneEl.setAttribute('templateId', templateId);
|
|
146
177
|
return cloneEl;
|
|
147
178
|
},
|
|
148
|
-
|
|
149
|
-
setValue:function(els, data, renderKey){
|
|
179
|
+
|
|
180
|
+
setValue: function(els, data, renderKey){
|
|
150
181
|
if (!data) return;
|
|
151
182
|
const that = this;
|
|
152
183
|
Array.from(els).forEach(el => {
|
|
@@ -164,27 +195,34 @@ const CoCreateRender = {
|
|
|
164
195
|
if(el.childNodes.length > 0) {
|
|
165
196
|
that.setValue(el.childNodes, data, renderKey);
|
|
166
197
|
}
|
|
167
|
-
if (el.classList.contains('template')) {
|
|
198
|
+
if (el.classList.contains('template') && !el.hasAttribute('template_id')) {
|
|
168
199
|
that.render(el, data);
|
|
169
200
|
}
|
|
170
201
|
}
|
|
171
202
|
|
|
172
203
|
if (el.nodeType == 3) {
|
|
204
|
+
let valueType = el.parentElement.getAttribute('value-type')
|
|
173
205
|
let textContent = el.textContent;
|
|
174
206
|
let text = that.__replaceValue(data, textContent, renderKey);
|
|
175
207
|
if (text || text == "") {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
208
|
+
if (valueType == 'text' || valueType == 'string'){
|
|
209
|
+
el.textContent = text;
|
|
210
|
+
} else {
|
|
211
|
+
const newNode = document.createElement('div');
|
|
212
|
+
newNode.innerHTML = text;
|
|
213
|
+
el.replaceWith(...newNode.childNodes)
|
|
214
|
+
}
|
|
179
215
|
}
|
|
180
216
|
}
|
|
181
217
|
|
|
182
218
|
});
|
|
183
219
|
},
|
|
184
220
|
|
|
221
|
+
dataOriginal: {},
|
|
185
222
|
data: function({selector, data, elements}) {
|
|
223
|
+
this.dataOriginal = data;
|
|
186
224
|
if (selector) {
|
|
187
|
-
let template =
|
|
225
|
+
let template = queryDocumentSelector(selector);
|
|
188
226
|
if (!template) return;
|
|
189
227
|
if (template.classList.contains('template')) {
|
|
190
228
|
this.render(template, data);
|
|
@@ -192,7 +230,10 @@ const CoCreateRender = {
|
|
|
192
230
|
else
|
|
193
231
|
this.setValue([template], data);
|
|
194
232
|
} else if (elements) {
|
|
195
|
-
|
|
233
|
+
if (elements.length == 1 && elements[0].classList.contains('template'))
|
|
234
|
+
this.render(elements[0], data);
|
|
235
|
+
else
|
|
236
|
+
this.setValue(elements, data);
|
|
196
237
|
}
|
|
197
238
|
|
|
198
239
|
}
|