Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: chrome/browser/resources/downloads/downloads.js

Issue 807593005: Make downloads list keyboard shortcuts more consistent. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Apply Feedback Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // TODO(jhawkins): Use hidden instead of showInline* and display:none. 5 // TODO(jhawkins): Use hidden instead of showInline* and display:none.
6 // TODO(hcarmona): This file is big: it may be good to split it up.
6 7
7 /** 8 /**
8 * The type of the download object. The definition is based on 9 * The type of the download object. The definition is based on
9 * chrome/browser/ui/webui/downloads_dom_handler.cc:CreateDownloadItemValue() 10 * chrome/browser/ui/webui/downloads_dom_handler.cc:CreateDownloadItemValue()
10 * @typedef {{by_ext_id: (string|undefined), 11 * @typedef {{by_ext_id: (string|undefined),
11 * by_ext_name: (string|undefined), 12 * by_ext_name: (string|undefined),
12 * danger_type: (string|undefined), 13 * danger_type: (string|undefined),
13 * date_string: string, 14 * date_string: string,
14 * file_externally_removed: boolean, 15 * file_externally_removed: boolean,
15 * file_name: string, 16 * file_name: string,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 */ 71 */
71 function createButton(onclick, value) { 72 function createButton(onclick, value) {
72 var button = document.createElement('input'); 73 var button = document.createElement('input');
73 button.type = 'button'; 74 button.type = 'button';
74 button.value = value; 75 button.value = value;
75 button.onclick = onclick; 76 button.onclick = onclick;
76 return button; 77 return button;
77 } 78 }
78 79
79 /////////////////////////////////////////////////////////////////////////////// 80 ///////////////////////////////////////////////////////////////////////////////
81 // DownloadFocusRow:
82
83 /**
84 * Provides an implementation for a single column grid.
85 * @constructor
86 * @extends {cr.ui.FocusRow}
87 */
88 function DownloadFocusRow() {}
89
90 /**
91 * Decorates |focusRow| so that it can be treated as a DownloadFocusRow.
92 * @param {Element} focusRow The element that has all the columns represented
93 * by |download|.
94 * @param {Download} download The Download representing this row.
95 * @param {Node} boundary Focus events are ignored outside of this node.
96 */
97 DownloadFocusRow.decorate = function(focusRow, download, boundary) {
98 focusRow.__proto__ = DownloadFocusRow.prototype;
99 focusRow.decorate(boundary);
100
101 // Add all clickable elements as a row into the grid.
102 focusRow.addElementIfFocusable_(download.nodeFileLink_, 'name');
103 focusRow.addElementIfFocusable_(download.nodeURL_, 'url');
104 focusRow.addElementIfFocusable_(download.controlShow_, 'show');
105 focusRow.addElementIfFocusable_(download.controlRetry_, 'retry');
106 focusRow.addElementIfFocusable_(download.controlPause_, 'pause');
107 focusRow.addElementIfFocusable_(download.controlResume_, 'resume');
108 focusRow.addElementIfFocusable_(download.controlRemove_, 'remove');
109 focusRow.addElementIfFocusable_(download.controlCancel_, 'cancel');
110
111 // Only one set of these two buttons will be added to a download row at a
112 // time. They have the same type so that focus is handled well in case
113 // there is a dangerous download right next to a malicious download.
Dan Beam 2015/01/31 18:28:30 nit: Elements with the same type are mutually excl
hcarmona 2015/02/02 18:35:01 Done.
114 focusRow.addElementIfFocusable_(download.malwareSave_, 'save');
115 focusRow.addElementIfFocusable_(download.malwareDiscard_, 'discard');
116 focusRow.addElementIfFocusable_(download.dangerSave_, 'save');
117 focusRow.addElementIfFocusable_(download.dangerDiscard_, 'discard');
118
119 focusRow.addElementIfFocusable_(download.controlByExtensionLink_,
120 'extension');
121 };
122
123 DownloadFocusRow.prototype = {
124 __proto__: cr.ui.FocusRow.prototype,
125
126 /** @override */
127 getEquivalentElement: function(element) {
128 if (this.contains(element))
129 return element;
130
131 // All elements default to another element with the same type.
132 var columnType = element.getAttribute('column-type');
133 var equivalent = this.querySelector('[column-type=' + columnType + ']');
134
135 if (!equivalent) {
136 var equivalentTypes =
137 ['show', 'retry', 'pause', 'resume', 'remove', 'cancel'];
138 if (equivalentTypes.indexOf(columnType) != -1) {
139 var allTypes = equivalentTypes.map(function(type) {
140 return '[column-type=' + type + ']';
141 }).join(', ');
142 equivalent = this.querySelector(allTypes);
143 }
144 }
145
146 // Return the first focusable element if no equivalent type is found.
147 return equivalent || this.focusableElements[0];
148 },
149
150 /**
151 * @param {Element} element The element that should be added.
152 * @param {string} type The column type to use for the element.
153 * @private
154 */
155 addElementIfFocusable_: function(element, type) {
156 if (this.shouldFocus_(element)) {
157 this.addFocusableElement(element);
158 element.setAttribute('column-type', type);
159 }
160 },
161
162 /**
163 * Determines if element should be focusable.
164 * @param {!Element} element
165 * @return {boolean}
166 * @private
167 */
168 shouldFocus_: function(element) {
169 if (!element)
170 return false;
171
172 // Hidden elements are not focusable.
173 var style = window.getComputedStyle(element);
174 if (style.visibility == 'hidden' || style.display == 'none')
175 return false;
176
177 // Verify all ancestors are focusable.
178 return !element.parentElement || this.shouldFocus_(element.parentElement);
179 },
180 };
181
182 ///////////////////////////////////////////////////////////////////////////////
80 // Downloads 183 // Downloads
81 /** 184 /**
82 * Class to hold all the information about the visible downloads. 185 * Class to hold all the information about the visible downloads.
83 * @constructor 186 * @constructor
84 */ 187 */
85 function Downloads() { 188 function Downloads() {
86 /** 189 /**
87 * @type {!Object.<string, Download>} 190 * @type {!Object.<string, Download>}
88 * @private 191 * @private
89 */ 192 */
90 this.downloads_ = {}; 193 this.downloads_ = {};
91 this.node_ = $('downloads-display'); 194 this.node_ = $('downloads-display');
92 this.summary_ = $('downloads-summary-text'); 195 this.summary_ = $('downloads-summary-text');
93 this.searchText_ = ''; 196 this.searchText_ = '';
197 this.focusGrid_ = new cr.ui.FocusGrid();
94 198
95 // Keep track of the dates of the newest and oldest downloads so that we 199 // Keep track of the dates of the newest and oldest downloads so that we
96 // know where to insert them. 200 // know where to insert them.
97 this.newestTime_ = -1; 201 this.newestTime_ = -1;
98 202
99 // Icon load request queue. 203 // Icon load request queue.
100 this.iconLoadQueue_ = []; 204 this.iconLoadQueue_ = [];
101 this.isIconLoading_ = false; 205 this.isIconLoading_ = false;
102 206
103 this.progressForeground1_ = new Image(); 207 this.progressForeground1_ = new Image();
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 var noDownloadsOrResults = $('no-downloads-or-results'); 273 var noDownloadsOrResults = $('no-downloads-or-results');
170 noDownloadsOrResults.textContent = loadTimeData.getString( 274 noDownloadsOrResults.textContent = loadTimeData.getString(
171 this.searchText_ ? 'no_search_results' : 'no_downloads'); 275 this.searchText_ ? 'no_search_results' : 'no_downloads');
172 276
173 var hasDownloads = this.size() > 0; 277 var hasDownloads = this.size() > 0;
174 this.node_.hidden = !hasDownloads; 278 this.node_.hidden = !hasDownloads;
175 noDownloadsOrResults.hidden = hasDownloads; 279 noDownloadsOrResults.hidden = hasDownloads;
176 280
177 if (loadTimeData.getBoolean('allow_deleting_history')) 281 if (loadTimeData.getBoolean('allow_deleting_history'))
178 $('clear-all').hidden = !hasDownloads || this.searchText_.length > 0; 282 $('clear-all').hidden = !hasDownloads || this.searchText_.length > 0;
283
284 this.rebuildFocusGrid_();
179 }; 285 };
180 286
181 /** 287 /**
288 * Rebuild the focusGrid_ using the elements that each download will have.
289 * @private
290 */
291 Downloads.prototype.rebuildFocusGrid_ = function() {
292 this.focusGrid_.destroy();
293
294 var keys = Object.keys(this.downloads_);
295 for (var i = 0; i < keys.length; ++i) {
296 var download = this.downloads_[keys[i]];
297 DownloadFocusRow.decorate(download.node, download, this.node_);
298 }
299
300 // The ordering of the keys is not guaranteed, and downloads should be added
301 // to the FocusGrid in the order they will be in the UI.
302 var downloads = document.querySelectorAll('.download');
303 for (var i = 0; i < downloads.length; ++i) {
304 this.focusGrid_.addRow(downloads[i]);
305 }
306 };
307
308 /**
182 * Returns the number of downloads in the model. Used by tests. 309 * Returns the number of downloads in the model. Used by tests.
183 * @return {number} Returns the number of downloads shown on the page. 310 * @return {number} Returns the number of downloads shown on the page.
184 */ 311 */
185 Downloads.prototype.size = function() { 312 Downloads.prototype.size = function() {
186 return Object.keys(this.downloads_).length; 313 return Object.keys(this.downloads_).length;
187 }; 314 };
188 315
189 /** 316 /**
190 * Called whenever the downloads lists items have changed (either by being 317 * Called whenever the downloads lists items have changed (either by being
191 * updated, added, or removed). 318 * updated, added, or removed).
(...skipping 786 matching lines...) Expand 10 before | Expand all | Expand 10 after
978 if (Date.now() - start > 50) { 1105 if (Date.now() - start > 50) {
979 clearTimeout(resultsTimeout); 1106 clearTimeout(resultsTimeout);
980 resultsTimeout = setTimeout(tryDownloadUpdatedPeriodically, 5); 1107 resultsTimeout = setTimeout(tryDownloadUpdatedPeriodically, 5);
981 break; 1108 break;
982 } 1109 }
983 } 1110 }
984 } 1111 }
985 1112
986 // Add handlers to HTML elements. 1113 // Add handlers to HTML elements.
987 window.addEventListener('DOMContentLoaded', load); 1114 window.addEventListener('DOMContentLoaded', load);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698