easyappointments/assets/js/utils/general_functions.js

602 lines
18 KiB
JavaScript
Raw Normal View History

2015-07-20 22:41:24 +03:00
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
2015-10-05 01:31:06 +03:00
*
2015-07-20 22:41:24 +03:00
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
2015-10-05 01:31:06 +03:00
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
2015-07-20 22:41:24 +03:00
* @link http://easyappointments.org
* @since v1.0.0
* ---------------------------------------------------------------------------- */
window.GeneralFunctions = window.GeneralFunctions || {};
/**
* General Functions Module
*
* It contains functions that apply both on the front and back end of the application.
2015-10-05 01:31:06 +03:00
*
* @module GeneralFunctions
*/
2018-01-23 12:08:37 +03:00
(function (exports) {
'use strict';
2020-12-02 20:57:49 +03:00
/**
* Register global error handler.
*/
2020-12-09 15:17:45 +03:00
document.addEventListener('DOMContentLoaded', function () {
$(document).ajaxError(function (event, jqxhr, settings, thrownError) {
GeneralFunctions.ajaxFailureHandler(jqxhr, settings, thrownError);
});
2020-12-02 20:57:49 +03:00
});
/**
2016-10-10 19:29:48 +03:00
* This functions displays a message box in the admin array. It is useful when user
* decisions or verifications are needed.
2015-10-05 01:31:06 +03:00
*
* @param {String} title The title of the message box.
* @param {String} message The message of the dialog.
* @param {Array} buttons Contains the dialog buttons along with their functions.
*/
2021-12-10 11:06:53 +03:00
exports.displayMessageBox = function (title, message, buttons = null) {
if (!title) {
title = '- No Title Provided -';
2015-10-05 01:31:06 +03:00
}
if (!message) {
message = '- No Message Provided -';
2015-10-05 01:31:06 +03:00
}
if (!buttons) {
buttons = [
{
text: EALang.close,
click: function () {
$('#message-box').dialog('close');
}
}
];
}
// Destroy previous dialog instances.
$('#message-box').dialog('destroy').remove();
// Create the html of the message box.
$('<div/>', {
'id': 'message-box',
'title': title,
'html': [
$('<p/>', {
'html': message
})
]
}).appendTo('body');
$('#message-box').dialog({
autoOpen: false,
modal: true,
resize: 'auto',
width: 'auto',
height: 'auto',
resizable: false,
buttons: buttons,
2013-12-29 15:57:09 +02:00
closeOnEscape: true
});
$('#message-box').dialog('open');
$('.ui-dialog .ui-dialog-buttonset button').addClass('btn btn-outline-secondary');
$('#message-box .ui-dialog-titlebar-close').hide();
};
/**
* This method centers a DOM element vertically and horizontally on the page.
2015-10-05 01:31:06 +03:00
*
* @param {Object} elementHandle The object that is going to be centered.
*/
2018-01-23 12:08:37 +03:00
exports.centerElementOnPage = function (elementHandle) {
// Center main frame vertical middle
2018-01-23 12:08:37 +03:00
$(window).resize(function () {
var elementLeft = ($(window).width() - elementHandle.outerWidth()) / 2;
var elementTop = ($(window).height() - elementHandle.outerHeight()) / 2;
elementTop = elementTop > 0 ? elementTop : 20;
elementHandle.css({
position: 'absolute',
left: elementLeft,
top: elementTop
2015-10-05 01:31:06 +03:00
});
});
$(window).resize();
};
/**
2015-10-05 01:31:06 +03:00
* This function retrieves a parameter from a "GET" formed url.
*
* @link http://www.netlobo.com/url_query_string_javascript.html
2015-10-05 01:31:06 +03:00
*
* @param {String} url The selected url.
* @param {String} parameterName The parameter name.
* @return {String} Returns the parameter value.
*/
2018-01-23 12:08:37 +03:00
exports.getUrlParameter = function (url, parameterName) {
var parsedUrl = url.substr(url.indexOf('?')).slice(1).split('&');
for (var index in parsedUrl) {
2018-01-23 12:08:37 +03:00
var parsedValue = parsedUrl[index].split('=');
if (parsedValue.length === 1 && parsedValue[0] === parameterName) {
2018-01-23 12:08:37 +03:00
return '';
}
if (parsedValue.length === 2 && parsedValue[0] === parameterName) {
2018-01-23 12:08:37 +03:00
return decodeURIComponent(parsedValue[1]);
}
}
return '';
};
/**
* Convert date to ISO date string.
*
* This function creates a RFC 3339 date string. This string is needed by the Google Calendar API
* in order to pass dates as parameters.
2015-10-05 01:31:06 +03:00
*
* @param {Date} date The given date that will be transformed.
* @return {String} Returns the transformed string.
*/
2018-01-23 12:08:37 +03:00
exports.ISODateString = function (date) {
function pad(n) {
return n < 10 ? '0' + n : n;
}
return (
date.getUTCFullYear() +
'-' +
pad(date.getUTCMonth() + 1) +
'-' +
pad(date.getUTCDate()) +
'T' +
pad(date.getUTCHours()) +
':' +
pad(date.getUTCMinutes()) +
':' +
pad(date.getUTCSeconds()) +
'Z'
);
};
2015-10-05 01:31:06 +03:00
/**
* Clone JS Object
2015-10-05 01:31:06 +03:00
*
* This method creates and returns an exact copy of the provided object. It is very useful whenever changes need to
* be made to an object without modifying the original data.
2015-10-05 01:31:06 +03:00
*
* @link https://stackoverflow.com/a/728694
*
* @param {Object} originalObject Object to be copied.
* @return {Object} Returns an exact copy of the provided element.
*/
2018-01-23 12:08:37 +03:00
exports.clone = function (originalObject) {
// Handle the 3 simple types, and null or undefined
if (!originalObject || typeof originalObject !== 'object') {
return originalObject;
}
var copy;
// Handle Date
if (originalObject instanceof Date) {
copy = new Date();
copy.setTime(originalObject.getTime());
return copy;
}
// Handle Array
if (originalObject instanceof Array) {
copy = [];
for (var i = 0, len = originalObject.length; i < len; i++) {
copy[i] = GeneralFunctions.clone(originalObject[i]);
}
return copy;
}
// Handle Object
if (originalObject instanceof Object) {
copy = {};
for (var attr in originalObject) {
if (originalObject.hasOwnProperty(attr)) copy[attr] = GeneralFunctions.clone(originalObject[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
};
2015-10-05 01:31:06 +03:00
/**
* Validate Email Address
*
* This method validates an email address. If the address is not on the proper
* form then the result is FALSE.
2015-10-05 01:31:06 +03:00
*
* @link http://badsyntax.co/post/javascript-email-validation-rfc822
2015-10-11 23:30:18 +03:00
*
* @param {String} email The email address to be checked.
* @return {Boolean} Returns the validation result.
*/
exports.validateEmail = function (email) {
var re =
/(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/;
2015-10-11 23:30:18 +03:00
return re.test(email);
};
2015-10-05 01:31:06 +03:00
/**
* Makes the first letter of the string upper case.
2015-10-05 01:31:06 +03:00
*
* @param {String} value The string to be converted.
*
* @return {String} Returns the capitalized string.
*/
exports.upperCaseFirstLetter = function (value) {
return value.charAt(0).toUpperCase() + value.slice(1);
};
2015-10-05 01:31:06 +03:00
/**
* Enable Language Selection
*
* Enables the language selection functionality. Must be called on every page has a language selection button.
* This method requires the global variable 'availableLanguages' to be initialized before the execution.
2015-10-05 01:31:06 +03:00
*
* @param {Object} $element Selected element button for the language selection.
*/
2018-01-23 12:08:37 +03:00
exports.enableLanguageSelection = function ($element) {
// Select Language
var $languageList = $('<ul/>', {
'id': 'language-list',
'html': availableLanguages.map(function (availableLanguage) {
return $('<li/>', {
'class': 'language',
'data-language': availableLanguage,
'text': GeneralFunctions.upperCaseFirstLetter(availableLanguage)
});
})
});
2015-10-05 01:31:06 +03:00
$element.popover({
placement: 'top',
title: 'Select Language',
2020-06-18 21:32:37 +03:00
content: $languageList[0],
html: true,
container: 'body',
trigger: 'manual'
});
2015-10-05 01:31:06 +03:00
$element.on('click', function (event) {
event.stopPropagation();
2018-01-23 12:08:37 +03:00
if ($('#language-list').length === 0) {
$(this).popover('show');
} else {
$(this).popover('hide');
}
$(this).toggleClass('active');
});
2015-10-05 01:31:06 +03:00
2018-01-23 12:08:37 +03:00
$(document).on('click', 'li.language', function () {
// Change language with ajax call and refresh page.
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_change_language';
var data = {
2016-07-15 21:52:21 +03:00
csrfToken: GlobalVariables.csrfToken,
2018-01-23 12:08:37 +03:00
language: $(this).attr('data-language')
2016-07-15 21:52:21 +03:00
};
$.post(url, data).done(function () {
document.location.reload(true);
});
});
2020-12-09 15:17:45 +03:00
$(document).on('click', function () {
$element.popover('hide');
});
};
/**
* AJAX Failure Handler
*
* @param {jqXHR} jqXHR
* @param {String} textStatus
* @param {Object} errorThrown
*/
exports.ajaxFailureHandler = function (jqXHR, textStatus, errorThrown) {
console.error('Unexpected HTTP Error: ', jqXHR, textStatus, errorThrown);
var response;
try {
response = JSON.parse(jqXHR.responseText); // JSON response
2020-12-09 15:17:45 +03:00
} catch (error) {
response = {message: jqXHR.responseText}; // String response
}
if (!response) {
return;
}
GeneralFunctions.displayMessageBox(EALang.unexpected_issues, EALang.unexpected_issues_message);
$('<div/>', {
'class': 'card',
'html': [
$('<div/>', {
'class': 'card-body overflow-auto',
'html': response.message || '→ No error information provided.'
})
]
}).appendTo('#message-box');
};
/**
* Escape JS HTML string values for XSS prevention.
*
* @param {String} content String to be escaped.
*
* @return {String} Returns the escaped string.
*/
exports.escapeHtml = function (content) {
return $('<div/>').text(content).html();
};
/**
* Format a given date according to the date format setting.
*
* @param {String} date The date to be formatted.
* @param {String} dateFormatSetting The setting provided by PHP must be one of the "DMY", "MDY" or "YMD".
* @param {Boolean} addHours (optional) Whether to add hours to the result.
* @return {String} Returns the formatted date string.
*/
2018-01-23 12:08:37 +03:00
exports.formatDate = function (date, dateFormatSetting, addHours) {
var timeFormat = GlobalVariables.timeFormat === 'regular' ? 'h:mm a' : 'HH:mm';
var hours = addHours ? ' ' + timeFormat : '';
var result;
var parsedDateMoment = moment(date);
2020-12-12 16:11:33 +03:00
if (!parsedDateMoment.isValid()) {
2020-12-12 16:11:33 +03:00
return date;
}
2018-01-23 12:08:37 +03:00
switch (dateFormatSetting) {
case 'DMY':
result = parsedDateMoment.format('DD/MM/YYYY' + hours);
break;
case 'MDY':
result = parsedDateMoment.format('MM/DD/YYYY' + hours);
break;
case 'YMD':
result = parsedDateMoment.format('YYYY/MM/DD' + hours);
break;
default:
throw new Error('Invalid date format setting provided!', dateFormatSetting);
}
return result;
};
/**
* Get the Id of a Weekday using the US week format and day names (Sunday=0) as used in the JS code of the
* application, case insensitive, short and long names supported.
*
* @param {String} weekDayName The weekday name among Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
* Saturday.
* @return {Number} Returns the ID of the weekday.
*/
exports.getWeekDayId = function (weekDayName) {
var result;
switch (weekDayName.toLowerCase()) {
case 'sunday':
case 'sun':
result = 0;
break;
case 'monday':
case 'mon':
result = 1;
break;
case 'tuesday':
case 'tue':
result = 2;
break;
case 'wednesday':
case 'wed':
result = 3;
break;
case 'thursday':
case 'thu':
result = 4;
break;
case 'friday':
case 'fri':
result = 5;
break;
case 'saturday':
case 'sat':
result = 6;
break;
default:
throw new Error('Invalid weekday name provided!', weekDayName);
}
return result;
};
/**
* Get the name in lowercase of a Weekday using its Id.
*
* @param {Number} weekDayId The Id (From 0 for sunday to 6 for saturday).
* @return {String} Returns the name of the weekday.
*/
exports.getWeekdayName = function (weekDayId) {
var result;
switch (weekDayId) {
case 0:
result = 'sunday';
break;
case 1:
result = 'monday';
break;
case 2:
result = 'tuesday';
break;
case 3:
result = 'wednesday';
break;
case 4:
result = 'thursday';
break;
case 5:
result = 'friday';
break;
case 6:
result = 'saturday';
break;
default:
throw new Error('Invalid weekday Id provided!', weekDayId);
}
return result;
};
/**
* Sort a dictionary where keys are weekdays
*
* @param {Object} weekDictionary A dictionary with weekdays as keys.
* @param {Number} startDayId Id of the first day to start sorting (From 0 for sunday to 6 for saturday).
* @return {Object} Returns a sorted dictionary
*/
exports.sortWeekDictionary = function (weekDictionary, startDayId) {
2020-12-09 15:17:45 +03:00
var sortedWeekDictionary = {};
2020-12-09 15:17:45 +03:00
for (var i = startDayId; i < startDayId + 7; i++) {
var weekdayName = GeneralFunctions.getWeekdayName(i % 7);
sortedWeekDictionary[weekdayName] = weekDictionary[weekdayName];
}
return sortedWeekDictionary;
};
/**
* Render a map icon that links to Google maps.
*
* @param {Object} user Should have the address, city, etc properties.
*
* @return {string} The rendered HTML.
*/
exports.renderMapIcon = function (user) {
var data = [];
if (user.address) {
data.push(user.address);
}
if (user.city) {
data.push(user.city);
}
if (user.state) {
data.push(user.state);
}
if (user.zip_code) {
data.push(user.zip_code);
}
if (!data.length) {
return '';
}
return $('<div/>', {
'html': [
$('<a/>', {
'href': 'https://www.google.com/maps/place/' + data.join(','),
'target': '_blank',
'html': [
$('<span/>', {
2020-06-29 16:23:51 +03:00
'class': 'fas fa-map-marker-alt'
})
]
})
]
}).html();
};
/**
* Render a mail icon.
*
* @param {String} email
*
* @return {string} The rendered HTML.
*/
exports.renderMailIcon = function (email) {
return $('<div/>', {
'html': [
$('<a/>', {
'href': 'mailto:' + email,
'target': '_blank',
'html': [
$('<span/>', {
'class': 'fas fa-envelope'
})
]
})
]
}).html();
};
/**
* Render a phone icon.
*
* @param {String} phone
*
* @return {string} The rendered HTML.
*/
exports.renderPhoneIcon = function (phone) {
return $('<div/>', {
'html': [
$('<a/>', {
'href': 'tel:' + phone,
'target': '_blank',
'html': [
$('<span/>', {
2020-06-29 16:23:51 +03:00
'class': 'fas fa-phone-alt'
})
]
})
]
}).html();
};
})(window.GeneralFunctions);