/*
* 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 representing the validated state of a comment
* @private
*/
Ext.define('Ametys.plugins.cms.comment.controller.CommentController', {
extend: 'Ametys.ribbon.element.ui.ButtonController',
/**
* @cfg {Boolean|String} enable-on-validated-only True to enable the controller only when comment is validated
*/
/**
* @cfg {Boolean|String} enable-on-invalidated-only True to enable the controller only when comment is invalidated
*/
/**
* @property {Object[]} _matchingComments The matching comments
* @private
*/
constructor: function(config)
{
this.callParent(arguments);
this._matchingComments = [];
Ametys.message.MessageBus.on(Ametys.message.Message.MODIFIED, this._onModified, this);
},
/**
* Listener when the content has been modified
* Will update the state of the buttons effectively upon the current selection.
* @param {Ametys.message.Message} message The modified message.
* @protected
*/
_onModified: function (message)
{
if (this.updateTargetsInCurrentSelectionTargets(message))
{
this.refresh();
}
},
updateState: function()
{
this._getComments(this.getMatchingTargets());
},
/**
* Get the alert state of given targets
* @param targets The content targets
* @private
*/
_getComments: function (targets)
{
var comments = {};
for (var i=0; i < targets.length; i++)
{
var contentId = targets[i].getParameters().content;
if (!comments[contentId])
{
comments[contentId] = [];
}
comments[contentId].push(targets[i].getParameters().id);
}
this.disable();
this.serverCall ('getComments', [comments], this._getCommentsCb, { errorMessage: true, refreshing: true });
},
/**
* @private
* Callback function called after retrieving the 'alert' state of content targets
* @param params The JSON result
*/
_getCommentsCb: function (params)
{
var comments = params['comments'];
var noRightContents = params['noright-contents'] || [];
this._matchingComments = [];
var validatedComments = [];
var invalidatedComments = [];
var enabledOnValidatedOnly = this.getConfig("enable-on-validated-only") == "true";
var enabledOnInvalidatedOnly = this.getConfig("enable-on-invalidated-only") == "true";
if (comments.length > 0)
{
for (var i=0; i < comments.length; i++)
{
var isValidated = comments[i].validated;
if ((!enabledOnValidatedOnly && !enabledOnInvalidatedOnly) || enabledOnValidatedOnly && isValidated || enabledOnInvalidatedOnly && !isValidated)
{
this._matchingComments.push({
id: comments[i].id,
content: comments[i].contentId
})
}
}
}
if (this._matchingComments.length == 0)
{
this.disable();
this.setDescription(this.getInitialConfig('disabled-description') || this.getInitialConfig('description') );
}
else
{
this.enable();
this.setDescription(this.getInitialConfig('description'));
}
if (noRightContents.length > 0)
{
var description = this.getInitialConfig("noright-start-description");
for (var i=0; i < noRightContents.length; i++)
{
if (i != 0)
{
description += ", ";
}
description += noRightContents[i].title;
}
description += this.getInitialConfig("noright-end-description");
this.setAdditionalDescription(description);
}
else
{
this.setAdditionalDescription('');
}
}
});