Convert the secretaries page to the new js module structure.

This commit is contained in:
Alex Tselegidis 2022-01-07 09:44:51 +01:00
parent 30418468e8
commit 41f25026de
5 changed files with 227 additions and 198 deletions

View file

@ -37,7 +37,7 @@ class Secretaries extends EA_Controller {
/** /**
* Render the backend secretaries page. * Render the backend secretaries page.
* *
* On this page secretary users will be able to manage secretaries, which are eventually selected by customers during the * On this page secretary users will be able to manage secretaries, which are eventually selected by customers during the
* booking process. * booking process.
*/ */
public function index() public function index()
@ -53,6 +53,25 @@ class Secretaries extends EA_Controller {
$role_slug = session('role_slug'); $role_slug = session('role_slug');
$providers = $this->providers_model->get();
foreach ($providers as &$provider)
{
$this->providers_model->only($provider, [
'id',
'first_name',
'last_name'
]);
}
script_vars([
'timezones' => $this->timezones->to_array(),
'user_id' => $user_id,
'role_slug' => $role_slug,
'min_password_length' => MIN_PASSWORD_LENGTH,
'providers' => $providers,
]);
html_vars([ html_vars([
'page_title' => lang('secretaries'), 'page_title' => lang('secretaries'),
'active_menu' => PRIV_USERS, 'active_menu' => PRIV_USERS,
@ -60,8 +79,8 @@ class Secretaries extends EA_Controller {
'timezones' => $this->timezones->to_array(), 'timezones' => $this->timezones->to_array(),
'privileges' => $this->roles_model->get_permissions_by_slug($role_slug), 'privileges' => $this->roles_model->get_permissions_by_slug($role_slug),
'providers' => $this->providers_model->get(), 'providers' => $this->providers_model->get(),
]); ]);
$this->load->view('pages/secretaries', html_vars()); $this->load->view('pages/secretaries', html_vars());
} }
@ -82,7 +101,7 @@ class Secretaries extends EA_Controller {
$order_by = 'first_name ASC, last_name ASC, email ASC'; $order_by = 'first_name ASC, last_name ASC, email ASC';
$limit = request('limit', 1000); $limit = request('limit', 1000);
$offset = 0; $offset = 0;
$secretaries = $this->secretaries_model->search($keyword, $limit, $offset, $order_by); $secretaries = $this->secretaries_model->search($keyword, $limit, $offset, $order_by);
@ -102,7 +121,7 @@ class Secretaries extends EA_Controller {
{ {
try try
{ {
$secretary = json_decode(request('secretary'), TRUE); $secretary = request('secretary');
if (cannot('add', PRIV_USERS)) if (cannot('add', PRIV_USERS))
{ {
@ -129,7 +148,7 @@ class Secretaries extends EA_Controller {
{ {
try try
{ {
$secretary = json_decode(request('secretary'), TRUE); $secretary = request('secretary');
if (cannot('edit', PRIV_USERS)) if (cannot('edit', PRIV_USERS))
{ {

View file

@ -206,7 +206,7 @@ class Secretaries_model extends EA_Model {
{ {
$secretary['id_roles'] = $this->get_secretary_role_id(); $secretary['id_roles'] = $this->get_secretary_role_id();
$providers = $secretary['providers']; $providers = $secretary['providers'] ?? [];
unset($secretary['providers']); unset($secretary['providers']);
$settings = $secretary['settings']; $settings = $secretary['settings'];
@ -239,7 +239,7 @@ class Secretaries_model extends EA_Model {
*/ */
protected function update(array $secretary): int protected function update(array $secretary): int
{ {
$provider_ids = $secretary['providers']; $provider_ids = $secretary['providers'] ?? [];
unset($secretary['providers']); unset($secretary['providers']);
$settings = $secretary['settings']; $settings = $secretary['settings'];

View file

@ -189,9 +189,9 @@
<br> <br>
<div class="custom-control custom-switch"> <div class="form-check form-switch me-4">
<input type="checkbox" class="custom-control-input" id="secretary-notifications"> <input class="form-check-input" type="checkbox" id="secretary-notifications">
<label class="custom-form-label" for="secretary-notifications"> <label class="form-check-label" for="secretary-notifications">
<?= lang('receive_notifications') ?> <?= lang('receive_notifications') ?>
</label> </label>
</div> </div>
@ -210,30 +210,12 @@
<?php section('scripts') ?> <?php section('scripts') ?>
<script src="<?= asset_url('assets/js/pages/backend_secretaries_helper.js') ?>"></script> <script src="<?= asset_url('assets/js/utils/message.js') ?>"></script>
<script src="<?= asset_url('assets/js/pages/backend_secretaries.js') ?>"></script> <script src="<?= asset_url('assets/js/utils/validation.js') ?>"></script>
<script> <script src="<?= asset_url('assets/js/utils/url.js') ?>"></script>
var GlobalVariables = { <script src="<?= asset_url('assets/js/http/account_http_client.js') ?>"></script>
csrfToken: <?= json_encode($this->security->get_csrf_hash()) ?>, <script src="<?= asset_url('assets/js/http/secretaries_http_client.js') ?>"></script>
baseUrl: <?= json_encode(config('base_url')) ?>, <script src="<?= asset_url('assets/js/pages/secretaries.js') ?>"></script>
dateFormat: <?= json_encode(setting('date_format')) ?>,
timeFormat: <?= json_encode(setting('time_format')) ?>,
firstWeekday: <?= json_encode(setting('first_weekday')) ?>,
providers: <?= json_encode($providers) ?>,
timezones: <?= json_encode($timezones) ?>,
user: {
id: <?= session('user_id') ?>,
email: <?= json_encode(session('user_email')) ?>,
timezone: <?= json_encode(session('timezone')) ?>,
role_slug: <?= json_encode(session('role_slug')) ?>,
privileges: <?= json_encode($privileges) ?>
}
};
$(function () {
BackendSecretaries.initialize(true);
});
</script>
<?php section('scripts') ?> <?php section('scripts') ?>

View file

@ -11,7 +11,18 @@
App.Http.Secretaries = (function () { App.Http.Secretaries = (function () {
/** /**
* Create an secretary. * Save (create or update) a secretary.
*
* @param {Object} secretary
*
* @return {Object}
*/
function save(secretary) {
return secretary.id ? update(secretary) : create(secretary);
}
/**
* Create a secretary.
* *
* @param {Object} secretary * @param {Object} secretary
* *
@ -29,7 +40,7 @@ App.Http.Secretaries = (function () {
} }
/** /**
* Update an secretary. * Update a secretary.
* *
* @param {Object} secretary * @param {Object} secretary
* *
@ -47,7 +58,7 @@ App.Http.Secretaries = (function () {
} }
/** /**
* Delete an secretary. * Delete a secretary.
* *
* @param {Number} secretaryId * @param {Number} secretaryId
* *
@ -89,7 +100,7 @@ App.Http.Secretaries = (function () {
} }
/** /**
* Find an secretary. * Find a secretary.
* *
* @param {Number} secretaryId * @param {Number} secretaryId
* *
@ -107,6 +118,7 @@ App.Http.Secretaries = (function () {
} }
return { return {
save,
create, create,
update, update,
destroy, destroy,

View file

@ -9,27 +9,57 @@
* @since v1.0.0 * @since v1.0.0
* ---------------------------------------------------------------------------- */ * ---------------------------------------------------------------------------- */
(function () { /**
'use strict'; * Secretaries page module.
*
/** * This module contains the methods that are used in the admins page.
* Secretaries Helper */
* App.Pages.Secretaries = (function () {
* This class contains the Secretaries helper class declaration, along with the "Secretaries" const $secretaries = $('#secretaries');
* tab event handlers. By dividing the backend/users tab functionality into separate files let filterResults = {};
* it is easier to maintain the code. let filterLimit = 20;
*
* @class SecretariesHelper
*/
var SecretariesHelper = function () {
this.filterResults = {}; // Store the results for later use.
this.filterLimit = 20;
};
/** /**
* Bind the event handlers for the backend/users "Secretaries" tab. * Bind the event handlers for the backend/users "Secretaries" tab.
*/ */
SecretariesHelper.prototype.bindEventHandlers = function () { function bindEventHandlers() {
/**
* Event: Admin Username "Blur"
*
* When the admin leaves the username input field we will need to check if the username
* is not taken by another record in the system.
*/
$secretaries.on('blur', '#secretary-username', function () {
const $input = $(this);
if ($input.prop('readonly') === true || $input.val() === '') {
return;
}
const secretaryId = $input.parents().eq(2).find('.record-id').val();
if (!secretaryId) {
return;
}
const username = $input.val();
App.Http.Account.validateUsername(secretaryId, username).done((response) => {
if (response.is_valid === 'false') {
$input.addClass('is-invalid');
$input.attr('already-exists', 'true');
$input.parents().eq(3).find('.form-message').text(App.Lang.username_already_exists);
$input.parents().eq(3).find('.form-message').show();
} else {
$input.removeClass('is-invalid');
$input.attr('already-exists', 'false');
if ($input.parents().eq(3).find('.form-message').text() === App.Lang.username_already_exists) {
$input.parents().eq(3).find('.form-message').hide();
}
}
});
});
/** /**
* Event: Filter Secretaries Form "Submit" * Event: Filter Secretaries Form "Submit"
* *
@ -37,15 +67,15 @@
* *
* @param {jQuery.Event} event * @param {jQuery.Event} event
*/ */
$('#secretaries').on( $secretaries.on(
'submit', 'submit',
'#filter-secretaries form', '#filter-secretaries form',
function (event) { function (event) {
event.preventDefault(); event.preventDefault();
var key = $('#filter-secretaries .key').val(); const key = $('#filter-secretaries .key').val();
$('#filter-secretaries .selected').removeClass('selected'); $('#filter-secretaries .selected').removeClass('selected');
this.resetForm(); resetForm();
this.filter(key); filter(key);
}.bind(this) }.bind(this)
); );
@ -54,7 +84,7 @@
* *
* Display the selected secretary data to the user. * Display the selected secretary data to the user.
*/ */
$('#secretaries').on( $secretaries.on(
'click', 'click',
'.secretary-row', '.secretary-row',
function (event) { function (event) {
@ -63,13 +93,13 @@
return; // exit because we are currently on edit mode return; // exit because we are currently on edit mode
} }
var secretaryId = $(event.currentTarget).attr('data-id'); const secretaryId = $(event.currentTarget).attr('data-id');
var secretary = this.filterResults.find(function (filterResult) { const secretary = filterResults.find(function (filterResult) {
return Number(filterResult.id) === Number(secretaryId); return Number(filterResult.id) === Number(secretaryId);
}); });
this.display(secretary); display(secretary);
$('#filter-secretaries .selected').removeClass('selected'); $('#filter-secretaries .selected').removeClass('selected');
$(event.currentTarget).addClass('selected'); $(event.currentTarget).addClass('selected');
@ -80,11 +110,11 @@
/** /**
* Event: Add New Secretary Button "Click" * Event: Add New Secretary Button "Click"
*/ */
$('#secretaries').on( $secretaries.on(
'click', 'click',
'#add-secretary', '#add-secretary',
function () { function () {
this.resetForm(); resetForm();
$('#filter-secretaries button').prop('disabled', true); $('#filter-secretaries button').prop('disabled', true);
$('#filter-secretaries .results').css('color', '#AAA'); $('#filter-secretaries .results').css('color', '#AAA');
@ -100,7 +130,7 @@
/** /**
* Event: Edit Secretary Button "Click" * Event: Edit Secretary Button "Click"
*/ */
$('#secretaries').on('click', '#edit-secretary', function () { $secretaries.on('click', '#edit-secretary', function () {
$('#filter-secretaries button').prop('disabled', true); $('#filter-secretaries button').prop('disabled', true);
$('#filter-secretaries .results').css('color', '#AAA'); $('#filter-secretaries .results').css('color', '#AAA');
$('#secretaries .add-edit-delete-group').hide(); $('#secretaries .add-edit-delete-group').hide();
@ -114,12 +144,12 @@
/** /**
* Event: Delete Secretary Button "Click" * Event: Delete Secretary Button "Click"
*/ */
$('#secretaries').on( $secretaries.on(
'click', 'click',
'#delete-secretary', '#delete-secretary',
function () { function () {
var secretaryId = $('#secretary-id').val(); const secretaryId = $('#secretary-id').val();
var buttons = [ const buttons = [
{ {
text: App.Lang.cancel, text: App.Lang.cancel,
click: function () { click: function () {
@ -129,24 +159,24 @@
{ {
text: App.Lang.delete, text: App.Lang.delete,
click: function () { click: function () {
this.delete(secretaryId); remove(secretaryId);
$('#message-box').dialog('close'); $('#message-box').dialog('close');
}.bind(this) }.bind(this)
} }
]; ];
GeneralFunctions.displayMessageBox(App.Lang.delete_secretary, App.Lang.delete_record_prompt, buttons); App.Utils.Message.show(App.Lang.delete_secretary, App.Lang.delete_record_prompt, buttons);
}.bind(this) }.bind(this)
); );
/** /**
* Event: Save Secretary Button "Click" * Event: Save Secretary Button "Click"
*/ */
$('#secretaries').on( $secretaries.on(
'click', 'click',
'#save-secretary', '#save-secretary',
function () { function () {
var secretary = { const secretary = {
first_name: $('#secretary-first-name').val(), first_name: $('#secretary-first-name').val(),
last_name: $('#secretary-last-name').val(), last_name: $('#secretary-last-name').val(),
email: $('#secretary-email').val(), email: $('#secretary-email').val(),
@ -160,13 +190,14 @@
timezone: $('#secretary-timezone').val(), timezone: $('#secretary-timezone').val(),
settings: { settings: {
username: $('#secretary-username').val(), username: $('#secretary-username').val(),
notifications: $('#secretary-notifications').prop('checked'), notifications: Number($('#secretary-notifications').prop('checked')),
calendar_view: $('#secretary-calendar-view').val() calendar_view: $('#secretary-calendar-view').val()
} }
}; };
// Include secretary services. // Include secretary services.
secretary.providers = []; secretary.providers = [];
$('#secretary-providers input:checkbox').each(function (index, checkbox) { $('#secretary-providers input:checkbox').each(function (index, checkbox) {
if ($(checkbox).prop('checked')) { if ($(checkbox).prop('checked')) {
secretary.providers.push($(checkbox).attr('data-id')); secretary.providers.push($(checkbox).attr('data-id'));
@ -183,11 +214,11 @@
secretary.id = $('#secretary-id').val(); secretary.id = $('#secretary-id').val();
} }
if (!this.validate()) { if (!validate()) {
return; return;
} }
this.save(secretary); save(secretary);
}.bind(this) }.bind(this)
); );
@ -196,33 +227,18 @@
* *
* Cancel add or edit of an secretary record. * Cancel add or edit of an secretary record.
*/ */
$('#secretaries').on( $secretaries.on(
'click', 'click',
'#cancel-secretary', '#cancel-secretary',
function () { function () {
var id = $('#secretary-id').val(); const id = $('#secretary-id').val();
this.resetForm(); resetForm();
if (id) { if (id) {
this.select(id, true); select(id, true);
} }
}.bind(this) }.bind(this)
); );
}; }
/**
* Remove the previously registered event handlers.
*/
SecretariesHelper.prototype.unbindEventHandlers = function () {
$('#secretaries')
.off('submit', '#filter-secretaries form')
.off('click', '#filter-secretaries .clear')
.off('click', '.secretary-row')
.off('click', '#add-secretary')
.off('click', '#edit-secretary')
.off('click', '#delete-secretary')
.off('click', '#save-secretary')
.off('click', '#cancel-secretary');
};
/** /**
* Save secretary record to database. * Save secretary record to database.
@ -230,58 +246,41 @@
* @param {Object} secretary Contains the secretary record data. If an 'id' value is provided * @param {Object} secretary Contains the secretary record data. If an 'id' value is provided
* then the update operation is going to be executed. * then the update operation is going to be executed.
*/ */
SecretariesHelper.prototype.save = function (secretary) { function save(secretary) {
var url = GlobalVariables.baseUrl + '/index.php/secretaries/' + (secretary.id ? 'update' : 'create'); App.Http.Secretaries.save(secretary).done((response) => {
Backend.displayNotification(App.Lang.secretary_saved);
var data = { resetForm();
csrf_token: GlobalVariables.csrfToken, $('#filter-secretaries .key').val('');
secretary: JSON.stringify(secretary) filter('', response.id, true);
}; });
}
$.post(url, data).done(
function (response) {
Backend.displayNotification(App.Lang.secretary_saved);
this.resetForm();
$('#filter-secretaries .key').val('');
this.filter('', response.id, true);
}.bind(this)
);
};
/** /**
* Delete a secretary record from database. * Delete a secretary record from database.
* *
* @param {Number} id Record id to be deleted. * @param {Number} id Record id to be deleted.
*/ */
SecretariesHelper.prototype.delete = function (id) { function remove(id) {
var url = GlobalVariables.baseUrl + '/index.php/secretaries/destroy'; App.Http.Secretaries.destroy(id).done(() => {
Backend.displayNotification(App.Lang.secretary_deleted);
var data = { resetForm();
csrf_token: GlobalVariables.csrfToken, filter($('#filter-secretaries .key').val());
secretary_id: id });
}; }
$.post(url, data).done(
function () {
Backend.displayNotification(App.Lang.secretary_deleted);
this.resetForm();
this.filter($('#filter-secretaries .key').val());
}.bind(this)
);
};
/** /**
* Validates a secretary record. * Validates a secretary record.
* *
* @return {Boolean} Returns the validation result. * @return {Boolean} Returns the validation result.
*/ */
SecretariesHelper.prototype.validate = function () { function validate() {
$('#secretaries .is-invalid').removeClass('is-invalid'); $('#secretaries .is-invalid').removeClass('is-invalid');
$('#secretaries .form-message').removeClass('alert-danger'); $('#secretaries .form-message').removeClass('alert-danger');
try { try {
// Validate required fields. // Validate required fields.
var missingRequired = false; let missingRequired = false;
$('#secretaries .required').each(function (index, requiredField) { $('#secretaries .required').each(function (index, requiredField) {
if (!$(requiredField).val()) { if (!$(requiredField).val()) {
$(requiredField).addClass('is-invalid'); $(requiredField).addClass('is-invalid');
@ -299,7 +298,7 @@
} }
if ( if (
$('#secretary-password').val().length < BackendSecretaries.MIN_PASSWORD_LENGTH && $('#secretary-password').val().length < App.Vars.min_password_length &&
$('#secretary-password').val() !== '' $('#secretary-password').val() !== ''
) { ) {
$('#secretary-password, #secretary-password-confirm').addClass('is-invalid'); $('#secretary-password, #secretary-password-confirm').addClass('is-invalid');
@ -309,7 +308,7 @@
} }
// Validate user email. // Validate user email.
if (!GeneralFunctions.validateEmail($('#secretary-email').val())) { if (!App.Utils.Validation.email($('#secretary-email').val())) {
$('#secretary-email').addClass('is-invalid'); $('#secretary-email').addClass('is-invalid');
throw new Error('Invalid email address!'); throw new Error('Invalid email address!');
} }
@ -325,12 +324,12 @@
$('#secretaries .form-message').addClass('alert-danger').text(error.message).show(); $('#secretaries .form-message').addClass('alert-danger').text(error.message).show();
return false; return false;
} }
}; }
/** /**
* Resets the secretary tab form back to its initial state. * Resets the secretary tab form back to its initial state.
*/ */
SecretariesHelper.prototype.resetForm = function () { function resetForm() {
$('#filter-secretaries .selected').removeClass('selected'); $('#filter-secretaries .selected').removeClass('selected');
$('#filter-secretaries button').prop('disabled', false); $('#filter-secretaries button').prop('disabled', false);
$('#filter-secretaries .results').css('color', ''); $('#filter-secretaries .results').css('color', '');
@ -344,14 +343,14 @@
$('#secretaries .form-message').hide(); $('#secretaries .form-message').hide();
$('#secretary-providers input:checkbox').prop('checked', false); $('#secretary-providers input:checkbox').prop('checked', false);
$('#secretaries .is-invalid').removeClass('is-invalid'); $('#secretaries .is-invalid').removeClass('is-invalid');
}; }
/** /**
* Display a secretary record into the secretary form. * Display a secretary record into the secretary form.
* *
* @param {Object} secretary Contains the secretary record data. * @param {Object} secretary Contains the secretary record data.
*/ */
SecretariesHelper.prototype.display = function (secretary) { function display(secretary) {
$('#secretary-id').val(secretary.id); $('#secretary-id').val(secretary.id);
$('#secretary-first-name').val(secretary.first_name); $('#secretary-first-name').val(secretary.first_name);
$('#secretary-last-name').val(secretary.last_name); $('#secretary-last-name').val(secretary.last_name);
@ -372,7 +371,7 @@
$('#secretary-providers input:checkbox').prop('checked', false); $('#secretary-providers input:checkbox').prop('checked', false);
secretary.providers.forEach(function (secretaryProviderId) { secretary.providers.forEach(function (secretaryProviderId) {
var $checkbox = $('#secretary-providers input[data-id="' + secretaryProviderId + '"]'); const $checkbox = $('#secretary-providers input[data-id="' + secretaryProviderId + '"]');
if (!$checkbox.length) { if (!$checkbox.length) {
return; return;
@ -380,63 +379,51 @@
$checkbox.prop('checked', true); $checkbox.prop('checked', true);
}); });
}; }
/** /**
* Filters secretary records depending a string keyword. * Filters secretary records based on a string keyword.
* *
* @param {String} keyword This is used to filter the secretary records of the database. * @param {String} keyword This is used to filter the secretary records of the database.
* @param {Numeric} selectId Optional, if provided the given ID will be selected in the filter results * @param {Number} selectId Optional, if provided the given ID will be selected in the filter results
* (only selected, not displayed). * (only selected, not displayed).
* @param {Bool} display Optional (false). * @param {Boolean} show Optional (false).
*/ */
SecretariesHelper.prototype.filter = function (keyword, selectId, display) { function filter(keyword, selectId = null, show = false) {
display = display || false; App.Http.Secretaries.search(keyword, filterLimit).done((response) => {
filterResults = response;
var url = GlobalVariables.baseUrl + '/index.php/secretaries/search'; $('#filter-secretaries .results').empty();
var data = { response.forEach(
csrf_token: GlobalVariables.csrfToken, function (secretary) {
keyword: keyword, $('#filter-secretaries .results').append(getFilterHtml(secretary)).append($('<hr/>'));
limit: this.filterLimit }.bind(this)
}; );
$.post(url, data).done( if (!response.length) {
function (response) { $('#filter-secretaries .results').append(
this.filterResults = response; $('<em/>', {
'text': App.Lang.no_records_found
$('#filter-secretaries .results').empty(); })
response.forEach(
function (secretary) {
$('#filter-secretaries .results').append(this.getFilterHtml(secretary)).append($('<hr/>'));
}.bind(this)
); );
} else if (response.length === filterLimit) {
$('<button/>', {
'type': 'button',
'class': 'btn btn-outline-secondary w-100 load-more text-center',
'text': App.Lang.load_more,
'click': function () {
filterLimit += 20;
filter(keyword, selectId, show);
}.bind(this)
}).appendTo('#filter-secretaries .results');
}
if (!response.length) { if (selectId) {
$('#filter-secretaries .results').append( select(selectId, show);
$('<em/>', { }
'text': App.Lang.no_records_found });
}) }
);
} else if (response.length === this.filterLimit) {
$('<button/>', {
'type': 'button',
'class': 'btn btn-outline-secondary w-100 load-more text-center',
'text': App.Lang.load_more,
'click': function () {
this.filterLimit += 20;
this.filter(keyword, selectId, display);
}.bind(this)
}).appendTo('#filter-secretaries .results');
}
if (selectId) {
this.select(selectId, display);
}
}.bind(this)
);
};
/** /**
* Get an secretary row html code that is going to be displayed on the filter results list. * Get an secretary row html code that is going to be displayed on the filter results list.
@ -445,10 +432,10 @@
* *
* @return {String} The html code that represents the record on the filter results list. * @return {String} The html code that represents the record on the filter results list.
*/ */
SecretariesHelper.prototype.getFilterHtml = function (secretary) { function getFilterHtml(secretary) {
var name = secretary.first_name + ' ' + secretary.last_name; const name = secretary.first_name + ' ' + secretary.last_name;
var info = secretary.email; let info = secretary.email;
info = secretary.mobile_number ? info + ', ' + secretary.mobile_number : info; info = secretary.mobile_number ? info + ', ' + secretary.mobile_number : info;
@ -468,34 +455,63 @@
$('<br/>') $('<br/>')
] ]
}); });
}; }
/** /**
* Select a specific record from the current filter results. If the secretary id does not exist * Select a specific record from the current filter results. If the secretary id does not exist
* in the list then no record will be selected. * in the list then no record will be selected.
* *
* @param {Number} id The record id to be selected from the filter results. * @param {Number} id The record id to be selected from the filter results.
* @param {Boolean} display Optional (false), if true the method will display the record in the form. * @param {Boolean} show Optional (false), if true the method will display the record in the form.
*/ */
SecretariesHelper.prototype.select = function (id, display) { function select(id, show = false) {
display = display || false;
$('#filter-secretaries .selected').removeClass('selected'); $('#filter-secretaries .selected').removeClass('selected');
$('#filter-secretaries .secretary-row[data-id="' + id + '"]').addClass('selected'); $('#filter-secretaries .secretary-row[data-id="' + id + '"]').addClass('selected');
if (display) { if (show) {
var secretary = this.filterResults.find( const secretary = filterResults.find(
function (filterResult) { function (filterResult) {
return Number(filterResult.id) === Number(id); return Number(filterResult.id) === Number(id);
}.bind(this) }.bind(this)
); );
this.display(secretary); display(secretary);
$('#edit-secretary, #delete-secretary').prop('disabled', false); $('#edit-secretary, #delete-secretary').prop('disabled', false);
} }
}; }
window.SecretariesHelper = SecretariesHelper; function init() {
resetForm();
filter('');
bindEventHandlers();
App.Vars.providers.forEach(function (provider) {
$('<div/>', {
'class': 'checkbox',
'html': [
$('<div/>', {
'class': 'checkbox form-check',
'html': [
$('<input/>', {
'class': 'form-check-input',
'type': 'checkbox',
'data-id': provider.id
}),
$('<label/>', {
'class': 'form-check-label',
'text': provider.first_name + ' ' + provider.last_name,
'for': provider.id
})
]
})
]
}).appendTo('#secretary-providers');
});
}
document.addEventListener('DOMContentLoaded', init);
return {};
})(); })();