Tower = {};
Tower.EUM = {};
Tower.EUO = {};
Tower.OF = {};
Tower.Layout = {};
Tower.Filter = {
toggleFilter: function(ns, update, element, container, ids, formName) {
var filterNs = (ids != null)? ns + ids : ns,
portletFilter = jQuery('#'+filterNs+'tower-filter'),
element = filterNs + element,
container = filterNs + container,
formFilter = filterNs + (formName != null? formName : 'fmFilter'),
filterFunnelIco = jQuery('#'+filterNs+'tower-list-results-grid').find('th.tower-list-col-filter');
if (portletFilter.length > 0) {
if (portletFilter.is(':visible')) {
filterFunnelIco.removeClass('filter-on').addClass('filter-off');
portletFilter.hide('normal', function(){
Tower.Util.resizePortletHeight(element, container, false);
});
} else {
filterFunnelIco.removeClass('filter-off').addClass('filter-on');
portletFilter.show('normal', function(){
Tower.Util.resizePortletHeight(element, container, true);
});
}
}
if (update) submitForm(document.forms[formFilter]);
},
resetFilter: function(ns, formName) {
var name = formName != null? formName : 'fmFilter',
form = jQuery('form[name="'+ ns + name +'"]');
jQuery('input:text', form).val('');
jQuery('input:hidden', form).val('');
jQuery('input:checkbox', form).removeAttr('checked');
},
resetFilterRule: function(ns, name, value, update, formName) {
var fname = formName != null? formName : 'fmFilter',
form = jQuery('form[name="'+ ns + fname +'"]'),
target = jQuery('input[name='+ ns + name + '][value=' + value + ']', form),
targetSelect = jQuery('select[name=' + ns + name + '] option[value=' + value + ']', form),
formFilter = ns + fname;
if (target.length > 0) target.val('');
if (targetSelect.length > 0) targetSelect.removeAttr('selected');
if (update) submitForm(document.forms[formFilter]);
},
resetAllFilterRules: function(ns, update, formName) {
var formFilter = ns + (formName != null? formName : 'fmFilter');
this.resetFilter(ns);
if (update) submitForm(document.forms[formFilter]);
}
};
Tower.PubblicationParameter = {
options: {
addClass: 'edit-page-parameter-add',
removeClass: 'edit-page-parameter-remove',
paramNameClass: 'edit-page-param-name',
paramValueClass: 'edit-page-param-value',
paramClass: 'edit-page-parameter',
paramListClass: 'edit-page-parameters',
paramNameId: 'page-param-name_',
paramValueId: 'page-param-value_',
publicationParams: 'publicationParameters',
newLineSep: '\n'
},
init: function(options) {
var inst = this,
options = options || inst.options;
if(options) inst.options = options;
jQuery('.' + options.addClass).click(function(){ inst.addParam(this); });
jQuery('.' + options.removeClass).click(function(){ inst.removeParam(this); });
jQuery('input.' + options.paramNameClass).bind('blur focusout', function(){ inst.updateParam(this); });
jQuery('input.' + options.paramValueClass).bind('blur focusout', function(){ inst.updateParam(this); });
},
addParam: function(obj) {
var inst = this,
options = inst.options,
$obj = jQuery(obj),
container = $obj.parent('.' + options.paramClass),
ns = container.attr('id').split(options.paramClass +'-')[0],
paramList = jQuery('#'+ ns + options.paramListClass).find('.' + options.paramClass),
index = paramList.length,
clone = container.clone(true),
cloneId = ns + options.paramClass + '-' + (index + 1);
paramName = ns + options.paramNameId + '_' + (index + 1),
paramValue = ns + options.paramValueId + '_' + (index + 1);
paramList.removeClass('is-last');
clone.attr('id', cloneId).addClass('is-last');
clone.find('input.' + options.paramNameClass).attr('id', paramName).attr('name', paramName).val('');
clone.find('input.' + options.paramValueClass).attr('id', paramValue).attr('name', paramValue).val('');
clone.insertAfter(paramList.eq(index - 1));
inst.updateValue(ns);
},
removeParam: function(obj) {
var inst = this,
options = inst.options,
$obj = jQuery(obj),
container = $obj.parent('.' + options.paramClass),
ns = container.attr('id').split(options.paramClass +'-')[0],
paramList = jQuery('#'+ ns + options.paramListClass).find('.' + options.paramClass);
if(paramList.length > 1) {
container.prev('.' + options.paramClass).addClass('is-last');
container.remove();
} else {
container.find('input').val('');
}
inst.updateValue(ns);
},
updateParam: function(obj) {
var inst = this,
options = inst.options,
$obj = jQuery(obj),
container = $obj.parent('.' + options.paramClass),
ns = container.attr('id').split(options.paramClass +'-')[0];
inst.updateValue(ns);
},
updateValue: function(ns) {
var inst = this,
options = inst.options,
paramList = jQuery('#'+ ns + options.paramListClass).find('.' + options.paramClass),
value = '';
paramList.each(function(i){
var param = jQuery(this),
paramName = param.find('input.' + options.paramNameClass).val(),
paramValue = param.find('input.' + options.paramValueClass).val();
if(paramName != '' && paramValue != '') value += paramName +'='+ paramValue + options.newLineSep;
});
//value = value.replace(/\\n$/g, '');
jQuery('input#'+ ns + options.publicationParams +':hidden').val(value);
}
};
Tower.Theme = {
toggleInfo: function(ppid) {
var plid = themeDisplay.getPlid(),
doAsUserId = themeDisplay.getDoAsUserIdEncoded(),
url = themeDisplay.getPathMain() + '/portal/render_portlet',
ww = Tower.Util.getWindowWidth(800);
ww = (ww - 800);
var position = 'center';
if (ww > 0) position = [(ww / 2), 50];
var infoPopup = Liferay.Popup({
width: 800,
message: '<div class="loading-animation" />',
position: position,
resizable: false,
title: Liferay.Language.get("theme-info")
});
jQuery.ajax({
url: url,
data: {
p_l_id: plid,
p_p_id: ppid,
p_p_state: 'exclusive',
doAsUserId: doAsUserId
},
success: function(message) {
jQuery(infoPopup).html(message);
}
});
}
};
Tower.Util = {
toggle: function(obj, targetName, classOn, classOff, titleOn, titleOff, slide, callback) {
var target =(typeof targetName == 'object')? targetName : jQuery('#' + targetName + ', .' + targetName),
$_obj = (obj == null || obj == '')? '' : jQuery(obj);
if (target.length > 0) {
if ($_obj.length > 0) {
if($_obj.hasClass(classOn)) {
$_obj.removeClass(classOn);
$_obj.addClass(classOff);
$_obj.attr('title',titleOff);
} else {
$_obj.removeClass(classOff);
$_obj.addClass(classOn);
$_obj.attr('title',titleOn);
}
}
if (slide) {
  target.slideToggle('normal', callback);
} else {
target.toggle(callback);
}
}
},
horizontalToggle: function(imageObj, targetName, imageOn, imageOff, titleOn, titleOff, slide, callback) {
var target = jQuery('#' + targetName + ', .' + targetName);
if (target.length > 0) {
if (imageObj) {
if (target.is(':visible')) {
imageObj.src = imageOff;
imageObj.alt = titleOff;
imageObj.title = titleOff;
} else {
imageObj.src = imageOn;
imageObj.alt = titleOn;
imageObj.title = titleOn;
}
}
target.animate({ 'width': 'toggle' }, 'normal', callback);
}
},
horizontalToggleClass: function(obj, targetName, classOn, classOff, titleOn, titleOff, callback) {
var target = jQuery('#' + targetName + ', .' + targetName);
if (target.length > 0) {
var $obj = jQuery(obj),
toggle = '';
if (target.is(':visible')) {
$obj.attr('title', titleOn);
$obj.removeClass(classOn).addClass(classOff);
target.hide(1, callback);
} else {
$obj.attr('title', titleOff);
$obj.removeClass(classOff).addClass(classOn);
target.animate({ 'width': 'show' }, 'normal', callback);
}
}
},
searchAdvancedToggle: function(obj, openTitle, closeTitle, n_tr) {
var link = jQuery(obj),
nextTr = link.parents('tr:first').nextAll('tr:lt('+(n_tr)+')'),
TowerUtil = this;
if(link.hasClass('closed')) {
nextTr.show();
link.removeClass('closed');
link.attr('title',closeTitle);
} else {
nextTr.hide();
link.addClass('closed');
link.attr('title',openTitle);
}
},
rowDetailToggle: function(obj, openTitle, closeTitle) {
jQuery('.tower-list-detail-txt:visible').slideUp('fast', function(){
jQuery(this).prevAll('.tower-list-detail-link').attr('title', openTitle).removeClass('close-detail');
});
var $_obj = (obj == null || obj == '')? '' : jQuery(obj),
detail = $_obj.nextAll('.tower-list-detail-txt'),
tr_container = $_obj.parents('tr.results-row:first');
if ($_obj.length > 0 && detail.length > 0) {
var totalW = tr_container.width(),
detailPadLeft = parseInt(detail.css('paddingLeft'),10),
detailPadRight = parseInt(detail.css('paddingRight'), 10),
finalW = totalW - detailPadLeft - detailPadRight;
detail.width(finalW);
if(detail.is(':visible')) {
detail.slideUp('fast', function(){
$_obj.attr('title', openTitle);
$_obj.removeClass('close-detail');
});
} else {
detail.slideDown('fast', function(){
$_obj.addClass('close-detail');
$_obj.attr('title', closeTitle);
});
}
}
},
resizePortletHeight: function(element, container, expand, ns, type_id) {
var _ns = ns != null ? (type_id != null ? ns + type_id : ns) : '',
obj = jQuery('#'+_ns+element+', .'+_ns+element),
objH = obj.outerHeight(),
containerObj = jQuery('#'+_ns+container+', .'+_ns+container);
if (containerObj.length > 0) {
var containerObjH = containerObj.outerHeight(),
containerMargin = parseInt(containerObj.css('marginBottom'), 10);
if(expand) {
if(objH > containerObjH) {
var diff = objH - containerObjH;
containerObj.css('marginBottom',containerMargin + diff + 'px');
containerObj.addClass('oriMargin_' + containerMargin + '_');
}
} else {
if(containerObj.attr('class').indexOf('oriMargin_') != -1) {
var oriMargin = parseInt(containerObj.attr('class').split('oriMargin_')[1].split('_')[0], 10);
containerObj.css('marginBottom', oriMargin + 'px');
containerObj.removeClass('oriMargin_' + oriMargin +'_');
}
}
}
},
resizeInputFile: function(targetId) {
var instance = this,
fileTarget = jQuery(targetId),
fileSize = fileTarget.parent().innerWidth() / 10;
fileTarget.attr('size', Math.round(fileSize));
},
getWindowWidth: function(defaultValue) {
var windowWidth = defaultValue;
if( typeof( window.innerWidth ) == 'number' ) { //Non-IE
windowWidth = window.innerWidth;
} else if( document.documentElement && ( document.documentElement.clientWidth ) ) { //IE 6+ in 'standards compliant mode'
windowWidth = document.documentElement.clientWidth;
} else if( document.body && ( document.body.clientWidth ) ) { //IE 4 compatible
windowWidth = document.body.clientWidth;
}
return windowWidth;
},
getWindowHeight: function(defaultValue) {
var windowHeight = defaultValue;
if( typeof( window.innerHeight ) == 'number' ) { //Non-IE
windowHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode'
windowHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientHeight ) ) { //IE 4 compatible
windowHeight = document.body.clientHeight;
}
return windowHeight;
},
addParamURL: function(url, name, value) {
var newUrl = url;
newUrl += '&' + name + '=' + escape(value);
return newUrl;
},
addParamArrayURL: function(url, name, values) {
var newUrl = url;
for ( var i = 0; i < values.length; i++) {
newUrl += '&' + name + '=' + escape(value);
}
return newUrl;
},
splitOFReference: function(ref) {
if (ref.indexOf('/') != -1) {
return ref.split('/');
} else {
return [ref];
}
},
getOFContentURL: function(pid, contentType, dsId) {
var url = towerDisplay.getOFDDPath();
url += '?' + 'pid=' + pid + '&contentType=' + contentType;
if (dsId) url += '&dsId=' + dsId;
return url;
},
getOFElaborateURL: function(ofType, pid, contentType, sourceElaborationId, op, queryString) {
var url = themeDisplay.getPathContext() + '/towerOFElaborate/?';
url += 'ofType=' + ofType + '&pid=' + pid + '&contentType=' + contentType;
if (sourceElaborationId) url += '&sourceElaborationId=' + sourceElaborationId;
url += '&op=' + op;
if (queryString) {
if (queryString.indexOf('?') != -1) queryString = queryString.substr(queryString.indexOf('?') + 1);
if (queryString != '') {
if (queryString.charAt(0) != '&') queryString = '&' + queryString;
url += queryString;
}
}
return url;
},
getEUTContentURL: function(pid, dsId, type, download, fileName) {
var url = towerDisplay.getEUTDDPath();
url += '?' + 'pid=' + pid;
if (download) {
url += '&contentType=application/octet-stream';
if (fileName) url += '&fileName=' + fileName;
} else {
if (type && type == 'xsl') {
url += '&contentType=text/xml';
} else {
url += '&contentType=text/plain';
}
}
if (dsId) url += '&dsId=' + dsId;
return url;
},
getValuesAndLabelsByCategories: function(categoryId, inclusive, depth, excludes) {
var instance = this,
cats = [],
prefix = '',
i = 0;
if (inclusive) {
var category =  Liferay.Service.Tower.TCategory.getTCategory({categoryId: categoryId});
cats[i] = {"value": category.categoryId, "label": category.name};
prefix = category.name + ' / ';
i++;
}
var children = instance._getValuesAndLabelsByCategories(categoryId, depth, excludes, prefix);
for (var y = 0; y < children.length; y++) {
cats[i] = children[y];
i++;
}
return cats;
},
_getValuesAndLabelsByCategories: function(categoryId, depth, excludes, prefix) {
var instance = this,
cats = [];
if (depth > 0) {
var categories = Liferay.Service.Tower.TCategory.getTCategories({
serviceParameterTypes: 'long,[Ljava.lang.String;',
parentCategoryId: categoryId,
excludes: excludes.join(',')
});
var j = 0;
jQuery.each(categories, function(i, category) {
cats[j] = {"value": category.categoryId, "label": prefix + category.name};
j++;
var children = instance._getValuesAndLabelsByCategories(category.categoryId, (depth - 1), excludes, prefix + category.name + ' / ');
for (var y = 0; y < children.length; y++) {
cats[j] = children[y];
j++;
}
});
}
return cats;
},
arrayContains: function(values, test) {
values = values || [];
test = test || '';
for (var i = 0; i < values.length; i++) {
if (values[i] == test) {
return true;
}
}
return false;
},
getTreePlids: function(projectId, channelId, privateLayout, parentLayoutId) {
var instance = this,
tlayouts = Liferay.Service.Tower.TLayout.getTLayouts({
serviceParameterTypes: 'long,long,boolean,long',
groupId: projectId,
channelId: channelId,
privateLayout: privateLayout,
parentLayoutId: parentLayoutId
});
var plids = [];
jQuery.each(tlayouts, function(i, tlayout) {
plids.push(tlayout.plid);
var layout = Liferay.Service.Portal.Layout.getLayout({
serviceParameterTypes: 'long',
plid: tlayout.plid
});
plids.push(instance.getTreePlids(projectId, channelId, privateLayout, layout.layoutId));
});
return plids;
},
checkAll: function(form, name, allBox, callback) {
var checkedInputs,
notCheckedInputs;
if(Liferay.Util.isArray(name)) {
var names = 'input[name="'+ name.join('"]:checkbox:checked, input[name="') + '"]:checkbox:checked';
checkedInputs = jQuery(names, form);
names = 'input[name="'+ name.join('"]:checkbox:not(:checked), input[name="') + '"]:checkbox:not(:checked)';
notCheckedInputs = jQuery(names, form);
} else {
checkedInputs = jQuery('input[name="' + name + '"]:checkbox:checked', form);
notCheckedInputs = jQuery('input[name="' + name + '"]:checkbox:not(:checked)', form);
}
var hasCallback = jQuery.isFunction(callback);
var values = new Array();
if (allBox.checked) {
notCheckedInputs.attr('checked', allBox.checked);
if (hasCallback) {
notCheckedInputs.each(function(i, input) {
values.push(jQuery(input).val());
});
}
} else {
checkedInputs.attr('checked', allBox.checked);
if (hasCallback) {
checkedInputs.each(function(i, input) {
values.push(jQuery(input).val());
});
}
}
if (hasCallback) callback.call(this, values, allBox);
},
checkAllBox: function(form, box, allBox, callback) {
var totalBoxes = 0,
totalOn = 0,
inputs,
name = box.name;
if (Liferay.Util.isArray(name)) {
var names = 'input[name="'+ name.join('"]:checkbox, input[name="') + '"]:checkbox';
inputs = jQuery(names, form);
} else {
inputs = jQuery('input[name="' + name + '"]:checkbox', form);
}
inputs = inputs.not(allBox);
totalBoxes = inputs.length;
totalOn = inputs.filter(':checked').length;
allBox.checked = (totalBoxes == totalOn);
if (jQuery.isFunction(callback)) callback.call(this, jQuery(box).val(), box);
},
saveOnlineTreeVisibility: function(ns, userId) {
// NOTE: FUNZIONA AL CONTRARIO ma i nomi delle chiavi sono rimasti uguali
if (jQuery('#'+ ns +'online-tree').hasClass('closed')) {
jQuery('#'+ ns +'online-tree').removeClass('closed');
jQuery('#'+ ns +'staging-tree').removeClass('opened');
jQuery.ajax({
url: themeDisplay.getPathMain() + '/portal/session_tree_js_click',
data: {
nodeId: userId,
openNode: false,
treeId: ns + 'onlineTreeOpened'
}
});
} else {
jQuery('#'+ ns +'online-tree').addClass('closed');
jQuery('#'+ ns +'staging-tree').addClass('opened');
jQuery.ajax({
url: themeDisplay.getPathMain() + '/portal/session_tree_js_click',
data: {
nodeId: userId,
openNode: true,
treeId: ns + 'onlineTreeOpened'
}
});
}
},
confirmGroupIds: function(ns, groupDialog, callback) {
var groupIds = groupDialog.find('input.tower-groupIdTMP:checked'),
values = [],
addDerived = [];
groupIds.each(function(){
var obj = jQuery(this),
derived = obj.nextAll('input.tower-groupAddDerivedTMP').is(':checked');
values.push(this.value);
addDerived.push(derived);
});
jQuery('#'+ ns +'groupIds').val(values.join(','));
jQuery('#'+ ns +'groupAddDerived').val(addDerived.join(','));
callback.call(this, values, addDerived);
},
 resetGroupIds: function(ns, groupDialog, groupsPopup) {
var groupIds = jQuery('#'+ ns +'groupIds'),
groupIdsArray = groupIds.val().split(',');
groupDialog.find('input').removeAttr('checked');
for(var i = 0; i < groupIdsArray.length; i++){
groupDialog.find('input[value="'+ groupIdsArray[i] +'"]').attr('checked', 'checked');
groupDialog.find('#'+ ns +'groupAddDerived'+ groupIdsArray[i]).attr('checked', 'checked');
}
Liferay.Popup.close(groupsPopup);
},
confirmUserGroupIds: function(ns, userGroupDialog, callback) {
var userGroupIds = userGroupDialog.find('input.tower-userGroupIdTMP:checked'),
values = [];
userGroupIds.each(function(){
values.push(this.value);
});
jQuery('#'+ ns +'userGroupIds').val(values.join(','));
callback.call(this, values);
},
 resetUserGroupIds: function(ns, userGroupDialog, userGroupsPopup) {
var userGroupIds = jQuery('#'+ ns +'userGroupIds'),
userGroupIdsArray = userGroupIds.val().split(',');
userGroupDialog.find('input').removeAttr('checked');
for(var i = 0; i < userGroupIdsArray.length; i++){
userGroupDialog.find('#'+ ns +'userGroupId'+ userGroupIdsArray[i]).attr('checked', 'checked');
}
Liferay.Popup.close(userGroupsPopup);
},
confirmVisibilityWorkspaceIds: function(ns, visibilityWorkspaceDialog, callback, suffix) {
var visibilityWorkspaceIds = visibilityWorkspaceDialog.find('input.tower-visibilityWorkspaceIdTMP:checked'),
values = [];
visibilityWorkspaceIds.each(function(){
values.push(this.value);
});
jQuery('#'+ ns +'visibilityWorkspaceIds'+ suffix).val(values.join(','));
callback.call(this);
},
 resetVisibilityWorkspaceIds: function(ns, visibilityWorkspaceDialog, visibilityWorkspacePopup, suffix) {
var visibilityWorkspaceIds = jQuery('#'+ ns +'visibilityWorkspaceIds'+ suffix),
visibilityWorkspaceIdsArray = visibilityWorkspaceIds.val().split(',');
visibilityWorkspaceDialog.find('input').removeAttr('checked');
for(var i = 0; i < visibilityWorkspaceIdsArray.length; i++){
visibilityWorkspaceDialog.find('#'+ ns +'visibilityWorkspaceId'+ suffix + visibilityWorkspaceIdsArray[i]).attr('checked', 'checked');
}
Liferay.Popup.close(visibilityWorkspacePopup);
}
};
jQuery.fn.towerConfiguration = function() {
var obj = this,
content = obj.find('.portlet'),
body = jQuery('body'),
cover = null,
config = null,
documentH = jQuery(document).height();
function init() {
if(obj.length > 0) createHtml();
}
function createHtml() {
cover = jQuery('<div class="tower-portlet-config-cover"></div>');
config = jQuery('<div class="tower-portlet-config"></div>');
cover.appendTo(body).height(documentH).show();
config.appendTo(body).show();
populateConfig();
}
function populateConfig() {
var contentH = content.height(),
clone = content.clone(true);
/* IE */
if(parseFloat(jQuery.fn.jquery) < 1.3 && document.compatMode && document.all) {
content.find('select').each(function(i){
var tag = this.tagName.toLowerCase(),
name = this.name,
selectedIndex = this.selectedIndex;
clone.find('select[name="'+ name +'"]').each(function(){
this.selectedIndex = selectedIndex;
});
});
}
//obj.height(contentH);
clone.find('script').remove();
clone.appendTo(config);
cover.height(jQuery(document).height());
content.remove();
}
init();
return obj;
}
