Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / view / BoundListKeyNav.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.view.BoundListKeyNav
17  * @extends Ext.util.KeyNav
18  * A specialized {@link Ext.util.KeyNav} implementation for navigating a {@link Ext.view.BoundList} using
19  * the keyboard. The up, down, pageup, pagedown, home, and end keys move the active highlight
20  * through the list. The enter key invokes the selection model's select action using the highlighted item.
21  */
22 Ext.define('Ext.view.BoundListKeyNav', {
23     extend: 'Ext.util.KeyNav',
24     requires: 'Ext.view.BoundList',
25
26     /**
27      * @cfg {Ext.view.BoundList} boundList (required)
28      * The {@link Ext.view.BoundList} instance for which key navigation will be managed.
29      */
30
31     constructor: function(el, config) {
32         var me = this;
33         me.boundList = config.boundList;
34         me.callParent([el, Ext.apply({}, config, me.defaultHandlers)]);
35     },
36
37     defaultHandlers: {
38         up: function() {
39             var me = this,
40                 boundList = me.boundList,
41                 allItems = boundList.all,
42                 oldItem = boundList.highlightedItem,
43                 oldItemIdx = oldItem ? boundList.indexOf(oldItem) : -1,
44                 newItemIdx = oldItemIdx > 0 ? oldItemIdx - 1 : allItems.getCount() - 1; //wraps around
45             me.highlightAt(newItemIdx);
46         },
47
48         down: function() {
49             var me = this,
50                 boundList = me.boundList,
51                 allItems = boundList.all,
52                 oldItem = boundList.highlightedItem,
53                 oldItemIdx = oldItem ? boundList.indexOf(oldItem) : -1,
54                 newItemIdx = oldItemIdx < allItems.getCount() - 1 ? oldItemIdx + 1 : 0; //wraps around
55             me.highlightAt(newItemIdx);
56         },
57
58         pageup: function() {
59             //TODO
60         },
61
62         pagedown: function() {
63             //TODO
64         },
65
66         home: function() {
67             this.highlightAt(0);
68         },
69
70         end: function() {
71             var me = this;
72             me.highlightAt(me.boundList.all.getCount() - 1);
73         },
74
75         enter: function(e) {
76             this.selectHighlighted(e);
77         }
78     },
79
80     /**
81      * Highlights the item at the given index.
82      * @param {Number} index
83      */
84     highlightAt: function(index) {
85         var boundList = this.boundList,
86             item = boundList.all.item(index);
87         if (item) {
88             item = item.dom;
89             boundList.highlightItem(item);
90             boundList.getTargetEl().scrollChildIntoView(item, false);
91         }
92     },
93
94     /**
95      * Triggers selection of the currently highlighted item according to the behavior of
96      * the configured SelectionModel.
97      */
98     selectHighlighted: function(e) {
99         var me = this,
100             boundList = me.boundList,
101             highlighted = boundList.highlightedItem,
102             selModel = boundList.getSelectionModel();
103         if (highlighted) {
104             selModel.selectWithEvent(boundList.getRecord(highlighted), e);
105         }
106     }
107
108 });