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 var Manager = Polymer({ | 6 var Manager = Polymer({ |
7 is: 'downloads-manager', | 7 is: 'downloads-manager', |
8 | 8 |
9 created: function() { | 9 created: function() { |
10 /** @private {!downloads.ActionService} */ | 10 /** @private {!downloads.ActionService} */ |
11 this.actionService_ = new downloads.ActionService; | 11 this.actionService_ = new downloads.ActionService; |
12 }, | 12 }, |
13 | 13 |
14 properties: { | 14 properties: { |
15 hasDownloads_: { | 15 hasDownloads_: { |
16 type: Boolean, | 16 type: Boolean, |
17 value: false, | 17 value: false, |
18 }, | 18 }, |
19 }, | 19 }, |
20 | 20 |
21 ready: function() { | |
22 window.addEventListener('resize', this.onResize_.bind(this)); | |
23 // onResize_() doesn't need to be called immediately here because it's | |
24 // guaranteed to be called again shortly when items are received. | |
25 }, | |
26 | |
27 /** | 21 /** |
28 * @return {number} A guess at how many items could be visible at once. | 22 * @return {number} A guess at how many items could be visible at once. |
29 * @private | 23 * @private |
30 */ | 24 */ |
31 guesstimateNumberOfVisibleItems_: function() { | 25 guesstimateNumberOfVisibleItems_: function() { |
32 var toolbarHeight = this.$.toolbar.offsetHeight; | 26 var toolbarHeight = this.$.toolbar.offsetHeight; |
33 return Math.floor((window.innerHeight - toolbarHeight) / 46) + 1; | 27 return Math.floor((window.innerHeight - toolbarHeight) / 46) + 1; |
34 }, | 28 }, |
35 | 29 |
36 /** | 30 /** |
(...skipping 29 matching lines...) Expand all Loading... |
66 | 60 |
67 cr.ui.decorate('command', cr.ui.Command); | 61 cr.ui.decorate('command', cr.ui.Command); |
68 document.addEventListener('canExecute', this.onCanExecute_.bind(this)); | 62 document.addEventListener('canExecute', this.onCanExecute_.bind(this)); |
69 document.addEventListener('command', this.onCommand_.bind(this)); | 63 document.addEventListener('command', this.onCommand_.bind(this)); |
70 | 64 |
71 // Shows all downloads. | 65 // Shows all downloads. |
72 this.actionService_.search(''); | 66 this.actionService_.search(''); |
73 }, | 67 }, |
74 | 68 |
75 /** @private */ | 69 /** @private */ |
76 onResize_: function() { | |
77 // TODO(dbeam): expose <paper-header-panel>'s #mainContainer in Polymer. | |
78 var container = this.$.panel.$.mainContainer; | |
79 var scrollbarWidth = container.offsetWidth - container.clientWidth; | |
80 this.items_.forEach(function(item) { | |
81 item.scrollbarWidth = scrollbarWidth; | |
82 }); | |
83 }, | |
84 | |
85 /** @private */ | |
86 rebuildFocusGrid_: function() { | 70 rebuildFocusGrid_: function() { |
87 var activeElement = this.shadowRoot.activeElement; | 71 var activeElement = this.shadowRoot.activeElement; |
88 | 72 |
89 var activeItem; | 73 var activeItem; |
90 if (activeElement && activeElement.tagName == 'download-item') | 74 if (activeElement && activeElement.tagName == 'download-item') |
91 activeItem = activeElement; | 75 activeItem = activeElement; |
92 | 76 |
93 var activeControl = activeItem && activeItem.shadowRoot.activeElement; | 77 var activeControl = activeItem && activeItem.shadowRoot.activeElement; |
94 | 78 |
95 /** @private {!cr.ui.FocusGrid} */ | 79 /** @private {!cr.ui.FocusGrid} */ |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 } | 163 } |
180 // If |before| is null, |item| will just get added at the end. | 164 // If |before| is null, |item| will just get added at the end. |
181 this.$['downloads-list'].insertBefore(item, before); | 165 this.$['downloads-list'].insertBefore(item, before); |
182 } | 166 } |
183 | 167 |
184 this.hasDownloads_ = this.size_() > 0; | 168 this.hasDownloads_ = this.size_() > 0; |
185 | 169 |
186 if (loadTimeData.getBoolean('allowDeletingHistory')) | 170 if (loadTimeData.getBoolean('allowDeletingHistory')) |
187 this.$.toolbar.downloadsShowing = this.hasDownloads_; | 171 this.$.toolbar.downloadsShowing = this.hasDownloads_; |
188 | 172 |
189 this.onResize_(); | |
190 this.$.panel.classList.remove('loading'); | 173 this.$.panel.classList.remove('loading'); |
191 | 174 |
192 var allReady = this.items_.map(function(i) { return i.readyPromise; }); | 175 var allReady = this.items_.map(function(i) { return i.readyPromise; }); |
193 Promise.all(allReady).then(this.rebuildFocusGrid_.bind(this)); | 176 Promise.all(allReady).then(this.rebuildFocusGrid_.bind(this)); |
194 }, | 177 }, |
195 | 178 |
196 /** | 179 /** |
197 * @param {!downloads.Data} data | 180 * @param {!downloads.Data} data |
198 * @private | 181 * @private |
199 */ | 182 */ |
200 updateItem_: function(data) { | 183 updateItem_: function(data) { |
201 var item = this.idMap_[data.id]; | 184 var item = this.idMap_[data.id]; |
202 | 185 |
203 var activeControl = this.shadowRoot.activeElement == item ? | 186 var activeControl = this.shadowRoot.activeElement == item ? |
204 item.shadowRoot.activeElement : null; | 187 item.shadowRoot.activeElement : null; |
205 | 188 |
206 item.update(data); | 189 item.update(data); |
207 | 190 |
208 if (activeControl && !cr.ui.FocusRow.isFocusable(activeControl)) { | 191 if (activeControl && !cr.ui.FocusRow.isFocusable(activeControl)) { |
209 var focusRow = this.focusGrid_.getRowForRoot(item.content); | 192 var focusRow = this.focusGrid_.getRowForRoot(item.content); |
210 focusRow.getEquivalentElement(activeControl).focus(); | 193 focusRow.getEquivalentElement(activeControl).focus(); |
211 } | 194 } |
212 | |
213 this.onResize_(); | |
214 }, | 195 }, |
215 }); | 196 }); |
216 | 197 |
217 Manager.size = function() { | 198 Manager.size = function() { |
218 return document.querySelector('downloads-manager').size_(); | 199 return document.querySelector('downloads-manager').size_(); |
219 }; | 200 }; |
220 | 201 |
221 Manager.updateAll = function(list) { | 202 Manager.updateAll = function(list) { |
222 document.querySelector('downloads-manager').updateAll_(list); | 203 document.querySelector('downloads-manager').updateAll_(list); |
223 }; | 204 }; |
224 | 205 |
225 Manager.updateItem = function(item) { | 206 Manager.updateItem = function(item) { |
226 document.querySelector('downloads-manager').updateItem_(item); | 207 document.querySelector('downloads-manager').updateItem_(item); |
227 }; | 208 }; |
228 | 209 |
229 Manager.onLoad = function() { | 210 Manager.onLoad = function() { |
230 document.querySelector('downloads-manager').onLoad_(); | 211 document.querySelector('downloads-manager').onLoad_(); |
231 }; | 212 }; |
232 | 213 |
233 return {Manager: Manager}; | 214 return {Manager: Manager}; |
234 }); | 215 }); |
235 | 216 |
236 window.addEventListener('load', downloads.Manager.onLoad); | 217 window.addEventListener('load', downloads.Manager.onLoad); |
OLD | NEW |