MaketRandevu/assets/js/backend_calendar_table_view.js

906 lines
36 KiB
JavaScript
Raw Normal View History

/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.2.0
* ---------------------------------------------------------------------------- */
window.BackendCalendarTableView = window.BackendCalendarTableView || {};
/**
* Backend Calendar
*
* This module implements the table calendar view of backend.
*
* @module BackendCalendarTableView
*/
2018-01-23 12:08:37 +03:00
(function (exports) {
'use strict';
/**
2018-01-23 12:08:37 +03:00
* Sticky Table Header Fix
*
* @type {Number}
*/
2018-01-23 12:08:37 +03:00
var stickyTableHeaderInterval;
/**
2018-01-23 12:08:37 +03:00
* Bind page event handlers.
*/
function _bindEventHandlers() {
2016-07-24 14:25:54 +03:00
var $calendarToolbar = $('#calendar-toolbar');
2018-01-23 12:08:37 +03:00
var $calendar = $('#calendar');
2018-01-23 12:08:37 +03:00
$calendar.on('click', '.calendar-header .btn.previous', function () {
var endDate = new Date($('#calendar .date-column:first').data('date')).add({days: -1});
var startDate = new Date(endDate.getTime()).add({days: -1 * (parseInt($('#select-filter-item').val()) - 1)});
$('.select-date').datepicker('setDate', startDate);
_createView(startDate, endDate);
});
2018-01-23 12:08:37 +03:00
$calendar.on('click', '.calendar-header .btn.next', function () {
var startDate = new Date($('#calendar .date-column:last').data('date')).add({days: 1});
var endDate = new Date(startDate.getTime()).add({days: parseInt($('#select-filter-item').val()) - 1});
$('.select-date').datepicker('setDate', startDate);
_createView(startDate, endDate);
});
2018-01-23 12:08:37 +03:00
$calendarToolbar.on('change', '#select-filter-item', function () {
var startDate = new Date($('.calendar-view .date-column:first').data('date'));
var endDate = new Date(startDate.getTime()).add({days: parseInt($(this).val()) - 1});
2016-07-18 21:18:55 +03:00
_createView(startDate, endDate);
// Horizontal scrolling fix for sticky table headers.
2018-01-23 12:08:37 +03:00
stickyTableHeaderInterval = setInterval(function () {
$(window).trigger('resize.stickyTableHeaders');
}, 1000);
2016-07-18 21:18:55 +03:00
});
2018-01-23 12:08:37 +03:00
$calendarToolbar.on('click', '#reload-appointments', function () {
// Remove all the events from the tables.
2018-01-23 12:08:37 +03:00
$('.calendar-view .event').remove();
// Fetch the events and place them in the existing HTML format.
2016-07-18 21:18:55 +03:00
var startDate = new Date($('.calendar-view .date-column:first').data('date'));
var endDate = new Date($('.calendar-view .date-column:last').data('date'));
_getCalendarEvents(startDate, endDate)
2018-01-23 12:08:37 +03:00
.done(function (response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
2018-01-23 12:08:37 +03:00
var currentDate = startDate;
2018-01-23 12:08:37 +03:00
while (currentDate <= endDate) {
$('.calendar-view .date-column').each(function (index, dateColumn) {
var $dateColumn = $(dateColumn);
var date = new Date($dateColumn.data('date'));
if (currentDate.getTime() !== date.getTime()) {
return true;
}
2018-01-23 12:08:37 +03:00
$(dateColumn).find('.provider-column').each(function (index, providerColumn) {
var $providerColumn = $(providerColumn);
var provider = $providerColumn.data('provider');
// Add the appointments to the column.
2018-01-23 12:08:37 +03:00
_createAppointments($providerColumn, response.appointments);
// Add the unavailabilities to the column.
2018-01-23 12:08:37 +03:00
_createUnavailabilities($providerColumn, response.unavailabilities);
// Add the provider breaks to the column.
2018-01-23 12:08:37 +03:00
var workingPlan = JSON.parse(provider.settings.working_plan);
var day = date.toString('dddd').toLowerCase();
if (workingPlan[day]) {
var breaks = workingPlan[day].breaks;
_createBreaks($providerColumn, breaks);
}
});
});
2018-01-23 12:08:37 +03:00
currentDate.add({days: 1});
}
_setCalendarSize();
Backend.placeFooterToBottom();
})
.fail(GeneralFunctions.ajaxFailureHandler);
2018-01-23 12:08:37 +03:00
});
$calendar.on('click', '.calendar-view table td', function () {
2016-07-18 21:18:55 +03:00
if ($(this).index() === 0) {
return; // Clicked on an hour slot.
}
// Open the appointments modal in the selected hour.
2018-01-23 12:08:37 +03:00
var hour = $(this).parent().find('td:first').text().split(':');
2016-07-18 21:18:55 +03:00
var date = new Date($(this).parents('.date-column').data('date'));
date.set({hour: parseInt(hour[0]), minute: parseInt(hour[1])});
// Open the appointments dialog.
$('#insert-appointment').trigger('click');
// Update start date field.
2018-01-23 12:08:37 +03:00
$('#start-datetime').datepicker('setDate', date);
2016-07-18 21:18:55 +03:00
// Select Service and provider.
var $providerColumn = $(this).parents('.provider-column');
var serviceId = $providerColumn.find('thead tr:last th').eq($(this).index()).data('id');
2018-01-23 12:08:37 +03:00
var providerId = $providerColumn.data('provider').id;
2016-07-18 21:18:55 +03:00
$('#select-service').val(serviceId).trigger('change');
$('#select-provider').val(providerId).trigger('change');
2018-01-23 12:08:37 +03:00
});
var lastFocusedEventData;
2018-01-23 12:08:37 +03:00
/**
* Event: On Table Event Click
2018-01-23 12:08:37 +03:00
*
* @param {jQuery.Event} event
*/
2018-01-23 12:08:37 +03:00
$calendar.on('click', '.event', function (event) {
event.stopPropagation();
if ($(this).hasClass('break')) {
return; // Do nothing with break events.
}
$('.popover').remove(); // Close all open popovers.
var html;
2018-01-23 12:08:37 +03:00
var entry = $(this).data();
2018-01-23 12:08:37 +03:00
if ($(this).hasClass('unavailability')) {
var notes = '<strong>Notes</strong> ' + entry.notes;
html =
2018-01-23 12:08:37 +03:00
'<style type="text/css">'
+ '.popover-content strong {min-width: 80px; display:inline-block;}'
+ '.popover-content button {margin-right: 10px;}'
+ '</style>' +
'<strong>' + EALang.start + '</strong> '
+ GeneralFunctions.formatDate(entry.start_datetime, GlobalVariables.dateFormat, true)
+ '<br>' +
'<strong>' + EALang.end + '</strong> '
+ GeneralFunctions.formatDate(entry.end_datetime, GlobalVariables.dateFormat, true)
+ '<br>'
+ notes
+ '<hr>' +
'<center>' +
'<button class="edit-popover btn btn-primary">' + EALang.edit + '</button>' +
'<button class="delete-popover btn btn-danger">' + EALang.delete + '</button>' +
'<button class="close-popover btn btn-default" data-po=' + event.target + '>' + EALang.close + '</button>' +
'</center>';
} else {
html =
2018-01-23 12:08:37 +03:00
'<style type="text/css">'
+ '.popover-content strong {min-width: 80px; display:inline-block;}'
+ '.popover-content button {margin-right: 10px;}'
+ '</style>' +
'<strong>' + EALang.start + '</strong> '
+ GeneralFunctions.formatDate(entry.start_datetime, GlobalVariables.dateFormat, true)
+ '<br>' +
'<strong>' + EALang.end + '</strong> '
+ GeneralFunctions.formatDate(entry.end_datetime, GlobalVariables.dateFormat, true)
+ '<br>' +
'<strong>' + EALang.service + '</strong> '
+ entry.service.name
+ '<br>' +
'<strong>' + EALang.provider + '</strong> '
+ entry.provider.first_name + ' '
+ entry.provider.last_name
+ '<br>' +
'<strong>' + EALang.customer + '</strong> '
+ entry.customer.first_name + ' '
+ entry.customer.last_name
+ '<hr>' +
'<center>' +
'<button class="edit-popover btn btn-primary">' + EALang.edit + '</button>' +
'<button class="delete-popover btn btn-danger">' + EALang.delete + '</button>' +
'<button class="close-popover btn btn-default" data-po=' + event.target + '>' + EALang.close + '</button>' +
'</center>';
}
var title = entry.is_unavailable !== '0' ? EALang.unavailable : entry.service.name + ' - '
2018-01-23 12:08:37 +03:00
+ entry.customer.first_name + ' ' + entry.customer.last_name;
$(event.target).popover({
placement: 'top',
title: title,
content: html,
html: true,
container: '#calendar',
trigger: 'manual'
});
lastFocusedEventData = entry;
$(event.target).popover('toggle');
// Fix popover position
if ($('.popover').length > 0 && $('.popover').position().top < 200) {
2018-01-23 12:08:37 +03:00
$('.popover').css('top', '200px');
}
});
/**
* Event: On Window Resize
*/
$(window).on('resize', _setCalendarSize);
/**
* Event: Popover Close Button "Click"
*
* Hides the open popover element.
*/
2018-01-23 12:08:37 +03:00
$calendar.on('click', '.close-popover', function () {
$(this).parents().eq(2).popover('destroy');
});
/**
* Event: Popover Edit Button "Click"
*
* Enables the edit dialog of the selected table event.
*/
2018-01-23 12:08:37 +03:00
$calendar.on('click', '.edit-popover', function () {
$(this).parents().eq(2).popover('destroy'); // Hide the popover
var $dialog;
if (lastFocusedEventData.is_unavailable == false) {
var appointment = lastFocusedEventData;
$dialog = $('#manage-appointment');
BackendCalendarAppointmentsModal.resetAppointmentDialog();
// Apply appointment data and show modal dialog.
$dialog.find('.modal-header h3').text(EALang.edit_appointment_title);
$dialog.find('#appointment-id').val(appointment.id);
$dialog.find('#select-service').val(appointment.id_services).trigger('change');
$dialog.find('#select-provider').val(appointment.id_users_provider);
// Set the start and end datetime of the appointment.
var startDatetime = Date.parseExact(appointment.start_datetime,
2018-01-23 12:08:37 +03:00
'yyyy-MM-dd HH:mm:ss');
$dialog.find('#start-datetime').datetimepicker('setDate', startDatetime);
var endDatetime = Date.parseExact(appointment.end_datetime,
2018-01-23 12:08:37 +03:00
'yyyy-MM-dd HH:mm:ss');
$dialog.find('#end-datetime').datetimepicker('setDate', endDatetime);
var customer = appointment.customer;
$dialog.find('#customer-id').val(appointment.id_users_customer);
$dialog.find('#first-name').val(customer.first_name);
$dialog.find('#last-name').val(customer.last_name);
$dialog.find('#email').val(customer.email);
$dialog.find('#phone-number').val(customer.phone_number);
$dialog.find('#address').val(customer.address);
$dialog.find('#city').val(customer.city);
$dialog.find('#zip-code').val(customer.zip_code);
$dialog.find('#appointment-notes').val(appointment.notes);
$dialog.find('#customer-notes').val(customer.notes);
} else {
var unavailable = lastFocusedEventData;
// Replace string date values with actual date objects.
unavailable.start_datetime = Date.parse(lastFocusedEventData.start_datetime);
unavailable.end_datetime = Date.parse(lastFocusedEventData.end_datetime);
$dialog = $('#manage-unavailable');
BackendCalendarUnavailabilitiesModal.resetUnavailableDialog();
// Apply unvailable data to dialog.
$dialog.find('.modal-header h3').text('Edit Unavailable Period');
$dialog.find('#unavailable-start').datetimepicker('setDate', unavailable.start_datetime);
$dialog.find('#unavailable-id').val(unavailable.id);
$dialog.find('#unavailable-provider').val(unavailable.id_users_provider);
$dialog.find('#unavailable-end').datetimepicker('setDate', unavailable.end_datetime);
$dialog.find('#unavailable-notes').val(unavailable.notes);
}
// :: DISPLAY EDIT DIALOG
$dialog.modal('show');
});
/**
* Event: Popover Delete Button "Click"
*
* Displays a prompt on whether the user wants the appointment to be deleted. If he confirms the
* deletion then an ajax call is made to the server and deletes the appointment from the database.
*/
2018-01-23 12:08:37 +03:00
$calendar.on('click', '.delete-popover', function () {
$(this).parents().eq(2).popover('destroy'); // Hide the popover
if (lastFocusedEventData.is_unavailable == false) {
var buttons = [
{
text: 'OK',
2018-01-23 12:08:37 +03:00
click: function () {
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_appointment';
var postData = {
csrfToken: GlobalVariables.csrfToken,
2018-01-23 12:08:37 +03:00
appointment_id: lastFocusedEventData.id,
delete_reason: $('#delete-reason').val()
};
2018-01-23 12:08:37 +03:00
$.post(postUrl, postData, function (response) {
$('#message_box').dialog('close');
if (response.exceptions) {
response.exceptions = GeneralFunctions.parseExceptions(response.exceptions);
GeneralFunctions.displayMessageBox(GeneralFunctions.EXCEPTIONS_TITLE,
GeneralFunctions.EXCEPTIONS_MESSAGE);
$('#message_box').append(GeneralFunctions.exceptionsToHtml(response.exceptions));
return;
}
if (response.warnings) {
response.warnings = GeneralFunctions.parseExceptions(response.warnings);
GeneralFunctions.displayMessageBox(GeneralFunctions.WARNINGS_TITLE,
GeneralFunctions.WARNINGS_MESSAGE);
$('#message_box').append(GeneralFunctions.exceptionsToHtml(response.warnings));
}
// Refresh calendar event items.
$('#select-filter-item').trigger('change');
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
}
},
{
text: EALang.cancel,
2018-01-23 12:08:37 +03:00
click: function () {
$('#message_box').dialog('close');
}
}
];
GeneralFunctions.displayMessageBox(EALang.delete_appointment_title,
2018-01-23 12:08:37 +03:00
EALang.write_appointment_removal_reason, buttons);
var $formGroup = $('<div/>', {
'class': 'form-group'
})
.appendTo('#message_box');
$('<textarea/>', {
'id': 'delete-reason',
'class': 'form-control'
})
.appendTo($formGroup);
} else {
// Do not display confirmation promt.
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_unavailable';
var data = {
csrfToken: GlobalVariables.csrfToken,
2018-01-23 12:08:37 +03:00
unavailable_id: lastFocusedEventData.id
};
2018-01-23 12:08:37 +03:00
$.post(url, data, function (response) {
$('#message_box').dialog('close');
if (response.exceptions) {
response.exceptions = GeneralFunctions.parseExceptions(response.exceptions);
GeneralFunctions.displayMessageBox(GeneralFunctions.EXCEPTIONS_TITLE, GeneralFunctions.EXCEPTIONS_MESSAGE);
$('#message_box').append(GeneralFunctions.exceptionsToHtml(response.exceptions));
return;
}
if (response.warnings) {
response.warnings = GeneralFunctions.parseExceptions(response.warnings);
GeneralFunctions.displayMessageBox(GeneralFunctions.WARNINGS_TITLE, GeneralFunctions.WARNINGS_MESSAGE);
$('#message_box').append(GeneralFunctions.exceptionsToHtml(response.warnings));
}
// Refresh calendar event items.
$('#select-filter-item').trigger('change');
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
}
});
}
/**
2018-01-23 12:08:37 +03:00
* Create table view header container.
*
* The header contains the date navigation elements (buttons and datepicker).
*/
function _createHeader() {
2018-01-23 12:08:37 +03:00
var $calendarFilter = $('#calendar-filter');
$calendarFilter
.find('select')
.empty()
2018-01-23 12:08:37 +03:00
.append(new Option('1 ' + EALang.day, 1))
.append(new Option('3 ' + EALang.days, 3));
2018-01-23 12:08:37 +03:00
var $calendarHeader = $('<div class="calendar-header" />').appendTo('#calendar');
$calendarHeader
.html(
'<button class="btn btn-xs btn-default previous">' +
2018-01-23 12:08:37 +03:00
'<span class="glyphicon glyphicon-chevron-left"></span>' +
'</button>' +
'<input type="text" class="select-date" value="' + GeneralFunctions.formatDate(new Date(), GlobalVariables.dateFormat) + '" />' +
'<button class="btn btn-xs btn-default next">' +
2018-01-23 12:08:37 +03:00
'<span class="glyphicon glyphicon-chevron-right"></span>' +
'</button>'
);
2018-01-23 12:08:37 +03:00
var dateFormat;
2018-01-23 12:08:37 +03:00
switch (GlobalVariables.dateFormat) {
case 'DMY':
dateFormat = 'dd/mm/yy';
break;
case 'MDY':
dateFormat = 'mm/dd/yy';
break;
case 'YMD':
dateFormat = 'yy/mm/dd';
break;
default:
throw new Error('Invalid date format setting provided!', GlobalVariables.dateFormat);
2018-01-23 12:08:37 +03:00
}
$calendarHeader.find('.select-date').datepicker({
defaultDate: new Date(),
dateFormat: dateFormat,
2018-01-23 12:08:37 +03:00
onSelect: function (dateText, instance) {
var startDate = new Date(instance.currentYear, instance.currentMonth, instance.currentDay);
2018-01-23 12:08:37 +03:00
var endDate = new Date(startDate.getTime()).add({days: parseInt($('#select-filter-item').val()) - 1});
_createView(startDate, endDate);
2018-01-23 12:08:37 +03:00
}
});
}
/**
2018-01-23 12:08:37 +03:00
* Create table schedule view.
*
* This method will destroy any previous instances and create a new view for displaying the appointments in
* a table format.
*
* @param {Date} startDate Start date to be displayed.
2018-01-23 12:08:37 +03:00
* @param {Date} endDate End date to be displayed.
*/
function _createView(startDate, endDate) {
// Disable date navigation.
$('#calendar .calendar-header .btn').addClass('disabled').prop('disabled', true);
$('#calendar .calendar-view table').stickyTableHeaders('destroy');
$('#calendar .calendar-view').remove();
var displayDate = startDate.getTime() !== endDate.getTime();
2018-01-23 12:08:37 +03:00
var $calendarView = $('<div class="calendar-view" />').appendTo('#calendar');
$calendarView.data({
startDate: startDate.toString('yyyy-MM-dd'),
endDate: endDate.toString('yyyy-MM-dd')
});
var $wrapper = $('<div />').appendTo($calendarView);
_getCalendarEvents(startDate, endDate)
2018-01-23 12:08:37 +03:00
.done(function (response) {
if (!GeneralFunctions.handleAjaxExceptions(response)) {
return;
}
2018-01-23 12:08:37 +03:00
var currentDate = startDate;
2018-01-23 12:08:37 +03:00
while (currentDate <= endDate) {
_createDateColumn($wrapper, currentDate, response, displayDate);
currentDate.add({days: 1});
}
_setCalendarSize();
Backend.placeFooterToBottom();
// Activate calendar navigation.
$('#calendar .calendar-header .btn').removeClass('disabled').prop('disabled', false)
})
.fail(GeneralFunctions.ajaxFailureHandler);
}
/**
2018-01-23 12:08:37 +03:00
* Create Date Column Container
*
2018-01-23 12:08:37 +03:00
* This element will contain the provider columns.
*
* @param {jQuery} $wrapper The wrapper div element of the table view.
* @param {Date} date Selected date for the column.
2018-01-23 12:08:37 +03:00
* @param {Object[]} events Events to be displayed on this date.
* @param {Boolean} displayDate Whether to display the date in the column container.
*/
function _createDateColumn($wrapper, date, events, displayDate) {
2018-01-23 12:08:37 +03:00
var $dateColumn = $('<div class="date-column" />').appendTo($wrapper);
$dateColumn.data('date', date.getTime());
if (displayDate) {
$dateColumn.append('<h5>' + GeneralFunctions.formatDate(date, GlobalVariables.dateFormat) + '</h5>');
}
var providers = GlobalVariables.availableProviders;
if (GlobalVariables.user.role_slug === 'provider') {
GlobalVariables.availableProviders.forEach(function (provider) {
if (provider.id == GlobalVariables.user.id) {
2018-01-23 12:08:37 +03:00
providers = [provider];
}
});
}
if (GlobalVariables.user.role_slug === 'secretary') {
providers = [];
GlobalVariables.availableProviders.forEach(function (provider) {
if (GlobalVariables.secretaryProviders.indexOf(provider.id) > -1) {
providers.push(provider)
}
});
}
for (var index in providers) {
var provider = providers[index];
_createProviderColumn($dateColumn, date, provider, events);
}
}
/**
2018-01-23 12:08:37 +03:00
* Create Provider Column Container
*
* @param {jQuery} $dateColumn Element to container the provider's column.
* @param {Date} date Selected date for the column.
2018-01-23 12:08:37 +03:00
* @param {Object} provider Contains the provider data.
* {Object[]} events Events to be displayed on this date.
*/
function _createProviderColumn($dateColumn, date, provider, events) {
if (provider.services.length === 0) {
return;
}
2018-01-23 12:08:37 +03:00
var $providerColumn = $('<div class="provider-column" />').appendTo($dateColumn);
2018-01-23 12:08:37 +03:00
$providerColumn.data('provider', provider);
// Create the table slots.
2018-01-23 12:08:37 +03:00
_createSlots($providerColumn, date, provider);
// Add the appointments to the column.
2018-01-23 12:08:37 +03:00
_createAppointments($providerColumn, events.appointments);
// Add the unavailabilities to the column.
2018-01-23 12:08:37 +03:00
_createUnavailabilities($providerColumn, events.unavailabilities);
// Add the provider breaks to the column.
2018-01-23 12:08:37 +03:00
var workingPlan = JSON.parse(provider.settings.working_plan);
var day = date.toString('dddd').toLowerCase();
if (workingPlan[day]) {
var breaks = workingPlan[day].breaks;
_createBreaks($providerColumn, breaks);
}
}
/**
2018-01-23 12:08:37 +03:00
* Create Table View Slots
*
* The time slots will be added to a specific provider's column.
*
* @param {jQuery} $providerColumn The selected provider column.
* @param {Date} date Selected date for the slot.
* @param {Object} provider Contains the provider data.
*/
function _createSlots($providerColumn, date, provider) {
2018-01-23 12:08:37 +03:00
var day = date.toString('dddd').toLowerCase();
var plan = JSON.parse(provider.settings.working_plan)[day];
if (!plan) {
2018-01-23 12:08:37 +03:00
$providerColumn.append('<div class="not-working">'
+ (provider.first_name + ' ' + provider.last_name).trim() + ' <br> ' + EALang.not_working
+ '</div>');
return;
}
var $table = $('<table class="slots" />')
.appendTo($providerColumn)
.addClass('table table-condensed table-responsive table-hover table-striped')
.html(
'<thead>' +
2018-01-23 12:08:37 +03:00
'<tr>' +
'<th colspan="' + (provider.services.length + 1) + '">' +
(provider.first_name + ' ' + provider.last_name).trim() +
'</th>' +
'</tr>' +
'<tr>' +
'<th>' + EALang.time + '</th>' +
'</tr>' +
'</thead>' +
'<tbody></tbody>'
);
for (var index in GlobalVariables.availableServices) {
2018-01-23 12:08:37 +03:00
var service = GlobalVariables.availableServices[index];
if (provider.services.indexOf(service.id) > -1) {
2018-01-23 12:08:37 +03:00
$table.find('thead tr:last').append('<th data-id="' + service.id + '">' + service.name + '</th>');
}
}
2018-01-23 12:08:37 +03:00
var start = parseInt(plan.start.split(':')[0]);
var end = parseInt(plan.end.split(':')[0]) + 1;
var current = start;
2018-01-23 12:08:37 +03:00
var $tbody = $table.find('tbody');
2018-01-23 12:08:37 +03:00
while (current <= end) {
var $tr = $('<tr/>').appendTo($tbody);
2018-01-23 12:08:37 +03:00
var time = (current < 10 ? '0' + parseInt(current) : parseInt(current)) + (current % 1 === 0 ? ':00' : ':30');
$tr
.append('<td>' + time + '</td>')
2018-01-23 12:08:37 +03:00
.append('<td/>'.repeat(provider.services.length));
2018-01-23 12:08:37 +03:00
current += 0.5;
}
$table.stickyTableHeaders();
}
/**
2018-01-23 12:08:37 +03:00
* Create Appointment Events
*
* This method will add the appointment events on the table view.
*
* @param {jQuery} $providerColumn The provider column container.
2018-01-23 12:08:37 +03:00
* @param {Object[]} appointments Contains the appointment events data.
*/
function _createAppointments($providerColumn, appointments) {
if (appointments.length === 0) {
return;
}
2018-01-23 12:08:37 +03:00
var currentDate = new Date($providerColumn.parents('.date-column').data('date'));
var $tbody = $providerColumn.find('table tbody');
for (var index in appointments) {
2018-01-23 12:08:37 +03:00
var appointment = appointments[index];
if (appointment.id_users_provider !== $providerColumn.data('provider').id) {
continue;
}
2018-01-23 12:08:37 +03:00
var eventDate = Date.parse(appointment.start_datetime);
var $event = $('<div class="event appointment" />');
var startDate = Date.parse(appointment.start_datetime);
var endDate = Date.parse(appointment.end_datetime);
var eventDuration = Math.round((endDate - startDate) / 60000);
2018-01-23 12:08:37 +03:00
$event.html(
appointment.customer.first_name.charAt(0) + '. ' + appointment.customer.last_name +
2018-03-17 20:00:55 +03:00
' <span class="hour">' + startDate.toString(GlobalVariables.timeFormat === 'regular' ? 'h:mm tt' : 'HH:mm') + '</span> '
2018-01-23 12:08:37 +03:00
+ (eventDuration !== parseInt(appointment.service.duration) ? '(' + eventDuration + '\')' : '')
);
$event.data(appointment);
2018-01-23 12:08:37 +03:00
$tbody.find('tr').each(function (index, tr) {
var $td = $(tr).find('td:first');
var cellDate = new Date(currentDate.getTime()).set({
hour: parseInt($td.text().split(':')[0]),
minute: parseInt($td.text().split(':')[1])
2018-01-23 12:08:37 +03:00
});
if (eventDate < cellDate) {
var cellIndex = $providerColumn
.find('thead tr:last th[data-id="' + appointment.service.id + '"]')
.index();
$event.appendTo($(tr).prev().find('td').eq(cellIndex));
// Remove the hour from the event if it is the same as the row.
2018-03-17 20:00:55 +03:00
if (eventDate.toString(GlobalVariables.timeFormat === 'regular' ? 'h:mm tt' : 'HH:mm') === $(tr).prev().find('td').eq(0).text()) {
$event.find('.hour').remove();
}
return false;
}
2018-01-23 12:08:37 +03:00
});
}
}
/**
2018-01-23 12:08:37 +03:00
* Create Unavailabilities Events
*
* This method will add the unavailability events on the table view.
*
* @param {jQuery} $providerColumn The provider column container.
2018-01-23 12:08:37 +03:00
* @param {Object[]} unavailabilities Contains the unavailability events data.
*/
function _createUnavailabilities($providerColumn, unavailabilities) {
if (unavailabilities.length === 0) {
return;
}
2018-01-23 12:08:37 +03:00
var currentDate = new Date($providerColumn.parents('.date-column').data('date'));
var $tbody = $providerColumn.find('table tbody');
for (var index in unavailabilities) {
2018-01-23 12:08:37 +03:00
var unavailability = unavailabilities[index];
if (unavailability.id_users_provider !== $providerColumn.data('provider').id) {
continue;
}
2018-01-23 12:08:37 +03:00
var eventDate = Date.parse(unavailability.start_datetime);
var endDate = Date.parse(unavailability.end_datetime);
var eventDuration = Math.round((endDate - eventDate) / 60000);
2018-01-23 12:08:37 +03:00
var $event = $('<div class="event unavailability" />');
$event.html((unavailability.notes || EALang.unavailable) +
2018-03-17 20:00:55 +03:00
' <span class="hour">' + eventDate.toString(GlobalVariables.timeFormat === 'regular' ? 'h:mm tt' : 'HH:mm') + '</span> (' + eventDuration + '\')');
$event.data(unavailability);
2018-01-23 12:08:37 +03:00
$tbody.find('tr').each(function (index, tr) {
var $td = $(tr).find('td:first');
var cellDate = new Date(currentDate.getTime()).set({
hour: parseInt($td.text().split(':')[0]),
minute: parseInt($td.text().split(':')[1])
2018-01-23 12:08:37 +03:00
});
if (eventDate < cellDate) {
$event.appendTo($(tr).prev().find('td').eq(1));
// Remove the hour from the event if it is the same as the row.
2018-03-17 20:00:55 +03:00
if (eventDate.toString(GlobalVariables.timeFormat === 'regular' ? 'h:mm tt' : 'HH:mm') === $(tr).prev().find('td').eq(0).text()) {
$event.find('.hour').remove();
}
return false;
}
2018-01-23 12:08:37 +03:00
});
}
}
/**
2018-01-23 12:08:37 +03:00
* Create break events in the table view.
*
* @param {jQuery} $providerColumn The provider column container.
* @param {Object[]} breaks Contains the break events data.
*/
function _createBreaks($providerColumn, breaks) {
if (breaks.length === 0) {
return;
}
2018-01-23 12:08:37 +03:00
var currentDate = new Date($providerColumn.parents('.date-column').data('date'));
var $tbody = $providerColumn.find('table tbody');
for (var index in breaks) {
var entry = breaks[index];
var startHour = entry.start.split(':');
2018-01-23 12:08:37 +03:00
var eventDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), startHour[0], startHour[1]);
var endHour = entry.end.split(':');
2018-01-23 12:08:37 +03:00
var endDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), endHour[0], endHour[1]);
var eventDuration = Math.round((endDate - eventDate) / 60000);
2018-01-23 12:08:37 +03:00
var $event = $('<div class="event unavailability break" />');
$event.html(
EALang.break +
2018-03-17 20:00:55 +03:00
' <span class="hour">' + eventDate.toString(GlobalVariables.timeFormat === 'regular' ? 'h:mm tt' : 'HH:mm') + '</span> (' + eventDuration + '\')');
$event.data(entry);
2018-01-23 12:08:37 +03:00
$tbody.find('tr').each(function (index, tr) {
var $td = $(tr).find('td:first');
var cellDate = new Date(currentDate.getTime()).set({
hour: parseInt($td.text().split(':')[0]),
minute: parseInt($td.text().split(':')[1])
2018-01-23 12:08:37 +03:00
});
if (eventDate < cellDate) {
// Remove the hour from the event if it is the same as the row.
2018-03-17 20:00:55 +03:00
if (eventDate.toString(GlobalVariables.timeFormat === 'regular' ? 'h:mm tt' : 'HH:mm') === $(tr).prev().find('td').eq(0).text()) {
$event.find('.hour').remove();
}
2018-01-23 12:08:37 +03:00
$(tr).prev().find('td:gt(0)').each(function (index, td) {
$event.clone().appendTo($(td));
});
return false;
}
2018-01-23 12:08:37 +03:00
});
}
}
/**
2018-01-23 12:08:37 +03:00
* Set Table Calendar View
*
* This method will set the optimal size in the calendar view elements in order to fit in the page without
* using scrollbars.
*/
function _setCalendarSize() {
2018-01-23 12:08:37 +03:00
var height = window.innerHeight - $('#header').outerHeight() - $('#footer').outerHeight()
- $('#calendar-toolbar').outerHeight() - $('.calendar-header').outerHeight() - 50;
if (height < 500) {
height = 500;
}
// $('.calendar-view').height(height);
$('.calendar-view > div').css('min-width', '1000%');
2018-01-23 12:08:37 +03:00
var width = 0;
2018-01-23 12:08:37 +03:00
$('.date-column').each(function (index, dateColumn) {
width += $(dateColumn).outerWidth();
});
2016-07-23 16:30:34 +03:00
$('.calendar-view > div').css('min-width', width + 200);
var dateColumnHeight = $('.date-column').outerHeight();
2018-01-23 12:08:37 +03:00
$('.calendar-view .not-working').outerHeight((dateColumnHeight > height ? dateColumnHeight : height) - 70);
}
/**
2018-01-23 12:08:37 +03:00
* Get the calendar events.
*
* @param {Date} startDate The start date of the selected period.
* @param {Date} endDate The end date of the selected period.
2018-01-23 12:08:37 +03:00
*
* @return {jQuery.jqXHR}
*/
function _getCalendarEvents(startDate, endDate) {
2018-01-23 12:08:37 +03:00
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_get_calendar_events';
var data = {
csrfToken: GlobalVariables.csrfToken,
startDate: startDate.toString('yyyy-MM-dd'),
endDate: endDate.toString('yyyy-MM-dd')
};
return $.ajax({
2018-01-23 12:08:37 +03:00
url: url,
data: data,
method: 'POST',
2018-01-23 12:08:37 +03:00
beforeSend: function () {
$('#loading').css('visibility', 'hidden');
2018-01-23 12:08:37 +03:00
},
complete: function () {
$('#loading').css('visibility', '');
}
2018-01-23 12:08:37 +03:00
});
}
/**
2018-01-23 12:08:37 +03:00
* Initialize Page
*/
2018-01-23 12:08:37 +03:00
exports.initialize = function () {
_createHeader();
_createView(Date.today(), Date.today().add({days: parseInt($('#select-filter-item').val() - 1)}));
_bindEventHandlers();
2018-01-23 12:08:37 +03:00
// Hide Google Calendar Sync buttons cause they can not be used within this view.
$('#enable-sync, #google-sync').hide();
// Auto-reload the results every one minute.
2018-01-23 12:08:37 +03:00
setInterval(function () {
$('#reload-appointments').trigger('click');
2018-01-23 12:08:37 +03:00
}, 20000);
};
})(window.BackendCalendarTableView);