]> git.proxmox.com Git - extjs.git/blame - extjs/classic/classic/src/selection/CellModel.js
add extjs 6.0.1 sources
[extjs.git] / extjs / classic / classic / src / selection / CellModel.js
CommitLineData
6527f429
DM
1/**\r
2 * A selection model for {@link Ext.grid.Panel grid panels} which allows selection of a single cell at a time.\r
3 *\r
4 * Implements cell based navigation via keyboard.\r
5 *\r
6 * @example\r
7 * var store = Ext.create('Ext.data.Store', {\r
8 * fields: ['name', 'email', 'phone'],\r
9 * data: [\r
10 * { name: 'Lisa', email: 'lisa@simpsons.com', phone: '555-111-1224' },\r
11 * { name: 'Bart', email: 'bart@simpsons.com', phone: '555-222-1234' },\r
12 * { name: 'Homer', email: 'homer@simpsons.com', phone: '555-222-1244' },\r
13 * { name: 'Marge', email: 'marge@simpsons.com', phone: '555-222-1254' }\r
14 * ]\r
15 * });\r
16 *\r
17 * Ext.create('Ext.grid.Panel', {\r
18 * title: 'Simpsons',\r
19 * store: store,\r
20 * width: 400,\r
21 * renderTo: Ext.getBody(),\r
22 * columns: [\r
23 * { text: 'Name', dataIndex: 'name' },\r
24 * { text: 'Email', dataIndex: 'email', flex: 1 },\r
25 * { text: 'Phone', dataIndex: 'phone' }\r
26 * ],\r
27 * selModel: 'cellmodel'\r
28 * });\r
29 */\r
30Ext.define('Ext.selection.CellModel', {\r
31 extend: 'Ext.selection.DataViewModel',\r
32 alias: 'selection.cellmodel',\r
33 requires: [\r
34 'Ext.grid.CellContext'\r
35 ],\r
36\r
37 /**\r
38 * @cfg {"SINGLE"} mode\r
39 * Mode of selection. Valid values are:\r
40 *\r
41 * - **"SINGLE"** - Only allows selecting one item at a time. This is the default.\r
42 */\r
43\r
44\r
45 isCellModel: true,\r
46\r
47 /**\r
48 * @inheritdoc\r
49 */\r
50 deselectOnContainerClick: false,\r
51\r
52 /**\r
53 * @cfg {Boolean} enableKeyNav\r
54 * Turns on/off keyboard navigation within the grid.\r
55 */\r
56 enableKeyNav: true,\r
57\r
58 /**\r
59 * @cfg {Boolean} preventWrap\r
60 * Set this configuration to true to prevent wrapping around of selection as\r
61 * a user navigates to the first or last column.\r
62 */\r
63 preventWrap: false,\r
64\r
65 /**\r
66 * @event deselect\r
67 * Fired after a cell is deselected\r
68 * @param {Ext.selection.CellModel} this\r
69 * @param {Ext.data.Model} record The record of the deselected cell\r
70 * @param {Number} row The row index deselected\r
71 * @param {Number} column The column index deselected\r
72 */\r
73\r
74 /**\r
75 * @event select\r
76 * Fired after a cell is selected\r
77 * @param {Ext.selection.CellModel} this\r
78 * @param {Ext.data.Model} record The record of the selected cell\r
79 * @param {Number} row The row index selected\r
80 * @param {Number} column The column index selected\r
81 */\r
82\r
83 bindComponent: function(view) {\r
84 var me = this,\r
85 grid;\r
86\r
87 // Unbind from a view\r
88 if (me.view && me.gridListeners) {\r
89 me.gridListeners.destroy();\r
90 }\r
91\r
92 // DataViewModel's bindComponent\r
93 me.callParent([view]);\r
94\r
95 if (view) {\r
96 // view.grid is present during View construction, before the view has been\r
97 // added as a child of the Panel, and an upward link it still needed.\r
98 grid = view.grid || view.ownerCt;\r
99\r
100 if (grid.optimizedColumnMove !== false) {\r
101 me.gridListeners = grid.on({\r
102 columnmove: me.onColumnMove,\r
103 scope: me,\r
104 destroyable: true\r
105 });\r
106 }\r
107 }\r
108 },\r
109\r
110 getViewListeners: function() {\r
111 var result = this.callParent();\r
112 result.refresh = this.onViewRefresh;\r
113 return result;\r
114 },\r
115\r
116 getHeaderCt: function() {\r
117 var selection = this.navigationModel.getPosition(),\r
118 view = selection ? selection.view : this.primaryView;\r
119\r
120 return view.headerCt;\r
121 },\r
122\r
123 // Selection blindly follows focus. For now.\r
124 onNavigate: function(e) {\r
125 // It was a navigate out event.\r
126 // Or stopSelection was stamped into the event by an upstream handler.\r
127 // This is used by ActionColumn and CheckColumn to implement their stopSelection config\r
128 if (!e.record || e.keyEvent.stopSelection) {\r
129 return;\r
130 }\r
131\r
132 this.setPosition(e.position);\r
133 },\r
134\r
135 selectWithEvent: function(record, e) {\r
136 this.select(record);\r
137 },\r
138 /** \r
139 * Selects a cell by row / column.\r
140 *\r
141 * var grid = Ext.create('Ext.grid.Panel', {\r
142 * title: 'Simpsons',\r
143 * store: {\r
144 * fields: ['name', 'email', 'phone'],\r
145 * data: [{\r
146 * name: "Lisa",\r
147 * email: "lisa@simpsons.com",\r
148 * phone: "555-111-1224"\r
149 * }]\r
150 * },\r
151 * columns: [{\r
152 * text: 'Name',\r
153 * dataIndex: 'name'\r
154 * }, {\r
155 * text: 'Email',\r
156 * dataIndex: 'email',\r
157 * hidden: true\r
158 * }, {\r
159 * text: 'Phone',\r
160 * dataIndex: 'phone',\r
161 * flex: 1\r
162 * }],\r
163 * height: 200,\r
164 * width: 400,\r
165 * renderTo: Ext.getBody(),\r
166 * selType: 'cellmodel',\r
167 * tbar: [{\r
168 * text: 'Select position Object',\r
169 * handler: function() {\r
170 * grid.getSelectionModel().select({\r
171 * row: grid.getStore().getAt(0),\r
172 * column: grid.down('gridcolumn[dataIndex=name]')\r
173 * });\r
174 * }\r
175 * }, {\r
176 * text: 'Select position by Number',\r
177 * handler: function() {\r
178 * grid.getSelectionModel().select({\r
179 * row: 0,\r
180 * column: 1\r
181 * });\r
182 * }\r
183 * }]\r
184 * });\r
185 *\r
186 * @param {Object} pos An object with row and column properties\r
187 * @param {Ext.data.Model/Number} pos.row\r
188 * A record or index of the record (starting at 0)\r
189 * @param {Ext.grid.column.Column/Number} pos.column\r
190 * A column or index of the column (starting at 0). Includes visible columns only.\r
191 */\r
192 select: function(pos, /* private */ keepExisting, suppressEvent) {\r
193 var me = this,\r
194 row,\r
195 oldPos = me.getPosition(),\r
196 store = me.view.store;\r
197\r
198 if (pos || pos === 0) {\r
199 if (pos.isModel) {\r
200 row = store.indexOf(pos);\r
201 if (row !== -1) {\r
202 pos = {\r
203 row: row,\r
204 column: oldPos ? oldPos.column : 0\r
205 };\r
206 } else {\r
207 pos = null;\r
208 } \r
209 } else if (typeof pos === 'number') {\r
210 pos = {\r
211 row: pos,\r
212 column: 0\r
213 };\r
214 }\r
215 } \r
216\r
217 if (pos) {\r
218 me.selectByPosition(pos, suppressEvent); \r
219 } else {\r
220 me.deselect();\r
221 }\r
222 },\r
223\r
224 /**\r
225 * Returns the current position in the format {row: row, column: column}\r
226 * @deprecated 5.0.1 This API uses column indices which include hidden columns in the count. Use {@link #getPosition} instead.\r
227 */\r
228 getCurrentPosition: function() {\r
229 // If it's during a select, return nextSelection since we buffer\r
230 // the real selection until after the event fires\r
231 var position = this.selecting ? this.nextSelection : this.selection;\r
232\r
233 // This is the previous Format of the private CellContext class which was used here.\r
234 // Do not return a CellContext so that if this object is passed into setCurrentPosition, it will be\r
235 // read in the legacy (including hidden columns) way.\r
236 return position ? {\r
237 view: position.view,\r
238 record: position.record,\r
239 row: position.rowIdx,\r
240 columnHeader: position.column,\r
241 // IMPORTANT: The historic API for columns has been to include hidden columns\r
242 // in the index. So we must report the index of the column in the "all" ColumnManager.\r
243 column: position.view.getColumnManager().indexOf(position.column)\r
244 } : position;\r
245 },\r
246\r
247 /**\r
248 * Returns the current position in the format {row: row, column: column}\r
249 * @return {Ext.grid.CellContext} A CellContext object describing the current cell.\r
250 */\r
251 getPosition: function() {\r
252 return (this.selecting ? this.nextSelection : this.selection) || null;\r
253 },\r
254\r
255 /**\r
256 * Sets the current position.\r
257 * @deprecated 5.0.1 This API uses column indices which include hidden columns in the count. Use {@link #setPosition} instead.\r
258 * @param {Ext.grid.CellContext/Object} position The position to set. May be an object of the form `{row:1, column:2}`\r
259 * @param {Boolean} suppressEvent True to suppress selection events\r
260 */\r
261 setCurrentPosition: function(pos, suppressEvent, /* private */ preventCheck) {\r
262 if (pos && !pos.isCellContext) {\r
263 pos = new Ext.grid.CellContext(this.view).setPosition({\r
264 row: pos.row,\r
265 // IMPORTANT: The historic API for columns has been to include hidden columns\r
266 // in the index. So we must index into the "all" ColumnManager.\r
267 column: typeof pos.column === 'number' ? this.view.getColumnManager().getColumns()[pos.column] : pos.column\r
268 });\r
269 }\r
270 return this.setPosition(pos, suppressEvent, preventCheck);\r
271 },\r
272\r
273 /**\r
274 * Sets the current position.\r
275 *\r
276 * Note that if passing a column index, it is the index within the *visible* column set.\r
277 *\r
278 * @param {Ext.grid.CellContext/Object} position The position to set. May be an object of the form `{row:1, column:2}`\r
279 * @param {Boolean} suppressEvent True to suppress selection events\r
280 */\r
281 setPosition: function(pos, suppressEvent, /* private */ preventCheck) {\r
282 var me = this,\r
283 last = me.selection;\r
284\r
285 // Normalize it into an Ext.grid.CellContext if necessary\r
286 if (pos) {\r
287 pos = pos.isCellContext ? pos.clone() : new Ext.grid.CellContext(me.view).setPosition(pos);\r
288 }\r
289 if (!preventCheck && last) {\r
290 // If the position is the same, jump out & don't fire the event\r
291 if (pos && (pos.record === last.record && pos.column === last.column && pos.view === last.view)) {\r
292 pos = null;\r
293 } else {\r
294 me.onCellDeselect(me.selection, suppressEvent);\r
295 }\r
296 }\r
297\r
298 if (pos) {\r
299 me.nextSelection = pos;\r
300 // set this flag here so we know to use nextSelection\r
301 // if the node is updated during a select\r
302 me.selecting = true;\r
303 me.onCellSelect(me.nextSelection, suppressEvent);\r
304 me.selecting = false;\r
305 // Deselect triggered by new selection will kill the selection property, so restore it here.\r
306 return (me.selection = pos);\r
307 }\r
308 // <debug>\r
309 // Enforce code correctness in unbuilt source.\r
310 return null;\r
311 // </debug>\r
312 },\r
313\r
314 isCellSelected: function(view, row, column) {\r
315 var me = this,\r
316 testPos,\r
317 pos = me.getPosition();\r
318\r
319 if (pos && pos.view === view) {\r
320 testPos = new Ext.grid.CellContext(view).setPosition({\r
321 row: row,\r
322 // IMPORTANT: The historic API for columns has been to include hidden columns\r
323 // in the index. So we must index into the "all" ColumnManager.\r
324 column: typeof column === 'number' ? view.getColumnManager().getColumns()[column] : column\r
325 });\r
326 return (testPos.record === pos.record) && (testPos.column === pos.column);\r
327 }\r
328 },\r
329\r
330 // Keep selection model in consistent state upon record deletion.\r
331 onStoreRemove: function(store, records, indices) {\r
332 var me = this,\r
333 pos = me.getPosition();\r
334\r
335 me.callParent(arguments);\r
336 if (pos && store.isMoving(pos.record)) {\r
337 return;\r
338 }\r
339 \r
340 if (pos && store.getCount() && store.indexOf(pos.record) !== -1) {\r
341 pos.setRow(pos.record);\r
342 } else {\r
343 me.selection = null;\r
344 }\r
345 },\r
346 \r
347 onStoreClear: function() {\r
348 this.callParent(arguments);\r
349 this.selection = null;\r
350 },\r
351 \r
352 onStoreAdd: function() {\r
353 var me = this,\r
354 pos = me.getPosition();\r
355\r
356 me.callParent(arguments);\r
357 if (pos) {\r
358 pos.setRow(pos.record);\r
359 } else {\r
360 me.selection = null;\r
361 }\r
362 },\r
363\r
364 /**\r
365 * Set the current position based on where the user clicks.\r
366 * @private\r
367 * IMPORTANT* Due to V4.0.0 history, the cellIndex here is the index within ALL columns, including hidden.\r
368 */\r
369 onCellClick: function(view, cell, cellIndex, record, row, recordIndex, e) {\r
370 // Record index will be -1 if the clicked record is a metadata record and not selectable\r
371 if (recordIndex !== -1) {\r
372 this.setPosition(e.position);\r
373 }\r
374 },\r
375\r
376 // notify the view that the cell has been selected to update the ui\r
377 // appropriately and bring the cell into focus\r
378 onCellSelect: function(position, supressEvent) {\r
379 if (position && position.rowIdx !== undefined && position.rowIdx > -1) {\r
380 this.doSelect(position.record, /*keepExisting*/false, supressEvent);\r
381 }\r
382 },\r
383\r
384 // notify view that the cell has been deselected to update the ui\r
385 // appropriately\r
386 onCellDeselect: function(position, supressEvent) {\r
387 if (position && position.rowIdx !== undefined) {\r
388 this.doDeselect(position.record, supressEvent);\r
389 }\r
390 },\r
391\r
392 onSelectChange: function(record, isSelected, suppressEvent, commitFn) {\r
393 var me = this,\r
394 pos, eventName, view;\r
395\r
396 if (isSelected) {\r
397 pos = me.nextSelection;\r
398 eventName = 'select';\r
399 } else {\r
400 pos = me.selection;\r
401 eventName = 'deselect';\r
402 }\r
403\r
404 // CellModel may be shared between two sides of a Lockable.\r
405 // The position must include a reference to the view in which the selection is current.\r
406 // Ensure we use the view specified by the position.\r
407 view = pos.view || me.primaryView;\r
408\r
409 if ((suppressEvent || me.fireEvent('before' + eventName, me, record, pos.rowIdx, pos.colIdx)) !== false &&\r
410 commitFn() !== false) {\r
411\r
412 if (isSelected) {\r
413 view.onCellSelect(pos);\r
414 } else {\r
415 view.onCellDeselect(pos);\r
416 delete me.selection;\r
417 }\r
418\r
419 if (!suppressEvent) {\r
420 me.fireEvent(eventName, me, record, pos.rowIdx, pos.colIdx);\r
421 }\r
422 }\r
423 },\r
424\r
425 refresh: function() {\r
426 var pos = this.getPosition(),\r
427 selRowIdx;\r
428\r
429 // Synchronize the current position's row with the row of the last selected record.\r
430 if (pos && (selRowIdx = this.store.indexOf(this.selected.last())) !== -1) {\r
431 pos.rowIdx = selRowIdx;\r
432 }\r
433 },\r
434\r
435 /**\r
436 * @private\r
437 * When grid uses {@link Ext.panel.Table#optimizedColumnMove optimizedColumnMove} (the default), this is added as a\r
438 * {@link Ext.panel.Table#columnmove columnmove} handler to correctly maintain the\r
439 * selected column using the same column header.\r
440 * \r
441 * If optimizedColumnMove === false, (which some grid Features set) then the view is refreshed,\r
442 * so this is not added as a handler because the selected column.\r
443 */\r
444 onColumnMove: function(headerCt, header, fromIdx, toIdx) {\r
445 var grid = headerCt.up('tablepanel');\r
446 if (grid) {\r
447 this.onViewRefresh(grid.view);\r
448 }\r
449 },\r
450 \r
451 onUpdate: function(record) {\r
452 var me = this,\r
453 pos;\r
454 \r
455 if (me.isSelected(record)) {\r
456 pos = me.selecting ? me.nextSelection : me.selection; \r
457 me.view.onCellSelect(pos);\r
458 }\r
459 },\r
460\r
461 onViewRefresh: function(view) {\r
462 var me = this,\r
463 pos = me.getPosition(),\r
464 newPos,\r
465 headerCt = view.headerCt,\r
466 record, column;\r
467\r
468 // Re-establish selection of the same cell coordinate.\r
469 // DO NOT fire events because the selected \r
470 if (pos && pos.view === view) {\r
471 record = pos.record;\r
472 column = pos.column;\r
473\r
474 // After a refresh, recreate the selection using the same record and grid column as before\r
475 if (!column.isDescendantOf(headerCt)) {\r
476 // column header is not a child of the header container\r
477 // this happens when the grid is reconfigured with new columns\r
478 // make a best effor to select something by matching on id, then text, then dataIndex\r
479 column = headerCt.queryById(column.id) || \r
480 headerCt.down('[text="' + column.text + '"]') ||\r
481 headerCt.down('[dataIndex="' + column.dataIndex + '"]');\r
482 }\r
483\r
484 // If we have a columnHeader (either the column header that already exists in\r
485 // the headerCt, or a suitable match that was found after reconfiguration)\r
486 // AND the record still exists in the store (or a record matching the id of\r
487 // the previously selected record) We are ok to go ahead and set the selection\r
488 if (pos.record) {\r
489 if (column && (view.store.indexOfId(record.getId()) !== -1)) {\r
490 newPos = new Ext.grid.CellContext(view).setPosition({\r
491 row: record,\r
492 column: column\r
493 });\r
494 me.setPosition(newPos);\r
495 }\r
496 } else {\r
497 me.selection = null;\r
498 }\r
499 }\r
500 },\r
501\r
502 /**\r
503 * @private\r
504 * Used internally by CellEditing\r
505 */\r
506 selectByPosition: function(position, suppressEvent) {\r
507 this.setPosition(position, suppressEvent);\r
508 }\r
509});\r