Refactored the backend/users js files.

This commit is contained in:
Alex Tselegidis 2016-04-26 21:33:30 +02:00
parent 967021acf7
commit bcc205522e
4 changed files with 1361 additions and 1292 deletions

View file

@ -9,28 +9,40 @@
* @since v1.0.0
* ---------------------------------------------------------------------------- */
window.BackendUsers = window.BackendUsers || {};
/**
* This namespace handles the js functionality of the users backend page. It uses three other
* 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.
*
* @namespace BackendUsers
* @module BackendUsers
*/
var BackendUsers = {
MIN_PASSWORD_LENGTH: 7,
(function(exports){
'use strict';
/**
* Minimum Password Length
*
* @type {int}
*/
exports.MIN_PASSWORD_LENGTH = 7;
/**
* Contains the current tab record methods for the page.
*
* @type AdminsHelper|ProvidersHelper|SecretariesHelper
*/
helper: {},
var helper = {};
/**
* Use this class instance for performing actions on the working plan.
*
* @type {object}
*/
wp: {},
exports.wp = {};
/**
* Initialize the backend users page.
@ -38,8 +50,8 @@ var BackendUsers = {
* @param {bool} defaultEventHandlers (OPTIONAL) Whether to bind the default event handlers
* (default: true).
*/
initialize: function(defaultEventHandlers) {
if (defaultEventHandlers == undefined) defaultEventHandlers = true;
exports.initialize = function(defaultEventHandlers) {
defaultEventHandlers = defaultEventHandlers || true;
// Initialize jScrollPane Scrollbars
$('#filter-admins .results').jScrollPane();
@ -47,12 +59,13 @@ var BackendUsers = {
$('#filter-secretaries .results').jScrollPane();
// Instanciate default helper object (admin).
BackendUsers.helper = new AdminsHelper();
BackendUsers.helper.resetForm();
BackendUsers.helper.filter('');
helper = new AdminsHelper();
helper.resetForm();
helper.filter('');
helper.bindEventHandlers();
BackendUsers.wp = new WorkingPlan();
BackendUsers.wp.bindEventHandlers();
exports.wp = new WorkingPlan();
exports.wp.bindEventHandlers();
// Fill the services and providers list boxes.
var html = '<div class="col-md-12">';
@ -96,14 +109,16 @@ var BackendUsers = {
});
// Bind event handlers.
if (defaultEventHandlers) BackendUsers.bindEventHandlers();
},
if (defaultEventHandlers) {
_bindEventHandlers();
}
};
/**
* Binds the defauly backend users event handlers. Do not use this method on a different
* page because it needs the backend users page DOM.
*/
bindEventHandlers: function() {
function _bindEventHandlers() {
/**
* Event: Page Tab Button "Click"
*
@ -113,27 +128,30 @@ var BackendUsers = {
$(this).parent().find('.active').removeClass('active');
$(this).addClass('active');
$('.tab-content').hide();
$('#admins, #providers, #secretaries').off();
if ($(this).hasClass('admins-tab')) { // display admins tab
$('#admins').show();
BackendUsers.helper = new AdminsHelper();
helper = new AdminsHelper();
} else if ($(this).hasClass('providers-tab')) { // display providers tab
$('#providers').show();
$('#provider-services').data('jsp').destroy();
$('#provider-services').jScrollPane({ mouseWheelSpeed: 70 });
BackendUsers.helper = new ProvidersHelper();
helper = new ProvidersHelper();
} else if ($(this).hasClass('secretaries-tab')) { // display secretaries tab
$('#secretaries').show();
BackendUsers.helper = new SecretariesHelper();
helper = new SecretariesHelper();
// Update the list with the all the available providers.
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_providers';
var postData = {
'csrfToken': GlobalVariables.csrfToken,
'key': ''
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_providers',
postData = {
csrfToken: GlobalVariables.csrfToken,
key: ''
};
$.post(postUrl, postData, function(response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
GlobalVariables.providers = response;
@ -158,8 +176,9 @@ var BackendUsers = {
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
}
BackendUsers.helper.resetForm();
BackendUsers.helper.filter('');
helper.resetForm();
helper.filter('');
helper.bindEventHandlers();
$('.filter-key').val('');
});
@ -182,15 +201,18 @@ var BackendUsers = {
return;
}
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_validate_username';
var postData = {
'csrfToken': GlobalVariables.csrfToken,
'username': $input.val(),
'user_id': userId
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_validate_username',
postData = {
csrfToken: GlobalVariables.csrfToken,
username: $input.val(),
user_id: userId
};
$.post(postUrl, postData, function(response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
if (response == false) {
$input.css('border', '2px solid red');
$input.attr('already-exists', 'true');
@ -205,19 +227,6 @@ var BackendUsers = {
}
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
});
// ------------------------------------------------------------------------
AdminsHelper.prototype.bindEventHandlers();
// ------------------------------------------------------------------------
ProvidersHelper.prototype.bindEventHandlers();
// ------------------------------------------------------------------------
SecretariesHelper.prototype.bindEventHandlers();
// ------------------------------------------------------------------------
}
};
})(window.BackendUsers);

View file

@ -9,6 +9,10 @@
* @since v1.0.0
* ---------------------------------------------------------------------------- */
(function() {
'use strict';
/**
* This class contains the Admins helper class declaration, along with the "Admins" tab
* event handlers. By deviding the backend/users tab functionality into separate files
@ -17,7 +21,7 @@
* @class AdminsHelper
*/
var AdminsHelper = function() {
this.filterResults = {}; // Store the results for later use.
this.filterResults = []; // Store the results for later use.
};
/**
@ -29,54 +33,54 @@ AdminsHelper.prototype.bindEventHandlers = function() {
*
* Filter the admin records with the given key string.
*/
$('#filter-admins form').submit(function(event) {
$('#admins').on('submit', '#filter-admins form', function(event) {
var key = $('#filter-admins .key').val();
$('#filter-admins .selected-row').removeClass('selected-row');
BackendUsers.helper.resetForm();
BackendUsers.helper.filter(key);
this.resetForm();
this.filter(key);
return false;
});
}.bind(this));
/**
* Event: Clear Filter Results Button "Click"
*/
$('#filter-admins .clear').click(function() {
BackendUsers.helper.filter('');
$('#admins').on('click', '#filter-admins .clear', function() {
this.filter('');
$('#filter-admins .key').val('');
BackendUsers.helper.resetForm();
});
this.resetForm();
}.bind(this));
/**
* Event: Filter Admin Row "Click"
*
* Display the selected admin data to the user.
*/
$(document).on('click', '.admin-row', function() {
$('#admins').on('click', '.admin-row', function(e) {
if ($('#filter-admins .filter').prop('disabled')) {
$('#filter-admins .results').css('color', '#AAA');
return; // exit because we are currently on edit mode
}
var adminId = $(this).attr('data-id');
var admin = {};
$.each(BackendUsers.helper.filterResults, function(index, item) {
var adminId = $(e.currentTarget).attr('data-id'),
admin = {};
$.each(this.filterResults, function(index, item) {
if (item.id === adminId) {
admin = item;
return false;
}
});
BackendUsers.helper.display(admin);
this.display(admin);
$('#filter-admins .selected-row').removeClass('selected-row');
$(this).addClass('selected-row');
$(e.currentTarget).addClass('selected-row');
$('#edit-admin, #delete-admin').prop('disabled', false);
});
}.bind(this));
/**
* Event: Add New Admin Button "Click"
*/
$('#add-admin').click(function() {
BackendUsers.helper.resetForm();
$('#admins').on('click', '#add-admin', function() {
this.resetForm();
$('#admins .add-edit-delete-group').hide();
$('#admins .save-cancel-group').show();
$('#admins .details').find('input, textarea').prop('readonly', false);
@ -84,12 +88,12 @@ AdminsHelper.prototype.bindEventHandlers = function() {
$('#admin-notifications').prop('disabled', false);
$('#filter-admins button').prop('disabled', true);
$('#filter-admins .results').css('color', '#AAA');
});
}.bind(this));
/**
* Event: Edit Admin Button "Click"
*/
$('#edit-admin').click(function() {
$('#admins').on('click', '#edit-admin', function() {
$('#admins .add-edit-delete-group').hide();
$('#admins .save-cancel-group').show();
$('#admins .details').find('input, textarea').prop('readonly', false);
@ -103,40 +107,40 @@ AdminsHelper.prototype.bindEventHandlers = function() {
/**
* Event: Delete Admin Button "Click"
*/
$('#delete-admin').click(function() {
$('#admins').on('click', '#delete-admin', function() {
var adminId = $('#admin-id').val();
var messageBtns = {};
messageBtns[EALang['delete']] = function() {
BackendUsers.helper.delete(adminId);
this.delete(adminId);
$('#message_box').dialog('close');
};
}.bind(this);
messageBtns[EALang['cancel']] = function() {
$('#message_box').dialog('close');
};
GeneralFunctions.displayMessageBox(EALang['delete_admin'],
EALang['delete_record_prompt'], messageBtns);
});
}.bind(this));
/**
* Event: Save Admin Button "Click"
*/
$('#save-admin').click(function() {
$('#admins').on('click', '#save-admin', function() {
var admin = {
'first_name': $('#admin-first-name').val(),
'last_name': $('#admin-last-name').val(),
'email': $('#admin-email').val(),
'mobile_number': $('#admin-mobile-number').val(),
'phone_number': $('#admin-phone-number').val(),
'address': $('#admin-address').val(),
'city': $('#admin-city').val(),
'state': $('#admin-state').val(),
'zip_code': $('#admin-zip-code').val(),
'notes': $('#admin-notes').val(),
'settings': {
'username': $('#admin-username').val(),
'notifications': $('#admin-notifications').hasClass('active')
first_name: $('#admin-first-name').val(),
last_name: $('#admin-last-name').val(),
email: $('#admin-email').val(),
mobile_number: $('#admin-mobile-number').val(),
phone_number: $('#admin-phone-number').val(),
address: $('#admin-address').val(),
city: $('#admin-city').val(),
state: $('#admin-state').val(),
zip_code: $('#admin-zip-code').val(),
notes: $('#admin-notes').val(),
settings: {
username: $('#admin-username').val(),
notifications: $('#admin-notifications').hasClass('active')
}
};
@ -150,23 +154,25 @@ AdminsHelper.prototype.bindEventHandlers = function() {
admin.id = $('#admin-id').val();
}
if (!BackendUsers.helper.validate(admin)) return;
if (!this.validate(admin)) {
return;
}
BackendUsers.helper.save(admin);
});
this.save(admin);
}.bind(this));
/**
* Event: Cancel Admin Button "Click"
*
* Cancel add or edit of an admin record.
*/
$('#cancel-admin').click(function() {
$('#admins').on('click', '#cancel-admin', function() {
var id = $('#admin-id').val();
BackendUsers.helper.resetForm();
this.resetForm();
if (id != '') {
BackendUsers.helper.select(id, true);
this.select(id, true);
}
});
}.bind(this));
};
/**
@ -176,19 +182,21 @@ AdminsHelper.prototype.bindEventHandlers = function() {
* then the update operation is going to be executed.
*/
AdminsHelper.prototype.save = function(admin) {
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_admin';
var postData = {
'csrfToken': GlobalVariables.csrfToken,
'admin': JSON.stringify(admin)
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_admin',
postData = {
csrfToken: GlobalVariables.csrfToken,
admin: JSON.stringify(admin)
};
$.post(postUrl, postData, function(response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
Backend.displayNotification(EALang['admin_saved']);
BackendUsers.helper.resetForm();
this.resetForm();
$('#filter-admins .key').val('');
BackendUsers.helper.filter('', response.id, true);
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
this.filter('', response.id, true);
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
};
/**
@ -197,18 +205,20 @@ AdminsHelper.prototype.save = function(admin) {
* @param {int} id Record id to be deleted.
*/
AdminsHelper.prototype.delete = function(id) {
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_admin';
var postData = {
'csrfToken': GlobalVariables.csrfToken,
'admin_id': id
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_admin',
postData = {
csrfToken: GlobalVariables.csrfToken,
admin_id: id
};
$.post(postUrl, postData, function(response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
Backend.displayNotification(EALang['admin_deleted']);
BackendUsers.helper.resetForm();
BackendUsers.helper.filter($('#filter-admins .key').val());
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
this.resetForm();
this.filter($('#filter-admins .key').val());
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
};
/**
@ -243,7 +253,6 @@ AdminsHelper.prototype.validate = function(admin) {
if ($('#admin-password').val().length < BackendUsers.MIN_PASSWORD_LENGTH
&& $('#admin-password').val() != '') {
$('#admin-password, #admin-password-confirm').css('border', '2px solid red');
throw EALang['password_length_notice'].replace('$number', BackendUsers.MIN_PASSWORD_LENGTH);
}
@ -323,25 +332,27 @@ AdminsHelper.prototype.display = function(admin) {
* to be displayed on the details column (requires a selected record though).
*/
AdminsHelper.prototype.filter = function(key, selectId, display) {
if (display == undefined) display = false;
display = display || false;
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_admins';
var postData = {
'csrfToken': GlobalVariables.csrfToken,
'key': key
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_admins',
postData = {
csrfToken: GlobalVariables.csrfToken,
key: key
};
$.post(postUrl, postData, function(response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
BackendUsers.helper.filterResults = response;
this.filterResults = response;
$('#filter-admins .results').data('jsp').destroy();
$('#filter-admins .results').html('');
$.each(response, function(index, admin) {
var html = AdminsHelper.prototype.getFilterHtml(admin);
var html = this.getFilterHtml(admin);
$('#filter-admins .results').append(html);
});
}.bind(this));
$('#filter-admins .results').jScrollPane({ mouseWheelSpeed: 70 });
if (response.length == 0) {
@ -349,9 +360,9 @@ AdminsHelper.prototype.filter = function(key, selectId, display) {
}
if (selectId != undefined) {
BackendUsers.helper.select(selectId, display);
this.select(selectId, display);
}
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
};
/**
@ -361,10 +372,12 @@ AdminsHelper.prototype.filter = function(key, selectId, display) {
* @returns {string} The html code that represents the record on the filter results list.
*/
AdminsHelper.prototype.getFilterHtml = function(admin) {
var name = admin.first_name + ' ' + admin.last_name;
var info = admin.email;
var name = admin.first_name + ' ' + admin.last_name,
info = admin.email;
info = (admin.mobile_number != '' && admin.mobile_number != null)
? info + ', ' + admin.mobile_number : info;
info = (admin.phone_number != '' && admin.phone_number != null)
? info + ', ' + admin.phone_number : info;
@ -386,7 +399,7 @@ AdminsHelper.prototype.getFilterHtml = function(admin) {
* on the form.
*/
AdminsHelper.prototype.select = function(id, display) {
if (display == undefined) display = false;
display = display || false;
$('#filter-admins .selected-row').removeClass('selected-row');
@ -398,12 +411,16 @@ AdminsHelper.prototype.select = function(id, display) {
});
if (display) {
$.each(BackendUsers.helper.filterResults, function(index, admin) {
$.each(this.filterResults, function(index, admin) {
if (admin.id == id) {
BackendUsers.helper.display(admin);
this.display(admin);
$('#edit-admin, #delete-admin').prop('disabled', false);
return false;
}
});
}.bind(this));
}
};
window.AdminsHelper = AdminsHelper;
})();

View file

@ -9,7 +9,13 @@
* @since v1.0.0
* ---------------------------------------------------------------------------- */
(function() {
'use strict';
/**
* Providers Helper
*
* This class contains the Providers helper class declaration, along with the "Providers" tab
* event handlers. By deviding the backend/users tab functionality into separate files
* it is easier to maintain the code.
@ -29,54 +35,54 @@ ProvidersHelper.prototype.bindEventHandlers = function() {
*
* Filter the provider records with the given key string.
*/
$('#filter-providers form').submit(function(event) {
$('#providers').on('submit', '#filter-providers form', function(event) {
var key = $('#filter-providers .key').val();
$('.selected-row').removeClass('selected-row');
BackendUsers.helper.resetForm();
BackendUsers.helper.filter(key);
this.resetForm();
this.filter(key);
return false;
});
}.bind(this));
/**
* Event: Clear Filter Button "Click"
*/
$('#filter-providers .clear').click(function() {
BackendUsers.helper.filter('');
$('#providers').on('click', '#filter-providers .clear', function() {
this.filter('');
$('#filter-providers .key').val('');
BackendUsers.helper.resetForm();
});
this.resetForm();
}.bind(this));
/**
* Event: Filter Provider Row "Click"
*
* Display the selected provider data to the user.
*/
$(document).on('click', '.provider-row', function() {
$('#providers').on('click', '.provider-row', function(e) {
if ($('#filter-providers .filter').prop('disabled')) {
$('#filter-providers .results').css('color', '#AAA');
return; // Exit because we are currently on edit mode.
}
var providerId = $(this).attr('data-id');
var provider = {};
$.each(BackendUsers.helper.filterResults, function(index, item) {
var providerId = $(e.currentTarget).attr('data-id'),
provider = {};
$.each(this.filterResults, function(index, item) {
if (item.id === providerId) {
provider = item;
return false;
}
});
BackendUsers.helper.display(provider);
this.display(provider);
$('#filter-providers .selected-row').removeClass('selected-row');
$(this).addClass('selected-row');
$(e.currentTarget).addClass('selected-row');
$('#edit-provider, #delete-provider').prop('disabled', false);
});
}.bind(this));
/**
* Event: Add New Provider Button "Click"
*/
$('#add-provider').click(function() {
BackendUsers.helper.resetForm();
$('#providers').on('click', '#add-provider', function() {
this.resetForm();
$('#filter-providers button').prop('disabled', true);
$('#filter-providers .results').css('color', '#AAA');
$('#providers .add-edit-delete-group').hide();
@ -91,12 +97,12 @@ ProvidersHelper.prototype.bindEventHandlers = function() {
// Apply default working plan
BackendUsers.wp.setup(GlobalVariables.workingPlan);
BackendUsers.wp.timepickers(false);
});
}.bind(this));
/**
* Event: Edit Provider Button "Click"
*/
$('#edit-provider').click(function() {
$('#providers').on('click', '#edit-provider', function() {
$('#providers .add-edit-delete-group').hide();
$('#providers .save-cancel-group').show();
$('#filter-providers button').prop('disabled', true);
@ -113,41 +119,41 @@ ProvidersHelper.prototype.bindEventHandlers = function() {
/**
* Event: Delete Provider Button "Click"
*/
$('#delete-provider').click(function() {
$('#providers').on('click', '#delete-provider', function() {
var providerId = $('#provider-id').val();
var messageBtns = {};
messageBtns[EALang['delete']] = function() {
BackendUsers.helper.delete(providerId);
this.delete(providerId);
$('#message_box').dialog('close');
};
}.bind(this);
messageBtns[EALang['cancel']] = function() {
$('#message_box').dialog('close');
};
GeneralFunctions.displayMessageBox(EALang['delete_provider'],
EALang['delete_record_prompt'], messageBtns);
});
}.bind(this));
/**
* Event: Save Provider Button "Click"
*/
$('#save-provider').click(function() {
$('#providers').on('click', '#save-provider', function() {
var provider = {
'first_name': $('#provider-first-name').val(),
'last_name': $('#provider-last-name').val(),
'email': $('#provider-email').val(),
'mobile_number': $('#provider-mobile-number').val(),
'phone_number': $('#provider-phone-number').val(),
'address': $('#provider-address').val(),
'city': $('#provider-city').val(),
'state': $('#provider-state').val(),
'zip_code': $('#provider-zip-code').val(),
'notes': $('#provider-notes').val(),
'settings': {
'username': $('#provider-username').val(),
'working_plan': JSON.stringify(BackendUsers.wp.get()),
'notifications': $('#provider-notifications').hasClass('active')
first_name: $('#provider-first-name').val(),
last_name: $('#provider-last-name').val(),
email: $('#provider-email').val(),
mobile_number: $('#provider-mobile-number').val(),
phone_number: $('#provider-phone-number').val(),
address: $('#provider-address').val(),
city: $('#provider-city').val(),
state: $('#provider-state').val(),
zip_code: $('#provider-zip-code').val(),
notes: $('#provider-notes').val(),
settings: {
username: $('#provider-username').val(),
working_plan: JSON.stringify(BackendUsers.wp.get()),
notifications: $('#provider-notifications').hasClass('active')
}
};
@ -169,28 +175,30 @@ ProvidersHelper.prototype.bindEventHandlers = function() {
provider.id = $('#provider-id').val();
}
if (!BackendUsers.helper.validate(provider)) return;
if (!this.validate(provider)) {
return;
}
BackendUsers.helper.save(provider);
});
this.save(provider);
}.bind(this));
/**
* Event: Cancel Provider Button "Click"
*
* Cancel add or edit of an provider record.
*/
$('#cancel-provider').click(function() {
$('#providers').on('click', '#cancel-provider', function() {
var id = $('#filter-providers .selected-row').attr('data-id');
BackendUsers.helper.resetForm();
this.resetForm();
if (id != '') {
BackendUsers.helper.select(id, true);
this.select(id, true);
}
});
}.bind(this));
/**
* Event: Display Provider Details "Click"
*/
$('#providers .display-details').click(function() {
$('#providers').on('click', '.display-details', function() {
$('#providers .switch-view .current').removeClass('current');
$(this).addClass('current');
$('.working-plan-view').hide('fade', function() {
@ -201,7 +209,7 @@ ProvidersHelper.prototype.bindEventHandlers = function() {
/**
* Event: Display Provider Working Plan "Click"
*/
$('#providers .display-working-plan').click(function() {
$('#providers').on('click', '.display-working-plan', function() {
$('#providers .switch-view .current').removeClass('current');
$(this).addClass('current');
$('.details-view').hide('fade', function() {
@ -212,7 +220,7 @@ ProvidersHelper.prototype.bindEventHandlers = function() {
/**
* Event: Reset Working Plan Button "Click".
*/
$('#reset-working-plan').click(function() {
$('#providers').on('click', '#reset-working-plan', function() {
$('.breaks').empty();
$('.work-start, .work-end').val('');
BackendUsers.wp.setup(GlobalVariables.workingPlan);
@ -227,19 +235,21 @@ ProvidersHelper.prototype.bindEventHandlers = function() {
* then the update operation is going to be executed.
*/
ProvidersHelper.prototype.save = function(provider) {
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_provider';
var postData = {
'csrfToken': GlobalVariables.csrfToken,
'provider': JSON.stringify(provider)
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_provider',
postData = {
csrfToken: GlobalVariables.csrfToken,
provider: JSON.stringify(provider)
};
$.post(postUrl, postData, function(response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
Backend.displayNotification(EALang['provider_saved']);
BackendUsers.helper.resetForm();
this.resetForm();
$('#filter-providers .key').val('');
BackendUsers.helper.filter('', response.id, true);
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
this.filter('', response.id, true);
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
};
/**
@ -248,18 +258,20 @@ ProvidersHelper.prototype.save = function(provider) {
* @param {numeric} id Record id to be deleted.
*/
ProvidersHelper.prototype.delete = function(id) {
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_provider';
var postData = {
'csrfToken': GlobalVariables.csrfToken,
'provider_id': id
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_provider',
postData = {
csrfToken: GlobalVariables.csrfToken,
provider_id: id
};
$.post(postUrl, postData, function(response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
Backend.displayNotification(EALang['provider_deleted']);
BackendUsers.helper.resetForm();
BackendUsers.helper.filter($('#filter-providers .key').val());
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
this.resetForm();
this.filter($('#filter-providers .key').val());
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
};
/**
@ -345,7 +357,6 @@ ProvidersHelper.prototype.resetForm = function() {
$('#providers input[type="checkbox"]').prop('checked', false);
$('#provider-services input[type="checkbox"]').prop('checked', false);
$('#providers .breaks tbody').empty();
};
/**
@ -399,26 +410,27 @@ ProvidersHelper.prototype.display = function(provider) {
* displayed.
*/
ProvidersHelper.prototype.filter = function(key, selectId, display) {
if (display == undefined) display = false;
display = display || false;
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_providers';
var postData = {
'csrfToken': GlobalVariables.csrfToken,
'key': key
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_providers',
postData = {
csrfToken: GlobalVariables.csrfToken,
key: key
};
$.post(postUrl, postData, function(response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
BackendUsers.helper.filterResults = response;
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
this.filterResults = response;
$('#filter-providers .results').data('jsp').destroy;
$('#filter-providers .results').html('');
$.each(response, function(index, provider) {
var html = ProvidersHelper.prototype.getFilterHtml(provider);
var html = this.getFilterHtml(provider);
$('#filter-providers .results').append(html);
});
}.bind(this));
$('#filter-providers .results').jScrollPane({ mouseWheelSpeed: 70 });
if (response.length == 0) {
@ -426,9 +438,9 @@ ProvidersHelper.prototype.filter = function(key, selectId, display) {
}
if (selectId != undefined) {
BackendUsers.helper.select(selectId, display);
this.select(selectId, display);
}
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
};
/**
@ -438,10 +450,12 @@ ProvidersHelper.prototype.filter = function(key, selectId, display) {
* @returns {string} The html code that represents the record on the filter results list.
*/
ProvidersHelper.prototype.getFilterHtml = function(provider) {
var name = provider.first_name + ' ' + provider.last_name;
var info = provider.email;
var name = provider.first_name + ' ' + provider.last_name,
info = provider.email;
info = (provider.mobile_number != '' && provider.mobile_number != null)
? info + ', ' + provider.mobile_number : info;
info = (provider.phone_number != '' && provider.phone_number != null)
? info + ', ' + provider.phone_number : info;
@ -473,21 +487,22 @@ ProvidersHelper.prototype.editableBreakDay = function($selector) {
$selector.editable(function(value, settings) {
return value;
}, {
'type': 'select',
// 'data': '{ "Monday": "Monday", "Tuesday": "Tuesday", "Wednesday": "Wednesday", '
// + '"Thursday": "Thursday", "Friday": "Friday", "Saturday": "Saturday", '
// + '"Sunday": "Sunday", "selected": "Monday"}',
'data': weekDays,
'event': 'edit',
'height': '30px',
'submit': '<button type="button" class="hidden submit-editable">Submit</button>',
'cancel': '<button type="button" class="hidden cancel-editable">Cancel</button>',
'onblur': 'ignore',
'onreset': function(settings, td) {
if (!BackendUsers.enableCancel) return false; // disable ESC button
type: 'select',
data: weekDays,
event: 'edit',
height: '30px',
submit: '<button type="button" class="hidden submit-editable">Submit</button>',
cancel: '<button type="button" class="hidden cancel-editable">Cancel</button>',
onblur: 'ignore',
onreset: function(settings, td) {
if (!BackendUsers.enableCancel) {
return false; // disable ESC button
}
},
'onsubmit': function(settings, td) {
if (!BackendUsers.enableSubmit) return false; // disable Enter button
onsubmit: function(settings, td) {
if (!BackendUsers.enableSubmit) {
return false; // disable Enter button
}
}
});
};
@ -502,16 +517,20 @@ ProvidersHelper.prototype.editableBreakTime = function($selector) {
// Do not return the value because the user needs to press the "Save" button.
return value;
}, {
'event': 'edit',
'height': '25px',
'submit': '<button type="button" class="hidden submit-editable">Submit</button>',
'cancel': '<button type="button" class="hidden cancel-editable">Cancel</button>',
'onblur': 'ignore',
'onreset': function(settings, td) {
if (!BackendUsers.enableCancel) return false; // disable ESC button
event: 'edit',
height: '25px',
submit: '<button type="button" class="hidden submit-editable">Submit</button>',
cancel: '<button type="button" class="hidden cancel-editable">Cancel</button>',
onblur: 'ignore',
onreset: function(settings, td) {
if (!BackendUsers.enableCancel) {
return false; // disable ESC button
}
},
'onsubmit': function(settings, td) {
if (!BackendUsers.enableSubmit) return false; // disable Enter button
onsubmit: function(settings, td) {
if (!BackendUsers.enableSubmit) {
return false; // disable Enter button
}
}
});
};
@ -523,7 +542,7 @@ ProvidersHelper.prototype.editableBreakTime = function($selector) {
* @param {bool} display (OPTIONAL = false) If true the record will be displayed on the form.
*/
ProvidersHelper.prototype.select = function(id, display) {
if (display == undefined) display = false;
display = display || false;
// Select record in filter results.
$('#filter-providers .provider-row').each(function() {
@ -535,12 +554,16 @@ ProvidersHelper.prototype.select = function(id, display) {
// Display record in form (if display = true).
if (display) {
$.each(BackendUsers.helper.filterResults, function(index, provider) {
$.each(this.filterResults, function(index, provider) {
if (provider.id == id) {
BackendUsers.helper.display(provider);
this.display(provider);
$('#edit-provider, #delete-provider').prop('disabled', false);
return false;
}
});
}.bind(this));
}
};
window.ProvidersHelper = ProvidersHelper;
})();

View file

@ -9,7 +9,13 @@
* @since v1.0.0
* ---------------------------------------------------------------------------- */
(function() {
'use strict';
/**
* Secretaries Helper
*
* This class contains the Secretaries helper class declaration, along with the "Secretaries"
* tab event handlers. By deviding the backend/users tab functionality into separate files
* it is easier to maintain the code.
@ -29,54 +35,54 @@ SecretariesHelper.prototype.bindEventHandlers = function() {
*
* Filter the secretary records with the given key string.
*/
$('#filter-secretaries form').submit(function(event) {
$('#secretaries').on('submit', '#filter-secretaries form', function(event) {
var key = $('#filter-secretaries .key').val();
$('#filter-secretaries .selected-row').removeClass('selected-row');
BackendUsers.helper.resetForm();
BackendUsers.helper.filter(key);
this.resetForm();
this.filter(key);
return false;
});
}.bind(this));
/**
* Event: Clear Filter Results Button "Click"
*/
$('#filter-secretaries .clear').click(function() {
BackendUsers.helper.filter('');
$('#secretaries').on('click', '#filter-secretaries .clear', function() {
this.filter('');
$('#filter-secretaries .key').val('');
BackendUsers.helper.resetForm();
});
this.resetForm();
}.bind(this));
/**
* Event: Filter Secretary Row "Click"
*
* Display the selected secretary data to the user.
*/
$(document).on('click', '.secretary-row', function() {
$('#secretaries').on('click', '.secretary-row', function(e) {
if ($('#filter-secretaries .filter').prop('disabled')) {
$('#filter-secretaries .results').css('color', '#AAA');
return; // exit because we are currently on edit mode
}
var secretaryId = $(this).attr('data-id');
var secretary = {};
$.each(BackendUsers.helper.filterResults, function(index, item) {
var secretaryId = $(e.currentTarget).attr('data-id'),
secretary = {};
$.each(this.filterResults, function(index, item) {
if (item.id === secretaryId) {
secretary = item;
return false;
}
});
BackendUsers.helper.display(secretary);
this.display(secretary);
$('#filter-secretaries .selected-row').removeClass('selected-row');
$(this).addClass('selected-row');
$(e.currentTarget).addClass('selected-row');
$('#edit-secretary, #delete-secretary').prop('disabled', false);
});
}.bind(this));
/**
* Event: Add New Secretary Button "Click"
*/
$('#add-secretary').click(function() {
BackendUsers.helper.resetForm();
$('#secretaries').on('click', '#add-secretary', function() {
this.resetForm();
$('#filter-secretaries button').prop('disabled', true);
$('#filter-secretaries .results').css('color', '#AAA');
@ -86,12 +92,12 @@ SecretariesHelper.prototype.bindEventHandlers = function() {
$('#secretary-password, #secretary-password-confirm').addClass('required');
$('#secretary-notifications').prop('disabled', false);
$('#secretary-providers input[type="checkbox"]').prop('disabled', false);
});
}.bind(this));
/**
* Event: Edit Secretary Button "Click"
*/
$('#edit-secretary').click(function() {
$('#secretaries').on('click', '#edit-secretary', function() {
$('#filter-secretaries button').prop('disabled', true);
$('#filter-secretaries .results').css('color', '#AAA');
@ -106,14 +112,14 @@ SecretariesHelper.prototype.bindEventHandlers = function() {
/**
* Event: Delete Secretary Button "Click"
*/
$('#delete-secretary').click(function() {
$('#secretaries').on('click', '#delete-secretary', function() {
var secretaryId = $('#secretary-id').val();
var messageBtns = {};
messageBtns[EALang['delete']] = function() {
BackendUsers.helper.delete(secretaryId);
this.delete(secretaryId);
$('#message_box').dialog('close');
};
}.bind(this);
messageBtns[EALang['cancel']] = function() {
$('#message_box').dialog('close');
};
@ -125,21 +131,21 @@ SecretariesHelper.prototype.bindEventHandlers = function() {
/**
* Event: Save Secretary Button "Click"
*/
$('#save-secretary').click(function() {
$('#secretaries').on('click', '#save-secretary', function() {
var secretary = {
'first_name': $('#secretary-first-name').val(),
'last_name': $('#secretary-last-name').val(),
'email': $('#secretary-email').val(),
'mobile_number': $('#secretary-mobile-number').val(),
'phone_number': $('#secretary-phone-number').val(),
'address': $('#secretary-address').val(),
'city': $('#secretary-city').val(),
'state': $('#secretary-state').val(),
'zip_code': $('#secretary-zip-code').val(),
'notes': $('#secretary-notes').val(),
'settings': {
'username': $('#secretary-username').val(),
'notifications': $('#secretary-notifications').hasClass('active')
first_name: $('#secretary-first-name').val(),
last_name: $('#secretary-last-name').val(),
email: $('#secretary-email').val(),
mobile_number: $('#secretary-mobile-number').val(),
phone_number: $('#secretary-phone-number').val(),
address: $('#secretary-address').val(),
city: $('#secretary-city').val(),
state: $('#secretary-state').val(),
zip_code: $('#secretary-zip-code').val(),
notes: $('#secretary-notes').val(),
settings: {
username: $('#secretary-username').val(),
notifications: $('#secretary-notifications').hasClass('active')
}
};
@ -161,23 +167,25 @@ SecretariesHelper.prototype.bindEventHandlers = function() {
secretary.id = $('#secretary-id').val();
}
if (!BackendUsers.helper.validate(secretary)) return;
if (!this.validate(secretary)) {
return;
}
BackendUsers.helper.save(secretary);
});
this.save(secretary);
}.bind(this));
/**
* Event: Cancel Secretary Button "Click"
*
* Cancel add or edit of an secretary record.
*/
$('#cancel-secretary').click(function() {
$('#secretaries').on('Click', '#cancel-secretary', function() {
var id = $('#secretary-id').val();
BackendUsers.helper.resetForm();
this.resetForm();
if (id != '') {
BackendUsers.helper.select(id, true);
this.select(id, true);
}
});
}.bind(this));
};
/**
@ -189,17 +197,19 @@ SecretariesHelper.prototype.bindEventHandlers = function() {
SecretariesHelper.prototype.save = function(secretary) {
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_secretary',
postData = {
'csrfToken': GlobalVariables.csrfToken,
'secretary': JSON.stringify(secretary)
csrfToken: GlobalVariables.csrfToken,
secretary: JSON.stringify(secretary)
};
$.post(postUrl, postData, function(response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
Backend.displayNotification(EALang['secretary_saved']);
BackendUsers.helper.resetForm();
this.resetForm();
$('#filter-secretaries .key').val('');
BackendUsers.helper.filter('', response.id, true);
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
this.filter('', response.id, true);
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
};
/**
@ -210,16 +220,18 @@ SecretariesHelper.prototype.save = function(secretary) {
SecretariesHelper.prototype.delete = function(id) {
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_secretary',
postData = {
'csrfToken': GlobalVariables.csrfToken,
'secretary_id': id
csrfToken: GlobalVariables.csrfToken,
secretary_id: id
};
$.post(postUrl, postData, function(response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
Backend.displayNotification(EALang['secretary_deleted']);
BackendUsers.helper.resetForm();
BackendUsers.helper.filter($('#filter-secretaries .key').val());
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
this.resetForm();
this.filter($('#filter-secretaries .key').val());
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
};
/**
@ -344,25 +356,27 @@ SecretariesHelper.prototype.display = function(secretary) {
* @param {bool} display (OPTIONAL = false)
*/
SecretariesHelper.prototype.filter = function(key, selectId, display) {
if (display == undefined) display = false;
display = display || false;
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_secretaries';
var postData = {
'csrfToken': GlobalVariables.csrfToken,
'key': key
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_secretaries',
postData = {
csrfToken: GlobalVariables.csrfToken,
key: key
};
$.post(postUrl, postData, function(response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
BackendUsers.helper.filterResults = response;
this.filterResults = response;
$('#filter-secretaries .results').data('jsp').destroy();
$('#filter-secretaries .results').html('');
$.each(response, function(index, secretary) {
var html = SecretariesHelper.prototype.getFilterHtml(secretary);
var html = this.getFilterHtml(secretary);
$('#filter-secretaries .results').append(html);
});
}.bind(this));
$('#filter-secretaries .results').jScrollPane({ mouseWheelSpeed: 70 });
if (response.length == 0) {
@ -370,9 +384,9 @@ SecretariesHelper.prototype.filter = function(key, selectId, display) {
}
if (selectId != undefined) {
BackendUsers.helper.select(selectId, display);
this.select(selectId, display);
}
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
};
/**
@ -382,10 +396,12 @@ SecretariesHelper.prototype.filter = function(key, selectId, display) {
* @returns {string} The html code that represents the record on the filter results list.
*/
SecretariesHelper.prototype.getFilterHtml = function(secretary) {
var name = secretary.first_name + ' ' + secretary.last_name;
var info = secretary.email;
var name = secretary.first_name + ' ' + secretary.last_name,
info = secretary.email;
info = (secretary.mobile_number != '' && secretary.mobile_number != null)
? info + ', ' + secretary.mobile_number : info;
info = (secretary.phone_number != '' && secretary.phone_number != null)
? info + ', ' + secretary.phone_number : info;
@ -407,7 +423,7 @@ SecretariesHelper.prototype.getFilterHtml = function(secretary) {
* on the form.
*/
SecretariesHelper.prototype.select = function(id, display) {
if (display == undefined) display = false;
display = display || false;
$('#filter-secretaries .selected-row').removeClass('selected-row');
@ -419,12 +435,16 @@ SecretariesHelper.prototype.select = function(id, display) {
});
if (display) {
$.each(BackendUsers.helper.filterResults, function(index, admin) {
$.each(this.filterResults, function(index, admin) {
if (admin.id == id) {
BackendUsers.helper.display(admin);
this.display(admin);
$('#edit-secretary, #delete-secretary').prop('disabled', false);
return false;
}
});
}.bind(this));
}
};
window.SecretariesHelper = SecretariesHelper;
})();