Upgrade to ExtJS 4.0.2 - Released 06/09/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
28      * @required
29      * The {@link Ext.view.BoundList} instance for which key navigation will be managed. This is required.
30      */
31
32     constructor: function(el, config) {
33         var me = this;
34         me.boundList = config.boundList;
35         me.callParent([el, Ext.apply({}, config, me.defaultHandlers)]);
36     },
37
38     defaultHandlers: {
39         up: function() {
40             var me = this,
41                 boundList = me.boundList,
42                 allItems = boundList.all,
43                 oldItem = boundList.highlightedItem,
44                 oldItemIdx = oldItem ? boundList.indexOf(oldItem) : -1,
45                 newItemIdx = oldItemIdx > 0 ? oldItemIdx - 1 : allItems.getCount() - 1; //wraps around
46             me.highlightAt(newItemIdx);
47         },
48
49         down: function() {
50             var me = this,
51                 boundList = me.boundList,
52                 allItems = boundList.all,
53                 oldItem = boundList.highlightedItem,
54                 oldItemIdx = oldItem ? boundList.indexOf(oldItem) : -1,
55                 newItemIdx = oldItemIdx < allItems.getCount() - 1 ? oldItemIdx + 1 : 0; //wraps around
56             me.highlightAt(newItemIdx);
57         },
58
59         pageup: function() {
60             //TODO
61         },
62
63         pagedown: function() {
64             //TODO
65         },
66
67         home: function() {
68             this.highlightAt(0);
69         },
70
71         end: function() {
72             var me = this;
73             me.highlightAt(me.boundList.all.getCount() - 1);
74         },
75
76         enter: function(e) {
77             this.selectHighlighted(e);
78         }
79     },
80
81     /**
82      * Highlights the item at the given index.
83      * @param {Number} index
84      */
85     highlightAt: function(index) {
86         var boundList = this.boundList,
87             item = boundList.all.item(index);
88         if (item) {
89             item = item.dom;
90             boundList.highlightItem(item);
91             boundList.getTargetEl().scrollChildIntoView(item, false);
92         }
93     },
94
95     /**
96      * Triggers selection of the currently highlighted item according to the behavior of
97      * the configured SelectionModel.
98      */
99     selectHighlighted: function(e) {
100         var me = this,
101             boundList = me.boundList,
102             highlighted = boundList.highlightedItem,
103             selModel = boundList.getSelectionModel();
104         if (highlighted) {
105             selModel.selectWithEvent(boundList.getRecord(highlighted), e);
106         }
107     }
108
109 });