Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / examples / ux / DataView / LabelEditor.js
1 /**
2  * @class Ext.ux.DataView.LabelEditor
3  * @extends Ext.Editor
4  */
5 Ext.define('Ext.ux.DataView.LabelEditor', {
6
7     extend: 'Ext.Editor',
8
9     alignment: 'tl-tl',
10
11     completeOnEnter: true,
12
13     cancelOnEsc: true,
14
15     shim: false,
16
17     autoSize: {
18         width: 'boundEl',
19         height: 'field'
20     },
21
22     labelSelector: 'x-editable',
23
24     requires: [
25         'Ext.form.field.Text'
26     ],
27
28     constructor: function(config) {
29         config.field = config.field || Ext.create('Ext.form.field.Text', {
30             allowBlank: false,
31             selectOnFocus:true
32         });
33         this.callParent([config]);
34     },
35
36     init: function(view) {
37         this.view = view;
38         this.mon(view, 'render', this.bindEvents, this);
39         this.on('complete', this.onSave, this);
40     },
41
42     // initialize events
43     bindEvents: function() {
44         this.mon(this.view.getEl(), {
45             click: {
46                 fn: this.onClick,
47                 scope: this
48             }
49         });
50     },
51
52     // on mousedown show editor
53     onClick: function(e, target) {
54         var me = this,
55             item, record;
56
57         if (Ext.fly(target).hasCls(me.labelSelector) && !me.editing && !e.ctrlKey && !e.shiftKey) {
58             e.stopEvent();
59             item = me.view.findItemByChild(target);
60             record = me.view.store.getAt(me.view.indexOf(item));
61             me.startEdit(target, record.data[me.dataIndex]);
62             me.activeRecord = record;
63         } else if (me.editing) {
64             me.field.blur();
65             e.preventDefault();
66         }
67     },
68
69     // update record
70     onSave: function(ed, value) {
71         this.activeRecord.set(this.dataIndex, value);
72     }
73 });
74