From 8ded0851d809ee65c8c193675cc114fa857dc498 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Mon, 27 Apr 2020 20:27:18 +0200 Subject: [PATCH] Move the javascript logic of the login page to an external file. --- application/views/user/login.php | 34 ++------------------------ assets/js/login.js | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 assets/js/login.js diff --git a/application/views/user/login.php b/application/views/user/login.php index 3223d6f6..522018f0 100644 --- a/application/views/user/login.php +++ b/application/views/user/login.php @@ -53,42 +53,11 @@ }; var EALang = lang->language) ?>; + var availableLanguages = ; $(document).ready(function() { GeneralFunctions.enableLanguageSelection($('#select-language')); - - /** - * Event: Login Button "Click" - * - * Make an ajax call to the server and check whether the user's credentials are right. - * If yes then redirect him to his desired page, otherwise display a message. - */ - $('#login-form').submit(function(event) { - event.preventDefault(); - - var url = GlobalVariables.baseUrl + '/index.php/user/ajax_check_login'; - var data = { - 'csrfToken': GlobalVariables.csrfToken, - 'username': $('#username').val(), - 'password': $('#password').val() - }; - - $('.alert').addClass('hidden'); - - $.post(url, data) - .done(function(response) { - if (response === GlobalVariables.AJAX_SUCCESS) { - window.location.href = GlobalVariables.destUrl; - } else { - $('.alert').text(EALang['login_failed']); - $('.alert') - .removeClass('hidden alert-danger alert-success') - .addClass('alert-danger'); - } - }) - .fail(GeneralFunctions.ajaxFailureHandler); - }); }); @@ -129,5 +98,6 @@ + diff --git a/assets/js/login.js b/assets/js/login.js new file mode 100644 index 00000000..2fa9da0d --- /dev/null +++ b/assets/js/login.js @@ -0,0 +1,41 @@ +$(function () { + 'use strict'; + + var $loginForm = $('#login-form'); + + /** + * Login Button "Click" + * + * Make an ajax call to the server and check whether the user's credentials are right. + * If yes then redirect him to his desired page, otherwise display a message. + */ + function onLoginFormSubmit(event) { + event.preventDefault(); + + var url = GlobalVariables.baseUrl + '/index.php/user/ajax_check_login'; + var data = { + 'csrfToken': GlobalVariables.csrfToken, + 'username': $('#username').val(), + 'password': $('#password').val() + }; + + var $alert = $('.alert'); + + $alert.addClass('hidden'); + + $.post(url, data) + .done(function (response) { + if (response === GlobalVariables.AJAX_SUCCESS) { + window.location.href = GlobalVariables.destUrl; + } else { + $alert.text(EALang['login_failed']); + $alert + .removeClass('hidden alert-danger alert-success') + .addClass('alert-danger'); + } + }) + .fail(GeneralFunctions.ajaxFailureHandler); + } + + $loginForm.on('submit', onLoginFormSubmit); +});