/* ---------------------------------------------------------------------------- * Easy!Appointments - Open Source Web Scheduler * * @package EasyAppointments * @author A.Tselegidis * @copyright Copyright (c) 2013 - 2016, 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'; /** * General Functions Constants */ exports.EXCEPTIONS_TITLE = EALang['unexpected_issues']; exports.EXCEPTIONS_MESSAGE = EALang['unexpected_issues_message']; exports.WARNINGS_TITLE = EALang['unexpected_warnings']; exports.WARNINGS_MESSAGE = EALang['unexpected_warnings_message']; /** * This functions displays a message box in the admin array. It is usefull 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} messageButtons Contains the dialog buttons along with their functions. */ exports.displayMessageBox = function(title, message, messageButtons) { // Check arguments integrity. if (title == undefined || title == '') { title = ''; } if (message == undefined || message == '') { message = ''; } if (messageButtons == undefined) { messageButtons = {}; messageButtons[EALang['close']] = function() { $('#message_box').dialog('close'); }; } // Destroy previous dialog instances. $('#message_box').dialog('destroy'); $('#message_box').remove(); // Create the html of the message box. $('body').append( '
' + '

' + message + '

' + '
' ); $("#message_box").dialog({ autoOpen: false, modal: true, resize: 'auto', width: 'auto', height: 'auto', resizable: false, buttons: messageButtons, 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} name The parameter name. * @return {string} Returns the parameter value. */ exports.getUrlParameter = function(url, parameterName) { parameterName = parameterName.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]'); var regexS = '[\\#&]' + parameterName + '=([^&#]*)', regex = new RegExp(regexS), results = regex.exec(url); return (results == null) ? '' : results[1]; }; /** * 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} dt The given date that will be transformed. * @return {String} Returns the transformed string. */ exports.ISODateString = function(dt) { function pad(n) { return n<10 ? '0'+n : n; } return dt.getUTCFullYear()+'-' + pad(dt.getUTCMonth()+1)+'-' + pad(dt.getUTCDate())+'T' + pad(dt.getUTCHours())+':' + pad(dt.getUTCMinutes())+':' + pad(dt.getUTCSeconds())+'Z'; }; /** * This method creates and returns an exact copy of the provided object. * It is very usefull whenever changes need to be made to an object without * modyfing the original data. * * @link http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object * * @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 (null == originalObject || 'object' != typeof originalObject) return originalObject; // Handle Date if (originalObject instanceof Date) { var copy = new Date(); copy.setTime(originalObject.getTime()); return copy; } // Handle Array if (originalObject instanceof Array) { var 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) { var 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.'); }; /** * 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 {bool} 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); }; /** * 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; }; /** * This method parse the json encoded strings that are fetched by ajax calls. * * @param {array} exceptions Exception array returned by an ajax call. * @returns {array} Returns the parsed js objects. */ exports.parseExceptions = function(exceptions) { var parsedExceptions = new Array(); $.each(exceptions, function(index, exception) { parsedExceptions.push($.parseJSON(exception)); }); return parsedExceptions; }; /** * Makes the first letter of the string upper case. * * @param {string} str The string to be converted. * @returns {string} Returns the capitalized string. */ exports.ucaseFirstLetter = function(str){ return str.charAt(0).toUpperCase() + str.slice(1); }; /** * All backend js code has the same way of dislaying exceptions that are raised on the * server during an ajax call. * * @param {object} response Contains the server response. If exceptions or warnings are * found, user friendly messages are going to be displayed to the user.4 * * @return {bool} Returns whether the the ajax callback should continue the execution or * stop, due to critical server exceptions. */ exports.handleAjaxExceptions = function(response) { 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 false; } if (response.warnings) { response.warnings = GeneralFunctions.parseExceptions(response.warnings); GeneralFunctions.displayMessageBox(GeneralFunctions.WARNINGS_TITLE, GeneralFunctions.WARNINGS_MESSAGE); $('#message_box').append(GeneralFunctions.exceptionsToHtml(response.warnings)); } return true; }; /** * 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 html = '
    '; $.each(availableLanguages, function() { html += '
  • ' + GeneralFunctions.ucaseFirstLetter(this) + '
  • '; }); html += '
'; $element.popover({ 'placement': 'top', 'title': 'Select Language', 'content': html, 'html': true, 'container': 'body', 'trigger': 'manual' }); $element.click(function() { if ($('#language-list').length == 0) { $(this).popover('show'); } else { $(this).popover('hide'); } $(this).toggleClass('active'); }); $(document).on('click', 'li.language', function() { // Change language with ajax call and refresh page. var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_change_language'; var postData = { 'csrfToken': GlobalVariables.csrfToken, 'language': $(this).attr('data-language'), }; $.post(postUrl, postData, function(response) { if (!GeneralFunctions.handleAjaxExceptions(response)) return; document.location.reload(true); }, 'json').fail(GeneralFunctions.ajaxFailureHandler); }); }; /** * Use this method for common error handling between * * @param {object} jqxhr * @param {string} textStatus * @param {object} errorThrown */ exports.ajaxFailureHandler = function(jqxhr, textStatus, errorThrown) { var exceptions = [ { message: 'AJAX Error: ' + errorThrown } ]; GeneralFunctions.displayMessageBox(GeneralFunctions.EXCEPTIONS_TITLE, GeneralFunctions.EXCEPTIONS_MESSAGE); $('#message_box').append(GeneralFunctions.exceptionsToHtml(exceptions)); }; /** * Escape JS HTML string values for XSS prevention. * * @param {string} str String to be escaped. * @returns {string} Returns the escaped string. */ exports.escapeHtml = function(str) { return $('
').text(str).html(); }; /** * Format a given date according to the date format setting. * * @param {Date} date The date to be formatted. * @param {string} dateFormatSetting The setting provided by PHP must be one of * the "DMY", "MDY" or "YMD". * @param {bool} addHours (optional) Whether to add hours to the result. * @return {string} Returns the formatted date string. */ exports.formatDate = function(date, dateFormatSetting, addHours) { var format, result, hours = addHours ? ' HH:mm' : ''; switch(dateFormatSetting) { case 'DMY': result = Date.parse(date).toString('dd/MM/yyyy' + hours); break; case 'MDY': result = Date.parse(date).toString('MM/dd/yyyy' + hours); break; case 'YMD': result = Date.parse(date).toString('yyyy/MM/dd' + hours); break; default: throw new Error('Invalid date format setting provided!', dateFormatSetting); } return result; }; })(window.GeneralFunctions);