-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.html
329 lines (290 loc) · 13.6 KB
/
manage.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="105600165694-08orfb5k9o0tit237hnohila4m694ufu.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script>
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
};
function getHash() {
return window.location.hash;
};
var id_token = null;
var img_url = null;
function return_to_login() {
hide_everything();
display_login(true);
};
function logged_out() {
hide_everything();
display_login(true);
};
function logged_in() {
hide_everything();
display_account_info(true);
display_collection(true);
};
function display_account_info(display) {
console.log('display_account_info: ' + display);
if (display) {
$('#account_info').css('visibility', 'visible');
$('#user_image').attr('src', img_url);
} else {
$('#account_info').css("visibility", "hidden");
}
};
function display_login(display) {
if (display) {
$('#signin').css("visibility", "visible");
} else {
$('#signin').css("visibility", "hidden");
}
};
function display_collection(display) {
if (display) {
hash = getHash()
if (hash) {
console.log('hash: ' + hash);
$('#title').html(hash.slice(2));
$('#stuff').css("visibility", "visible");
refresh_rows(hash.slice(2));
}
} else {
$('#stuff').css("visibility", "hidden");
}
};
function display_not_found(display) {
if (display) {
$('#notfound').css('visibility', 'visible');
} else {
$('#notfound').css('visibility', 'hidden');
}
};
function display_insufficient(display) {
if (display) {
$('#insufficient').css('visibility', 'visible');
} else {
$('#insufficient').css('visibility', 'hidden');
}
};
function display_unauthorized(display) {
if (display) {
$('#unauthorized').css('visibility', 'visible');
} else {
$('#unauthorized').css('visibility', 'hidden');
}
};
function hide_everything() {
display_not_found(false);
display_insufficient(false);
display_unauthorized(false);
display_collection(false);
display_login(false);
display_account_info(false);
};
function refresh_rows(collection) {
if (collection) {
console.log('fetching rows');
$.ajax({
url: '/hoppers/rest/' + collection,
type: "GET",
contentType: "application/json;",
dataType: "json",
beforeSend : function( xhr ) {
xhr.setRequestHeader( "Authorization", id_token );
},
success: function (msg) {
console.log(msg);
json_response = msg;
//json_response = jQuery.parseJSON(msg);
$('#rows_holder').empty();
$('#rows_holder').append( '<table id="rows_table"></table>' );
for (var i = 0; i < json_response.length; i++) {
console.log(json_response[i]);
$('#rows_table').append('<tr id="rows_table_' + i.toString() + '"></tr>');
for (var key in json_response[i]) {
if (json_response[i].hasOwnProperty(key)) {
$('#rows_table_'+i.toString()).append('<td>'+json_response[i][key].toString()+'</td>');
console.log(key + " -> " + json_response[i][key]);
}
}
$('#rows_table_'+i.toString()).append('<td><a href="#" id="rows_table_edit' + i.toString() + '_id">Edit</a></td>');
$('#rows_table_edit'+i.toString()+'_id').click({p1: json_response, p2: i}, function(event) {
value = $('#location_name').val();
data = { 'location_name': value };
$.ajax({
url: '/hoppers/rest/'+collection+'/'+event.data.p1[event.data.p2].location_id.toString(),
type: "PUT",
contentType: "application/json;",
data: JSON.stringify(data),
dataType: "json",
beforeSend : function( xhr ) {
xhr.setRequestHeader( "Authorization", id_token );
},
success: function (msg) {
console.log(msg);
refresh_rows(collection);
},
error: function (err){
console.log(err)
refresh_rows(collection);
}
});
return false;
});
$('#rows_table_'+i.toString()).append('<td><a href="#" id="rows_table_delete' + i.toString() + '_id">Delete</a></td>');
$('#rows_table_delete'+i.toString()+'_id').click({p1: json_response, p2: i}, function(event) {
$.ajax({
url: '/hoppers/rest/'+collection+'/'+event.data.p1[event.data.p2].location_id.toString(),
type: "DELETE",
contentType: "application/json;",
//data: JSON.stringify(data),
beforeSend : function( xhr ) {
xhr.setRequestHeader( "Authorization", id_token );
},
success: function (msg) {
console.log(msg);
refresh_rows(collection);
},
error: function (err){
console.log(err)
refresh_rows(collection);
}
});
return false;
});
}
},
error: function (err){
console.log(err)
if ( err['status'] == 403 ) {
display_insufficient(true);
};
if ( err['status'] == 404 ) {
display_not_found(true);
}
}
});
};
};
function onSignIn(googleUser) {
// Useful data for your client-side scripts:
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId()); // Don't send this directly to your server!
console.log('Full Name: ' + profile.getName());
console.log('Given Name: ' + profile.getGivenName());
console.log('Family Name: ' + profile.getFamilyName());
img_url = profile.getImageUrl();
console.log("Image URL: " + profile.getImageUrl());
console.log("Email: " + profile.getEmail());
// The ID token you need to pass to your backend:
id_token = googleUser.getAuthResponse().id_token;
console.log("ID Token: " + id_token);
console.log('signing in');
$.ajax({
url: '/hoppers/tokensignin',
type: "GET",
contentType: "application/json;",
dataType: "json",
beforeSend : function( xhr ) {
xhr.setRequestHeader( "Authorization", id_token );
},
success: function (msg) {
console.log(msg);
logged_in();
},
error: function (err){
console.log('tokensignin error');
console.log(err);
hide_everything();
display_unauthorized(true);
}
});
return false;
};
function signout() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
logged_out();
});
}
$(document).ready(function() {
$('#signout').click(function() {
signout();
return false;
});
$('#add_button').click(function() {
hash = getHash();
console.log('hash: ' + hash);
collection = hash.slice(2);
value = $('#location_name').val();
console.log(value);
data = { 'location_name': value };
console.log(data);
$.ajax({
url: '/hoppers/rest/'+collection+'/',
type: "POST",
contentType: "application/json;",
data: JSON.stringify(data),
dataType: "json",
beforeSend : function( xhr ) {
xhr.setRequestHeader( "Authorization", id_token );
},
success: function (msg) {
console.log(msg);
refresh_rows(collection);
},
error: function (err){
console.log(err)
refresh_rows(collection);
}
});
return false;
});
$('#return').click(function() {
signout();
return false;
});
});
</script>
</head>
<body>
<div id="signin" style="visibility:visible;" class="g-signin2" data-onsuccess="onSignIn" data-theme="dark">
</div>
<div id="stuff" style="visibility:hidden;">
<h1 id="title">Collection</h1>
<div id="rows_holder">
</div>
<p>Location Name: <input type="text" name="location_name" id="location_name"/><button id="add_button">Add</button></p>
</div>
<div id="account_info" style="visibility:hidden;">
<p><img id="user_image"/></p>
<a id="signout" href="#">Sign out</a>
</div>
<div id="unauthorized" style="visibility:hidden;">
<h1>Unauthorized</h1>
<p>User could not be authenticated or is not authorized.</p>
<a id="return" href="#">Return to login</a>
</div>
<div id="insufficient" style="visibility:hidden;">
<h1>Insufficient Priveleges</h1>
<p>User does not have sufficient privelges.</p>
</div>
<div id="notfound" style="visibility:hidden;">
<h1>Not found</h1>
<p>Resource not found.</p>
</div>
</body>
</html>