Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / examples / keynav / keynav.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 Ext.require([
16     'Ext.XTemplate',
17     'Ext.util.KeyNav',
18     'Ext.fx.*'
19 ]);
20
21 Ext.define('Board', {
22     constructor: function(size, activeCls){
23         this.size = size;
24         this.activeCls = activeCls;
25     },
26
27     render: function(el){
28         el = Ext.get(el);
29
30         var tpl = Ext.create('Ext.XTemplate',
31             '<tpl for=".">',
32                 '<div class="square">{.}</div>',
33                 '<tpl if="xindex % ' + this.size + ' === 0"><div class="x-clear"></div></tpl>',
34             '</tpl>'),
35             data = [],
36             i = 0,
37             max = this.size * this.size;
38
39         for (; i < max; ++i) {
40             data.push(i + 1);
41         }
42         tpl.append(el, data);
43         this.cells = el.select('.square');
44     },
45
46     getIndex: function(xy){
47         return this.size * xy[0] + xy[1];
48     },
49
50     constrain: function(x, y) {
51         x = Ext.Number.constrain(x, 0, this.size - 1);
52         y = Ext.Number.constrain(y, 0, this.size - 1);
53         return [x, y];
54     },
55
56     setActive: function(x, y) {
57         var xy = this.constrain(x, y),
58             cell = this.cells.item(this.getIndex(xy));
59
60         cell.radioCls(this.activeCls);
61         this.active = xy;
62     },
63
64     setActiveCls: function(activeCls){
65         this.cells.removeCls(this.activeCls);
66         this.activeCls = activeCls;
67         var active = this.active;
68         this.setActive(active[0], active[1]);
69     },
70
71     highlightActive: function(){
72         var cell = this.cells.item(this.getIndex(this.active));
73         Ext.create('Ext.fx.Anim', {
74             target: cell,
75             duration: 1000,
76             alternate: true,
77             iterations: 2,
78             to: {
79                 backgroundColor: '#FFFF00'
80             },
81             callback: function(){
82                 cell.setStyle('background-color', '');
83             }
84         });
85     },
86
87     moveUp: function(){
88         var active = this.active;
89         this.setActive(active[0] - 1, active[1]);
90     },
91
92     moveDown: function(){
93         var active = this.active;
94         this.setActive(active[0] + 1, active[1]);
95     },
96
97     moveLeft: function(){
98         var active = this.active;
99         this.setActive(active[0], active[1] - 1);
100     },
101
102     moveRight: function(){
103         var active = this.active;
104         this.setActive(active[0], active[1] + 1);
105     }
106 });
107
108 Ext.onReady(function(){
109
110     var cls = 'active-green';
111     var board = new Board(4, cls);
112     board.render('board');
113     board.setActive(0, 0);
114
115     var nav = Ext.create('Ext.util.KeyNav', Ext.getDoc(), {
116         scope: board,
117         left: board.moveLeft,
118         up: board.moveUp,
119         right: board.moveRight,
120         down: board.moveDown,
121         space: board.highlightActive,
122         home: function(){
123             cls = Ext.String.toggle(cls, 'active-green', 'active-red');
124             board.setActiveCls(cls);
125         }
126     });
127
128 });
129