
/// class PD.Web.UI.Controls.GridRowContextMenuModule
/// using PD.Web.UI.Controls.GridModule

PD.Web.UI.Controls.GridRowContextMenuModule =
PD.Web.UI.Controls.GridModule.extend({

    //===================================
    // FIELDS
    //===================================

    m_menu: null,

    //===================================
    // PUBLIC METHODS
    //===================================

    start: function () {

        var option = this.getOption("contextMenu");

        if (_.isFunction(option)) {
            option(this.get_grid());
            return;
        }

        if (_.isArray(option) || _.isObject(option)) {

            this.m_menu = new PD.Web.UI.Controls.ContextMenu({
                target: this.get_kendo().content,
                filter: "TR[role=row][data-uid]",
                segments: this._getSegments(),
                animation: false
            });

            this.get_grid().get_children().add(this.m_menu);
            return;

        }

    },

    unload: function () {
        if (this.m_menu) {
            this.m_menu.remove();
        }
    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _getSegments: function () {

        var segments = this.getOption("contextMenu", []);
    
        if (!$.isArray(segments)) {
            segments = [segments];
        }

        segments = this._transformSegments(segments);

        return segments;

    },

    _transformSegments: function (segments) {

        var that = this,
            result = new PD.BCL.List(segments);

        result = result.transform(function (item) {
            var type = PD.Type.getType(item.type);
            return new type($.proxy(item.context, that.get_grid()));
        });

        return result.toArray();

    }

});