﻿///<reference path="Extjs_Intellisense.js" />

Ext.DataView.LabelEditor = function(cfg, field) {
    Ext.DataView.LabelEditor.superclass.constructor.call(this,
        field || new Ext.form.TextField({
            allowBlank: false,
            growMin: 90,
            growMax: 240,
            grow: true,
            selectOnFocus: false
        }), cfg
    );
}

Ext.extend(Ext.DataView.LabelEditor, Ext.Editor, {
    alignment: "tl-tl",
    hideEl: false,
    cls: "x-small-editor",
    shim: false,
    completeOnEnter: true,
    cancelOnEsc: true,
    labelSelector: 'div.x-editable',
    autoSize: false,

    init: function(view) {
        this.view = view;
        view.on('render', this.initEditor, this);
        this.on('complete', this.onSave, this);
    },

    initEditor: function() {
        this.view.getEl().on('mousedown', this.onMouseDown, this, { delegate: this.labelSelector });
    },

    onMouseDown: function(e, target) {
        if (!e.ctrlKey && !e.shiftKey) {
            var item = this.view.findItemFromChild(target);
            e.stopEvent();
            var record = this.view.store.getAt(this.view.indexOf(item));
            this.startEdit(target, record.data[this.dataIndex]);
            if (this.autoSize) {
                this.field.setWidth(Ext.get(target).getWidth() + 8);
                if (this.field.el.getWidth() < 200) this.field.setWidth(200);
                if (this.field.el.getHeight() < 50) this.field.setHeight(50);
            }
            this.activeRecord = record;
        } else {
            e.preventDefault();
        }
    },

    onSave: function(ed, value) {
        this.activeRecord.set(this.dataIndex, value);
    }
});

