/**
 * Gestion des RowActions
 *
 * @package Imhotep
 */

Ext.ns('Imhotep.ux');

Imhotep.ux.RowActions = Class.create({

	/**
	 * @cfg {Ext.grid.GridPanel} grid the grid panel
	 */

	/**
	 * @cfg {object} add the actions to add for RowActions plugin
	 */

    /**
	 * @cfg {array} remove the actions to remove
	 */


    _actions: {},
    _actionsObject: null,

    initialize: function(config) {
        Ext.apply(this, config);
        this.initActions();
        if (this.remove) {
            this.removeActions(this.remove);
        }
        if (this.add) {
            this.addActions(this.add);
        }
        this._loadActions();
    },

    getObject: function() {
        return this._actionsObject;
    },

    initActions: function() {},


    removeActions: function(actions) {
        var to_keep = {};
        for (var i in this._actions) {
            if (actions.indexOf(i) == -1) {
                to_keep[i] = this._actions[i];
            }
        }
        this._actions = to_keep;
    },

    addActions: function(actions) {
        this._actions = Ext.applyIf(actions, this._actions);
    },

    _loadActions: function() {
        var action_items = [];
        for (var i in this._actions) {
            if (typeof(this._actions[i].action) != 'undefined') {
                action_items.push(this._actions[i].action);
            }
        }

        // création du RowActions Plugin
        var action = new Ext.ux.grid.RowActions({
            header: '&nbsp;',
            actions: action_items
        });

        // les actions lorsqu'on clique sur les boutons d'action
        var rowaction_manager = this;
        action.on({
            action: function(grid, record, action) {
                if (typeof(rowaction_manager._actions[action].listener) == 'function') {
                    rowaction_manager._actions[action].listener(grid, record);
                }
            }
        });

        this._actionsObject = action;
    }

});