/* ---------------------------------------------------------------------------- * Easy!Appointments - Open Source Web Scheduler * * @package EasyAppointments * @author A.Tselegidis * @copyright Copyright (c) 2013 - 2020, Alex Tselegidis * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 * @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. * * @module GeneralFunctions */ (function (exports) { 'use strict'; /** * This functions displays a message box in the admin array. It is useful when user * decisions or verifications are needed. * * @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. */ exports.displayMessageBox = function (title, message, buttons) { if (!title) { title = '- No Title Provided -'; } if (!message) { message = '- No Message Provided -'; } 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. $('
', { 'id': 'message_box', 'title': title, 'html': [ $('

', { 'html': message }) ] }) .appendTo('body'); $("#message_box").dialog({ autoOpen: false, modal: true, resize: 'auto', width: 'auto', height: 'auto', resizable: false, buttons: buttons, closeOnEscape: true }); $('#message_box').dialog('open'); $('.ui-dialog .ui-dialog-buttonset button').addClass('btn btn-default'); $('#message_box .ui-dialog-titlebar-close').hide(); }; /** * This method centers a DOM element vertically and horizontally on the page. * * @param {Object} elementHandle The object that is going to be centered. */ exports.centerElementOnPage = function (elementHandle) { // Center main frame vertical middle $(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 }); }); $(window).resize(); }; /** * This function retrieves a parameter from a "GET" formed url. * * @link http://www.netlobo.com/url_query_string_javascript.html * * @param {String} url The selected url. * @param {String} parameterName The parameter name. * @return {String} Returns the parameter value. */ exports.getUrlParameter = function (url, parameterName) { var parsedUrl = url.substr(url.indexOf('?')).slice(1).split('&'); for (var index in parsedUrl) { var parsedValue = parsedUrl[index].split('='); if (parsedValue.length === 1 && parsedValue[0] === parameterName) { return ''; } if (parsedValue.length === 2 && parsedValue[0] === parameterName) { 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. * * @param {Date} date The given date that will be transformed. * @return {String} Returns the transformed string. */ 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'; }; /** * Clone JS Object * * 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. * * @link https://stackoverflow.com/a/728694 * * @param {Object} originalObject Object to be copied. * @return {Object} Returns an exact copy of the provided element. */ 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.'); }; /** * Validate Email Address * * This method validates an email address. If the address is not on the proper * form then the result is FALSE. * * @link http://badsyntax.co/post/javascript-email-validation-rfc822 * * @param {String} email The email address to be checked. * @return {Boolean} Returns the validation result. */ exports.validateEmail = function (email) { var re = /^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/; return re.test(email); }; /** * Convert AJAX exceptions to HTML. * * This method returns the exception HTML display for javascript ajax calls. It uses the Bootstrap collapse * module to show exception messages when the user opens the "Details" collapse component. * * @param {Array} exceptions Contains the exceptions to be displayed. * * @return {String} Returns the html markup for the exceptions. */ exports.exceptionsToHtml = function (exceptions) { var html = '

' + '
' + '
' + '' + '
' + '
'; $.each(exceptions, function (index, exception) { html += '
' + '
' + '
' + exception.message + '
' + '
' + '
'; }); html += '
'; return html; }; /** * Makes the first letter of the string upper case. * * @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); }; /** * 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. * * @param {Object} $element Selected element button for the language selection. */ exports.enableLanguageSelection = function ($element) { // Select Language var $languageList = $('