)
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /|<\?(?:php)?[\s\S]*?\?>/gi;
return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
};
/**
* escape html, turn html to a string
*/
this.htmlspecialchars = function(string){
if(!string)
return(string);
return string
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
};
/**
* escape double slash
*/
this.escapeDoubleQuote = function(str){
if(!str)
return(str);
return str.replace('"','\"');
}
/**
* capitalize first letter
*/
this.capitalizeFirstLetter = function(str){
str = str.substr(0, 1).toUpperCase() + str.substr(1).toLowerCase();
return(str);
};
/**
* get transparency value from 0 to 100
*/
this.getTransparencyFromRgba = function(rgba, inPercent){
var temp = rgba.split(',');
if(temp.length == 4){
inPercent = (typeof inPercent !== 'undefined') ? inPercent : true;
return (inPercent) ? temp[3].replace(/[^\d.]/g, "") : temp[3].replace(/[^\d.]/g, "") * 100;
}
return false;
};
/**
* add px or leave % if needed
*/
this.normalizeSizeValue = function(strValue){
strValue = String(strValue);
strValue.toLowerCase();
if(jQuery.isNumeric(strValue))
strValue += "px";
return(strValue);
}
/**
* remove line breaks and tabs
*/
this.removeLineBreaks = function(str, replaceSign){
if(!replaceSign)
var replaceSign = "";
str.replace(/\s+/g, replaceSign);
return(str);
}
/**
* remove amp from string
*/
this.convertAmpSign = function(str){
var str = str.replace(/&/g, '&');
return(str);
};
/**
* filter object, leave child items by keys
*/
this.filterObjectByKeys = function(obj, arrKeys){
if(typeof obj != "object")
return(obj);
if(jQuery.isArray(arrKeys) == false)
throw new Error("filterObjectByKeys error - arrKeys should be array");
var outputObj = {};
jQuery.each(arrKeys, function(index, key){
if(obj.hasOwnProperty(key))
outputObj[key] = obj[key];
});
return(outputObj);
};
this.__________PATHS_AND_URLS_____ = function(){};
/**
* get base name from path
*/
this.pathinfo = function(path) {
var obj = {};
if(typeof path == "object"){
trace(path);
throw new Error("pathinfo error: path is object");
}
obj.basename = path.replace(/\\/g,'/').replace(/.*\//, '');
obj.filename = obj.basename.substr(0,obj.basename.lastIndexOf('.'));
return(obj);
}
/**
* strip path slashes from both sides
*/
this.stripPathSlashes = function(path){
return path.replace(/^\/|\/$/g, '');
}
/**
* convert to full url
*/
this.urlToFull = function(url, urlBase){
if(!url)
return(url);
if(!urlBase)
var urlBase = g_urlBaseUC;
//try to convert assets path from provider
url = g_providerAdmin.urlAssetsToFull(url);
var urlSmall = url.toLowerCase();
if(urlSmall.indexOf("http://") !== -1 || urlSmall.indexOf("https://") !== -1)
return(url);
if(url.indexOf(urlBase) !== -1)
return(url);
url = jQuery.trim(url);
if(!url || url == "")
return("");
url = urlBase + url;
return(url);
}
/**
* convert to relative url
*/
this.urlToRelative = function(url, urlBase){
if(!urlBase)
var urlBase = g_urlBaseUC;
url = url.replace(urlBase, "");
return(url);
}
/**
* get url of some view
*/
this.getUrlView = function(view, options){
var url = g_urlViewBaseUC+"&view="+view;
if(options && options != "")
url += "&"+options;
return(url);
};
/**
* get current view url
*/
this.getUrlCurrentView = function(options){
var url = g_urlViewBaseUC+"&view=" + g_view;
if(options)
url += "&"+options;
return(url);
};
this.__________VALIDATION_FUNCTIONS_____ = function(){};
/**
* validate that object has some element name
*/
this.validateObjProperty = function(obj, propertyName, objName){
if(typeof obj != "object")
throw new Error("The object is empty (with property: " + elementName);
if(typeof propertyName == "object"){
jQuery(propertyName).each(function(index, pname){
t.validateObjProperty(obj, pname, objName);
});
return(false);
}
if(obj.hasOwnProperty(propertyName) == false){
trace(obj);
if(!objName)
objName = "";
throw new Error("The "+objName+" object should has property: " + propertyName);
}
};
/**
* validate that the dom object exists
* the obj has to be jquery object of don element
*/
this.validateDomElement = function(obj, objName){
if(typeof obj != "object")
throw new Error("The object: "+objName+" not inited well");
if(obj.length == 0)
throw new Error(objName+" not found!");
};
/**
* validate that field not empty
*/
this.validateNotEmpty = function(val, fieldName){
if(typeof val == "undefined" || jQuery.trim(val) == "")
throw new Error("Please fill "+ fieldName + " field");
};
/**
* validate that some value is object
*/
this.validateIsObject = function(val, fieldName){
if(typeof val !== "object")
throw new Error("The field must be object: "+fieldName);
};
/**
* validate name field
*/
this.validateNameField = function(val, fieldName){
var errorMessage = "The field "+ fieldName + " allow only english lowercase letters, numbers and underscore. Example: first_name ";
var regex = /^[a-z0-9_]+$/;
if(regex.test(val) == false)
throw new Error(errorMessage);
}
this._____________DIALOGS__________ = function(){};
/**
* set image browser dialog path
*/
this.setAddImagePath = function(path, url){
if(typeof g_providerAdmin.setPathSelectImages == "function")
g_providerAdmin.setPathSelectImages(path, path, url);
}
/**
* open "add image" dialog
*/
this.openAddImageDialog = function(title, onInsert, isMultiple, source){
g_providerAdmin.setParent(t); //for convert to relative
g_providerAdmin.openAddImageDialog(title, onInsert, isMultiple, source);
};
/**
* open "add image" dialog
*/
this.openAddMp3Dialog = function(title, onInsert, isMultiple, source){
g_providerAdmin.setParent(t); //for convert to relative
g_providerAdmin.openAddMp3Dialog(title, onInsert, isMultiple, source);
};
/**
* open "add image" dialog
*/
this.openAddPostDialog = function(title, onInsert, postType){
g_providerAdmin.setParent(t); //for convert to relative
g_providerAdmin.openSelectArticleDialog(title, onInsert, postType);
};
/**
* open video dialog
*/
this.openVideoDialog = function(callbackFunction, itemData){
g_ugMediaDialog.openVideoDialog(callbackFunction, itemData);
};
/**
* common dialog ajax request
*/
this.dialogAjaxRequest = function(dialogID, action, data, funcSuccess, params){
dialogID = dialogID.replace("#", "");
g_ucAdmin.setAjaxLoaderID(dialogID + "_loader");
g_ucAdmin.setErrorMessageID(dialogID + "_error");
g_ucAdmin.setSuccessMessageID(dialogID + "_success");
g_ucAdmin.setAjaxHideButtonID(dialogID + "_action");
var isNoClose = t.getVal(params, "noclose");
g_ucAdmin.ajaxRequest(action, data, function(response){
if(isNoClose !== true){
setTimeout(function(){
jQuery("#"+dialogID).dialog("close");
},500);
}
if(typeof(funcSuccess) == "function")
funcSuccess(response);
});
}
/**
* open common dialog with all inside actions
*/
this.openCommonDialog = function(id, onOpen, options, closeOnEmptyClick){
if(typeof id == "object"){
var id = id.prop("id");
if(!id)
throw new Error("The dialog should have ID");
id = "#"+id;
}
if(id.charAt(0) != "#")
id = "#"+id;
if(jQuery(id).length == 0)
throw new Error("Dialog with ID: "+id + " don't exists!");
var buttonOpts = {};
buttonOpts[g_uctext.close] = function(){
jQuery(id).dialog("close");
};
jQuery(id+"_loader").hide();
jQuery(id+"_error").hide();
jQuery(id+"_success").hide();
jQuery(id+"_action").show();
var dialogOptions = {
buttons:buttonOpts,
minWidth:600,
modal:true,
dialogClass:"unite-ui",
open:function(){
if(typeof onOpen == "function")
onOpen();
//set close on empty click events
if(closeOnEmptyClick === true){
var objDialogWrapper = jQuery(id).parents(".ui-dialog");
var objOverlay = objDialogWrapper.siblings(".ui-widget-overlay");
objOverlay.click(function(){
jQuery(id).dialog("close");
objOverlay.off("click");
});
}
}
};
if(options && typeof options == "object")
dialogOptions = jQuery.extend(dialogOptions, options);
jQuery(id).dialog(dialogOptions);
}
/**
* add button on the left.
* use it on create
*/
this.dialogAddLeftButton = function(objDialog, title, funcOnClick){
var objButtonPane = objDialog.closest(".ui-dialog").find(".ui-dialog-buttonpane");
var html = "";
html += "";
html += "
";
var objButtonset = jQuery(html);
objButtonPane.append(objButtonset);
var objButton = objButtonset.children("button");
if(typeof funcOnClick == "function")
objButton.click(funcOnClick);
return(objButton);
}
this.__________AJAX_REQUEST_____ = function(){};
/**
* show error message or call once custom handler function
*/
this.showErrorMessage = function(htmlError){
if(g_errorMessageID !== null){
switch(typeof g_errorMessageID){
case "object":
g_errorMessageID.show().html(htmlError);
break;
case "function":
g_errorMessageID(htmlError);
break;
default:
jQuery("#"+g_errorMessageID).show().html(htmlError);
break;
}
}else
jQuery("#error_message").show().html(htmlError);
showAjaxButton();
}
/**
* hide error message
*/
function hideErrorMessage(){
if(g_errorMessageID !== null){
switch(typeof g_errorMessageID){
case "object":
g_errorMessageID.hide();
break;
case "string":
jQuery("#"+g_errorMessageID).hide();
break;
case "function":
if(typeof g_errorMessageHideFunc == "function")
g_errorMessageHideFunc();
break;
}
if(g_hideMessageCounter > 0){
g_hideMessageCounter = 0;
g_errorMessageID = null;
g_errorMessageHideFunc = null;
}else
g_hideMessageCounter++;
}else
jQuery("#error_message").hide();
};
/**
* set error message id
*/
this.setErrorMessageID = function(id){
g_errorMessageID = id;
g_hideMessageCounter = 0;
};
/**
* set hide error func
*/
this.setErrorMessageOnHide = function(func){
g_errorMessageHideFunc = func;
}
/**
* set success message id
*/
this.setSuccessMessageID = function(id){
g_successMessageID = id;
};
/**
* show success message
*/
this.showSuccessMessage = function(htmlSuccess){
var id = "#success_message";
var delay = 2000;
if(g_successMessageID){
id = "#"+g_successMessageID;
delay = 500;
}
if (htmlSuccess !== 'Layout Updated'){
jQuery(id).show().html(htmlSuccess);
} else {
var content ='' + 'Saved Successfully' + '';
jQuery(id).show().html(content);
}
setTimeout(t.hideSuccessMessage,delay);
};
/**
* hide success message
*/
this.hideSuccessMessage = function(){
if(g_successMessageID){
jQuery("#"+g_successMessageID).hide();
g_successMessageID = null; //can be used only once.
}
else
jQuery("#success_message").slideUp("slow").fadeOut("slow");
showAjaxButton();
};
/**
* set ajax loader id that will be shown, and hidden on ajax request
* this loader will be shown only once, and then need to be sent again.
*/
this.setAjaxLoaderID = function(id){
g_ajaxLoaderID = id;
};
/**
* show loader on ajax actions
*/
var showAjaxLoader = function(){
if(!g_ajaxLoaderID)
return(false);
if(typeof(g_ajaxLoaderID) == "function")
g_ajaxLoaderID("show_loader");
else
jQuery("#"+g_ajaxLoaderID).show();
};
/**
* hide and remove ajax loader. next time has to be set again before "ajaxRequest" function.
*/
var hideAjaxLoader = function(){
if(!g_ajaxLoaderID)
return(false);
if(typeof g_ajaxLoaderID == "function"){
g_ajaxLoaderID("hide_loader");
}else{
jQuery("#"+g_ajaxLoaderID).hide();
g_ajaxLoaderID = null;
}
};
/**
* set button to hide / show on ajax operations.
*/
this.setAjaxHideButtonID = function(buttonID){
g_ajaxHideButtonID = buttonID;
};
/**
* if exist ajax button to hide, hide it.
*/
function hideAjaxButton(){
if(!g_ajaxHideButtonID)
return(false);
if(typeof g_ajaxHideButtonID == "function"){
g_ajaxHideButtonID("hide_button");
}else{
jQuery("#"+g_ajaxHideButtonID).hide();
}
};
/**
* if exist ajax button, show it, and remove the button id.
*/
function showAjaxButton(){
if(!g_ajaxHideButtonID)
return(false);
if(typeof g_ajaxHideButtonID == "function"){
g_ajaxHideButtonID("show_button");
}else{
jQuery("#"+g_ajaxHideButtonID).show();
g_ajaxHideButtonID = null;
}
};
/**
* add url param
*/
function addUrlParam(url, param, value){
if(url.indexOf("?") == -1)
url += "?";
else
url += "&";
if(typeof value == "undefined")
url += param;
else
url += param + "=" + value;
return(url);
}
/**
* get ajax url with action and params
*/
this.getUrlAjax = function(action, params){
var url = g_urlAjaxActionsUC;
url = addUrlParam(url, "action", g_pluginNameUC+"_ajax_action");
if(typeof g_ucNonce == "string")
url = addUrlParam(url, "nonce", g_ucNonce);
if(action)
url = addUrlParam(url, "client_action", action);
if(params)
url = addUrlParam(url, params);
return(url);
}
/**
* add form files to data
*/
this.addFormFilesToData = function(formID, objData){
var objForm = jQuery("#"+formID);
if(objForm.length == 0)
throw new Error("form with ID: "+ formID + " not found");
var objFiles = objForm.find("input[type='file']");
if(objFiles.length == 0)
throw new Error("no file inputs found in form: " + formID);
jQuery.each(objFiles, function(index, objFile){
var fieldName = objFile.name;
jQuery.each(objFile.files, function(index2, file){
objData.append(fieldName, file);
});
});
}
/**
* check ajax return
*/
this.ajaxReturnCheck = function(response, successFunction){
if(!response){
t.showErrorMessage("Empty ajax response!");
return(false);
}
if(typeof response != "object"){
try{
response = jQuery.parseJSON(response);
}catch(e){
t.showErrorMessage("Ajax Error!!! not ajax response");
t.debug(response);
return(false);
}
}
if(response == -1){
t.showErrorMessage("ajax error!!!");
return(false);
}
if(response == 0){
t.showErrorMessage("ajax error, action: "+action+" not found");
return(false);
}
if(response.success == undefined){
t.showErrorMessage("The 'success' param is a must!");
return(false);
}
if(response.success == false){
t.showErrorMessage(response.message);
return(false);
}
//run a success event function
if(typeof successFunction == "function"){
//show success message only if custom id exists
if(response.message && g_successMessageID)
t.showSuccessMessage(response.message);
successFunction(response);
}
else{
if(response.message)
t.showSuccessMessage(response.message);
}
if(response.is_redirect)
location.href=response.redirect_url;
}
/**
* Ajax request function. call wp ajax, if error - print error message.
* if success, call "success function"
*/
this.ajaxRequest = function(action,data,successFunction){
if(typeof data == "undefined")
var data = {};
//raw mode - for including file uploads
var isRawMode = false;
if(typeof data.append == "function"){
isRawMode = true;
var objData = data;
objData.append("action", g_pluginNameUC+"_ajax_action");
objData.append("client_action", action);
if(typeof g_ucNonce == "string")
objData.append("nonce", g_ucNonce);
}else{
//simple mode
var objData = {
action:g_pluginNameUC+"_ajax_action",
client_action:action,
data:data
};
if(typeof g_ucNonce == "string")
objData.nonce = g_ucNonce;
}
hideErrorMessage();
showAjaxLoader();
hideAjaxButton();
var ajaxOptions = {
type:"post",
url:g_urlAjaxActionsUC,
dataType: 'json',
data:objData,
success:function(response){
hideAjaxLoader();
t.ajaxReturnCheck(response, successFunction);
},
error:function(jqXHR, textStatus, errorThrown){
hideAjaxLoader();
switch(textStatus){
case "parsererror":
case "error":
t.debug(jqXHR.responseText);
break;
}
t.showErrorMessage("Ajax Error!!! " + textStatus);
}
}
//add some options for raw mode
if(isRawMode == true){
ajaxOptions.global = false;
ajaxOptions.processData = false;
ajaxOptions.contentType = false;
}
jQuery.ajax(ajaxOptions);
};//ajaxrequest
/**
* ajax request for creating thumb from image and get thumb url
* instead of the url can get image id as well
*/
this.requestThumbUrl = function(urlImage, imageID, callbackFunction){
var data = {
urlImage: urlImage,
imageID: imageID
};
t.ajaxRequest("get_thumb_url",data, function(response){
callbackFunction(response.urlThumb);
});
};
/**
* init version dialog
*/
function initVersionDialog(){
/**
* open the version dialog
*/
jQuery("#uc_version_link").click(function(){
var objDialog = jQuery("#uc_dialog_version");
var buttonOpts = {};
buttonOpts[g_uctext.cancel] = function(){
objDialog.dialog("close");
};
objDialog.dialog({
dialogClass:"unite-ui",
buttons:buttonOpts,
minWidth:900,
modal:true,
open:function(){
var objContent = jQuery("#uc_dialog_version_content");
var isContentLoaded = objContent.data("loaded");
if(isContentLoaded === true)
return(false);
t.ajaxRequest("get_version_text", {}, function(response){
var html = ""+response.text+"
";
objContent.html(html);
objContent.data("loaded", true);
});
}
});
});
}
this.z_________DATA_FUNCTIONS_______ = function(){}
/**
* set data value
*/
this.storeGlobalData = function(key, value){
key = "unite_data_"+key;
jQuery.data(document.body, key, value);
};
/**
* get global data
*/
this.getGlobalData = function(key){
key = "unite_data_"+key;
var value = jQuery.data(document.body, key);
return(value);
};
this.__________THIRD_PARTY_____ = function(){};
/**
* get settings of dropzone that turn it to single line
*/
this.getDropzoneSingleLineSettings = function(){
var htmlTemplate = '';
htmlTemplate += '
';
htmlTemplate += '
';
htmlTemplate += '
';
htmlTemplate += '
';
htmlTemplate += '
';
htmlTemplate += '
';
htmlTemplate += '
';
var settings = {
createImageThumbnails:false,
addRemoveLinks:true,
previewTemplate: htmlTemplate,
dictRemoveFile: "remove",
dictCancelUpload: "cancel"
//dictRemoveFile: ""
};
return(settings);
}
this.__________CHECK_CATALOG_____ = function(){};
/**
* handle catalog check
*/
function handleCheckCatalog(){
if(typeof g_ucCheckCatalog === "undefined")
return(false);
if(g_ucCheckCatalog !== true)
return(false);
//don't show error on page refresh
t.setErrorMessageID(function(){});
setTimeout(function(){
t.ajaxRequest("check_catalog", {}, function(response){});
}, 5000);
}
this.__________GLOBAL_INIT_____ = function(){};
/**
* global init
*/
this.globalInit = function(){
g_providerAdmin.setParent(t);
g_providerAdmin.init();
if(typeof g_ugMediaDialog != "undefined")
g_ugMediaDialog.init();
initVersionDialog();
handleCheckCatalog();
};
}
if(!g_ucAdmin)
var g_ucAdmin;
//user functions:
function trace(data,clear){
if(!g_ucAdmin)
g_ucAdmin = new UniteAdminUC();
g_ucAdmin.trace(data,clear);
}
function clearTrace(){
console.clear();
}
function debug(data){
if(!g_ucAdmin)
g_ucAdmin = new UniteAdminUC();
g_ucAdmin.debug(data);
}
/**
* debug line by line
*/
function debugLine(data){
data += " "+Math.random();
var html = jQuery("#div_debug").html();
html += "
";
html += data;
jQuery("#div_debug").show().html(html);
}
//run the init function
jQuery(document).ready(function(){
if(!g_ucAdmin)
g_ucAdmin = new UniteAdminUC();
g_ucAdmin.globalInit();
});