/*
* 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 blank page<br/>
* - page is modifiable<br/>
* - user has convenient right on page
* @private
*/
Ext.define('Ametys.plugins.web.page.controller.BlankController', {
extend: 'Ametys.web.controller.WebButtonController',
/**
* @property {String[]} [_pageIds=[]] List of identifiers of pages concerned by the action of the controller
* @private
*/
constructor: function(config)
{
this.callParent(arguments);
this._pageIds = [];
Ametys.message.MessageBus.on(Ametys.message.Message.MODIFIED, this._onModified, this);
},
/**
* Listener when the lock has changed.
* Will update the lock state of the buttons effectively upon the current selection.
* @param {Ametys.message.Message} message The lock changed message.
* @protected
*/
_onModified: function (message)
{
if (this.updateTargetsInCurrentSelectionTargets (message))
{
this.refresh();
}
},
updateState: function()
{
this._getStatus(this.getMatchingTargets());
},
/**
* Get the matching targets that match set blank 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)
{
var result = this._calculateStatus(targets);
this._getStatusCb(result);
},
/**
* Simulation of getStatus server side, done only client-side.
* @param {Object[]} pageTargets page target see Ametys.ribbon.element.ui.CommonController#getMatchingTargets
* @return {Object} a map of arrays representing contents in different states that will be sent to _getStatusCb
*/
_calculateStatus: function(pageTargets)
{
var result = {};
result["nomodifiable-pages"] = [];
result["noright-pages"] = [];
result["blank-pages"] = [];
result["noblank-pages"] = [];
Ext.Array.each(pageTargets, function(pageTarget)
{
var page = null;
if (pageTarget)
{
page = pageTarget.getParameters().page;
}
if (page != null)
{
if (!page.getIsModifiable())
{
var i18nStr = this.getConfig("nomodifiable-page-description");
var description = Ext.String.format(i18nStr, page.getTitle());
var contentParam = this._getPageDefaultParameters(page);
contentParam["description"] = description;
result["nomodifiable-pages"].push(contentParam);
}
else if (!this.hasRightOnAny([pageTarget]))
{
var i18nStr = this.getConfig("noright-page-description");
var description = Ext.String.format(i18nStr, page.getTitle());
var contentParam = this._getPageDefaultParameters(page);
contentParam["description"] = description;
result["noright-pages"].push(contentParam);
}
else if (page.getType() == "NODE")
{
var i18nStr = this.getConfig("blank-page-description");
var description = Ext.String.format(i18nStr, page.getTitle());
var contentParam = this._getPageDefaultParameters(page);
contentParam["description"] = description;
result["blank-pages"].push(contentParam);
}
else
{
var i18nStr = this.getConfig("noblank-page-description");
var description = Ext.String.format(i18nStr, page.getTitle());
var contentParam = this._getPageDefaultParameters(page);
contentParam["description"] = description;
result["noblank-pages"].push(contentParam);
}
}
}, this);
return result;
},
/**
* create a json object representing a page
* @private
* @param {Ametys.web.page.Page} page The page to analyse
* @return {Object} containing id and title
*/
_getPageDefaultParameters : function(page)
{
var pageParams = {};
pageParams["id"] = page.getId();
pageParams["title"] = page.getTitle();
return pageParams;
},
/**
* @private
* Callback function called after retrieving the status of page targets
* @param params The JSON result
*/
_getStatusCb: function (params)
{
this._pageIds = [];
var allRightPages = params['noblank-pages'];
if (allRightPages.length > 0)
{
for (var i=0; i < allRightPages.length; i++)
{
this._pageIds.push(allRightPages[i].id)
}
this.enable();
}
else
{
this.disable();
}
this.toggle(params['blank-pages'].length > 0);
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, 'noblank', params['noblank-pages']);
description = this._handlingMultiple(description, "blank", params['blank-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;
}
});