/*
* Copyright 2015 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 controller controls move operations to perform on aliases
*/
Ext.define('Ametys.plugins.web.alias.MoveAliasController', {
extend: 'Ametys.ribbon.element.ui.ButtonController',
/**
* @cfg {String} role the role of the button. Can be "move-first", "move-up", "move-down" or "move-last"
*/
contructor: function(config)
{
this.callParent(arguments);
this.role = config.role;
Ametys.message.MessageBus.on(Ametys.message.Message.MOVED, this._onMoved, this);
},
/**
* @private
* Listener when an alias was moved
* Will update the state of the buttons effectively upon the current selection.
* @param {Ametys.message.Message} message The moved message.
*/
_onMoved: function (message)
{
if (this.updateTargetsInCurrentSelectionTargets(message))
{
this.refresh();
}
},
updateState: function()
{
this._getStatus(this.getMatchingTargets());
},
areSameTargets: function(target1, target2)
{
var match = this.callParent(arguments);
if (match && target1.getParameters().index != null && target2.getParameters().index != null)
{
return target1.getParameters().index == target2.getParameters().index;
}
else
{
return match;
}
},
/**
* @private
* Get the status of the selected alias targets
* @param targets The alias targets
*/
_getStatus: function (targets)
{
targets.length == 1 && this._testAlias(targets[0]) ? this.enable() : this.disable();
},
/**
* @private
* Test alias position
* @return {Ametys.message.MessageTarget} The alias target to check
*/
_testAlias: function(target)
{
var role = this.role,
index = target.getParameters().index,
size= target.getParameters().size;
return (((role == 'move-up' || role == 'move-first') && index != 0) || ((role == 'move-down' || role == 'move-last') && index != size - 1)) ? true : false;
}
});