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

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

    //===================================
    // PROPERTIES
    //===================================

    get_kendo: function () {
        return this.get_grid().get_kendo();
    },

    get_viewStateId: function () {
        return this.get_grid().get_viewStateId();
    },

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

    getOptionSettings: function () {
        return {
            resizable: true
        };
    },

    start: function () {
        this.get_kendo().bind("columnResize", $.proxy(this._onColumnResize, this));
        this._configureColumns();
    },

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

    _configureColumns: function () {
    
        var that = this,
            grid = this.get_kendo(),
            columns = this.get_grid().get_configuration().get_columns();

        _.each(columns, function (column, index) {
            that._setTextOverflow(column);
        });

    },

    _setTextOverflow: function (column, newWidth) {

        var width = newWidth || column.width,
            $header = this.get_kendo().thead.find(".k-header[data-field=" + column.field + "]");

        if (column.title === "&nbsp;") {

            $header.find(".k-link").css({
                textOverflow: width < 30 ? "clip" : "ellipsis"
            });

        }

    },

    _onColumnResize: function (e) {

        var that = this;

        PD.ajax({
            url: PD.Path.resolve("~/viewcolumn/setwidth"),
            type: "POST",
            data: {
                viewStateId: this.get_viewStateId(),
                columnId: e.column.field,
                width: e.newWidth
            },
            success: function () {
                that.get_kendo()._rowHeight = 0;
                that.get_kendo().virtualScrollable.refresh();
            }
        });

    }

});