vines-services 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,485 @@
1
+ var SetupPage;
2
+ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
3
+ SetupPage = (function() {
4
+ var SERVICES, SYSTEMS, USERS;
5
+ SERVICES = 'http://getvines.com/protocol/services';
6
+ SYSTEMS = 'http://getvines.com/protocol/systems';
7
+ USERS = 'http://getvines.com/protocol/users';
8
+ function SetupPage(session) {
9
+ this.session = session;
10
+ this.api = new Api(this.session);
11
+ this.layout = null;
12
+ this.selected = null;
13
+ this.services = [];
14
+ this.users = [];
15
+ }
16
+ SetupPage.prototype.findSystem = function(name) {
17
+ return this.api.get(SYSTEMS, {
18
+ name: name
19
+ }, __bind(function(result) {
20
+ return this.drawSystemInfo(result);
21
+ }, this));
22
+ };
23
+ SetupPage.prototype.findServices = function() {
24
+ return this.api.get(SERVICES, {}, __bind(function(result) {
25
+ this.services = result.rows;
26
+ return this.drawServices();
27
+ }, this));
28
+ };
29
+ SetupPage.prototype.drawServices = function() {
30
+ var node, service, _i, _len, _ref;
31
+ if ($('#setup-page #services').length === 0) {
32
+ return;
33
+ }
34
+ $('#services').empty();
35
+ _ref = this.services;
36
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
37
+ service = _ref[_i];
38
+ node = $("<li>\n <input id='service-" + service.id + "' type='checkbox' value='" + service.id + "'/>\n <label for='service-" + service.id + "'></label>\n</li>").appendTo('#services');
39
+ $('label', node).text(service.name);
40
+ }
41
+ if (this.selected) {
42
+ $('#services input[type="checkbox"]').val(this.selected.services);
43
+ }
44
+ if (this.selected && !this.api.user.permissions.services) {
45
+ return $('#services input[type="checkbox"]').prop('disabled', true);
46
+ }
47
+ };
48
+ SetupPage.prototype.findUsers = function() {
49
+ return this.api.get(USERS, {}, __bind(function(result) {
50
+ this.users = result.rows;
51
+ return this.drawUsers();
52
+ }, this));
53
+ };
54
+ SetupPage.prototype.drawUsers = function() {
55
+ var systems, user, _i, _len, _ref, _results;
56
+ if ($('#setup-page #users').length === 0) {
57
+ return;
58
+ }
59
+ $('#users').empty();
60
+ systems = $('#systems-nav').hasClass('selected');
61
+ _ref = this.users;
62
+ _results = [];
63
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
64
+ user = _ref[_i];
65
+ _results.push(user.system === systems ? this.userNode(user) : void 0);
66
+ }
67
+ return _results;
68
+ };
69
+ SetupPage.prototype.userNode = function(user) {
70
+ var name, node;
71
+ node = $("<li data-name=\"\" data-jid=\"" + user.jid + "\" id=\"" + user.jid + "\">\n <span class=\"text\"></span>\n <span class=\"jid\">" + user.jid + "</span>\n</li>").appendTo('#users');
72
+ name = this.userName(user);
73
+ $('.text', node).text(name);
74
+ node.attr('data-name', name);
75
+ node.click(__bind(function(event) {
76
+ return this.selectUser(event.currentTarget);
77
+ }, this));
78
+ return node;
79
+ };
80
+ SetupPage.prototype.userName = function(user) {
81
+ return user.name || user.jid.split('@')[0];
82
+ };
83
+ SetupPage.prototype.selectUser = function(node) {
84
+ var jid, name;
85
+ jid = $(node).attr('data-jid');
86
+ name = $(node).attr('data-name');
87
+ $('#users li').removeClass('selected');
88
+ $(node).addClass('selected');
89
+ $('#remove-user-msg').html("Are you sure you want to remove " + ("<strong>" + name + "</strong>?"));
90
+ $('#remove-user-form .buttons').fadeIn(200);
91
+ return this.api.get(USERS, {
92
+ jid: jid
93
+ }, __bind(function(result) {
94
+ this.selected = result;
95
+ if (result.system) {
96
+ return this.drawSystemEditor(result);
97
+ } else {
98
+ return this.drawUserEditor(result);
99
+ }
100
+ }, this));
101
+ };
102
+ SetupPage.prototype.removeUser = function() {
103
+ var selected;
104
+ this.toggleForm('#remove-user-form');
105
+ selected = $("#users li[data-jid='" + this.selected.jid + "']");
106
+ this.api.remove(USERS, this.selected.jid, __bind(function(result) {
107
+ return selected.fadeOut(200, __bind(function() {
108
+ var u;
109
+ this.users = (function() {
110
+ var _i, _len, _ref, _results;
111
+ _ref = this.users;
112
+ _results = [];
113
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
114
+ u = _ref[_i];
115
+ if (u.jid !== this.selected.jid) {
116
+ _results.push(u);
117
+ }
118
+ }
119
+ return _results;
120
+ }).call(this);
121
+ selected.remove();
122
+ this.selected = null;
123
+ if ($('#users-nav').hasClass('selected')) {
124
+ return this.drawUserBlankSlate();
125
+ } else {
126
+ return this.drawSystemBlankSlate();
127
+ }
128
+ }, this));
129
+ }, this));
130
+ return false;
131
+ };
132
+ SetupPage.prototype.selectTask = function(event) {
133
+ this.selected = null;
134
+ $('#setup li').removeClass('selected secondary');
135
+ $(event.currentTarget).addClass('selected secondary');
136
+ switch ($(event.currentTarget).attr('id')) {
137
+ case 'users-nav':
138
+ $('#beta-header').text('Users');
139
+ this.drawUsers();
140
+ this.drawUserBlankSlate();
141
+ if (this.api.user.permissions.users) {
142
+ return $('#beta-controls div').show();
143
+ } else {
144
+ return $('#beta-controls div').hide();
145
+ }
146
+ break;
147
+ case 'systems-nav':
148
+ $('#beta-header').text('Systems');
149
+ this.drawUsers();
150
+ this.drawSystemBlankSlate();
151
+ if (this.api.user.permissions.systems) {
152
+ return $('#beta-controls div').show();
153
+ } else {
154
+ return $('#beta-controls div').hide();
155
+ }
156
+ }
157
+ };
158
+ SetupPage.prototype.toggleForm = function(form, fn) {
159
+ form = $(form);
160
+ $('form.overlay').each(function() {
161
+ if (this.id !== form.attr('id')) {
162
+ return $(this).hide();
163
+ }
164
+ });
165
+ if (form.is(':hidden')) {
166
+ if (fn) {
167
+ fn();
168
+ }
169
+ return form.fadeIn(100);
170
+ } else {
171
+ return form.fadeOut(100, function() {
172
+ form[0].reset();
173
+ if (fn) {
174
+ return fn();
175
+ }
176
+ });
177
+ }
178
+ };
179
+ SetupPage.prototype.validateUser = function() {
180
+ var node, password1, password2, valid;
181
+ $('#user-name-error').empty();
182
+ $('#password-error').empty();
183
+ valid = true;
184
+ node = $.trim($('#user-name').val());
185
+ password1 = $.trim($('#password1').val());
186
+ password2 = $.trim($('#password2').val());
187
+ if (this.selected) {
188
+ if (password2.length > 0 && password2.length < 8) {
189
+ $('#password-error').text('Password must be at least 8 characters.');
190
+ valid = false;
191
+ }
192
+ if (this.session.bareJid() !== this.selected.jid) {
193
+ if (password1 !== password2) {
194
+ $('#password-error').text('Passwords must match.');
195
+ valid = false;
196
+ }
197
+ }
198
+ } else {
199
+ if (node === '') {
200
+ $('#user-name-error').text('User name is required.');
201
+ valid = false;
202
+ }
203
+ if (node.match(/[\s"&'\/:<>@]/)) {
204
+ $('#user-name-error').text('User name contains forbidden characters.');
205
+ valid = false;
206
+ }
207
+ if (password1.length === 0 || password2.length === 0) {
208
+ $('#password-error').text('Password is required.');
209
+ valid = false;
210
+ }
211
+ if (password1 !== password2) {
212
+ $('#password-error').text('Passwords must match.');
213
+ valid = false;
214
+ }
215
+ if (password2.length < 8) {
216
+ $('#password-error').text('Password must be at least 8 characters.');
217
+ valid = false;
218
+ }
219
+ }
220
+ return valid;
221
+ };
222
+ SetupPage.prototype.saveUser = function() {
223
+ var user;
224
+ if (!this.validateUser()) {
225
+ return false;
226
+ }
227
+ user = {
228
+ jid: $('#jid').val(),
229
+ username: $('#user-name').val(),
230
+ name: $('#name').val(),
231
+ password1: $('#password1').val(),
232
+ password2: $('#password2').val(),
233
+ services: $('#services :checked').map(function() {
234
+ return $(this).val();
235
+ }).get(),
236
+ permissions: {
237
+ systems: $('#perm-systems').prop('checked'),
238
+ services: $('#perm-services').prop('checked'),
239
+ files: $('#perm-files').prop('checked'),
240
+ users: $('#perm-users').prop('checked')
241
+ }
242
+ };
243
+ this.api.save(USERS, user, __bind(function(result) {
244
+ var node;
245
+ new Notification('User saved successfully');
246
+ $('#jid').val(result.jid);
247
+ node = $("#users li[data-jid='" + result.jid + "']");
248
+ if (node.length === 0) {
249
+ this.users.push(result);
250
+ node = this.userNode(result);
251
+ return this.selectUser(node);
252
+ } else {
253
+ return $('.text', node).text(this.userName(result));
254
+ }
255
+ }, this));
256
+ return false;
257
+ };
258
+ SetupPage.prototype.validateSystem = function() {
259
+ var node, valid;
260
+ $('#user-name-error').empty();
261
+ valid = true;
262
+ node = $.trim($('#user-name').val());
263
+ if (!this.selected) {
264
+ if (node === '') {
265
+ $('#user-name-error').text('Hostname is required.');
266
+ valid = false;
267
+ }
268
+ if (node.match(/[\s"&'\/:<>@]/)) {
269
+ $('#user-name-error').text('Hostname contains forbidden characters.');
270
+ valid = false;
271
+ }
272
+ }
273
+ return valid;
274
+ };
275
+ SetupPage.prototype.saveSystem = function() {
276
+ var user;
277
+ if (!this.validateSystem()) {
278
+ return false;
279
+ }
280
+ user = {
281
+ jid: $('#jid').val(),
282
+ username: $('#user-name').val(),
283
+ password1: $('#password1').val(),
284
+ password2: $('#password1').val(),
285
+ system: true
286
+ };
287
+ this.api.save(USERS, user, __bind(function(result) {
288
+ var node;
289
+ new Notification('System saved successfully');
290
+ $('#jid').val(result.jid);
291
+ node = $("#users li[data-jid='" + result.jid + "']");
292
+ if (node.length === 0) {
293
+ this.users.push(result);
294
+ node = this.userNode(result);
295
+ return this.selectUser(node);
296
+ } else {
297
+ return $('.text', node).text(result.name);
298
+ }
299
+ }, this));
300
+ return false;
301
+ };
302
+ SetupPage.prototype.rand = function() {
303
+ return Math.floor(Math.random() * 16);
304
+ };
305
+ SetupPage.prototype.token = function() {
306
+ var i;
307
+ return ((function() {
308
+ var _results;
309
+ _results = [];
310
+ for (i = 0; i <= 127; i++) {
311
+ _results.push(this.rand().toString(16));
312
+ }
313
+ return _results;
314
+ }).call(this)).join('');
315
+ };
316
+ SetupPage.prototype.drawUserBlankSlate = function() {
317
+ var msg;
318
+ $('#charlie').empty();
319
+ msg = this.api.user.permissions.users ? 'Select a user account to update or add a new user.' : 'Select a user account to update.';
320
+ $("<form id=\"blank-slate\">\n <p>" + msg + "</p>\n <input type=\"submit\" id=\"blank-slate-add\" value=\"Add User\"/>\n</form>").appendTo('#charlie');
321
+ if (!this.api.user.permissions.users) {
322
+ $('#blank-slate-add').remove();
323
+ }
324
+ return $('#blank-slate').submit(__bind(function() {
325
+ this.drawUserEditor();
326
+ return false;
327
+ }, this));
328
+ };
329
+ SetupPage.prototype.drawSystemBlankSlate = function() {
330
+ $('#charlie').empty();
331
+ $("<form id=\"blank-slate\">\n <p>\n Systems need a user account before they can connect and\n authenticate with the chat server.\n </p>\n <input type=\"submit\" id=\"blank-slate-add\" value=\"Add System\"/>\n</form>").appendTo('#charlie');
332
+ if (!this.api.user.permissions.systems) {
333
+ $('#blank-slate-add').remove();
334
+ }
335
+ return $('#blank-slate').submit(__bind(function() {
336
+ this.drawSystemEditor();
337
+ return false;
338
+ }, this));
339
+ };
340
+ SetupPage.prototype.draw = function() {
341
+ var fn;
342
+ if (!this.session.connected()) {
343
+ window.location.hash = '';
344
+ return;
345
+ }
346
+ $('body').attr('id', 'setup-page');
347
+ $('#container').hide().empty();
348
+ $("<div id=\"alpha\" class=\"sidebar column y-fill\">\n <h2>Setup</h2>\n <ul id=\"setup\" class=\"selectable scroll y-fill\">\n <li id=\"users-nav\" class='selected secondary'>\n <span class=\"text\">Users</span>\n </li>\n <li id=\"systems-nav\">\n <span class=\"text\">Systems</span>\n </li>\n </ul>\n <div id=\"alpha-controls\" class=\"controls\"></div>\n</div>\n<div id=\"beta\" class=\"sidebar column y-fill\">\n <h2><span id=\"beta-header\">Users</span> <div id=\"search-users-icon\"></div></h2>\n <div id=\"search-users-form\"></div>\n <ul id=\"users\" class=\"selectable scroll y-fill\"></ul>\n <form id=\"remove-user-form\" class=\"overlay\" style=\"display:none;\">\n <h2>Remove User</h2>\n <p id=\"remove-user-msg\">Select a user to delete.</p>\n <fieldset class=\"buttons\" style=\"display:none;\">\n <input id=\"remove-user-cancel\" type=\"button\" value=\"Cancel\"/>\n <input id=\"remove-user-ok\" type=\"submit\" value=\"Remove\"/>\n </fieldset>\n </form>\n <div id=\"beta-controls\" class=\"controls\">\n <div id=\"add-user\"></div>\n <div id=\"remove-user\"></div>\n </div>\n</div>\n<div id=\"charlie\" class=\"primary column x-fill y-fill\"></div>").appendTo('#container');
349
+ this.drawUserBlankSlate();
350
+ $('#setup li').click(__bind(function(event) {
351
+ return this.selectTask(event);
352
+ }, this));
353
+ this.findUsers();
354
+ this.findServices();
355
+ $('#container').show();
356
+ this.layout = this.resize();
357
+ new Button('#add-user', ICONS.plus);
358
+ new Button('#remove-user', ICONS.minus);
359
+ if (!this.api.user.permissions.users) {
360
+ $('#beta-controls div').hide();
361
+ }
362
+ if (!this.api.user.permissions.systems) {
363
+ $('#systems-nav').hide();
364
+ }
365
+ $('#add-user').click(__bind(function() {
366
+ if ($('#users-nav').hasClass('selected')) {
367
+ return this.drawUserEditor();
368
+ } else {
369
+ return this.drawSystemEditor();
370
+ }
371
+ }, this));
372
+ $('#remove-user').click(__bind(function() {
373
+ return this.toggleForm('#remove-user-form');
374
+ }, this));
375
+ $('#remove-user-cancel').click(__bind(function() {
376
+ return this.toggleForm('#remove-user-form');
377
+ }, this));
378
+ $('#remove-user-form').submit(__bind(function() {
379
+ return this.removeUser();
380
+ }, this));
381
+ fn = __bind(function() {
382
+ this.layout.resize();
383
+ return this.layout.resize();
384
+ }, this);
385
+ return new Filter({
386
+ list: '#users',
387
+ icon: '#search-users-icon',
388
+ form: '#search-users-form',
389
+ attrs: ['data-jid', 'data-name'],
390
+ open: fn,
391
+ close: fn
392
+ });
393
+ };
394
+ SetupPage.prototype.drawUserEditor = function(user) {
395
+ var name, _i, _len, _ref;
396
+ if (!user) {
397
+ this.selected = null;
398
+ $('#users li').removeClass('selected');
399
+ }
400
+ $('#charlie').empty();
401
+ $("<form id=\"editor-form\" class=\"sections y-fill scroll\">\n <div>\n <section>\n <h2>User</h2>\n <fieldset id=\"jid-fields\">\n <input id=\"jid\" type=\"hidden\" value=\"\"/>\n <label for=\"name\">Real Name</label>\n <input id=\"name\" type=\"text\" maxlength=\"1024\"/>\n </fieldset>\n </section>\n <section>\n <h2>Password</h2>\n <fieldset>\n <label id=\"password1-label\" for=\"password1\">Current Password</label>\n <input id=\"password1\" type=\"password\" maxlength=\"1024\"/>\n <label id=\"password2-label\" for=\"password2\">New Password</label>\n <input id=\"password2\" type=\"password\" maxlength=\"1024\"/>\n <p id=\"password-error\" class=\"error\"></p>\n </fieldset>\n </section>\n <section>\n <h2>Permissions</h2>\n <fieldset>\n <label>Manage</label>\n <ul id=\"permissions\">\n <li>\n <input id=\"perm-systems\" type=\"checkbox\" value=\"systems\"/>\n <label for=\"perm-systems\">Systems</label>\n </li>\n <li>\n <input id=\"perm-services\" type=\"checkbox\" value=\"services\"/>\n <label for=\"perm-services\">Services</label>\n </li>\n <li>\n <input id=\"perm-users\" type=\"checkbox\" value=\"users\"/>\n <label for=\"perm-users\">Users</label>\n </li>\n <li>\n <input id=\"perm-files\" type=\"checkbox\" value=\"files\"/>\n <label for=\"perm-files\">Files</label>\n </li>\n </ul>\n </fieldset>\n </section>\n <section>\n <h2>Services</h2>\n <fieldset>\n <label>Access To</label>\n <ul id=\"services\" class=\"scroll\"></ul>\n </fieldset>\n </section>\n </div>\n</form>\n<form id=\"editor-buttons\">\n <input id=\"save\" type=\"submit\" value=\"Save\"/>\n</form>").appendTo('#charlie');
402
+ if (user) {
403
+ $("<label>Account Name</label>\n<p>" + user.jid + "</p>").prependTo('#jid-fields');
404
+ $('#name').focus();
405
+ if (this.session.bareJid() !== user.jid) {
406
+ $('#password1-label').text('Password');
407
+ $('#password2-label').text('Password Again');
408
+ }
409
+ $('#jid').val(user.jid);
410
+ $('#name').val(user.name);
411
+ $('#user-name').val(user.jid.split('@')[0]);
412
+ _ref = 'services systems files users'.split(' ');
413
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
414
+ name = _ref[_i];
415
+ if (user.permissions[name]) {
416
+ $("#perm-" + name).prop('checked', true);
417
+ }
418
+ if (this.session.bareJid() === user.jid) {
419
+ $("#perm-" + name).prop('disabled', true);
420
+ }
421
+ }
422
+ } else {
423
+ $("<label for=\"user-name\">User Name</label>\n<input id=\"user-name\" type=\"text\" maxlength=\"1023\"/>\n<p id=\"user-name-error\" class=\"error\"></p>").prependTo('#jid-fields');
424
+ $('#password1-label').text('Password');
425
+ $('#password2-label').text('Password Again');
426
+ $('#user-name').focus();
427
+ }
428
+ if (this.services.length > 0) {
429
+ this.drawServices();
430
+ }
431
+ this.layout.resize();
432
+ $('#editor-form').submit(__bind(function() {
433
+ return this.saveUser();
434
+ }, this));
435
+ return $('#editor-buttons').submit(__bind(function() {
436
+ return this.saveUser();
437
+ }, this));
438
+ };
439
+ SetupPage.prototype.drawSystemEditor = function(user) {
440
+ if (!user) {
441
+ this.selected = null;
442
+ $('#users li').removeClass('selected');
443
+ }
444
+ $('#charlie').empty();
445
+ $("<form id=\"editor-form\" class=\"sections y-fill scroll\">\n <div>\n <section>\n <h2>System</h2>\n <fieldset id=\"jid-fields\">\n <input id=\"jid\" type=\"hidden\" value=\"\"/>\n <label id=\"password1-label\" for=\"password1\">Authentication Token</label>\n <div id=\"token-container\">\n <input id=\"password1\" type=\"text\" readonly placeholder=\"Press Generate to create a new token\"/>\n <input id=\"new-token\" type=\"button\" value=\"Generate\"/>\n </div>\n </fieldset>\n </section>\n <section id=\"info\">\n <h2>Info</h2>\n <fieldset>\n <label>Platform</label>\n <p id=\"info-platform\">-</p>\n <label>Hostname</label>\n <p id=\"info-fqdn\">-</p>\n <label>IP Address</label>\n <p id=\"info-ip\">-</p>\n <label>MAC Address</label>\n <p id=\"info-mac\">-</p>\n </fieldset>\n </section>\n </div>\n</form>\n<form id=\"editor-buttons\">\n <input id=\"save\" type=\"submit\" value=\"Save\"/>\n</form>").appendTo('#charlie');
446
+ $('#new-token').click(__bind(function() {
447
+ return $('#password1').val(this.token());
448
+ }, this));
449
+ if (user) {
450
+ this.findSystem(user.jid.split('@')[0]);
451
+ }
452
+ if (user) {
453
+ $("<label>Account Name</label>\n<p>" + user.jid + "</p>").prependTo('#jid-fields');
454
+ $('#jid').val(user.jid);
455
+ $('#user-name').val(user.jid.split('@')[0]);
456
+ } else {
457
+ $("<label for=\"user-name\">Hostname</label>\n<input id=\"user-name\" type=\"text\" maxlength=\"1023\"/>\n<p id=\"user-name-error\" class=\"error\"></p>").prependTo('#jid-fields');
458
+ $('#user-name').focus();
459
+ $('#password1').val(this.token());
460
+ }
461
+ this.layout.resize();
462
+ $('#editor-form').submit(__bind(function() {
463
+ return this.saveSystem();
464
+ }, this));
465
+ return $('#editor-buttons').submit(__bind(function() {
466
+ return this.saveSystem();
467
+ }, this));
468
+ };
469
+ SetupPage.prototype.drawSystemInfo = function(system) {
470
+ $('#info-platform').text(system.platform);
471
+ $('#info-fqdn').text(system.fqdn);
472
+ $('#info-ip').text(system.ipaddress);
473
+ return $('#info-mac').text(system.macaddress);
474
+ };
475
+ SetupPage.prototype.resize = function() {
476
+ var a, b, c;
477
+ a = $('#alpha');
478
+ b = $('#beta');
479
+ c = $('#charlie');
480
+ return new Layout(function() {
481
+ return c.css('left', a.outerWidth() + b.outerWidth());
482
+ });
483
+ };
484
+ return SetupPage;
485
+ })();