-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUiTemplateRenderer.js
265 lines (241 loc) · 7.49 KB
/
UiTemplateRenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* Requires
*/
import { Exception, isPojo, mergeObject, str2node, strCreate } from '@squirrel-forge/ui-util';
/**
* Ui template renderer exception
* @class
* @extends Exception
*/
class UiTemplateRendererException extends Exception {}
/**
* @typedef {string|UiTemplateRendererTemplateData|Array<UiTemplateRendererTemplateData>|Function} UiTemplateRendererData
*/
/**
* @typedef {Object} UiTemplateRendererTemplateData
* @property {string} template - Template reference name
* @property {UiTemplateData} data - Template data
* @property {UiTemplateRendererCompileData} as - Precompile template data
*/
/**
* @typedef {Object} UiTemplateRendererCompileData
* @property {UiTemplateRendererData} * - Any template data property that needs to be compiled
*/
/**
* Example nested data
* {
* template : 'name',
* data : {},
* as : {
* id : () => { return 'foo'; },
* content : [
* {
* template : 'name',
* data : {...},
* as : {...},
* },
* 'foo',
* ],
* 'header.controls.custom' => 'foo',
* };
*/
/**
* Ui template renderer
* @class
*/
export class UiTemplateRenderer {
/**
* Check for render data structure
* @public
* @static
* @param data
* @return {boolean} - True if render data
*/
static isRenderData( data ) {
return isPojo( data ) && typeof data.template === 'string' && isPojo( data.data );
}
/**
* Debug object
* @public
* @static
* @property
* @type {null|console|Object}
*/
static debug = null;
/**
* Templates reference
* @public
* @static
* @type {Object}
*/
static tmpl = {};
/**
* Keep marker
* @public
* @static
* @property
* @type {string}
*/
static keepMarker = '__keep__';
/**
* Set template
* @public
* @static
* @param {string} name - Template reference
* @param {UiTemplate} tmpl - Template instance
* @return {void}
*/
static add( name, tmpl ) {
if ( typeof tmpl !== 'object' ) throw new UiTemplateRendererException( 'Must be a template object: ' + name );
if ( this.tmpl[ name ] ) throw new UiTemplateRendererException( 'Template already defined: ' + name );
this.tmpl[ name ] = tmpl;
}
/**
* Require template
* @public
* @static
* @param {string} name - Template reference
* @param {UiTemplate} tmpl - Template instance
* @return {void}
*/
static require( name, tmpl ) {
try {
this.add( name, tmpl )
} catch ( e ) {
if ( typeof tmpl !== 'object' ) throw e;
}
}
/**
* Get template class
* @public
* @static
* @param {string} name - Template reference
* @return {UiTemplate} - Template instance
*/
static get( name ) {
if ( this.tmpl && this.tmpl[ name ] ) return this.tmpl[ name ];
throw new UiTemplateRendererException( 'Unknown template: ' + name );
}
/**
* Render data.as block
* @public
* @static
* @param {Object} data - As data block
* @param {string} trace - Trace string
* @return {Object} - Rendered object data
*/
static as( data, trace ) {
const result = {};
const entries = Object.entries( data );
for ( let i = 0; i < entries.length; i++ ) {
const [ item_path, item_data ] = entries[ i ];
const rendered = this.recursive( item_data, trace + '.' + item_path );
strCreate( item_path, rendered, result, true, false, this.debug );
}
return result;
}
/**
* Render data
* @public
* @static
* @param {UiTemplateRendererTemplateData} data - Render object
* @param {string} trace - Trace string
* @return {string} - Rendered template
*/
static data( data, trace ) {
const tmpl = this.get( data.template );
if ( this.debug ) this.debug.log( this.name + '::data template:', data.template, '[' + trace + ']' );
if ( isPojo( data.as ) ) {
const as = this.as( data.as, trace + '.as' );
if ( this.debug ) this.debug.log( this.name + '::data as:', as, '[' + trace + ']' );
mergeObject( data.data, as, true, true );
}
return tmpl.render( data.data );
}
/**
* Render recursive
* @public
* @static
* @param {UiTemplateRendererData} data - Render object or array
* @param {string} trace - Trace string
* @return {string|*[]} - Rendered data
*/
static recursive( data, trace ) {
const to = typeof data;
if ( to === 'string' ) {
return data;
} else if ( to === 'function' ) {
// Render custom function
let result;
try {
result = data( trace );
} catch ( e ) {
throw new UiTemplateRendererException( 'Failed to render custom callback [' + trace + ']', e );
}
if ( typeof result !== 'string' ) throw new UiTemplateRendererException( 'A custom callback must always return a string [' + trace + ']' );
return result;
} else if ( data instanceof Array ) {
// Render array of unknowns
const result = [];
for ( let i = 0; i < data.length; i++ ) {
result.push( this.recursive( data[ i ], trace + `[${i}]` ) );
}
// Keep array structure for processing inside template
if ( result[ 0 ] === this.keepMarker ) {
result.shift();
return result;
}
return result.join( '' );
} else if ( this.isRenderData( data ) ) {
return this.data( data, trace );
} else if ( isPojo( data ) && data[ this.keepMarker ] === true ) {
return data;
} else if ( typeof data.toString === 'function' ) {
return data.toString();
} else {
if ( this.debug ) this.debug.error( this.name + '::render', data );
throw new UiTemplateRendererException( 'Unknown data type: ' + typeof data + ' [' + trace + ']' );
}
}
/**
* Render
* @public
* @static
* @param {UiTemplateRendererData} data - Render object or array
* @return {string} - Rendered data
*/
static render( data ) {
if ( this.debug ) this.debug.groupCollapsed( this.name + '::render' );
const result = this.recursive( data, 'data' );
if ( this.debug ) this.debug.groupEnd();
return result;
}
/**
* Render as node
* @public
* @static
* @param {null|UiTemplateData|Object|Array<UiTemplateData|Object>} data - Template data /list
* @return {NodeList|Array} - Rendered nodes or empty array
*/
static node( data = null ) {
const rendered = this.render( data );
if ( rendered ) return str2node( rendered );
return [];
}
/**
* Append rendered data
* @public
* @static
* @param {HTMLElement} to - Element to append to
* @param {null|Object|Array} data - Template data /list
* @return {NodeList|Array} - Rendered nodes or empty array
*/
static append( to, data = null ) {
if ( !( to instanceof HTMLElement ) ) throw new UiTemplateException( 'Requires a HTMLElement to append to' );
const nodes = this.node( data );
for ( let i = 0; i < nodes.length; i++ ) {
to.appendChild( nodes[ i ] );
}
return nodes;
}
}