-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.html
132 lines (125 loc) · 3.77 KB
/
example.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Block JS</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background: #095642;
color: #333;
}
#page {
padding:2rem;
border:1px solid #ccc;
background: #fff;
width:640px;
margin:0 auto;
}
</style>
</head>
<body id="block-js-html">
<script src="block.js"></script>
<script id="blocktemplate">
///////////////////////////////////////////
// Usage
const storyurl = (st) => { return '/story/' + st.id; }
const MyTemplate = {
data: {
stories() {
let stories = [],
s = [
// sample data
{
id:1,
headline:'Story 1',
content:'This is a story about a story'
},
{
id:2,
headline:'Story 2',
content:'This is another story about a story'
}
]
for (let i=0; i < s.length; ++i)
stories.push(s[i])
return stories;
}
},
blocks: [
{ tag: 'h1', content: 'Hello Block.js' },
{
tag: 'div',
content: '<b>Block.js</b> is an experiment for building HTML structures with JavaScript',
attr: {
style : 'padding:2rem 0;border-top:2px solid #ccc;'
}
},
{ tag: 'hr', attr: { style: 'border:0; border-top:1px solid #ccc; margin:1rem 0;'} },
{ tag: 'p', content: '😎 <i>cool</i> stuff. Lets insert a ButtonBlock component next...' },
{
type: 'ButtonBlock',
content:'CLICK ME',
attr: {
onclick: `alert('HI THERE')`,
style: 'padding:1rem 2rem; background:#333; color:#fff; border:0; border-radius:4px; cursor:pointer;'
}
},
{ tag: 'hr', attr: { style: 'border:0; border-top:1px solid #ccc; margin:1rem 0;'} },
{ tag: 'p', content: 'Now lets insert a list of stories...' },
{ tag: 'h2', content: 'Stories'},
{
type: 'ListBlock',
tag: 'ul',
attr: {
style: 'list-style:none;margin: 2rem 0;padding:0;'
},
items: '::stories',
format: function(st) {
return {
type: 'ListItemBlock',
tag: 'a',
content: `<h3>${st.headline}</h3><p style="font-weight:normal;color:#888">${st.content}</p>`,
attr: {
href: storyurl(st),
style: 'display:block;text-decoration:none;font-weight:bold; border-bottom:1px solid #aaa; padding: 1rem;',
onclick: `alert('Story ${st.id}'); return false;`
}
}
}
},
{ tag: 'p', content: 'Then an image thumbnail next...' },
{ tag: 'img', attr: { src: 'https://placedog.net/120x120', style: 'border:1px solid #ccc;padding:.25rem;border-radius:5px' } },
{ tag: 'hr', attr: { style: 'border:0; border-top:1px solid #ccc; margin:1rem 0;'} },
{ tag: 'p', content: 'This is just a brief example of using Block.js' },
{ tag: 'p', content: '<strong>That\'s all for now folks ✌️</strong>' }
],
components: {
ButtonBlock,
ListBlock,
ListItemBlock
}
}
function Render() {
// the MyTemplate const here ideally will come from
// either the database or an env var
let body = new BlockTemplate(MyTemplate)
// render it all. header, body, footer
let html = `
<div id="page">
${body}
</div>
`
document.body.innerHTML = html;
//document.querySelector('#block-js-html').innerHTML = html;
//document.write(html)
}
window.onload = () => {
Render()
}
</script>
</body>
</html>