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
3 // found in the LICENSE file.
5 cr.define('downloads', function() {
6 var Manager = Polymer({
7 is: 'downloads-manager',
10 /** @private {!downloads.ActionService} */
11 this.actionService_ = new downloads.ActionService;
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.
28 * @return {number} A guess at how many items could be visible at once.
31 guesstimateNumberOfVisibleItems_: function() {
32 var toolbarHeight = this.$.toolbar.offsetHeight;
33 return Math.floor((window.innerHeight - toolbarHeight) / 46) + 1;
40 onCanExecute_: function(e) {
41 e = /** @type {cr.ui.CanExecuteEvent} */(e);
42 switch (e.command.id) {
44 e.canExecute = this.$.toolbar.canUndo();
46 case 'clear-all-command':
47 e.canExecute = this.$.toolbar.canClearAll();
56 onCommand_: function(e) {
57 if (e.command.id == 'clear-all-command')
58 this.actionService_.clearAll();
59 else if (e.command.id == 'undo-command')
60 this.actionService_.undo();
65 this.$.toolbar.setActionService(this.actionService_);
67 cr.ui.decorate('command', cr.ui.Command);
68 document.addEventListener('canExecute', this.onCanExecute_.bind(this));
69 document.addEventListener('command', this.onCommand_.bind(this));
71 // Shows all downloads.
72 this.actionService_.search('');
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;
86 * @return {number} The number of downloads shown on the page.
90 return this.items_.length;
94 * Called when all items need to be updated.
95 * @param {!Array<!downloads.Data>} list A list of new download data.
98 updateAll_: function(list) {
99 var oldIdMap = this.idMap_ || {};
101 /** @private {!Object<!downloads.Item>} */
104 /** @private {!Array<!downloads.Item>} */
107 if (!this.iconLoader_) {
108 var guesstimate = Math.max(this.guesstimateNumberOfVisibleItems_(), 1);
109 /** @private {downloads.ThrottledIconLoader} */
110 this.iconLoader_ = new downloads.ThrottledIconLoader(guesstimate);
113 for (var i = 0; i < list.length; ++i) {
117 // Re-use old items when possible (saves work, preserves focus).
118 var item = oldIdMap[id] ||
119 new downloads.Item(this.iconLoader_, this.actionService_);
121 this.idMap_[id] = item; // Associated by ID for fast lookup.
122 this.items_.push(item); // Add to sorted list for order.
124 // Render |item| but don't actually add to the DOM yet. |this.items_|
125 // must be fully created to be able to find the right spot to insert.
128 // Collapse redundant dates.
129 var prev = list[i - 1];
130 item.hideDate = !!prev && prev.date_string == data.date_string;
135 // Remove stale, previously rendered items from the DOM.
136 for (var id in oldIdMap) {
137 if (oldIdMap[id].parentNode)
138 oldIdMap[id].parentNode.removeChild(oldIdMap[id]);
142 for (var i = 0; i < this.items_.length; ++i) {
143 var item = this.items_[i];
144 if (item.parentNode) // Already in the DOM; skip.
148 // Find the next rendered item after this one, and insert before it.
149 for (var j = i + 1; !before && j < this.items_.length; ++j) {
150 if (this.items_[j].parentNode)
151 before = this.items_[j];
153 // If |before| is null, |item| will just get added at the end.
154 this.$['downloads-list'].insertBefore(item, before);
157 this.hasDownloads_ = this.size_() > 0;
159 if (loadTimeData.getBoolean('allowDeletingHistory'))
160 this.$.toolbar.downloadsShowing = this.hasDownloads_;
163 this.$.panel.classList.remove('loading');
167 * @param {!downloads.Data} data
170 updateItem_: function(data) {
171 this.idMap_[data.id].update(data);
176 Manager.size = function() {
177 return document.querySelector('downloads-manager').size_();
180 Manager.updateAll = function(list) {
181 document.querySelector('downloads-manager').updateAll_(list);
184 Manager.updateItem = function(item) {
185 document.querySelector('downloads-manager').updateItem_(item);
188 Manager.onLoad = function() {
189 document.querySelector('downloads-manager').onLoad_();
192 return {Manager: Manager};
195 window.addEventListener('load', downloads.Manager.onLoad);