//init
var jData = {};
var jVars = {};
var typeAheadFunction = Array();
jData.returnFunction = {};
/**
* @deprecated Use $jData.cfunc.preciseRound
* @param num
* @param dec
* @returns {*}
*/
function precise_round(num, dec) {
if ((typeof num !== 'number') || (typeof dec !== 'number'))
return false;
var num_sign = num >= 0 ? 1 : -1;
return (Math.round((num * Math.pow(10, dec)) + (num_sign * 0.0001)) / Math.pow(10, dec)).toFixed(dec);
}
/**
*
* @type {{mergeObjects: Function, addMultiselect: Function}}
*/
jData.cfunc = jData.jfunc = {
mergeObjects: function (object1, object2) {
for (var prop in object2) {
object1[prop] = object2[prop];
}
return object1;
},
addMultiselect: function (id, filtering, includeSelectAllOption) {
if (typeof filtering == 'undefined') filtering = true;
if (typeof includeSelectAllOption == 'undefined') includeSelectAllOption = true;
$("#" + id).multiselect({
maxHeight: 200,
includeSelectAllOption: includeSelectAllOption,
enableFiltering: filtering,
enableCaseInsensitiveFiltering: filtering,
selectAllText: getClng('selectall'),
filterPlaceholder: getClng('searches'),
nonSelectedText: getClng('nothingselected'),
nSelectedText: getClng('selected'),
allSelectedText: getClng('allselected'),
templates: {
filter: '
',
filterClearBtn: '',
}
});
},
findPropertyWithValue: function (obj, val) {
for (var i in obj) {
if (parseInt(obj[i], 10) === parseInt(val, 10)) {
return i;
}
}
return null;
},
preciseRound: function (num, dec) {
if ((typeof num !== 'number') || (typeof dec !== 'number'))
return false;
var num_sign = num >= 0 ? 1 : -1;
return (Math.round((num * Math.pow(10, dec)) + (num_sign * 0.0001)) / Math.pow(10, dec)).toFixed(dec);
},
makeSafeForCSS: function (name) {
return name.replace(/[^a-z0-9]/g, function (s) {
var c = s.charCodeAt(0);
if (c == 32) return '-';
if (c >= 65 && c <= 90) return '_' + s.toLowerCase();
return '__' + ('000' + c.toString(16)).slice(-4);
});
},
parseBool: function (value) {
if (typeof value === "boolean") return value;
if (typeof value === "number") {
return value === 1 ? true : value === 0 ? false : undefined;
}
if (typeof value != "string") return undefined;
return value.toLowerCase() === 'true' ? true : false;
},
stripHtml: function (html) {
// Create a new div element
var temporalDivElement = document.createElement("div");
// Set the HTML content with the providen
temporalDivElement.innerHTML = html;
// Retrieve the text property of the element (cross-browser support)
return temporalDivElement.textContent || temporalDivElement.innerText || "";
}
}
function loadScript(url, callback) {
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState) { //IE
script.onreadystatechange = function () {
if (script.readyState == "loaded" ||
script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};
} else { //Others
script.onload = function () {
callback();
};
}
script.src = url;
document.getElementsByTagName("head")[0].appendChild(script);
}
//Language class
var clng = Array();
var lng = Array();
$(document).ready(function () {
jData.clng.addModule("default");
});
jData.lng = {
modules: [],
loadedModules: [],
lngResult: [],
lastLoaded: false,
queue: true,
addModule: function (module, appId) {
if (module == undefined) module = "";
if (appId == undefined) appId = 1;
// This is a fix to make sure loadAjax actually loads module
this.lastLoaded = false;
var moduleList = module.split(',');
for (var i = 0, l = moduleList.length; i < l; i++) {
var key = moduleList[i] + "-" + appId;
this.modules[this.modules.length] = key;
}
//console.log(this.modules);
},
load: function (modules, appId) {
if (appId == undefined) appId = 1;
this.addModule(modules, appId);
this.lastLoaded = false;
this.loadAjax();
/*jQuery.ajax({
url: dataApps[1] + 'loadlngJSON.php?lng=' + currentLng + '&appId=' + appId + '&modules='+modules,
dataType: 'json',
success: function (result) {
jData.cfunc.mergeObjects(clng, result);
},
async: false
});*/
},
loadAjax: function () {
if (this.lastLoaded == false) {
this.lastLoaded = true;
//console.log("try load ajax");
var modulesToLoad = [];
for (var i = 0, l = this.modules.length; i < l; i++) {
//console.log("try " + this.modules[i]);
if ($.inArray(this.modules[i], this.loadedModules) !== -1) {
//console.log("already loaded "+this.modules[i]);
} else {
//console.log("load "+this.modules[i]);
this.loadedModules[this.loadedModules.length] = this.modules[i];
modulesToLoad[modulesToLoad.length] = this.modules[i];
}
}
if (modulesToLoad.length > 0) {
jQuery.ajax({
url: dataApps[1] + 'loadlngJSON.php?lng=' + currentLng + '&modules=' + modulesToLoad.join(","),
dataType: 'json',
success: function (result) {
jData.cfunc.mergeObjects(lng, result);
},
async: false
});
}
}
},
show: function (key) {
this.loadAjax();
var args = Array.prototype.slice.call(arguments);
var replaceArray = Array();
for (i = 1; i < args.length; i++) {
//console.log(i+" "+args[i]);
replaceArray[i] = args[i];
}
if (this.lngResult == undefined) this.lngResult = Array();
if (lng[key] == undefined) {
// console.log("lng " + key + " not found");
this.lngResult[key] = key;
} else {
this.lngResult[key] = lng[key];
for (i = 1; i < replaceArray.length; i++) {
//console.log('{a:'+i+'} '+replaceArray[i]);
this.lngResult[key] = this.lngResult[key].replace('{a:' + i + '}', replaceArray[i]);
}
}
return this.lngResult[key];
}
};
jData.clng = jData.lng;
//URLbuilder
jData.urlB = {
createUrl: function (urlStr) {
var urlArray = urlStr.split("/");
var get = false;
var extraUrl = "?p";
var first = true;
var i = 0;
for (i = 0; i < urlArray.length; ++i) {
var urlItem = urlArray[i];
if (get == true) {
if (extraUrl == "") extraUrl += "?"; else extraUrl += "/";
extraUrl += urlItem;
get = false;
} else {
if (first) {
extraUrl += "=" + urlItem;
get = true;
first = false;
} else {
extraUrl += "/" + urlItem;
get = true;
}
}
}
if (extraUrl.indexOf("/") == -1) {
extraUrl += "/";
}
return extraUrl;
},
index: function (urlStr, appID) {
var extraUrl = this.createUrl(urlStr);
return dataApps[appID] + "index.php" + extraUrl;
},
extern: function (urlStr, appID) {
var extraUrl = this.createUrl(urlStr);
return dataApps[appID] + "extern.php" + extraUrl;
},
url: function (page, urlStr, appID) {
var extraUrl = this.createUrl(urlStr);
return dataApps[appID] + page + ".php" + extraUrl;
},
indexC: function (urlStr) {
var extraUrl = this.createUrl(urlStr);
return "index.php" + extraUrl;
},
system: function (urlStr, appID) {
return dataAppsExtra[appID]['systemUrl'] + urlStr;
},
pub: function (urlStr, appID) {
return dataAppsExtra[appID]['publicUrl'] + urlStr;
}
};
jData.box = {
confirmBasic: function (text, url) {
if (text == undefined || text == "") text = jData.clng.show("areyousure");
var args = Array.prototype.slice.call(arguments);
var replaceArray = Array();
bootbox.confirm(text, function (result) {
if (result) {
//disable confirmbox
jData.box.disableConfirmBtn();
//actions
var checkFunction = url.substring(0, 9);
if (checkFunction == "function:") {
var functionName = url.substring(9);
if (args.length == 3) {
replaceArray = args[2];
} else {
for (i = 0; i < args.length; i++) {
//console.log(i+" "+args[i]);
if (args[i + 2] != null) replaceArray[i] = args[i + 2];
}
}
window[functionName](replaceArray);
} else {
location.href = url;
}
}
});
},
confirm: function (action, what, url) {
var actionTxt = jData.clng.show("ayst_" + action, what);
var args = Array.prototype.slice.call(arguments);
var replaceArray = Array();
bootbox.confirm(jData.clng.show("areyousureto", actionTxt), function (result) {
if (result) {
//disable confirmbox
jData.box.disableConfirmBtn();
//actions
var checkFunction = url.substring(0, 9);
if (checkFunction == "function:") {
var functionName = url.substring(9);
for (i = 0; i < args.length; i++) {
//console.log(i+" "+args[i]);
if (args[i + 3] != null) replaceArray[i] = args[i + 3];
}
window[functionName](replaceArray);
} else {
location.href = url;
}
}
});
},
confirmBasicFunction: function (text, functionData) {
bootbox.confirm(text, functionData);
},
confirmFunction: function (action, what, functionData) {
var actionTxt = jData.clng.show("ayst_" + action, what);
bootbox.confirm(jData.clng.show("areyousureto", actionTxt), functionData);
},
confirmMultiButtons: function (text, buttons) {
// A button object is: { "label": "", "class": "", "callback": function() {} }
// Buttons is an array of a button objects [ buttonObject, buttonObject, ... ]
bootbox.dialog(text, buttons);
},
disableConfirmBtn: function () {
$(".bootbox a").prop("disabled", true);
$(".bootbox a[data-handler='1']").html("");
$(".bootbox a[data-handler='2']").html("");
}
};
/**
* The drop zones which will cache the created drop zones
* @constructor
*/
function DropZones() {
/**
* The object which will hold the following objects
* 1) files: All the files from the drop zones (files.{dropZoneId} => Array)
* 2) fileIndexMapper: All the filenames with their index from the drop zones (fileIndexMapper.{dropZoneId}.file1 => 1)
* 3) dropZones: All the drop zones (dropZones.{dropZoneId}
* 4) configs: All the configs from a certain drop zone (configs.{dropZoneId}.clearOnClose => true)
* @type {{files: {}, fileIndexMapper: {}, dropZones: {}, configs: {}}}
*/
this.cache = {files: {}, fileIndexMapper: {}, dropZones: {}, configs: {}};
/**
* Set the drop zone into the cache
*
* @param {string} dropZoneId The drop zone id
* @param {Dropzone} dropZone The drop zone object
* @returns {DropZones}
*/
this.set = function (dropZoneId, dropZone) {
this.cache.dropZones[dropZoneId] = dropZone;
this.setFilesInput(dropZoneId);
return this;
};
/**
* Get the drop zone object by the drop zone id
*
* @param {string} dropZoneId The drop zone id
* @returns {Dropzone}
*/
this.get = function (dropZoneId) {
return this.cache.dropZones[dropZoneId];
};
/**
* Show the drop zone by id
*
* @param {string} dropZoneId The drop zone id
* @returns {DropZones}
*/
this.show = function (dropZoneId) {
var $dropZone = $('#' + dropZoneId);
$dropZone.show();
// Check if the drop zone is in a control-group then we also need to hide the control-group
var $parent = $dropZone.parent();
if (typeof $parent !== 'undefined' && $parent.length > 0 && $parent.hasClass('controls')) {
$parent.closest('.control-group').show();
}
return this;
};
/**
* Hide the drop zone by id
*
* @param {string} dropZoneId The drop zone id
* @returns {DropZones}
*/
this.hide = function (dropZoneId) {
var $dropZone = $('#' + dropZoneId);
$dropZone.hide();
// Check if the drop zone is in a control-group then we also need to hide the control-group
var $parent = $dropZone.parent();
if (typeof $parent !== 'undefined' && $parent.length > 0 && $parent.hasClass('controls')) {
$parent.closest('.control-group').hide();
}
var resetOnClose = this.getConfig(dropZoneId, 'resetOnClose');
if (resetOnClose) {
this.reset(dropZoneId);
}
return this;
};
/**
* Delete the cached drop zone id
*
* @param {string} dropZoneId The drop zone id
* @returns {DropZones}
*/
this.delete = function (dropZoneId) {
// Delete the drop zone
if (typeof this.cache.dropZones[dropZoneId] === 'undefined') {
return this;
}
delete this.cache.dropZones[dropZoneId];
// Also delete the files from this drop zone
if (typeof this.cache.files[dropZoneId] === 'undefined') {
return this;
}
delete this.cache.files[dropZoneId];
return this;
};
/**
* Add the file to the dropzone (this is an already uploaded file)
*
* @param {string} dropZoneId The drop zone id
* @param {*} file The file object
*
* @returns {DropZones}
*/
this.addFile = function (dropZoneId, file) {
this.get(dropZoneId).emit('addedfile', file);
this.get(dropZoneId).emit('thumbnail', file, file.url);
this.get(dropZoneId).createThumbnailFromUrl(file, file.url);
this.get(dropZoneId).emit('complete', file);
this.setFile(dropZoneId, file);
return this;
};
/**
* Set a file for a drop zone in cache
*
* @param {string} dropZoneId The drop zone id
* @param {*} file The file object
* @returns {DropZones}
*/
this.setFile = function (dropZoneId, file) {
if (typeof this.cache.files[dropZoneId] === 'undefined') {
this.cache.files[dropZoneId] = [];
}
var fileIndex = this.cache.files[dropZoneId].length;
this.cache.files[dropZoneId].push(file);
if (typeof this.cache.fileIndexMapper[dropZoneId] === 'undefined') {
this.cache.fileIndexMapper[dropZoneId] = {};
}
var filename = file['name'];
if (typeof file['storedFilename'] !== 'undefined' && file['storedFilename'] !== '') {
filename = file['storedFilename'];
}
this.cache.fileIndexMapper[dropZoneId][filename] = fileIndex;
this.setFilesInput(dropZoneId);
return this;
};
/**
* Get the file by an index
*
* @param {string} dropZoneId The drop zone id
* @param {number} fileIndex The file index if known
* @param {string} filename The file name (used to get the file index, if this is known you can pass -1 or null for the file index)
* @returns {*}
*/
this.getFile = function (dropZoneId, fileIndex, filename) {
if (fileIndex < 0 && filename === '') {
return null;
}
// Get the file index if the file name has been given and the file index is below 0
if (fileIndex === null || fileIndex < 0) {
fileIndex = this.getFileIndexByName(dropZoneId, filename);
}
// If the file index is below 0 the index is not found and we stop here
if (fileIndex < 0) {
return null;
}
return this.cache.files[dropZoneId][fileIndex];
};
/**
* Delete the file from the cache
*
* @param {string} dropZoneId The drop zone id
* @param {number} fileIndex The file index if known
* @param {string} filename The file name (used to get the file index, if this is known you can pass -1 or null for the file index)
* @returns {DropZones}
*/
this.deleteFile = function (dropZoneId, fileIndex, filename) {
if (fileIndex < 0 && filename === '') {
return this;
}
// Get the file index if the file name has been given and the file index is below 0
if (fileIndex === null || fileIndex < 0) {
fileIndex = this.getFileIndexByName(dropZoneId, filename);
}
// If the file index is below 0 the index is not found and we stop here
if (fileIndex < 0) {
return this;
}
// Remove the cached files
if (typeof this.cache.files[dropZoneId] !== 'undefined' &&
typeof this.cache.files[dropZoneId][fileIndex] !== 'undefined') {
delete this.cache.files[dropZoneId][fileIndex];
}
// Remove the file mapper
if (typeof this.cache.fileIndexMapper[dropZoneId] !== 'undefined' &&
typeof this.cache.fileIndexMapper[dropZoneId][filename] !== 'undefined') {
delete this.cache.fileIndexMapper[dropZoneId][filename];
}
this.setFilesInput(dropZoneId);
return this;
};
/**
* Delete all the files from a drop zone
*
* @param {string} dropZoneId The drop zone id
* @returns {DropZones}
*/
this.deleteAllFiles = function (dropZoneId) {
// Remove the cached files
if (typeof this.cache.files[dropZoneId] !== 'undefined') {
delete this.cache.files[dropZoneId];
}
// Remove the file mapper
if (typeof this.cache.fileIndexMapper[dropZoneId] !== 'undefined') {
delete this.cache.fileIndexMapper[dropZoneId];
}
// Reset the file input
this.setFilesInput(dropZoneId);
// Remove all the files from the drop zone
if (typeof this.cache.dropZones[dropZoneId] !== 'undefined') {
this.cache.dropZones[dropZoneId].removeAllFiles(true);
}
return this;
};
/**
* Set the hidden file input for a specific drop zone
*
* @param {string} dropZoneId The drop zone id
* @returns {DropZones}
*/
this.setFilesInput = function (dropZoneId) {
var $input = $('#' + dropZoneId + '-files');
if ($input.length > 0) {
var files = this.cache.files[dropZoneId];
var output = [];
// Check if the files has been set, otherwise create an empty array for this drop zone
if (typeof files === 'undefined' || files === null) {
this.cache.files[dropZoneId] = [];
$input.val(JSON.stringify(files));
return this;
}
// We need to loop the files and remove the dataURL if it exists
for (var i = 0, length = files.length; i < length; i++) {
var value = files[i];
// Check if the value is undefined or null, we create an empty array then
if (typeof value === 'undefined' || value === null) {
continue;
}
// Remove the dataURL since we do not need to post this with our form
if (typeof value['dataURL'] !== 'undefined') {
delete value['dataURL'];
}
output.push(value);
}
$input.val(JSON.stringify(output));
}
return this;
};
/**
* Get the file index by file name
*
* @param {string} dropZoneId The drop zone id
* @param {string} filename The file name
* @returns {number} The file index (note: -1 if not found)
*/
this.getFileIndexByName = function (dropZoneId, filename) {
if (
typeof this.cache.fileIndexMapper[dropZoneId] === 'undefined' ||
typeof this.cache.fileIndexMapper[dropZoneId][filename] === 'undefined'
) {
return -1;
}
return this.cache.fileIndexMapper[dropZoneId][filename];
};
/**
* Set all the listeners
*/
this.setListeners = function () {
// Hide the drop zone container
$('body')
.off('click', '.dropzone-outer .dropzone-wrapper .close-uploader')
.on('click', '.dropzone-outer .dropzone-wrapper .close-uploader', function () {
// Get the drop zone outer element where the id is set
var $outer = $(this).closest('.dropzone-outer');
// Get the drop zone id
var dropZoneId = $outer.attr('id');
// Hide the drop zone by id
jData.dropZones.hide(dropZoneId);
});
};
/**
* Reset a drop zone by id
*
* @param {string} dropZoneId The drop zone id
* @returns {DropZones}
*/
this.reset = function (dropZoneId) {
this.deleteAllFiles(dropZoneId);
return this;
};
/**
* Set the configs
*
* @param {string} dropZoneId The drop zone id
* @param {{}} configs The configs object {configName: configValue, ...}
* @returns {DropZones}
*/
this.setConfigs = function (dropZoneId, configs) {
if (configs === null) {
configs = {};
}
this.cache.configs[dropZoneId] = configs;
return this;
};
/**
* Set a specific config value for a config
*
* @param {string} dropZoneId The drop zone id
* @param {string} configName The config name to set
* @param {*} configValue The config value to set
* @returns {DropZones}
*/
this.setConfig = function (dropZoneId, configName, configValue) {
if (typeof this.cache.configs[dropZoneId] === 'undefined') {
this.cache.configs[dropZoneId] = {};
}
this.cache.configs[dropZoneId][configName] = configValue;
return this;
};
/**
* Get a specific config value by name, with a default if the config is not set
*
* @param {string} dropZoneId The drop zone id
* @param {string} configName The config name to search
* @param {*} defaultValue The default value which will be returned when the config is not set
* @returns {*}
*/
this.getConfig = function (dropZoneId, configName, defaultValue) {
// Check if there are configs set for this drop zone
if (typeof this.cache.configs[dropZoneId] === 'undefined') {
this.cache.configs[dropZoneId] = {};
}
/*
Check if the config name is set for this drop zone configs
If the config is not set it will return the default value
If the default value is undefined or null, null will be returned
*/
if (typeof this.cache.configs[dropZoneId][configName] === 'undefined') {
if (typeof defaultValue === 'undefined' || defaultValue === null) {
return null;
}
return defaultValue;
}
// Returned the config value
return this.cache.configs[dropZoneId][configName];
};
}
jData.dropZones = new DropZones();
$(function () {
jData.dropZones.setListeners();
});