From 8baf73493d24c85bc5d13069044fc1150ed62c2e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 17 Sep 2015 16:57:31 +1200 Subject: Move login calls to controller --- web/app/Application.js | 7 +++- web/app/LoginManager.js | 42 +-------------------- web/app/view/device/DeviceController.js | 5 ++- web/app/view/login/LoginController.js | 65 +++++++++++++++++++-------------- web/app/view/user/UserController.js | 6 +-- 5 files changed, 48 insertions(+), 77 deletions(-) diff --git a/web/app/Application.js b/web/app/Application.js index c488818a4..1a9326dd1 100644 --- a/web/app/Application.js +++ b/web/app/Application.js @@ -51,8 +51,11 @@ Ext.define('Traccar.Application', { 'Root' ], - setUser: function(user) { - this.user = user; + setUser: function(data) { + var reader = Ext.create('Ext.data.reader.Json', { + model: 'Traccar.model.User' + }); + this.user = reader.readRecords(data).getRecords()[0]; }, getUser: function() { diff --git a/web/app/LoginManager.js b/web/app/LoginManager.js index 34069574b..0137ccf9c 100644 --- a/web/app/LoginManager.js +++ b/web/app/LoginManager.js @@ -17,13 +17,6 @@ Ext.define('Traccar.LoginManager', { singleton: true, - setUser: function(data) { - var reader = Ext.create('Ext.data.reader.Json', { - model: 'Traccar.model.User' - }); - Traccar.app.setUser(reader.readRecords(data).getRecords()[0]); - }, - server: function(options) { Ext.Ajax.request({ scope: this, @@ -61,43 +54,10 @@ Ext.define('Traccar.LoginManager', { if (Traccar.ErrorManager.check(success, response)) { var result = Ext.decode(response.responseText); if (result.success) { - this.setUser(result.data); + Traccar.app.setUser(result.data); } Ext.callback(options.callback, options.scope, [result.success]); } - }, - - login: function(options) { - Ext.Ajax.request({ - scope: this, - url: '/api/login', - params: options.data, - callback: this.onLoginReturn, - original: options - }); - }, - - onLoginReturn: function(options, success, response) { - options = options.original; - if (Traccar.ErrorManager.check(success, response)) { - var result = Ext.decode(response.responseText); - if (result.success) { - this.setUser(result.data); - } - Ext.callback(options.callback, options.scope, [result.success]); - } - }, - - logout: function() { - Ext.Ajax.request({ - scope: this, - url: '/api/logout', - callback: this.onLogoutReturn - }); - }, - - onLogoutReturn: function() { - window.location.reload(); } }); diff --git a/web/app/view/device/DeviceController.js b/web/app/view/device/DeviceController.js index 59b777f8b..3cfe3888d 100644 --- a/web/app/view/device/DeviceController.js +++ b/web/app/view/device/DeviceController.js @@ -23,7 +23,8 @@ Ext.define('Traccar.view.device.DeviceController', { 'Traccar.view.command.CommandDialog', 'Traccar.view.user.UserDialog', 'Traccar.view.admin.ServerDialog', - 'Traccar.view.user.User' + 'Traccar.view.user.User', + 'Traccar.view.login.LoginController' ], config: { @@ -44,7 +45,7 @@ Ext.define('Traccar.view.device.DeviceController', { }, onLogoutClick: function() { - Traccar.LoginManager.logout(); + Ext.create('Traccar.view.login.LoginController').logout(); }, onAddClick: function() { diff --git a/web/app/view/login/LoginController.js b/web/app/view/login/LoginController.js index 48a4aa0f7..0344f5542 100644 --- a/web/app/view/login/LoginController.js +++ b/web/app/view/login/LoginController.js @@ -24,10 +24,43 @@ Ext.define('Traccar.view.login.LoginController', { init: function() { this.lookupReference('registerButton').setDisabled( - !Traccar.app.getServer().get('registration') - ); + !Traccar.app.getServer().get('registration')); this.lookupReference('languageField').setValue(language); + }, + login: function() { + var form = this.lookupReference('form'); + if (form.isValid()) { + Ext.getBody().mask(strings.sharedLoading); + Ext.Ajax.request({ + scope: this, + url: '/api/login', + params: form.getValues(), + callback: function(options, success, response) { + Ext.getBody().unmask(); + if (Traccar.ErrorManager.check(success, response)) { + var result = Ext.decode(response.responseText); + if (result.success) { + Traccar.app.setUser(result.data); + this.fireViewEvent('login'); + } else { + Traccar.ErrorManager.error(strings.loginFailed); + } + } + + } + }); + } + }, + + logout: function() { + Ext.Ajax.request({ + scope: this, + url: '/api/logout', + callback: function() { + window.location.reload(); + } + }); }, onSelectLanguage: function(selected) { @@ -56,37 +89,15 @@ Ext.define('Traccar.view.login.LoginController', { onSpecialKey: function(field, e) { if (e.getKey() === e.ENTER) { - this.doLogin(); + this.login(); } }, onLoginClick: function() { Ext.getElementById('submitButton').click(); - this.doLogin(); + this.login(); }, - - doLogin: function() { - var form = this.lookupReference('form'); - if (form.isValid()) { - Ext.getBody().mask(strings.sharedLoading); - - Traccar.LoginManager.login({ - data: form.getValues(), - scope: this, - callback: 'onLoginReturn' - }); - } - }, - - onLoginReturn: function(success) { - Ext.getBody().unmask(); - if (success) { - this.fireViewEvent('login'); - } else { - Traccar.ErrorManager.error(strings.loginFailed); - } - }, - + onRegisterClick: function() { Ext.create('Traccar.view.login.Register').show(); } diff --git a/web/app/view/user/UserController.js b/web/app/view/user/UserController.js index 041c70166..156d33a86 100644 --- a/web/app/view/user/UserController.js +++ b/web/app/view/user/UserController.js @@ -25,11 +25,7 @@ Ext.define('Traccar.view.user.UserController', { init: function() { Ext.getStore('Users').load(); }, - - onLogoutClick: function() { - Traccar.LoginManager.logout(); - }, - + onAddClick: function() { var user = Ext.create('Traccar.model.User'); var dialog = Ext.create('Traccar.view.user.UserDialog'); -- cgit v1.2.3