3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.tree.TreeEditor
10 * Provides editor functionality for inline tree node editing. Any valid {@link Ext.form.Field} subclass can be used
11 * as the editor field.
13 * @param {TreePanel} tree
14 * @param {Object} fieldConfig (optional) Either a prebuilt {@link Ext.form.Field} instance or a Field config object
15 * that will be applied to the default field instance (defaults to a {@link Ext.form.TextField}).
16 * @param {Object} config (optional) A TreeEditor config object
18 Ext.tree.TreeEditor = function(tree, fc, config){
20 var field = fc.events ? fc : new Ext.form.TextField(fc);
21 Ext.tree.TreeEditor.superclass.constructor.call(this, field, config);
26 tree.on('render', this.initEditor, this);
28 this.initEditor(tree);
32 Ext.extend(Ext.tree.TreeEditor, Ext.Editor, {
34 * @cfg {String} alignment
35 * The position to align to (see {@link Ext.Element#alignTo} for more details, defaults to "l-l").
41 * @cfg {Boolean} hideEl
42 * True to hide the bound element while the editor is displayed (defaults to false)
47 * CSS class to apply to the editor (defaults to "x-small-editor x-tree-editor")
49 cls: "x-small-editor x-tree-editor",
52 * True to shim the editor if selects/iframes could be displayed beneath it (defaults to false)
58 * @cfg {Number} maxWidth
59 * The maximum width in pixels of the editor field (defaults to 250). Note that if the maxWidth would exceed
60 * the containing tree element's size, it will be automatically limited for you to the container width, taking
61 * scroll and client offsets into account prior to each edit.
65 * @cfg {Number} editDelay The number of milliseconds between clicks to register a double-click that will trigger
66 * editing on the current node (defaults to 350). If two clicks occur on the same node within this time span,
67 * the editor for the node will display, otherwise it will be processed as a regular click.
71 initEditor : function(tree){
74 beforeclick: this.beforeNodeClick,
75 dblclick: this.onNodeDblClick
79 complete: this.updateNode,
80 beforestartedit: this.fitToTree,
81 specialkey: this.onSpecialKey
83 this.on('startedit', this.bindScroll, this, {delay:10});
87 fitToTree : function(ed, el){
88 var td = this.tree.getTreeEl().dom, nd = el.dom;
89 if(td.scrollLeft > nd.offsetLeft){ // ensure the node left point is visible
90 td.scrollLeft = nd.offsetLeft;
94 (td.clientWidth > 20 ? td.clientWidth : td.offsetWidth) - Math.max(0, nd.offsetLeft-td.scrollLeft) - /*cushion*/5);
99 * Edit the text of the passed {@link Ext.tree.TreeNode TreeNode}.
100 * @param node {Ext.tree.TreeNode} The TreeNode to edit. The TreeNode must be {@link Ext.tree.TreeNode#editable editable}.
102 triggerEdit : function(node, defer){
104 if(node.attributes.editable !== false){
106 * The {@link Ext.tree.TreeNode TreeNode} this editor is bound to. Read-only.
107 * @type Ext.tree.TreeNode
110 this.editNode = node;
111 if(this.tree.autoScroll){
112 Ext.fly(node.ui.getEl()).scrollIntoView(this.tree.body);
114 var value = node.text || '';
115 if (!Ext.isGecko && Ext.isEmpty(node.text)){
116 node.setText(' ');
118 this.autoEditTimer = this.startEdit.defer(this.editDelay, this, [node.ui.textNode, value]);
124 bindScroll : function(){
125 this.tree.getTreeEl().on('scroll', this.cancelEdit, this);
129 beforeNodeClick : function(node, e){
130 clearTimeout(this.autoEditTimer);
131 if(this.tree.getSelectionModel().isSelected(node)){
133 return this.triggerEdit(node);
137 onNodeDblClick : function(node, e){
138 clearTimeout(this.autoEditTimer);
142 updateNode : function(ed, value){
143 this.tree.getTreeEl().un('scroll', this.cancelEdit, this);
144 this.editNode.setText(value);
149 Ext.tree.TreeEditor.superclass.onHide.call(this);
151 this.editNode.ui.focus.defer(50, this.editNode.ui);
156 onSpecialKey : function(field, e){
161 }else if(k == e.ENTER && !e.hasModifier()){
167 onDestroy : function(){
168 clearTimeout(this.autoEditTimer);
169 Ext.tree.TreeEditor.superclass.onDestroy.call(this);
170 var tree = this.tree;
171 tree.un('beforeclick', this.beforeNodeClick, this);
172 tree.un('dblclick', this.onNodeDblClick, this);