/*
* Copyright 2013 Anyware Services
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This class controls a ribbon button enabled if at least one page on the current selection fills the following conditions:<br/>
* - page is not a page of redirection<br/>
* - page is modifiable<br/>
* - user has convenient right on page
* @private
*/
Ext.define('Ametys.plugins.web.page.controller.TemplateMenuController', {
extend: 'Ametys.web.controller.WebButtonController',
/**
* @property {String[]} [_pageIds=[]] List of identifiers of pages concerned by the action of the controller
* @private
*/
constructor: function(config)
{
config['toggle-enabled'] = true;
this.callParent(arguments);
this._pageIds = [];
Ametys.message.MessageBus.on(Ametys.message.Message.MODIFIED, this._onModified, this);
},
/**
* Listener when the page has changed.
* Will update the available items upon the current selection.
* @param {Ametys.message.Message} message The modified message.
* @protected
*/
_onModified: function (message)
{
// FIXME CMS-6419 DO NOT WORK because CommonController compare 2 selection targets to determine if a refresh is needed.
// Here we have a MODIFIED message different from current selection. But the current selection and last selection are equals.
if (this.updateTargetsInCurrentSelectionTargets (message))
{
this.refresh();
}
},
updateState: function()
{
this._getStatus(this.getMatchingTargets());
},
/**
* Get the matching targets that match set link page conditions
* @return {Ametys.message.MessageTarget[]} targets filling all conditions.
*/
getAllRightPageTargets: function ()
{
var matchingTargets = this.getMatchingTargets();
var me = this;
return Ext.Array.filter (matchingTargets, function (target) {
return Ext.Array.contains(me._pageIds, target.getParameters().id)
});
},
/**
* Get the lock state of given targets
* @param targets The page targets
* @private
*/
_getStatus: function (targets)
{
this.disable();
var pageIds = [];
for (var i=0; i < targets.length; i++)
{
pageIds.push(targets[i].getParameters().id);
}
this.serverCall ('getStatus', [pageIds], this._getStatusCb, { errorMessage: true, refreshing: true });
},
/**
* @private
* Callback function called after retrieving the status of page targets
* @param params The JSON result
*/
_getStatusCb: function (params)
{
var allRightPages = params['allright-pages'];
this._pageIds = [];
var templates = params.templates;
var atLeastOneTemplatePage = false;
var id = this.getId();
Ext.Array.each (this.getReferencedControllerIds(), function (controlId) {
// handle just menu item galery button
if (controlId.startsWith(id + '.'))
{
var elmt = Ametys.ribbon.RibbonManager.getUI(controlId);
var available = Ext.Array.contains(templates, elmt.name);
elmt.setDisabled(!available);
elmt.setAdditionalDescription(available ? "": "{{i18n PLUGINS_WEB_PAGE_TEMPLATESMENU_UNAVAILABLE_DESCRIPTION}}");
var toggleTemplate = false;
Ext.Array.each (allRightPages, function (page) {
if (page.template == elmt.name)
{
toggleTemplate = true;
atLeastOneTemplatePage = true;
}
});
elmt.toggle(toggleTemplate);
}
});
this.toggle (atLeastOneTemplatePage);
if (allRightPages.length > 0)
{
for (var i=0; i < allRightPages.length; i++)
{
this._pageIds.push(allRightPages[i].id)
}
this.enable();
}
else
{
this.disable();
}
this._updateTooltipDescription(this.getInitialConfig('description'), params);
},
/**
* @protected
* Update the tooltip description according state of the current selection
* @param description The initial description. Can be empty.
* @param params The JSON result received
*/
_updateTooltipDescription: function (description, params)
{
description = this._handlingMultiple(description, 'allright', params['allright-pages']);
description = this._handlingMultiple(description, "noright", params['noright-pages']);
description = this._handlingMultiple(description, "nomodifiable", params['nomodifiable-pages']);
this.setDescription (description);
},
/**
* @private
* Add text to description
* @param description The initial description to concatenate. Can be empty.
* @param {String} prefix The parameters prefix to used to retrieve the start and end description. The start and end description are retrieved from initial configuration with [prefix]-start-description and [prefix]-end-description
* @param {Object[]} pages The concerned pages. If empty, no text will be concatenated
*/
_handlingMultiple: function(description, prefix, pages)
{
if (pages.length > 0)
{
if (description != "")
{
description += "<br/><br/>";
}
description += this.getInitialConfig(prefix + "-start-description");
for (var i=0; i < pages.length; i++)
{
if (i != 0)
{
description += ", ";
}
description += pages[i].description;
}
description += this.getInitialConfig(prefix + "-end-description");
}
return description;
}
});