/*
 *  Copyright 2014 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 provides a TreePanel of the explorer that displays attachments.
 */
Ext.define('Ametys.cms.attach.AttachmentsExplorerTree', {
	extend: 'Ametys.explorer.tree.ExplorerTree',
	
	/**
	 * @cfg {String/RegExp/Function} ownerMessageTargetFilter (required) the target filter to be used on selection message to find the tree owner.
	 */
	
	/**
	 * @cfg {String} rightId (required) The id of right to test for tree in-line edition (rename / drag&drop)
	 */
	
	getRightIdOnRename: function (node)
	{
		return this.rightId;
	},
	
	getRightIdOnDrop: function (node)
	{
		return this.rightId;
	},
	
	getRightIdOnDrag: function (node)
	{
		return this.rightId;
	},
	
	hasRight: function(rightId, node, callback)
	{
		if (!rightId)
		{
			return true;
		}
		
		// For attachments, the rights are check on owner target (instead of resource itself)
		var me = this,
			rights = [],
			message = Ametys.message.MessageBus.getCurrentSelectionMessage(),
			target = message.getTarget(me.ownerMessageTargetFilter);
	
		if (target)
		{
			rights = target.getParameters().rights || [];
		}
		
		if (Ext.Array.contains(rights, rightId))
		{
			return true;
		}
		
		return false;
	}
});