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>
|
2016-01-02 15:47:04 +02:00
|
|
|
* @copyright Copyright (c) 2013 - 2016, 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
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2016-04-24 20:09:33 +03:00
|
|
|
window.GeneralFunctions = window.GeneralFunctions || {};
|
|
|
|
|
2013-05-08 17:31:17 +03:00
|
|
|
/**
|
2016-04-24 20:09:33 +03:00
|
|
|
* 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
|
|
|
*
|
2016-04-24 20:09:33 +03:00
|
|
|
* @module GeneralFunctions
|
2013-05-08 17:31:17 +03:00
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
(function(exports) {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2013-09-23 18:42:36 +03:00
|
|
|
/**
|
|
|
|
* General Functions Constants
|
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
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'];
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-06-13 19:25:34 +03:00
|
|
|
/**
|
2016-04-24 20:09:33 +03:00
|
|
|
* This functions displays a message box in the admin array. It is usefull when user
|
2013-06-13 19:25:34 +03:00
|
|
|
* decisions or verifications are needed.
|
2015-10-05 01:31:06 +03:00
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @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.
|
2013-06-13 19:25:34 +03:00
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
exports.displayMessageBox = function(title, message, messageButtons) {
|
2013-06-13 19:25:34 +03:00
|
|
|
// Check arguments integrity.
|
2015-04-26 17:20:16 +03:00
|
|
|
if (title == undefined || title == '') {
|
|
|
|
title = '<No Title Given>';
|
2015-10-05 01:31:06 +03:00
|
|
|
}
|
2013-05-08 17:31:17 +03:00
|
|
|
|
2015-04-26 17:20:16 +03:00
|
|
|
if (message == undefined || message == '') {
|
|
|
|
message = '<No Message Given>';
|
2015-10-05 01:31:06 +03:00
|
|
|
}
|
2013-05-08 17:31:17 +03:00
|
|
|
|
2013-06-13 19:25:34 +03:00
|
|
|
if (messageButtons == undefined) {
|
2013-12-19 18:28:19 +02:00
|
|
|
messageButtons = {};
|
2013-12-20 19:44:44 +02:00
|
|
|
messageButtons[EALang['close']] = function() {
|
2015-04-26 17:20:16 +03:00
|
|
|
$('#message_box').dialog('close');
|
2013-06-13 19:25:34 +03:00
|
|
|
};
|
|
|
|
}
|
2013-05-08 17:31:17 +03:00
|
|
|
|
2013-06-13 19:25:34 +03:00
|
|
|
// Destroy previous dialog instances.
|
2015-04-26 17:20:16 +03:00
|
|
|
$('#message_box').dialog('destroy');
|
|
|
|
$('#message_box').remove();
|
2013-06-03 17:42:19 +03:00
|
|
|
|
2013-06-13 19:25:34 +03:00
|
|
|
// Create the html of the message box.
|
2015-04-26 17:20:16 +03:00
|
|
|
$('body').append(
|
|
|
|
'<div id="message_box" title="' + title + '">' +
|
|
|
|
'<p>' + message + '</p>' +
|
|
|
|
'</div>'
|
2015-10-05 01:31:06 +03:00
|
|
|
);
|
2013-06-10 18:51:23 +03:00
|
|
|
|
2015-04-26 17:20:16 +03:00
|
|
|
$("#message_box").dialog({
|
2013-07-06 03:00:04 +03:00
|
|
|
autoOpen: false,
|
|
|
|
modal: true,
|
2015-04-26 17:20:16 +03:00
|
|
|
resize: 'auto',
|
|
|
|
width: 'auto',
|
|
|
|
height: 'auto',
|
2013-07-06 03:00:04 +03:00
|
|
|
resizable: false,
|
|
|
|
buttons: messageButtons,
|
2013-12-29 15:57:09 +02:00
|
|
|
closeOnEscape: true
|
2013-06-13 19:25:34 +03:00
|
|
|
});
|
|
|
|
|
2015-10-05 01:31:06 +03:00
|
|
|
$('#message_box').dialog('open');
|
|
|
|
$('.ui-dialog .ui-dialog-buttonset button').addClass('btn btn-default');
|
2015-04-26 17:20:16 +03:00
|
|
|
$('#message_box .ui-dialog-titlebar-close').hide();
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
2013-06-13 19:25:34 +03:00
|
|
|
|
|
|
|
/**
|
2016-04-24 20:09:33 +03:00
|
|
|
* This method centers a DOM element vertically and horizontally on the page.
|
2015-10-05 01:31:06 +03:00
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @param {Object} elementHandle The object that is going to be centered.
|
2013-06-13 19:25:34 +03:00
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
exports.centerElementOnPage = function(elementHandle) {
|
2013-06-13 19:25:34 +03:00
|
|
|
// 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({
|
2013-07-06 03:00:04 +03:00
|
|
|
position: 'absolute',
|
|
|
|
left: elementLeft,
|
|
|
|
top: elementTop
|
2015-10-05 01:31:06 +03:00
|
|
|
});
|
2013-06-13 19:25:34 +03:00
|
|
|
});
|
|
|
|
$(window).resize();
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
2013-06-13 19:25:34 +03:00
|
|
|
|
|
|
|
/**
|
2015-10-05 01:31:06 +03:00
|
|
|
* This function retrieves a parameter from a "GET" formed url.
|
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* {@link http://www.netlobo.com/url_query_string_javascript.html}
|
2015-10-05 01:31:06 +03:00
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @param {String} url The selected url.
|
|
|
|
* @param {String} name The parameter name.
|
2016-04-24 20:09:33 +03:00
|
|
|
|
2016-05-14 13:25:20 +03:00
|
|
|
* @return {String} Returns the parameter value.
|
2013-06-13 19:25:34 +03:00
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
exports.getUrlParameter = function(url, parameterName) {
|
2015-12-02 00:25:59 +02:00
|
|
|
parameterName = parameterName.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]');
|
2016-07-15 21:52:21 +03:00
|
|
|
var regexS = '[\\#&]' + parameterName + '=([^&#]*)';
|
|
|
|
var regex = new RegExp(regexS);
|
|
|
|
var results = regex.exec(url);
|
2015-12-02 00:25:59 +02:00
|
|
|
return (results == null) ? '' : results[1];
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
2013-06-13 19:25:34 +03:00
|
|
|
|
|
|
|
/**
|
2016-05-14 13:25:20 +03:00
|
|
|
* Convert date to ISO date string.
|
|
|
|
*
|
2016-04-24 20:09:33 +03:00
|
|
|
* This function creates a RFC 3339 date string. This string is needed by the Google Calendar API
|
2016-05-14 13:25:20 +03:00
|
|
|
* in order to pass dates as parameters.
|
2015-10-05 01:31:06 +03:00
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @param {Date} date The given date that will be transformed.
|
2016-04-24 20:09:33 +03:00
|
|
|
|
|
|
|
* @return {String} Returns the transformed string.
|
2013-06-13 19:25:34 +03:00
|
|
|
*/
|
2016-05-14 13:25:20 +03:00
|
|
|
exports.ISODateString = function(date) {
|
2015-12-02 00:25:59 +02:00
|
|
|
function pad(n) {
|
2016-05-14 13:25:20 +03:00
|
|
|
return n < 10 ? '0' + n : n;
|
2015-12-02 00:25:59 +02:00
|
|
|
}
|
2013-06-13 19:25:34 +03:00
|
|
|
|
2016-05-14 13:25:20 +03:00
|
|
|
return date.getUTCFullYear()+'-'
|
|
|
|
+ pad(date.getUTCMonth()+1)+'-'
|
|
|
|
+ pad(date.getUTCDate())+'T'
|
|
|
|
+ pad(date.getUTCHours())+':'
|
|
|
|
+ pad(date.getUTCMinutes())+':'
|
|
|
|
+ pad(date.getUTCSeconds())+'Z';
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-06-18 19:06:34 +03:00
|
|
|
/**
|
2016-05-14 13:25:20 +03:00
|
|
|
* Clone JS Object
|
2015-10-05 01:31:06 +03:00
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* 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.
|
2015-10-05 01:31:06 +03:00
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* {@link http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object}
|
|
|
|
*
|
|
|
|
* @param {Object} originalObject Object to be copied.
|
2016-04-24 20:09:33 +03:00
|
|
|
|
2016-05-14 13:25:20 +03:00
|
|
|
* @return {Object} Returns an exact copy of the provided element.
|
2013-06-18 19:06:34 +03:00
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
exports.clone = function(originalObject) {
|
2013-06-18 19:06:34 +03:00
|
|
|
// Handle the 3 simple types, and null or undefined
|
2015-10-05 01:31:06 +03:00
|
|
|
if (null == originalObject || 'object' != typeof originalObject)
|
2013-06-18 19:06:34 +03:00
|
|
|
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) {
|
2015-10-05 01:31:06 +03:00
|
|
|
if (originalObject.hasOwnProperty(attr))
|
2013-06-18 19:06:34 +03:00
|
|
|
copy[attr] = GeneralFunctions.clone(originalObject[attr]);
|
|
|
|
}
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2015-04-26 17:20:16 +03:00
|
|
|
throw new Error('Unable to copy obj! Its type isn\'t supported.');
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-06-29 00:54:12 +03:00
|
|
|
/**
|
2016-05-14 13:25:20 +03:00
|
|
|
* Validate Email Address
|
|
|
|
*
|
2013-06-29 00:54:12 +03:00
|
|
|
* 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
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* {@link http://badsyntax.co/post/javascript-email-validation-rfc822}
|
2015-10-11 23:30:18 +03:00
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @param {String} email The email address to be checked.
|
2016-04-24 20:09:33 +03:00
|
|
|
|
2016-05-14 13:25:20 +03:00
|
|
|
* @return {Boolean} Returns the validation result.
|
2013-06-29 00:54:12 +03:00
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
exports.validateEmail = function (email) {
|
2016-02-15 23:19:04 +02:00
|
|
|
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))*$/;
|
2015-10-11 23:30:18 +03:00
|
|
|
return re.test(email);
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-07-02 20:18:19 +03:00
|
|
|
/**
|
2016-05-14 13:25:20 +03:00
|
|
|
* 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.
|
2015-10-05 01:31:06 +03:00
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @param {Array} exceptions Contains the exceptions to be displayed.
|
2016-04-24 20:09:33 +03:00
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @return {String} Returns the html markup for the exceptions.
|
2013-07-02 20:18:19 +03:00
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
exports.exceptionsToHtml = function(exceptions) {
|
2015-10-05 01:31:06 +03:00
|
|
|
var html =
|
|
|
|
'<div class="accordion" id="error-accordion">' +
|
|
|
|
'<div class="accordion-group">' +
|
2013-07-02 20:18:19 +03:00
|
|
|
'<div class="accordion-heading">' +
|
|
|
|
'<a class="accordion-toggle" data-toggle="collapse" ' +
|
2015-10-05 01:31:06 +03:00
|
|
|
'data-parent="#error-accordion" href="#error-technical">' +
|
|
|
|
EALang['details'] +
|
2013-07-02 20:18:19 +03:00
|
|
|
'</a>' +
|
|
|
|
'</div>';
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-07-02 20:18:19 +03:00
|
|
|
$.each(exceptions, function(index, exception) {
|
|
|
|
html +=
|
|
|
|
'<div id="error-technical" class="accordion-body collapse">' +
|
2015-10-05 01:31:06 +03:00
|
|
|
'<div class="accordion-inner">' +
|
2013-07-02 20:18:19 +03:00
|
|
|
'<pre>' + exception['message'] + '</pre>' +
|
|
|
|
'</div>' +
|
|
|
|
'</div>';
|
|
|
|
});
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-07-02 20:18:19 +03:00
|
|
|
html += '</div></div>';
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-07-02 20:18:19 +03:00
|
|
|
return html;
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-07-02 20:18:19 +03:00
|
|
|
/**
|
2016-05-14 13:25:20 +03:00
|
|
|
* Parse AJAX Exceptions
|
|
|
|
*
|
|
|
|
* This method parse the JSON encoded strings that are fetched by AJAX calls.
|
2015-10-05 01:31:06 +03:00
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @param {Array} exceptions Exception array returned by an ajax call.
|
|
|
|
*
|
|
|
|
* @return {Array} Returns the parsed js objects.
|
2013-07-02 20:18:19 +03:00
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
exports.parseExceptions = function(exceptions) {
|
2013-07-02 20:18:19 +03:00
|
|
|
var parsedExceptions = new Array();
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-07-02 20:18:19 +03:00
|
|
|
$.each(exceptions, function(index, exception) {
|
2015-04-26 17:20:16 +03:00
|
|
|
parsedExceptions.push($.parseJSON(exception));
|
2013-07-02 20:18:19 +03:00
|
|
|
});
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-07-02 20:18:19 +03:00
|
|
|
return parsedExceptions;
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-09-18 19:36:29 +03:00
|
|
|
/**
|
|
|
|
* Makes the first letter of the string upper case.
|
2015-10-05 01:31:06 +03:00
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @param {String} str The string to be converted.
|
|
|
|
*
|
|
|
|
* @return {String} Returns the capitalized string.
|
2013-09-18 19:36:29 +03:00
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
exports.ucaseFirstLetter = function(str){
|
2013-09-18 19:36:29 +03:00
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-09-23 18:42:36 +03:00
|
|
|
/**
|
2016-05-14 13:25:20 +03:00
|
|
|
* Handle AJAX Exceptions Callback
|
|
|
|
*
|
2013-09-23 18:42:36 +03:00
|
|
|
* All backend js code has the same way of dislaying exceptions that are raised on the
|
2015-10-05 01:31:06 +03:00
|
|
|
* server during an ajax call.
|
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @param {Object} response Contains the server response. If exceptions or warnings are
|
2016-04-24 20:09:33 +03:00
|
|
|
* found, user friendly messages are going to be displayed to the user.4
|
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @return {Boolean} Returns whether the the ajax callback should continue the execution or
|
2013-09-23 18:42:36 +03:00
|
|
|
* stop, due to critical server exceptions.
|
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
exports.handleAjaxExceptions = function(response) {
|
2013-09-23 18:42:36 +03:00
|
|
|
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));
|
|
|
|
}
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-09-23 18:42:36 +03:00
|
|
|
return true;
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-12-23 18:55:42 +02:00
|
|
|
/**
|
2016-05-14 13:25:20 +03:00
|
|
|
* Enable Language Selection
|
|
|
|
*
|
2013-12-23 18:55:42 +02:00
|
|
|
* 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
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @param {Object} $element Selected element button for the language selection.
|
2013-12-23 18:55:42 +02:00
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
exports.enableLanguageSelection = function($element) {
|
2013-12-23 18:55:42 +02:00
|
|
|
// Select Language
|
|
|
|
var html = '<ul id="language-list">';
|
|
|
|
$.each(availableLanguages, function() {
|
2015-10-05 01:31:06 +03:00
|
|
|
html += '<li class="language" data-language="' + this + '">'
|
2013-12-23 18:55:42 +02:00
|
|
|
+ GeneralFunctions.ucaseFirstLetter(this) + '</li>';
|
|
|
|
});
|
|
|
|
html += '</ul>';
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-12-23 18:55:42 +02:00
|
|
|
$element.popover({
|
2016-05-14 13:25:20 +03:00
|
|
|
placement: 'top',
|
|
|
|
title: 'Select Language',
|
|
|
|
content: html,
|
|
|
|
html: true,
|
|
|
|
container: 'body',
|
|
|
|
trigger: 'manual'
|
2013-12-23 18:55:42 +02:00
|
|
|
});
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-12-23 18:55:42 +02:00
|
|
|
$element.click(function() {
|
2016-05-14 13:25:20 +03:00
|
|
|
if ($('#language-list').length === 0) {
|
2013-12-23 18:55:42 +02:00
|
|
|
$(this).popover('show');
|
|
|
|
} else {
|
|
|
|
$(this).popover('hide');
|
|
|
|
}
|
2015-10-06 01:20:55 +03:00
|
|
|
|
|
|
|
$(this).toggleClass('active');
|
2013-12-23 18:55:42 +02:00
|
|
|
});
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2013-12-23 18:55:42 +02:00
|
|
|
$(document).on('click', 'li.language', function() {
|
|
|
|
// Change language with ajax call and refresh page.
|
2016-07-15 21:52:21 +03:00
|
|
|
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_change_language';
|
|
|
|
var postData = {
|
|
|
|
csrfToken: GlobalVariables.csrfToken,
|
|
|
|
language: $(this).attr('data-language'),
|
|
|
|
};
|
2013-12-23 18:55:42 +02:00
|
|
|
$.post(postUrl, postData, function(response) {
|
2016-05-14 13:25:20 +03:00
|
|
|
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-23 18:55:42 +02:00
|
|
|
document.location.reload(true);
|
2015-10-05 01:31:06 +03:00
|
|
|
|
2015-10-09 00:12:59 +03:00
|
|
|
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
2013-12-23 18:55:42 +02:00
|
|
|
});
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
2015-10-09 00:12:59 +03:00
|
|
|
|
|
|
|
/**
|
2016-05-14 13:25:20 +03:00
|
|
|
* AJAX Failure Handler
|
2015-10-09 00:12:59 +03:00
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @param {jqXHR} jqxhr
|
|
|
|
* @param {String} textStatus
|
|
|
|
* @param {Object} errorThrown
|
2015-10-09 00:12:59 +03:00
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
exports.ajaxFailureHandler = function(jqxhr, textStatus, errorThrown) {
|
2015-10-09 00:12:59 +03:00
|
|
|
var exceptions = [
|
|
|
|
{
|
2016-01-09 23:55:07 +02:00
|
|
|
message: 'AJAX Error: ' + errorThrown
|
2015-10-09 00:12:59 +03:00
|
|
|
}
|
|
|
|
];
|
2016-05-14 13:25:20 +03:00
|
|
|
GeneralFunctions.displayMessageBox(GeneralFunctions.EXCEPTIONS_TITLE, GeneralFunctions.EXCEPTIONS_MESSAGE);
|
2015-10-09 00:12:59 +03:00
|
|
|
$('#message_box').append(GeneralFunctions.exceptionsToHtml(exceptions));
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
2015-11-28 13:55:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape JS HTML string values for XSS prevention.
|
|
|
|
*
|
2016-05-14 13:25:20 +03:00
|
|
|
* @param {String} str String to be escaped.
|
|
|
|
*
|
|
|
|
* @return {String} Returns the escaped string.
|
2015-11-28 13:55:03 +02:00
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
exports.escapeHtml = function(str) {
|
2015-11-28 13:55:03 +02:00
|
|
|
return $('<div/>').text(str).html();
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
2015-12-02 00:25:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a given date according to the date format setting.
|
|
|
|
*
|
2015-12-31 00:02:07 +02:00
|
|
|
* @param {Date} date The date to be formatted.
|
2016-05-14 13:25:20 +03:00
|
|
|
* @param {String} dateFormatSetting The setting provided by PHP must be one of
|
2015-12-02 00:25:59 +02:00
|
|
|
* the "DMY", "MDY" or "YMD".
|
2016-05-14 13:25:20 +03:00
|
|
|
* @param {Boolean} addHours (optional) Whether to add hours to the result.
|
2016-04-24 20:09:33 +03:00
|
|
|
|
2016-05-14 13:25:20 +03:00
|
|
|
* @return {String} Returns the formatted date string.
|
2015-12-02 00:25:59 +02:00
|
|
|
*/
|
2016-04-24 20:09:33 +03:00
|
|
|
exports.formatDate = function(date, dateFormatSetting, addHours) {
|
2016-07-15 21:52:21 +03:00
|
|
|
var format, result;
|
|
|
|
var hours = addHours ? ' HH:mm' : '';
|
2015-12-02 00:25:59 +02:00
|
|
|
|
|
|
|
switch(dateFormatSetting) {
|
|
|
|
case 'DMY':
|
2015-12-11 01:04:40 +02:00
|
|
|
result = Date.parse(date).toString('dd/MM/yyyy' + hours);
|
2015-12-02 00:25:59 +02:00
|
|
|
break;
|
|
|
|
case 'MDY':
|
2015-12-11 01:04:40 +02:00
|
|
|
result = Date.parse(date).toString('MM/dd/yyyy' + hours);
|
2015-12-02 00:25:59 +02:00
|
|
|
break;
|
|
|
|
case 'YMD':
|
2015-12-11 01:04:40 +02:00
|
|
|
result = Date.parse(date).toString('yyyy/MM/dd' + hours);
|
2015-12-02 00:25:59 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('Invalid date format setting provided!', dateFormatSetting);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2016-04-24 20:09:33 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
})(window.GeneralFunctions);
|