iflrandevu/assets/js/backend_users.js

202 lines
7 KiB
JavaScript
Raw Normal View History

2015-07-20 22:41:24 +03:00
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
2015-07-20 22:41:24 +03:00
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
2015-07-20 22:41:24 +03:00
* @link http://easyappointments.org
* @since v1.0.0
* ---------------------------------------------------------------------------- */
2016-04-26 22:33:30 +03:00
window.BackendUsers = window.BackendUsers || {};
/**
2016-04-26 22:33:30 +03:00
* Backend Users
*
* This module handles the js functionality of the users backend page. It uses three other
* classes (defined below) in order to handle the admin, provider and secretary record types.
*
2016-04-26 22:33:30 +03:00
* @module BackendUsers
*/
2018-01-23 12:08:37 +03:00
(function (exports) {
2016-04-26 22:33:30 +03:00
'use strict';
/**
* Minimum Password Length
*
* @type {Number}
2016-04-26 22:33:30 +03:00
*/
exports.MIN_PASSWORD_LENGTH = 7;
/**
* Contains the current tab record methods for the page.
*
* @type {AdminsHelper|ProvidersHelper|SecretariesHelper}
*/
2016-04-26 22:33:30 +03:00
var helper = {};
/**
* Use this class instance for performing actions on the working plan.
*
* @type {WorkingPlan}
*/
2016-04-26 22:33:30 +03:00
exports.wp = {};
/**
* Initialize the backend users page.
*
* @param {Boolean} defaultEventHandlers (OPTIONAL) Whether to bind the default event handlers.
*/
2018-01-23 12:08:37 +03:00
exports.initialize = function (defaultEventHandlers) {
2016-04-26 22:33:30 +03:00
defaultEventHandlers = defaultEventHandlers || true;
2017-11-20 16:04:05 +03:00
exports.wp = new WorkingPlan();
exports.wp.bindEventHandlers();
2016-10-10 19:29:48 +03:00
// Instantiate default helper object (admin).
2017-11-20 16:04:05 +03:00
helper = new ProvidersHelper();
2016-04-26 22:33:30 +03:00
helper.resetForm();
helper.filter('');
helper.bindEventHandlers();
// Fill the services and providers list boxes.
var html = '<div>';
2018-01-23 12:08:37 +03:00
$.each(GlobalVariables.services, function (index, service) {
html +=
2015-07-25 15:04:23 +03:00
'<div class="checkbox">' +
2018-01-23 12:08:37 +03:00
'<label class="checkbox">' +
'<input type="checkbox" data-id="' + service.id + '" />' +
service.name +
'</label>' +
2015-07-25 15:04:23 +03:00
'</div>';
});
2015-07-25 15:04:23 +03:00
html += '</div>';
$('#provider-services').html(html);
html = '<div>';
2018-01-23 12:08:37 +03:00
$.each(GlobalVariables.providers, function (index, provider) {
html +=
2015-07-25 15:04:23 +03:00
'<div class="checkbox">' +
2018-01-23 12:08:37 +03:00
'<label class="checkbox">' +
'<input type="checkbox" data-id="' + provider.id + '" />' +
provider.first_name + ' ' + provider.last_name +
'</label>' +
2015-07-25 15:04:23 +03:00
'</div>';
});
2015-07-25 15:04:23 +03:00
html += '</div>';
$('#secretary-providers').html(html);
$('#reset-working-plan').qtip({
position: {
my: 'top center',
at: 'bottom center'
},
style: {
classes: 'qtip-green qtip-shadow custom-qtip'
}
});
// Bind event handlers.
2016-04-26 22:33:30 +03:00
if (defaultEventHandlers) {
_bindEventHandlers();
}
};
/**
2016-10-10 19:29:48 +03:00
* Binds the default backend users event handlers. Do not use this method on a different
* page because it needs the backend users page DOM.
*/
2016-04-26 22:33:30 +03:00
function _bindEventHandlers() {
/**
* Event: Page Tab Button "Click"
*
* Changes the displayed tab.
*/
2018-01-23 12:08:37 +03:00
$('a[data-toggle="tab"]').on('shown.bs.tab', function () {
if ($(this).attr('href') === '#admins') {
2016-04-26 22:33:30 +03:00
helper = new AdminsHelper();
} else if ($(this).attr('href') === '#providers') {
2016-04-26 22:33:30 +03:00
helper = new ProvidersHelper();
} else if ($(this).attr('href') === '#secretaries') {
2016-04-26 22:33:30 +03:00
helper = new SecretariesHelper();
// Update the list with the all the available providers.
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_providers';
var data = {
2016-07-15 21:52:21 +03:00
csrfToken: GlobalVariables.csrfToken,
key: ''
};
2018-01-23 12:08:37 +03:00
$.post(url, data, function (response) {
GlobalVariables.providers = response;
var html = '<div>';
2018-01-23 12:08:37 +03:00
$.each(GlobalVariables.providers, function (index, provider) {
html +=
2015-07-25 15:04:23 +03:00
'<div class="checkbox">' +
2018-01-23 12:08:37 +03:00
'<label class="checkbox">' +
'<input type="checkbox" data-id="' + provider.id + '" />' +
provider.first_name + ' ' + provider.last_name +
'</label>' +
2015-07-25 15:04:23 +03:00
'</div>';
});
html += '</div>';
$('#secretary-providers').html(html);
$('#secretary-providers input:checkbox').prop('disabled', true);
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
}
2016-04-26 22:33:30 +03:00
helper.resetForm();
helper.filter('');
helper.bindEventHandlers();
$('.filter-key').val('');
});
/**
* Event: Admin, Provider, Secretary Username "Focusout"
*
* When the user leaves the username input field we will need to check if the username
* is not taken by another record in the system. Usernames must be unique.
*/
2018-01-23 12:08:37 +03:00
$('#admin-username, #provider-username, #secretary-username').focusout(function () {
var $input = $(this);
if ($input.prop('readonly') == true || $input.val() == '') {
return;
}
var userId = $input.parents().eq(2).find('.record-id').val();
if (userId == undefined) {
return;
}
2016-07-15 21:52:21 +03:00
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_validate_username';
var postData = {
csrfToken: GlobalVariables.csrfToken,
username: $input.val(),
user_id: userId
};
2018-01-23 12:08:37 +03:00
$.post(postUrl, postData, function (response) {
if (response == false) {
$input.closest('.form-group').addClass('has-error');
2013-11-23 21:10:31 +02:00
$input.attr('already-exists', 'true');
$input.parents().eq(3).find('.form-message').text(EALang.username_already_exists);
$input.parents().eq(3).find('.form-message').show();
} else {
$input.closest('.form-group').removeClass('has-error');
2013-11-23 21:10:31 +02:00
$input.attr('already-exists', 'false');
if ($input.parents().eq(3).find('.form-message').text() == EALang.username_already_exists) {
$input.parents().eq(3).find('.form-message').hide();
}
}
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
});
}
2016-04-26 22:33:30 +03:00
})(window.BackendUsers);