diff --git a/nodejs/public/js/layout.js b/nodejs/public/js/layout.js index cb9ae28..111df6d 100755 --- a/nodejs/public/js/layout.js +++ b/nodejs/public/js/layout.js @@ -13,9 +13,12 @@ app.api = (function(app){ data: JSON.stringify(data), contentType: "application/json; charset=utf-8", dataType: "json", - success: callack, - error: function(info, error, type){ - callack(JSON.parse(info.responseText), info) + complete: function(res, text){ + callack( + text !== 'success' ? res.statusText : null, + JSON.parse(res.responseText), + res.status + ) } }); } @@ -29,8 +32,13 @@ app.api = (function(app){ }, contentType: "application/json; charset=utf-8", dataType: "json", - success: callack, - error: callack + complete: function(res, text){ + callack( + text !== 'success' ? res.statusText : null, + JSON.parse(res.responseText), + res.status + ) + } }); } @@ -48,8 +56,8 @@ app.auth = (function(app) { function isLoggedIn(callack){ if(getToken()){ - return app.api.get('users/me', function(data){ - return callack(null, data.username); + return app.api.get('users/me', function(error, data){ + return callack(error, data.username); }) }else{ callack(false, false); @@ -57,11 +65,11 @@ app.auth = (function(app) { } function logIn(args, callack){ - app.api.post('auth/login', args, function(data){ + app.api.post('auth/login', args, function(error, data){ if(data.login){ setToken(data.token); } - callack(!data.token, !!data.token); + callack(error, !!data.token); }); } @@ -75,21 +83,47 @@ app.auth = (function(app) { app.users = (function(app){ function createInvite(callack){ - app.api.post('users/invite', function(data){ - - callack(!data.token, data.token); + app.api.post('users/invite', function(error, data, status){ + callack(error, data.token); }); } function consumeInvite(args){ - app.api.post('/auth/invite/'+args.token, args, function(data){ + app.api.post('/auth/invite/'+args.token, args, function(error, data){ if(data.token){ app.auth.setToken(data.token) return callack(null, true) } + callack(error) }); } })(app); +app.hosts = (function(app){ + function list(callack){ + app.api.get('hosts/?detail=true', function(error, data){ + callack(error, data.hosts) + }); + } + + function get(host, callack){ + app.api.get('hosts/' + host, function(error, data){ + callack(error, data) + }); + } + + function add(args, callack){ + app.api.post('hosts/', args, function(error, data){ + callack(error, data); + }); + } + + return { + list: list, + get: get, + add: add + } +})(app); + app.util = (function(app){ function getUrlParameter(name) { @@ -99,6 +133,21 @@ app.util = (function(app){ return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); }; + function actionMessage(message, $target){ + $target = $target || $('div.actionMessage'); + + if($target.html() === message) return; + + if($target.html()){ + $target.slideUp('fast', function(){ + $target.html('') + if(message) actionMessage(message); + }) + return; + }else{ + $target.html(message).slideDown('fast'); + } + } $.fn.serializeObject = function() { var @@ -119,21 +168,11 @@ app.util = (function(app){ }; return { - getUrlParameter: getUrlParameter + getUrlParameter: getUrlParameter, + actionMessage: actionMessage } })(app); -// app.hosts = (function(app){ -// var hosts = [] - -// function getHost(callack){ -// app.api.get('hosts/?detail=true', function(data){ -// hosts = data.hosts -// callack(hosts) -// }); -// } -// })(app); - $( document ).ready( function () { diff --git a/nodejs/views/hosts.ejs b/nodejs/views/hosts.ejs index b819ac8..d87e542 100755 --- a/nodejs/views/hosts.ejs +++ b/nodejs/views/hosts.ejs @@ -17,51 +17,61 @@ function editHost(btn){ var btn = $(btn); currentEditHost = btn.data('host'); - $.getJSON('api/' + btn.data('host'), function( data ) { - $('.editWindow').slideDown('fast'); - $('.editWindow .panel-body').slideDown('fast'); - $('.editWindow .panel-title .pull-left').html("Edit "+btn.data('host')) - $.each( data, function( key, val ) { - $(".editWindow input[name='" + key + "']").attr('value', val); + $('.editWindow').slideDown('fast'); + $('.editWindow .panel-body').slideDown('fast'); + $('.editWindow .panel-title .pull-left').html("Edit "+btn.data('host')) + + $('div.editWindow .panel-body span').html($('#addHost').html()) + $('div.editWindow .panel-body span button').remove() + + app.hosts.get(currentEditHost, function(error, data){ + console.log(data) + $.each( data.results, function( key, val ) { + + $(".editWindow input[name='" + key + "']").val(val); }); - $("input[name='old_host']").attr('value', btn.data('host')); }); + // $.getJSON('api/hosts/' + btn.data('host'), function( data ) { + // $("input[name='old_host']").attr('value', btn.data('host')); + // }); } function tableAJAX(actionMessage){ + $('#tableAJAX').html('').hide(); - $('div.actionMessage').html('Refreshing host list...').show(); - $.get('hosts' ,function(data){ - var c = 0; - $.each( data, function( key, value ) { - if(currentEditHost == value.host){c++} - host_row = ich.rowTemplate(value); - $('#tableAJAX').append(host_row); - }); + app.util.actionMessage('') - $('#tableAJAX').fadeIn('slow'); - if(c == 0){//host is not in list - if(currentEditHost){ - $('div.editWindow').slideUp('fast'); - actionMessage = 'Host, '+ currentEditHost+' gone! Editor sleeping...'; - currentEditHost = null; - } - } - - if(!actionMessage){ - $('div.actionMessage').slideUp('fast'); + app.hosts.list(function(err, data){ + if(err) return app.util.actionMessage(err); + if(data){ + $.each(data, function( key, value ) { + host_row = ich.rowTemplate(value); + $('#tableAJAX').append(host_row); + }); + $('#tableAJAX').fadeIn('slow'); }else{ - $('div.actionMessage').html(actionMessage).slideDown('fast'); + app.util.actionMessage('No hosts...') } }); } - /*$.validateSettings( - });*/ - $(document).ready(function(){ tableAJAX(); //populate the table setInterval(tableAJAX, 30000); + + $('#addHost').on('submit', function(){ + event.preventDefault(); + + $form = $(this); + app.util.actionMessage('') + + if($form.attr('isValid') === 'true'){ + app.hosts.add($form.serializeObject(), function(error, data){ + app.util.actionMessage(data.message || 'Error!'); + if(!error) $form.trigger('reset'); + }) + } + }); });
- -
- - -
-
- - -
-
- - -
- - + + +
@@ -108,17 +106,58 @@
-
+ + +
+ +
+ +
+
+ +
+
+
+ +
+
- - + + +
+ +
+ + +
+ +
+ +
+ +
+
+ +
- +
diff --git a/nodejs/views/login.ejs b/nodejs/views/login.ejs index 2755454..f25800e 100755 --- a/nodejs/views/login.ejs +++ b/nodejs/views/login.ejs @@ -1,40 +1,25 @@ <%- include('top') %>