MaketRandevu/assets/js/backend_users.js

233 lines
8.1 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.
GlobalVariables.services.forEach(function(service) {
$('<div/>', {
'class': 'checkbox',
'html': [
$('<label/>', {
'class': 'checkbox',
'html': [
$('<input/>', {
'type': 'checkbox',
'data-id': service.id
}),
$('<span/>', {
'text': service.name
})
]
})
]
})
.appendTo('#provider-services');
});
GlobalVariables.providers.forEach(function(provider) {
$('<div/>', {
'class': 'checkbox',
'html': [
$('<label/>', {
'class': 'checkbox',
'html': [
$('<input/>', {
'type': 'checkbox',
'data-id': provider.id
}),
$('<span/>', {
'text': provider.first_name + ' ' + provider.last_name
})
]
})
]
})
.appendTo('#secretary-providers');
});
$('#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-04-26 22:33:30 +03:00
}
};
/**
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.
*/
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: ''
};
$.post(url, data)
.done(function (response) {
GlobalVariables.providers = response;
GlobalVariables.providers.forEach(function(provider) {
$('<div/>', {
'class': 'checkbox',
'html': [
$('<label/>', {
'class': 'checkbox',
'html': [
$('<input/>', {
'type': 'checkbox',
'data-id': provider.id,
'prop': {
'disabled': true
}
}),
$('<span/>', {
'text': provider.first_name + ' ' + provider.last_name
})
]
})
]
})
.appendTo('#secretary-providers');
});
})
.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) {
return;
}
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_validate_username';
var data = {
2016-07-15 21:52:21 +03:00
csrfToken: GlobalVariables.csrfToken,
username: $input.val(),
user_id: userId
};
$.post(url, data)
.done(function (response) {
if (response === 'false') {
$input.closest('.form-group').addClass('has-error');
$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');
$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();
}
}
})
.fail(GeneralFunctions.ajaxFailureHandler);
});
}
2016-04-26 22:33:30 +03:00
})(window.BackendUsers);