This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
293 lines (256 loc) · 8.93 KB
/
index.php
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Actor' rel='stylesheet' type='text/css'/ >
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-lightness/jquery-ui.css" type="text/css" media="all" />
<!-- Bootstrap core CSS -->
<!-- link href="http://startbootstrap.com/templates/sb-admin/css/bootstrap.css" rel="stylesheet"/ -->
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" />
<!-- JavaScript -->
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<style>
.key { width:200px; display:block;float:left;clear:left}
</style>
<div class="container">
<div class="page-wrapper">
<div data-role="content">
<h1>WEIRd</h1>
<div class="row">
<div class="col-md-2">
<div class="panel panel-default connectpanel">
<div class="panel-heading">
<button class="btn connect">Connect</button>
</div>
<div class="panel-body">
<div role="form">
<label for="bean">Current Collection</label>
<input type="text" name="bean" class="bean form-control" value="books">
</div>
</div>
</div>
</div>
<div class="col-md-10">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Arbitrary form fields to store in collection</h3>
<small>* Add a couple (even if they are the same) and play with the collection</small>
</div>
<div class="panel-body">
<form role="form" id="formA">
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" type="text" name="name" value="Leo Tolstoy ">
</div>
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" name="title" value="War and peace">
</div>
<div class="form-group">
<a class="SET btn btn-success" href="#SET"><i class="fa fa-plus"></i> ADD RECORD</a>
</div>
</form>
</div>
<div class="panel-footer">
<small><b>NOTE:</b> You could add more form fields in the index.html and it will save them without requiring a schema change!</small>
</div>
</div>
</div>
</div>
<!--
<div class="row">
<div class="col-md-6">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Single record</h3>
</div>
<div class="panel-body">
<form id="formB">
<label for="id">RECORD ID</label> <input type="text" name="id" value="" size="10"> <a class="GET btn btn-sm btn-default" href="#GET"><i class="fa fa-arrow-down"></i> GET</a> <a class="SUB btn btn-sm btn-primary" href="#SUB"><i class="fa fa-link"></i> SUB</a> <a class="DEL btn btn-sm btn-danger" href="#DEL"><i class="fa fa-trash-o"></i> DEL</a>
</form>
</div>
<div class="panel-footer">
<small><b>NOTE:</b>Click "LIST" on the collection to see all the IDs</small>
</div>
</div>
</div>
-->
<div class="col-md-6">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">Collection output will show up here</h3>
</div>
<div class="panel-body">
<a class="GET btn btn-primary" href="#GET"><i class="fa fa-list"></i> LIST</a>
<a class="POP btn btn-primary" href="#POP"><i class="fa fa-eject"></i> POP</a><br />
</div>
<div class="panel-footer">
<small><b>NOTE:</b> POP will FIFO GET/DELETE one record from the TOP of the collection</small>
<div id="output"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
var weird;
var dscount = 0 ;
// FormA the data input form
// FormB is the "ID" input form
$(document).ready(function() {
connect();
});
//$('.connect').on('click',function() { connect() });
$('.SET').on('click',function() {
doWeird('set',$('.bean').val(),JSON.stringify($('#formA').serializeArray()));
});
$('.DEL').on('click',function() {
doWeird('del',$('.bean').val(),JSON.stringify($('#formB').serializeArray()));
});
$('.SUB').on('click',function() {
doWeird('sub',$('.bean').val(),JSON.stringify($('#formB').serializeArray()));
});
$('.POP').on('click',function() {
doWeird('pop',$('.bean').val(),JSON.stringify($('#formB').serializeArray()));
});
$('.GET').on('click',function() {
doWeird('get',$('.bean').val(),JSON.stringify($('#formB').serializeArray()));
});
function doWeird(cmd, bean, data_payload)
{
$('#formB input').val('');
if (typeof weird == 'undefined')
{
// This could be whatever, but for now:
alert('Please click the "Connect" button.');
return(false);
}
else
{
console.log('Performing: '+cmd+' '+bean, data_payload);
weird.send(cmd.toUpperCase()+' '+bean+' '+data_payload);
return(true);
}
}
function perform_magic(message)
{
$('#formB input').attr('value','');
var dataSet = '';
var bean = 'books';
$.each(message, function(idx,dataSet)
{
if (idx =='ERR')
{
$('#output').text('Error: '+dataSet.message);
return false;
}
if (idx == 'NEW')
{
doWeird('get',$('.bean').val(),JSON.stringify($('#formB').serializeArray()));
}
else if (idx == 'DEL')
{
//bean = dataSet.BEAN;
console.log(dataSet.id);
$('#'+bean+'panel_'+dataSet.id).remove();
}
else if (idx == 'OK')
{
var magic = '';
var obj = '';
$.each(dataSet, function (i,obj)
{
var record_id = '';
// Get the record id
$.each(obj, function(k,v) {
if (k == 'id') record_id = v;
});
console.log($('#'+bean+'panel_'+record_id).length);
if ($('#'+bean+'panel_'+record_id).length) {
$.each(obj, function(k,v) {
// This is this particular record id
$('#'+bean+'panel_'+record_id+' ._'+k).val(v);
$('#'+bean+'panel_'+record_id+' ._'+k).fadeTo('slow',0.5,function() {
console.log('#'+bean+'panel_'+record_id+' ._'+k+' = '+v);
});
});
}
else {
// Maybe use some sort of templating system here for wigitization of data?
magic += '<div class="panel" id="'+bean+'panel_'+record_id+'"><div class="panel-body">';
magic += '<form role="form" id="magic_'+record_id+'" class="magic">'
$.each(obj, function(k,v)
{
magic += '<div class="form-group">';
magic += '<label for="'+k+'">'+k+'</label>';
magic += '<input class="form-control _'+k+'" name="'+k+'" value="'+v+'">';
magic += '</div>';
});
magic += '<a class="UPDATE btn btn-default" href="#SET" datatype="'+bean+'" id="mod_'+record_id+'" ><i class="fa fa-check"></i> SET</a>';
magic += ' <a class="SUBSCRIBE btn btn-primary" href="#SUB" datatype="'+bean+'" id="sub_'+record_id+'"><i class="fa fa-link"></i> SUB</a>';
magic += ' <a class="DELETE btn btn-danger" href="#DEL" datatype="'+bean+'" id="del_'+record_id+'"><i class="fa fa-trash-o"></i> DEL</a>';
magic += '</form></div></div>';
}
});
if (magic.length) $('#output').append(magic);
}
else
{
console.log('Nothing special here: ',idx,message);
}
});
// For clicking sub on specific item in list
$('.DELETE').on('click', function()
{
id = $(this).attr('id').split('_');
var magic_form_id = 'form#magic_'+id[1];
var magic_panel = '#'+$(this).attr('datatype')+'panel_'+id[1];
del_json = { "id" : id[1] };
doWeird('del',$('.bean').val(), JSON.stringify(del_json));
console.log('Magic Panel',magic_panel);
$(magic_panel).remove();
});
// For clicking sub on specific item in list
$('.SUBSCRIBE').on('click', function()
{
id = $(this).attr('id').split('_');
subscribe_json = { "id" : id[1] };
doWeird('sub',$('.bean').val(), JSON.stringify(subscribe_json));
});
// Clicking "SET" in the list
$('.UPDATE').on('click',function()
{
var id = $(this).attr('id').split('_');
var magic_form_id = 'form#magic_'+id[1];
var json = JSON.stringify($(magic_form_id).serializeArray());
doWeird('set', $('.bean').val(), json);
});
}
function connect()
{
weird = new WebSocket("ws://"+window.location.hostname+":8888");
weird.onopen = function (event) {
$('.connectpanel').css('background','lightgreen');
$('.connect').css('background','lightgreen');
$('.connect').text('Connected');
};
weird.onclose = function (event) {
$('.connectpanel').css('background','none');
$('.connect').css('background','lightgrey');
$('.connect').text('Connect');
}
weird.onmessage = function (data, start, end)
{
console.log('Got message.', data, start, end);
var message = JSON.parse(data.data);
perform_magic(message);
}
}
</script>
</body>