-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.js
More file actions
261 lines (224 loc) · 6.96 KB
/
block.js
File metadata and controls
261 lines (224 loc) · 6.96 KB
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
/***************************************************************
____ ___ __
/\ _`\ /\_ \ /\ \ __
\ \ \L\ \//\ \ ___ ___\ \ \/'\ /\_\ ____
\ \ _ <'\ \ \ / __`\ /'___\ \ , < \/\ \ /',__\
\ \ \L\ \\_\ \_/\ \L\ \/\ \__/\ \ \\`\ __ \ \ \/\__, `\
\ \____//\____\ \____/\ \____\\ \_\ \_\/\_\_\ \ \/\____/
\/___/ \/____/\/___/ \/____/ \/_/\/_/\/_/\ \_\ \/___/
\ \____/
\/___/
block.js - Render HTML with JavaScript ¯\_(ツ)_/¯
@author: Beau Bishop - github.com/bhbdev/blockjs
@version: 0.1.0
***************************************************************/
const Trace = (s) => { console.log(s) }
const _type = (x) => { return x.constructor.name }
const ShortTags = ['hr','br']
class BlockInterface {
constructor (map=[]) {
const _default = [
{ key: "name", default: 'Block' },
{ key: "tag", default: 'div' },
{ key: "block", default: null },
{ key: "content", default: '' },
{ key: "attr", default: {} },
{ key: "short", default: false },
{ key: "slots", default: [] },
{ key: "items", default: null },
{ key: "item", default: null },
{ key: "format", default: function(item){ return item } },
{ key: "hide", default: false },
]
this._map = [..._default, ...map]
}
get def() {
return this._map;
}
}
class Block {
constructor (obj, template={}, objMap=[]) {
this._interface = new BlockInterface(objMap);
this.init(obj,template)
}
init (obj,template) {
try {
if (!obj) throw 'undefined';
this.template = template;
this._interface.def.map(type => {
if (type.required && !obj[type.key])
throw 'missing required key: ' + type.key
this[type.key] = obj[type.key] || type.default
})
if (!template) throw 'template null or undefined'
// Trace('template data:' + typeof template.data)
if (template.data) this.data = template.data
if (template.$components) {
this.components = template.$components;
}
if (this.block) {
Trace('render -> <' + this.block + '>' )
}
else
Trace('render -> ' + this.constructor.name + ' <' + this.tag + '>' )
} catch (e) {
Trace('invalid block def\nerror:' + e)
}
}
attrs (str) {
let attr = ''
for (const [key,value] of Object.entries(this.attr)) {
attr += ` ${key}="${value}"`;
}
return attr;
}
compile () {
let res = '';
try {
if (this.format.constructor.name != 'Function')
throw 'format must be defined as function'
if (this.item) {
res += this.format(this.item);
}
else if (this.items)
{
if (typeof this.items == "string")
{
let method = this.items.replace('::','').split(':');
let datakey = method[0]
let arg = method.length==2? method[1]:null
if (this.template.data[datakey]) {
this.items = this.template.data[datakey](arg)
}
}
if (this.items.constructor.name !== 'Array')
throw 'items must be array'
this.items.forEach((i) => {
if (typeof i === "string") {
res += this.format(i);
}
else {
let block = (i.constructor.name == 'Object' && i.hasOwnProperty('tag')) ? i : this.format(i);
let cls = block.type || 'Block';
res += new this.components[cls](block, this.template) + '\n'
}
})
}
} catch (e) {
res = 'compile failed: ' + e
Trace(res);
}
return res;
}
_content() {
if (this.block)
{
let blockdef = new this.components[this.block]()
Trace('type: ' + _type(blockdef))
// if (blockdef.constructor.name == 'Array') {
// }
return new Block(blockdef,this.template).toString()
}
if (this.items || this.item)
{
//Trace('compiled ' + (this.item? 'item':'items'))
this.content = this.compile()
}
if (this.content.constructor.name == 'Object' && this.content.hasOwnProperty('tag'))
{
return new Block(this.content,this.template)
}
if (this.content.constructor.name == 'Array')
{
let val= '';
this.content.forEach((v)=>{
if (this.block)
{
v = new this.components[this.block]()
}
val += new this.components[v.type || 'Block'](v,this.template) + '\n'
})
return val;
}
if (Object.getPrototypeOf(this.content.constructor).name == 'Block' || this.content.constructor.name == 'Block')
{
return this.content.toString()
}
if (this.content.constructor.name == 'String')
{
return this.content;
}
}
toString() {
if (this.hide===true || typeof this.hide == 'function' && this.hide()) return '';
if (this.block) return this._content();
// Trace('render -> ' + this.constructor.name + ' <' + this.tag + '>' )
let str = `<${this.tag}`;
str += this.attrs()
if (ShortTags.find(t => t==this.tag) || this.short)
str += ` />`;
else
str += `>${this._content()}</${this.tag}>`;
return str;
}
}
class BlockTemplate {
constructor(template) {
this.template = template || { blocks: [] };
}
compile () {
const BlockClass = this.template.components ?
Object.assign({}, { Block, ...this.template.components })
: Object.assign({}, { Block })
this.template.$components = BlockClass;
let body = ''
this.template.blocks.forEach(b => {
body += new BlockClass[b.type || 'Block'](b, this.template) + '\n'
})
return body;
}
toString () {
return this.compile()
}
render () {
Emit(this)
}
}
////////////////////////////////
// define some custom Block classes
class ButtonBlock extends Block {
constructor(props,template={}) {
const tag = 'button';
super( {tag,...props}, template )
}
}
class ListBlock extends Block {
constructor(props, template={}) {
const tag = 'ul';
const ListInterface = [
{ key: "items", default: [] },
{ key: "format",
default: (item) => {
let obj = ''
if (typeof item === 'string')
obj = item;
else
//item may not be a RealV8Obj
// attempt to convert
obj = Object.assign({}, item )
return new Block({ tag:'li', content: obj.toString() }).toString()
}
}
]
super( {tag,...props}, template, ListInterface )
this.template = template;
//this.compile()
}
}
class ListItemBlock extends Block {
constructor(props, template={}) {
const tag = 'li';
super( {tag,...props}, template)
this.template = template
}
}