|
8 | 8 | <script type="text/javascript" src="../../ext4/ext-all-debug.js"></script>
|
9 | 9 | <script type='text/javascript'>
|
10 | 10 |
|
| 11 | +/** |
| 12 | + * Require dependencies |
| 13 | + */ |
11 | 14 | Ext.require(['Ext.data.*', 'Ext.window.Window', 'Ext.tree.*']);
|
12 | 15 |
|
| 16 | + |
| 17 | +/** |
| 18 | + * Delete confirmation handler |
| 19 | + */ |
13 | 20 | var onConfirmDelete = function(answer, value, cfg, button) {
|
14 |
| - if (answer != 'yes') { |
| 21 | + if (answer !== 'yes') { |
15 | 22 | return;
|
16 | 23 | }
|
17 | 24 |
|
18 | 25 | var menu = button.up(),
|
19 |
| - node = menu.treeNode; |
| 26 | + node = menu.treeNode; |
20 | 27 |
|
21 | 28 | node.remove(true);
|
22 | 29 | };
|
| 30 | + |
| 31 | +/** |
| 32 | + * Handler for delete menu item |
| 33 | + */ |
23 | 34 | var onDelete = function(button) {
|
24 | 35 | var callback = Ext.bind(onConfirmDelete, undefined, [button], true);
|
25 | 36 | Ext.Msg.confirm(
|
26 |
| - 'Approve deletion', |
27 |
| - 'Are you sure you want to delete this node?', |
28 |
| - callback |
| 37 | + 'Approve deletion', |
| 38 | + 'Are you sure you want to delete this node?', |
| 39 | + callback |
29 | 40 | );
|
30 | 41 | };
|
| 42 | + |
| 43 | +/** |
| 44 | + * Edit handler |
| 45 | + */ |
31 | 46 | var onEdit = function(button, node) {
|
32 | 47 | var menu = button.up(),
|
33 | 48 | node = node || menu.treeNode,
|
34 | 49 | view = menu.treeView,
|
35 | 50 | tree = view.ownerCt,
|
36 | 51 | selMdl = view.getSelectionModel(),
|
37 |
| - colHdr = tree.headerCt.getHeaderAtIndex(0); |
| 52 | + colHdr = tree.headerCt.getHeaderAtIndex(0), |
| 53 | + pos; |
38 | 54 |
|
39 | 55 | if (selMdl.getCurrentPosition) { //only in cellmodel selection model
|
40 | 56 | pos = selMdl.getCurrentPosition();
|
41 | 57 | colHdr = tree.headerCt.getHeaderAtIndex(pos.column);
|
42 | 58 | }
|
43 | 59 | treeEditor.startEdit(node, colHdr);
|
44 | 60 | };
|
| 61 | + |
| 62 | + |
| 63 | +/** |
| 64 | + * Create a new entry |
| 65 | + */ |
| 66 | +var doCreate = function(node, button) { |
| 67 | + var newNode = node.appendChild({text : 'New employee', leaf : true}); |
| 68 | + |
| 69 | + // Turn on editing right away |
| 70 | + onEdit(button, newNode); |
| 71 | +}; |
| 72 | + |
| 73 | + |
| 74 | +/** |
| 75 | + * Add item handler |
| 76 | + */ |
45 | 77 | var onAdd = function(button) {
|
46 | 78 | var menu = button.up(),
|
47 |
| - node = menu.treeNode, |
48 |
| - view = menu.treeView, |
49 |
| - delay = view.expandDuration + 50, |
50 |
| - newNode, |
51 |
| - doCreate; |
52 |
| - |
53 |
| - doCreate = function() { |
54 |
| - newNode = node.appendChild({text : 'New employee', leaf : true}); |
55 |
| - onEdit(button, newNode); |
56 |
| - }; |
| 79 | + node = menu.treeNode, |
| 80 | + view = menu.treeView, |
| 81 | + delay = view.expandDuration + 50; |
57 | 82 |
|
58 | 83 | if (!node.isExpanded()) {
|
59 |
| - node.expand(false, Ext.callback(doCreate, this, [], delay)); |
| 84 | + // expand and wait for animation before appending a new node |
| 85 | + node.expand(false, Ext.callback(doCreate, this, [node, button], delay)); |
60 | 86 | }
|
61 | 87 | else {
|
62 |
| - doCreate(); |
| 88 | + doCreate(node, button); |
63 | 89 | }
|
64 | 90 | };
|
65 | 91 |
|
| 92 | + |
| 93 | +/** |
| 94 | + * Context menu |
| 95 | + * @returns {Ext.menu.Menu} |
| 96 | + */ |
66 | 97 | var buildCtxMenu = function() {
|
67 | 98 | return Ext.create('Ext.menu.Menu', {
|
68 | 99 | items : [
|
|
81 | 112 | ]
|
82 | 113 | });
|
83 | 114 | };
|
84 |
| -//Configuring a context menu factory method |
| 115 | + |
| 116 | +/** |
| 117 | + * Configuring a context menu factory method |
| 118 | + */ |
| 119 | + |
85 | 120 | var onCtxMenu = function(view, record, element, index, evtObj) {
|
86 | 121 | view.select(record);
|
87 | 122 | evtObj.stopEvent();
|
|
90 | 125 | this.ctxMenu = buildCtxMenu();
|
91 | 126 | }
|
92 | 127 |
|
| 128 | + var ctxMenu = this.ctxMenu, |
| 129 | + addItem = ctxMenu.getComponent('add'), |
| 130 | + editItem = ctxMenu.getComponent('edit'), |
| 131 | + deleteItem = ctxMenu.getComponent('delete'); |
| 132 | + |
93 | 133 | this.ctxMenu.treeNode = record;
|
94 | 134 | this.ctxMenu.treeView = view;
|
95 | 135 |
|
96 |
| - var ctxMenu = this.ctxMenu; |
97 |
| - var addItem = ctxMenu.getComponent('add'); |
98 |
| - var editItem = ctxMenu.getComponent('edit'); |
99 |
| - var deleteItem = ctxMenu.getComponent('delete'); |
100 |
| - |
101 | 136 | if (record.getId() == 'mycompany') {
|
102 | 137 | addItem.setText('Add Department');
|
103 | 138 | editItem.setText('Nope, not changing the name');
|
|
142 | 177 |
|
143 | 178 | getEditingContext : function(record, columnHeader) {
|
144 | 179 | var me = this,
|
145 |
| - grid = me. |
146 |
| - |
147 |
| - if (Ext.isNumber(record)) { |
148 |
| - rowIdx = record; |
149 |
| - //record = grid, |
| 180 | + grid = me.grid, |
150 | 181 | store = grid.store,
|
151 | 182 | rowIdx,
|
152 | 183 | colIdx,
|
153 | 184 | view = grid.getView(),
|
154 | 185 | root = grid.getRootNode(),
|
155 |
| - value;store.getAt(rowIdx); |
| 186 | + value; |
| 187 | + |
| 188 | + if (Ext.isNumber(record)) { |
| 189 | + rowIdx = record; |
156 | 190 | record = root.getChildAt(rowIdx);
|
157 | 191 | }
|
158 | 192 | else {
|
159 |
| - //rowIdx = store.indexOf(record); |
160 | 193 | rowIdx = root.indexOf(record);
|
161 | 194 | }
|
162 | 195 | if (Ext.isNumber(columnHeader)) {
|
|
178 | 211 | rowIdx : rowIdx,
|
179 | 212 | colIdx : colIdx
|
180 | 213 | };
|
| 214 | + }, |
| 215 | + |
| 216 | + onEditComplete : function(ed, value, startValue) { |
| 217 | + var me = this, |
| 218 | + activeColumn = me.getActiveColumn(), |
| 219 | + context = me.context, |
| 220 | + record; |
| 221 | + |
| 222 | + if (activeColumn) { |
| 223 | + record = context.record; |
| 224 | + |
| 225 | + me.setActiveEditor(null); |
| 226 | + me.setActiveColumn(null); |
| 227 | + me.setActiveRecord(null); |
| 228 | + |
| 229 | + context.value = value; |
| 230 | + if (!me.validateEdit()) { |
| 231 | + return; |
| 232 | + } |
| 233 | + |
| 234 | + if (!record.isEqual(value, startValue)) { |
| 235 | + record.set(activeColumn.dataIndex, value); |
| 236 | + } |
| 237 | + |
| 238 | + if (Ext.isObject(context.view)) { |
| 239 | + context.view.focus(false, true); |
| 240 | + } |
| 241 | + |
| 242 | + me.fireEvent('edit', me, context); |
| 243 | + me.editing = false; |
| 244 | + } |
181 | 245 | }
|
| 246 | + |
182 | 247 | });
|
183 | 248 |
|
184 | 249 | var store = Ext.create('Ext.data.TreeStore', {
|
|
213 | 278 | }
|
214 | 279 | });
|
215 | 280 |
|
216 |
| -//{ptype: 'treecellediting', clicksToEdit: 1} |
217 |
| - |
218 | 281 | var treeEditor = Ext.create('TreeCellEditing', {clicksToEdit : 1});
|
219 | 282 |
|
220 | 283 | Ext.onReady(function() {
|
|
0 commit comments