2014-08-31 23:22:56 +02:00
|
|
|
var App = App || {};
|
|
|
|
App.Presenters = App.Presenters || {};
|
|
|
|
|
|
|
|
App.Presenters.LoginPresenter = function(
|
|
|
|
jQuery,
|
2014-09-02 09:36:42 +02:00
|
|
|
util,
|
2014-09-04 18:06:25 +02:00
|
|
|
promise,
|
2014-09-04 19:06:12 +02:00
|
|
|
router,
|
|
|
|
auth,
|
|
|
|
topNavigationPresenter,
|
|
|
|
messagePresenter) {
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
var $el = jQuery('#content');
|
|
|
|
var $messages;
|
2014-10-05 10:41:12 +02:00
|
|
|
var templates = {};
|
2014-10-09 21:41:46 +02:00
|
|
|
var previousLocation;
|
2014-09-02 09:36:42 +02:00
|
|
|
|
2014-10-09 21:41:46 +02:00
|
|
|
function init(params, loaded) {
|
2014-09-03 09:10:26 +02:00
|
|
|
topNavigationPresenter.select('login');
|
2014-09-11 13:59:12 +02:00
|
|
|
topNavigationPresenter.changeTitle('Login');
|
2014-10-09 21:41:46 +02:00
|
|
|
previousLocation = params.previousLocation;
|
2014-09-17 22:37:49 +02:00
|
|
|
promise.wait(util.promiseTemplate('login-form'))
|
2014-10-05 10:41:12 +02:00
|
|
|
.then(function(template) {
|
|
|
|
templates.login = template;
|
2014-09-17 22:37:49 +02:00
|
|
|
if (auth.isLoggedIn()) {
|
|
|
|
finishLogin();
|
|
|
|
} else {
|
|
|
|
render();
|
2014-09-30 22:15:37 +02:00
|
|
|
$el.find('input:eq(0)').focus();
|
2014-09-17 22:37:49 +02:00
|
|
|
}
|
|
|
|
loaded();
|
|
|
|
});
|
2014-09-02 23:07:50 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function render() {
|
2014-10-05 10:41:12 +02:00
|
|
|
$el.html(templates.login());
|
2014-09-02 23:07:50 +02:00
|
|
|
$el.find('form').submit(loginFormSubmitted);
|
2014-08-31 23:22:56 +02:00
|
|
|
$messages = $el.find('.messages');
|
|
|
|
$messages.width($el.find('form').width());
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
2014-09-02 23:07:50 +02:00
|
|
|
function loginFormSubmitted(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
messagePresenter.hideMessages($messages);
|
|
|
|
|
2014-09-09 19:38:16 +02:00
|
|
|
var userNameOrEmail = $el.find('[name=user]').val();
|
2014-09-02 23:07:50 +02:00
|
|
|
var password = $el.find('[name=password]').val();
|
2014-09-23 20:45:59 +02:00
|
|
|
var remember = $el.find('[name=remember]').is(':checked');
|
2014-09-02 23:07:50 +02:00
|
|
|
|
2014-09-09 19:38:16 +02:00
|
|
|
if (userNameOrEmail.length === 0) {
|
2014-09-08 13:06:32 +02:00
|
|
|
messagePresenter.showError($messages, 'User name cannot be empty.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-08 22:02:28 +02:00
|
|
|
if (password.length === 0) {
|
2014-09-08 13:06:32 +02:00
|
|
|
messagePresenter.showError($messages, 'Password cannot be empty.');
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-02 23:07:50 +02:00
|
|
|
|
2014-10-02 00:30:25 +02:00
|
|
|
promise.wait(auth.loginFromCredentials(userNameOrEmail, password, remember))
|
2014-09-02 23:07:50 +02:00
|
|
|
.then(function(response) {
|
2014-09-10 13:55:35 +02:00
|
|
|
finishLogin();
|
2014-09-04 18:06:25 +02:00
|
|
|
}).fail(function(response) {
|
2014-09-02 23:07:50 +02:00
|
|
|
messagePresenter.showError($messages, response.json && response.json.error || response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-09-10 13:55:35 +02:00
|
|
|
function finishLogin() {
|
2014-10-09 21:41:46 +02:00
|
|
|
if (previousLocation && !previousLocation.match(/logout|password-reset|activate|register/)) {
|
|
|
|
router.navigate(previousLocation);
|
2014-09-10 13:55:35 +02:00
|
|
|
} else {
|
|
|
|
router.navigateToMainPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-31 23:22:56 +02:00
|
|
|
return {
|
2014-09-03 09:10:26 +02:00
|
|
|
init: init,
|
2014-08-31 23:22:56 +02:00
|
|
|
render: render,
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-10-05 10:41:12 +02:00
|
|
|
App.DI.register('loginPresenter', ['jQuery', 'util', 'promise', 'router', 'auth', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.LoginPresenter);
|