/*
 *  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 smart button with a gallery of content types.
 * On menu show event, the items of the gallery will be toggle/untoggle according the current selected content
 */
Ext.define('Ametys.plugins.cms.content.controller.ContentTypeGalleryController', {
	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 target = this.getMatchingTargets()[0];
        var content = target.getParameters().content
        
		var cTypes = content.getTypes();

        // Iterating over each item in galleries
        this._getGalleries(menu.up()).each(function (gallery) 
        {
            gallery.items.each(function (item) {
                var contentTypeController = Ametys.ribbon.RibbonManager.getUI(item.controlId);
                var cTypeId = contentTypeController.getInitialConfig('contentTypes');

                var state = Ext.Array.contains(cTypes, cTypeId);
                contentTypeController.toggle(state);
            });
        });
	}
});