OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('cr.ui', function() { | 5 cr.define('cr.ui', function() { |
6 /** | 6 /** |
7 * A class to manage grid of focusable elements in a 2D grid. For example, | 7 * A class to manage grid of focusable elements in a 2D grid. For example, |
8 * given this grid: | 8 * given this grid: |
9 * | 9 * |
10 * focusable [focused] focusable (row: 0, col: 1) | 10 * focusable [focused] focusable (row: 0, col: 1) |
(...skipping 25 matching lines...) Expand all Loading... |
36 /** @private {boolean} */ | 36 /** @private {boolean} */ |
37 ignoreFocusChange_: false, | 37 ignoreFocusChange_: false, |
38 | 38 |
39 /** @override */ | 39 /** @override */ |
40 onFocus: function(row, e) { | 40 onFocus: function(row, e) { |
41 if (this.ignoreFocusChange_) | 41 if (this.ignoreFocusChange_) |
42 this.ignoreFocusChange_ = false; | 42 this.ignoreFocusChange_ = false; |
43 else | 43 else |
44 this.lastFocused_ = e.currentTarget; | 44 this.lastFocused_ = e.currentTarget; |
45 | 45 |
46 this.rows.forEach(function(r) { r.makeActive(r == row); }); | 46 this.rows.forEach(function(r) { |
| 47 r.makeActive(r == row); |
| 48 }); |
47 }, | 49 }, |
48 | 50 |
49 /** @override */ | 51 /** @override */ |
50 onKeydown: function(row, e) { | 52 onKeydown: function(row, e) { |
51 var rowIndex = this.rows.indexOf(row); | 53 var rowIndex = this.rows.indexOf(row); |
52 assert(rowIndex >= 0); | 54 assert(rowIndex >= 0); |
53 | 55 |
54 var newRow = -1; | 56 var newRow = -1; |
55 | 57 |
56 if (e.key == 'ArrowUp') | 58 if (e.key == 'ArrowUp') |
(...skipping 13 matching lines...) Expand all Loading... |
70 return true; | 72 return true; |
71 } | 73 } |
72 | 74 |
73 return false; | 75 return false; |
74 }, | 76 }, |
75 | 77 |
76 /** | 78 /** |
77 * Unregisters event handlers and removes all |this.rows|. | 79 * Unregisters event handlers and removes all |this.rows|. |
78 */ | 80 */ |
79 destroy: function() { | 81 destroy: function() { |
80 this.rows.forEach(function(row) { row.destroy(); }); | 82 this.rows.forEach(function(row) { |
| 83 row.destroy(); |
| 84 }); |
81 this.rows.length = 0; | 85 this.rows.length = 0; |
82 }, | 86 }, |
83 | 87 |
84 /** | 88 /** |
85 * @param {!Element} target A target item to find in this grid. | 89 * @param {!Element} target A target item to find in this grid. |
86 * @return {number} The row index. -1 if not found. | 90 * @return {number} The row index. -1 if not found. |
87 */ | 91 */ |
88 getRowIndexForTarget: function(target) { | 92 getRowIndexForTarget: function(target) { |
89 for (var i = 0; i < this.rows.length; ++i) { | 93 for (var i = 0; i < this.rows.length; ++i) { |
90 if (this.rows[i].getElements().indexOf(target) >= 0) | 94 if (this.rows[i].getElements().indexOf(target) >= 0) |
(...skipping 11 matching lines...) Expand all Loading... |
102 if (this.rows[i].root == root) | 106 if (this.rows[i].root == root) |
103 return this.rows[i]; | 107 return this.rows[i]; |
104 } | 108 } |
105 return null; | 109 return null; |
106 }, | 110 }, |
107 | 111 |
108 /** | 112 /** |
109 * Adds |row| to the end of this list. | 113 * Adds |row| to the end of this list. |
110 * @param {!cr.ui.FocusRow} row The row that needs to be added to this grid. | 114 * @param {!cr.ui.FocusRow} row The row that needs to be added to this grid. |
111 */ | 115 */ |
112 addRow: function(row) { this.addRowBefore(row, null); }, | 116 addRow: function(row) { |
| 117 this.addRowBefore(row, null); |
| 118 }, |
113 | 119 |
114 /** | 120 /** |
115 * Adds |row| before |nextRow|. If |nextRow| is not in the list or it's | 121 * Adds |row| before |nextRow|. If |nextRow| is not in the list or it's |
116 * null, |row| is added to the end. | 122 * null, |row| is added to the end. |
117 * @param {!cr.ui.FocusRow} row The row that needs to be added to this grid. | 123 * @param {!cr.ui.FocusRow} row The row that needs to be added to this grid. |
118 * @param {cr.ui.FocusRow} nextRow The row that should follow |row|. | 124 * @param {cr.ui.FocusRow} nextRow The row that should follow |row|. |
119 */ | 125 */ |
120 addRowBefore: function(row, nextRow) { | 126 addRowBefore: function(row, nextRow) { |
121 row.delegate = row.delegate || this; | 127 row.delegate = row.delegate || this; |
122 | 128 |
(...skipping 28 matching lines...) Expand all Loading... |
151 } | 157 } |
152 | 158 |
153 this.rows[0].makeActive(true); | 159 this.rows[0].makeActive(true); |
154 }, | 160 }, |
155 }; | 161 }; |
156 | 162 |
157 return { | 163 return { |
158 FocusGrid: FocusGrid, | 164 FocusGrid: FocusGrid, |
159 }; | 165 }; |
160 }); | 166 }); |
OLD | NEW |