easyappointments/src/assets/js/backend_users.js

210 lines
7.3 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>
2017-01-31 09:35:34 +03:00
* @copyright Copyright (c) 2013 - 2017, 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
*/
2016-04-26 22:33:30 +03:00
(function(exports){
'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.
*/
2016-04-26 22:33:30 +03:00
exports.initialize = function(defaultEventHandlers) {
defaultEventHandlers = defaultEventHandlers || true;
2016-10-10 19:29:48 +03:00
// Instantiate default helper object (admin).
2016-04-26 22:33:30 +03:00
helper = new AdminsHelper();
helper.resetForm();
helper.filter('');
helper.bindEventHandlers();
2016-04-26 22:33:30 +03:00
exports.wp = new WorkingPlan();
exports.wp.bindEventHandlers();
// Fill the services and providers list boxes.
2017-06-14 10:07:41 +03:00
var html = '<div class="col-xs-12">';
$.each(GlobalVariables.services, function(index, service) {
html +=
2015-07-25 15:04:23 +03:00
'<div class="checkbox">' +
'<label class="checkbox">' +
'<input type="checkbox" data-id="' + service.id + '" />' +
service.name +
2015-07-25 15:04:23 +03:00
'</label>' +
'</div>';
});
2015-07-25 15:04:23 +03:00
html += '</div>';
$('#provider-services').html(html);
2017-06-14 10:07:41 +03:00
html = '<div class="col-xs-12">';
$.each(GlobalVariables.providers, function(index, provider) {
html +=
2015-07-25 15:04:23 +03:00
'<div class="checkbox">' +
'<label class="checkbox">' +
'<input type="checkbox" data-id="' + provider.id + '" />' +
provider.first_name + ' ' + provider.last_name +
2015-07-25 15:04:23 +03:00
'</label>' +
'</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.
*/
$('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: ''
};
$.post(url, data, function(response) {
2016-04-26 22:33:30 +03:00
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
GlobalVariables.providers = response;
var html = '<div class="col-xs-12">';
$.each(GlobalVariables.providers, function(index, provider) {
html +=
2015-07-25 15:04:23 +03:00
'<div class="checkbox">' +
'<label class="checkbox">' +
'<input type="checkbox" data-id="' + provider.id + '" />' +
provider.first_name + ' ' + provider.last_name +
2015-07-25 15:04:23 +03:00
'</label>' +
'</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.
*/
$('#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
};
$.post(postUrl, postData, function(response) {
2016-04-26 22:33:30 +03:00
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
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);