| 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 focus between given horizontally arranged elements. | 7 * A class to manage focus between given horizontally arranged elements. |
| 8 * | 8 * |
| 9 * Pressing left cycles backward and pressing right cycles forward in item | 9 * Pressing left cycles backward and pressing right cycles forward in item |
| 10 * order. Pressing Home goes to the beginning of the list and End goes to the | 10 * order. Pressing Home goes to the beginning of the list and End goes to the |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 // Only accept left mouse clicks. | 247 // Only accept left mouse clicks. |
| 248 if (e.button) | 248 if (e.button) |
| 249 return; | 249 return; |
| 250 | 250 |
| 251 // Allow the element under the mouse cursor to be focusable. | 251 // Allow the element under the mouse cursor to be focusable. |
| 252 if (!e.currentTarget.disabled) | 252 if (!e.currentTarget.disabled) |
| 253 e.currentTarget.tabIndex = 0; | 253 e.currentTarget.tabIndex = 0; |
| 254 }, | 254 }, |
| 255 | 255 |
| 256 /** | 256 /** |
| 257 * @param {Event} e The keydown event. | 257 * @param {!Event} e The keydown event. |
| 258 * @private | 258 * @private |
| 259 */ | 259 */ |
| 260 onKeydown_: function(e) { | 260 onKeydown_: function(e) { |
| 261 var elements = this.getFocusableElements(); | 261 var elements = this.getFocusableElements(); |
| 262 var currentElement = /** @type {!Element} */(e.currentTarget); | 262 var currentElement = /** @type {!Element} */(e.currentTarget); |
| 263 var elementIndex = elements.indexOf(currentElement); | 263 var elementIndex = elements.indexOf(currentElement); |
| 264 assert(elementIndex >= 0); | 264 assert(elementIndex >= 0); |
| 265 | 265 |
| 266 if (this.delegate && this.delegate.onKeydown(this, e)) | 266 if (this.delegate && this.delegate.onKeydown(this, e)) |
| 267 return; | 267 return; |
| 268 | 268 |
| 269 if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) | 269 if (hasKeyModifiers(e)) |
| 270 return; | 270 return; |
| 271 | 271 |
| 272 var index = -1; | 272 var index = -1; |
| 273 | 273 |
| 274 if (e.key == 'ArrowLeft') | 274 if (e.key == 'ArrowLeft') |
| 275 index = elementIndex + (isRTL() ? 1 : -1); | 275 index = elementIndex + (isRTL() ? 1 : -1); |
| 276 else if (e.key == 'ArrowRight') | 276 else if (e.key == 'ArrowRight') |
| 277 index = elementIndex + (isRTL() ? -1 : 1); | 277 index = elementIndex + (isRTL() ? -1 : 1); |
| 278 else if (e.key == 'Home') | 278 else if (e.key == 'Home') |
| 279 index = 0; | 279 index = 0; |
| 280 else if (e.key == 'End') | 280 else if (e.key == 'End') |
| 281 index = elements.length - 1; | 281 index = elements.length - 1; |
| 282 | 282 |
| 283 var elementToFocus = elements[index]; | 283 var elementToFocus = elements[index]; |
| 284 if (elementToFocus) { | 284 if (elementToFocus) { |
| 285 this.getEquivalentElement(elementToFocus).focus(); | 285 this.getEquivalentElement(elementToFocus).focus(); |
| 286 e.preventDefault(); | 286 e.preventDefault(); |
| 287 } | 287 } |
| 288 }, | 288 }, |
| 289 }; | 289 }; |
| 290 | 290 |
| 291 return { | 291 return { |
| 292 FocusRow: FocusRow, | 292 FocusRow: FocusRow, |
| 293 }; | 293 }; |
| 294 }); | 294 }); |
| OLD | NEW |