forked from SwellRT/swellrt
-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo-form.html
274 lines (212 loc) · 7.07 KB
/
demo-form.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
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
266
267
268
269
270
271
272
273
274
<!--
SwellRT Form demo
Author: Pablo Ojanguren (pablojan@gmail.com)
This demo shows main features of SwellRT API, in particular how to use
a collaborative object to share and update forms in real time across
different clients.
Important objects are exposed globally to be available from browser
debug console.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>SwellRT, Form Demo</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/swell.css">
<style type="text/css">
</style>
</head>
<body>
<div class="container-fluid">
<div class="page-header">
<h4>
<span class="text-swellrt">
<span class="text-swell">Swell</span><span class="text-rt">RT</span>
</span>
<span class="title">Form Demo</span><span class="net-status" id="netStatus"></span>
</h4>
</div>
<div class="row">
<div class="col-md-offset-3 col-md-6 col-xs-offset-1 col-xs-10">
<form class="form-horizontal">
<div class="form-group">
<label for="inputName">Name</label>
<input type="text" class="form-control" id="inputName" placeholder="Name">
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" id="inputAddress" placeholder="Address">
</div>
<div class="form-group">
<label for="inputAge">Age</label>
<input type="number" class="form-control" id="inputAge" placeholder="Age">
</div>
<div class="checkbox">
<label>
<input id="inputCheck" type="checkbox"> Check me out
</label>
</div>
<!--
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" id="exampleInputFile">
<p class="help-block">Example block-level help text here.</p>
</div>
-->
<br/>
<button id="btnSubmit" type="button" class="btn btn-default">Save changes</button>
<br/><br/>
<div class="alert alert-info" role="alert">
Current Version: <span id="version">none</span><br />
Timestamp: <span id="timestamp">none</span>
</div>
</form>
</div>
</div>
</div><!-- container -->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="/swell.js"></script>
<script>
swell.onReady(function(service) {
window.service = service;
// Put on eye on live communication turbulence
service.addConnectionHandler(connectionHandler);
service.login({
// anonymous login
})
.then(r=>{
return service.open({
id : getURLSearchParam('form','form-01')
});
})
.then(_object => {
object = _object;
var lastVersion;
// Initialize data
if (!object.node('form')) {
// This syntax puts an object
// in property 'form'.
// Changes in properties of form.* cannot be listened.
// You must assign a new object to form instead.
form = {
name: "",
address : "",
age : 0,
check : false,
timestamp : "",
version : 0
};
object.set('form', form);
// Make public after initialization
object.setPublic(true);
lastVersion = 0;
} else {
// Show current data
var form = object.get('form');
populateFormData(form);
showVersion(form.version, form.timestamp);
lastVersion = form.version;
}
// Listen updates in the form property
object.addListener(function(event) {
// Note: this hanlder is invoked
// both from local or remote changes.
if (!event) return;
if (event.key == 'form' &&
event.type == swell.Event.UPDATED_VALUE) {
var formData = event.node.value;
if (lastVersion != formData.version) {
lastVersion = formData.version;
populateFormData(formData);
showVersion(formData.version, formData.timestamp);
}
}
});
// Save changes
submit = document.getElementById('btnSubmit');
submit.addEventListener("click", function(e) {
var data = getFormData();
data.timestamp = new Date();
lastVersion = data.version = lastVersion+1;
// update the object
object.set('form', data);
showVersion(data.version, data.timestamp);
return false;
}, true);
})
.catch(e=>{
_exception = e;
console.log(e);
window.alert("Error "+e);
});
});
function getFormData() {
return {
name : document.getElementById('inputName').value,
address : document.getElementById('inputAddress').value,
age : document.getElementById('inputAge').value,
check : document.getElementById('inputCheck').checked
};
}
function populateFormData(data) {
document.getElementById('inputName').value = data.name;
document.getElementById('inputAddress').value = data.address;
document.getElementById('inputAge').value = data.age;
document.getElementById('inputCheck').checked = data.check;
}
function showVersion(version, timestamp) {
document.getElementById('version').innerHTML = version;
document.getElementById('timestamp').innerHTML = timestamp;
}
// Utility functions
function objectHandler(s) {
var m = "";
if (s.type == 1) {
m += "error "+s.exception;
}
if (s.type == 2) {
if (s.uncommitted == s.unacknowledge == s.inflight == 0)
m += "all changes saved v="+s.lastCommittedVersion;
else
return;
}
if (s.type == 3) {
m += "closed";
}
console.log(m);
}
function connectionHandler(s, e) {
console.log(s);
var netStatus = document.getElementById("netStatus");
if (s == "CONNECTED")
netStatus.innerHTML = '<span class="label label-info">Connected</span>';
if (s == "CONNECTING")
netStatus.innerHTML = '<span class="label label-warning">Connecting</span>';
if (s == "DISCONNECTED")
netStatus.innerHTML = '<span class="label label-danger">Disconnected</span>';
if (s == "ERROR")
netStatus.innerHTML = '<span class="label label-danger">Connection error. Please refresh the page!</span>';
}
function getURLSearchParam(name, defaultValue) {
var search = window.location.search;
if (search.indexOf('?') == 0)
search = search.substring(1, search.length);
var searchMap = {};
var searchArray = search.split('&');
for(var i in searchArray) {
if (searchArray.hasOwnProperty(i)) {
var item = searchArray[i].split('=');
searchMap[item[0]] = item[1];
}
}
if (searchMap.hasOwnProperty(name))
return searchMap[name];
return defaultValue;
}
</script>
</body>
</html>