OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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('downloads', function() { | 5 cr.define('downloads', function() { |
6 /** | 6 /** |
7 * Provides an implementation for a single column grid. | 7 * Provides an implementation for a single column grid. |
8 * @constructor | 8 * @constructor |
9 * @extends {cr.ui.FocusRow} | 9 * @extends {cr.ui.FocusRow} |
10 */ | 10 */ |
11 function FocusRow() {} | 11 function FocusRow() {} |
12 | 12 |
13 /** | 13 /** |
14 * Decorates |focusRow| so that it can be treated as a FocusRow. | 14 * Decorates |focusRow| so that it can be treated as a FocusRow. |
15 * @param {Element} focusRow The element that has all the columns represented | 15 * @param {Element} focusRow The element that has all the columns represented |
16 * by |itemView|. | 16 * by |itemView|. |
17 * @param {!downloads.ItemView} itemView The item view this row cares about. | 17 * @param {!downloads.ItemView} itemView The item view this row cares about. |
18 * @param {Node} boundary Focus events are ignored outside of this node. | 18 * @param {Node} boundary Focus events are ignored outside of this node. |
19 */ | 19 */ |
20 FocusRow.decorate = function(focusRow, itemView, boundary) { | 20 FocusRow.decorate = function(focusRow, itemView, boundary) { |
21 focusRow.__proto__ = FocusRow.prototype; | 21 focusRow.__proto__ = FocusRow.prototype; |
22 focusRow.decorate(boundary); | 22 focusRow.decorate(boundary); |
23 | 23 focusRow.addFocusableElements_(); |
24 // Add all clickable elements as a row into the grid. | |
25 focusRow.addElementIfFocusable_(itemView.fileLink, 'name'); | |
26 focusRow.addElementIfFocusable_(itemView.srcUrl, 'url'); | |
27 focusRow.addElementIfFocusable_(itemView.show, 'show'); | |
28 focusRow.addElementIfFocusable_(itemView.retry, 'retry'); | |
29 focusRow.addElementIfFocusable_(itemView.pause, 'pause'); | |
30 focusRow.addElementIfFocusable_(itemView.resume, 'resume'); | |
31 focusRow.addElementIfFocusable_(itemView.safeRemove, 'remove'); | |
32 focusRow.addElementIfFocusable_(itemView.cancel, 'cancel'); | |
33 focusRow.addElementIfFocusable_(itemView.restore, 'save'); | |
34 focusRow.addElementIfFocusable_(itemView.save, 'save'); | |
35 focusRow.addElementIfFocusable_(itemView.dangerRemove, 'discard'); | |
36 focusRow.addElementIfFocusable_(itemView.discard, 'discard'); | |
37 focusRow.addElementIfFocusable_(itemView.controlledBy, 'extension'); | |
38 }; | 24 }; |
39 | 25 |
40 FocusRow.prototype = { | 26 FocusRow.prototype = { |
41 __proto__: cr.ui.FocusRow.prototype, | 27 __proto__: cr.ui.FocusRow.prototype, |
42 | 28 |
43 /** @override */ | 29 /** @override */ |
44 getEquivalentElement: function(element) { | 30 getEquivalentElement: function(element) { |
45 if (this.focusableElements.indexOf(element) > -1) | 31 if (this.focusableElements.indexOf(element) > -1) |
46 return assert(element); | 32 return assert(element); |
47 | 33 |
48 // All elements default to another element with the same type. | 34 // All elements default to another element with the same type. |
49 var columnType = element.getAttribute('column-type'); | 35 var columnType = element.getAttribute('column-type'); |
50 var equivalent = this.querySelector('[column-type=' + columnType + ']'); | 36 var equivalent = this.querySelector('[column-type=' + columnType + ']'); |
51 | 37 |
52 if (this.focusableElements.indexOf(equivalent) < 0) { | 38 if (this.focusableElements.indexOf(equivalent) < 0) { |
| 39 equivalent = null; |
53 var equivalentTypes = | 40 var equivalentTypes = |
54 ['show', 'retry', 'pause', 'resume', 'remove', 'cancel']; | 41 ['show', 'retry', 'pause', 'resume', 'remove', 'cancel']; |
55 if (equivalentTypes.indexOf(columnType) != -1) { | 42 if (equivalentTypes.indexOf(columnType) != -1) { |
56 var allTypes = equivalentTypes.map(function(type) { | 43 var allTypes = equivalentTypes.map(function(type) { |
57 return '[column-type=' + type + ']:not([hidden])'; | 44 return '[column-type=' + type + ']:not([hidden])'; |
58 }).join(', '); | 45 }).join(', '); |
59 equivalent = this.querySelector(allTypes); | 46 equivalent = this.querySelector(allTypes); |
60 } | 47 } |
61 } | 48 } |
62 | 49 |
63 // Return the first focusable element if no equivalent element is found. | 50 // Return the first focusable element if no equivalent element is found. |
64 return assert(equivalent || this.focusableElements[0]); | 51 return assert(equivalent || this.focusableElements[0]); |
65 }, | 52 }, |
66 | 53 |
67 /** | 54 /** @private */ |
68 * @param {Element} element The element that should be added. | 55 addFocusableElements_: function() { |
69 * @param {string} type The column type to use for the element. | 56 var possiblyFocusableElements = this.querySelectorAll('[column-type]'); |
70 * @private | 57 for (var i = 0; i < possiblyFocusableElements.length; ++i) { |
71 */ | 58 var possiblyFocusableElement = possiblyFocusableElements[i]; |
72 addElementIfFocusable_: function(element, type) { | 59 if (this.shouldFocus_(possiblyFocusableElement)) |
73 if (this.shouldFocus_(element)) { | 60 this.addFocusableElement(possiblyFocusableElement); |
74 this.addFocusableElement(element); | |
75 element.setAttribute('column-type', type); | |
76 } | 61 } |
77 }, | 62 }, |
78 | 63 |
79 /** | 64 /** |
80 * Determines if element should be focusable. | 65 * Determines if element should be focusable. |
81 * @param {Element} element | 66 * @param {Element} element |
82 * @return {boolean} | 67 * @return {boolean} |
83 * @private | 68 * @private |
84 */ | 69 */ |
85 shouldFocus_: function(element) { | 70 shouldFocus_: function(element) { |
86 if (!element) | 71 if (!element) |
87 return false; | 72 return false; |
88 | 73 |
89 // Hidden elements are not focusable. | 74 // Hidden elements are not focusable. |
90 var style = window.getComputedStyle(element); | 75 var style = window.getComputedStyle(element); |
91 if (style.visibility == 'hidden' || style.display == 'none') | 76 if (style.visibility == 'hidden' || style.display == 'none') |
92 return false; | 77 return false; |
93 | 78 |
94 // Verify all ancestors are focusable. | 79 // Verify all ancestors are focusable. |
95 return !element.parentElement || this.shouldFocus_(element.parentElement); | 80 return !element.parentElement || this.shouldFocus_(element.parentElement); |
96 }, | 81 }, |
97 }; | 82 }; |
98 | 83 |
99 return {FocusRow: FocusRow}; | 84 return {FocusRow: FocusRow}; |
100 }); | 85 }); |
OLD | NEW |