diff --git a/public/js/app/models/server.js b/public/js/app/models/server.js index 41bbfb3..9340864 100644 --- a/public/js/app/models/server.js +++ b/public/js/app/models/server.js @@ -1,3 +1,4 @@ +var $ = require('jquery') var Backbone = require('backbone') module.exports = Backbone.Model.extend({ @@ -22,5 +23,44 @@ module.exports = Backbone.Model.extend({ von: false, verify_signatures: false }, - urlRoot: '/api/servers/' + urlRoot: '/api/servers/', + start: function (cb) { + var self = this + $.ajax({ + url: '/api/servers/' + self.get('id') + '/start', + type: 'POST', + success: function (resp) { + self.set('pid', resp.pid) + + if (cb) { + cb() + } + }, + error: function (err) { + if (cb) { + cb(err) + } + } + }) + }, + + stop: function (cb) { + var self = this + $.ajax({ + url: '/api/servers/' + self.get('id') + '/stop', + type: 'POST', + success: function (resp) { + self.set('pid', resp.pid) + + if (cb) { + cb() + } + }, + error: function (err) { + if (cb) { + cb(err) + } + } + }) + } }) diff --git a/public/js/app/views/servers/info.js b/public/js/app/views/servers/info.js index 1d2ac16..fb5de69 100644 --- a/public/js/app/views/servers/info.js +++ b/public/js/app/views/servers/info.js @@ -1,4 +1,3 @@ -var $ = require('jquery') var _ = require('underscore') var Marionette = require('marionette') var sweetAlert = require('sweet-alert') @@ -16,14 +15,18 @@ module.exports = Marionette.LayoutView.extend({ start: function (event) { var self = this event.preventDefault() - $.ajax({ - url: '/api/servers/' + this.model.get('id') + '/start', - type: 'POST', - success: function (resp) { - self.model.set('pid', resp.pid) - self.render() - }, - error: $.noop + + this.model.start(function (err) { + if (err) { + sweetAlert({ + title: 'Error', + text: err.responseText, + type: 'error' + }) + return + } + + self.render() }) }, @@ -39,14 +42,19 @@ module.exports = Marionette.LayoutView.extend({ confirmButtonText: 'Yes, stop it!' }, function () { - $.ajax({ - url: '/api/servers/' + self.model.get('id') + '/stop', - type: 'POST', - success: function (resp) { - self.model.set('pid', resp.pid) - self.render() - }, - error: $.noop + event.preventDefault() + + self.model.stop(function (err) { + if (err) { + sweetAlert({ + title: 'Error', + text: err.responseText, + type: 'error' + }) + return + } + + self.render() }) }) } diff --git a/public/js/app/views/servers/list_item.js b/public/js/app/views/servers/list_item.js index e61eb19..575e899 100644 --- a/public/js/app/views/servers/list_item.js +++ b/public/js/app/views/servers/list_item.js @@ -12,7 +12,9 @@ module.exports = Marionette.ItemView.extend({ events: { 'click .clone': 'clone', - 'click .delete': 'delete' + 'click .delete': 'delete', + 'click .start': 'start', + 'click .stop': 'stop' }, modelEvents: { @@ -41,6 +43,50 @@ module.exports = Marionette.ItemView.extend({ }) }, + start: function (event) { + var self = this + event.preventDefault() + this.model.start(function (err) { + if (err) { + sweetAlert({ + title: 'Error', + text: err.responseText, + type: 'error' + }) + return + } + + self.render() + }) + }, + + stop: function (event) { + var self = this + event.preventDefault() + sweetAlert({ + title: 'Are you sure?', + text: 'The server will stopped.', + type: 'warning', + showCancelButton: true, + confirmButtonClass: 'btn-warning', + confirmButtonText: 'Yes, stop it!' + }, + function () { + self.model.stop(function (err) { + if (err) { + sweetAlert({ + title: 'Error', + text: err.responseText, + type: 'error' + }) + return + } + + self.render() + }) + }) + }, + serverUpdated: function (event) { this.render() } diff --git a/public/js/tpl/servers/list.html b/public/js/tpl/servers/list.html index b9fb836..fd4f04e 100644 --- a/public/js/tpl/servers/list.html +++ b/public/js/tpl/servers/list.html @@ -1,7 +1,7 @@
Status | +Status | Port | Title | diff --git a/public/js/tpl/servers/list_item.html b/public/js/tpl/servers/list_item.html index 1d462f4..dd374fc 100644 --- a/public/js/tpl/servers/list_item.html +++ b/public/js/tpl/servers/list_item.html @@ -1,13 +1,28 @@ | <% if (typeof(pid) != "undefined" && pid) { %> <% if (state) { %> - Online + Online + (<%- (state.players) ? state.players.length : '?' %>/<%- state.maxplayers %>) + <% } else { %> Launching <% } %> <% } else { %> Offline <% } %> + | ++ <% if (typeof(pid) != "undefined" && pid) { %> + + <% } else { %> + + <% } %> + + | <%-port%> |
---|