forked from resgateio/resgate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
177 lines (173 loc) · 5.5 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Book Collection Example</title>
<script src="https://unpkg.com/resclient@latest/dist/resclient.min.js"></script>
<script src="https://unpkg.com/modapp-base-component@latest/dist/modapp-base-component.min.js"></script>
<script src="https://unpkg.com/modapp-resource-component@latest/dist/modapp-resource-component.min.js"></script>
<style>
body {
background: #eee;
font-family: Arial, Helvetica, sans-serif;
padding: 0;
margin: 0;
}
header {
background: #000;
color: #fff;
padding: 16px 1em;
}
h1 {
margin: 0;
padding: 0;
line-height: 32px;
font-size: 24px;
}
.top {
margin: 1em 1em;
}
.shadow {
box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);
}
#books {
max-width: 800px;
margin: 0 1em;
}
.new-container { margin: 1em 0; }
label {
margin: 0.5em 1em 0.5em 0;
font-weight: bold;
}
.new-container input { margin-right: 1em; }
#error-msg { color: #800 }
button {
display: inline-block;
border: none;
background: none;
color: #006;
}
button:hover { background: rgba(0,0,0,0.12); }
.list-item { padding: 8px 0; }
.card {
background: #fff;
padding: 1em 1em;
box-sizing: border-box;
}
.action { float: right }
.editing > .card { background: #eaeaff; }
.card > .edit { display: none; }
.editing > .card > .edit { display: inherit; }
.editing > .card > .view { display: none; }
.card h3 { margin: 0 0 8px 0; }
.card .author { font-style: italic; }
.label {
display: inline-block;
width: 80px;
font-weight: bold;
}
.edit-input + .edit-input { margin-top: 4px; }
</style>
</head>
<body>
<header class="shadow">
<h1>Book Collection Example</h1>
</header>
<div class="top">
<div class="new-container">
<label for="new-title">Title</label><input id="new-title" />
<label for="new-author">Author</label><input id="new-author" />
<button id="add-new">Add new</button>
</div>
<div id="error-msg"></div>
</div>
<hr>
<div id="books"></div>
<script>
const { Elem, Txt, Button, Input } = window["modapp-base-component"];
const { CollectionList, ModelTxt } = window["modapp-resource-component"];
const ResClient = resclient.default;
let client = new ResClient('ws://localhost:8080');
// Error handling
let errMsg = new Txt();
let errTimer = null;
errMsg.render(document.getElementById('error-msg'));
let showError = (err) => {
errMsg.setText(err && err.message ? err.message : String(err));
clearTimeout(errTimer);
errTimer = setTimeout(() => errMsg.setText(''), 7000);
};
// Add new click callback
document.getElementById('add-new').addEventListener('click', () => {
let newTitle = document.getElementById('new-title');
let newAuthor = document.getElementById('new-author');
client.call('library.books', 'new', {
title: newTitle.value,
author: newAuthor.value
}).then(() => {
// Clear values on successful add
newTitle.value = "";
newAuthor.value = "";
}).catch(showError);
});
// Get the collection from the service.
client.get('library.books').then(books => {
// Here we use modapp components to render the view.
// It is a protest against all these frameworks with virtual doms.
// Why use virtual doms when it is faster with vanilla javascript?
new Elem(n =>
n.component(new CollectionList(books, book => {
let c = new Elem(n =>
n.elem('div', { className: 'list-item' }, [
n.elem('div', { className: 'card shadow' }, [
n.elem('div', { className: 'view' }, [
n.elem('div', { className: 'action' }, [
n.component(new Button(`Edit`, () => {
c.getNode('titleInput').setValue(book.title);
c.getNode('authorInput').setValue(book.author);
c.addClass('editing');
})),
n.component(new Button(`Delete`, () => books.call('delete', { id: book.id }).catch(showError)))
]),
n.elem('div', { className: 'title' }, [
n.component(new ModelTxt(book, book => book.title, { tagName: 'h3'}))
]),
n.elem('div', { className: 'author' }, [
n.component(new Txt("By ")),
n.component(new ModelTxt(book, book => book.author))
])
]),
n.elem('div', { className: 'edit' }, [
n.elem('div', { className: 'action' }, [
n.component(new Button(`OK`, () => {
book.set({
title: c.getNode('titleInput').getValue(),
author: c.getNode('authorInput').getValue()
})
.then(() => c.removeClass('editing'))
.catch(showError);
})),
n.component(new Button(`Cancel`, () => c.removeClass('editing')))
]),
n.elem('div', { className: 'edit-input' }, [
n.component(new Txt("Title", { className: 'label' })),
n.component('titleInput', new Input())
]),
n.elem('div', { className: 'edit-input' }, [
n.component(new Txt("Author", { className: 'label' })),
n.component('authorInput', new Input())
])
])
])
])
);
return c;
}, { className: 'list' }))
).render(document.getElementById('books'));
}).catch(err => showError(err.code === 'system.connectionError'
? "Connection error. Are NATS Server and Resgate running?"
: err
));
</script>
</body>
</html>