/*
 *  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.
 */

/**
 * @private
 * This class control a ribbon menu representing the workflow state.
 * It HAS to be used in conjunction with the java org.ametys.cms.clientsideelement.WorkflowStepsClientSideElement
 */
Ext.define('Ametys.plugins.cms.content.controller.MixinGalleryController', {
	extend: 'Ametys.plugins.cms.content.controller.SmartContentController',
	
	/**
	 * @inheritdoc
	 * Add a listener on 'menushow' event
	 */
	createUI: function ()
	{
		var elt = this.callParent(arguments);
		elt.on ('menushow', this._onMenuShow, this);
		return elt;
	},
	
	/**
	 * Listener on 'menushow' event<br>
	 * @param {Ext.button.Button} btn The button
	 * @param {Ext.menu.Menu} menu The menu
	 * @private
	 */
	_onMenuShow: function (btn, menu)
	{
        var content = this.getMatchingTargets()[0].getParameters().content;
		var mixins = content.getMixins();
		
		// Iterating over each item in gallery groups
        this._getGalleries(btn).each(function(gallery) {
            gallery.items.each(function (item) {
				var mixinController = Ametys.ribbon.RibbonManager.getUI(item.controlId);
				var mixinId = mixinController.getInitialConfig('contentTypes');

				var state = Ext.Array.contains(mixins, mixinId);
				mixinController.toggle(state);
            })
        });
	}
});