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 properties: { | 9 properties: { |
10 hasDownloads_: { | 10 hasDownloads_: { |
11 observer: 'hasDownloadsChanged_', | |
11 type: Boolean, | 12 type: Boolean, |
12 value: false, | |
13 }, | 13 }, |
14 | 14 |
15 items_: { | 15 items_: { |
16 type: Array, | 16 type: Array, |
17 value: function() { return []; }, | |
17 }, | 18 }, |
18 }, | 19 }, |
19 | 20 |
21 observers: [ | |
22 'itemsChanged_(items_.*)', | |
23 ], | |
24 | |
25 /** @private */ | |
26 clearAll_: function() { | |
27 this.set('items_', []); | |
28 }, | |
29 | |
30 /** @private */ | |
31 hasDownloadsChanged_: function() { | |
32 if (loadTimeData.getBoolean('allowDeletingHistory')) | |
33 this.$.toolbar.downloadsShowing = this.hasDownloads_; | |
34 | |
35 if (this.hasDownloads_) { | |
36 this.$['downloads-list'].fire('iron-resize'); | |
37 } else { | |
38 var isSearching = downloads.ActionService.getInstance().isSearching(); | |
39 var messageToShow = isSearching ? 'noSearchResults' : 'noDownloads'; | |
40 this.$['no-downloads'].querySelector('span').textContent = | |
41 loadTimeData.getString(messageToShow); | |
42 } | |
43 }, | |
44 | |
45 /** | |
46 * @param {number} index | |
47 * @param {!Array<!downloads.Data>} list | |
48 * @private | |
49 */ | |
50 insertItems_: function(index, list) { | |
51 this.splice.apply(this, ['items_', index, 0].concat(list)); | |
52 this.updateHideDates_(index, index + list.length); | |
53 this.$.panel.classList.remove('loading'); | |
54 }, | |
55 | |
56 /** @private */ | |
57 itemsChanged_: function() { | |
58 this.hasDownloads_ = this.size_() > 0; | |
59 }, | |
60 | |
20 /** | 61 /** |
21 * @param {Event} e | 62 * @param {Event} e |
22 * @private | 63 * @private |
23 */ | 64 */ |
24 onCanExecute_: function(e) { | 65 onCanExecute_: function(e) { |
25 e = /** @type {cr.ui.CanExecuteEvent} */(e); | 66 e = /** @type {cr.ui.CanExecuteEvent} */(e); |
26 switch (e.command.id) { | 67 switch (e.command.id) { |
27 case 'undo-command': | 68 case 'undo-command': |
28 e.canExecute = this.$.toolbar.canUndo(); | 69 e.canExecute = this.$.toolbar.canUndo(); |
29 break; | 70 break; |
(...skipping 18 matching lines...) Expand all Loading... | |
48 onLoad_: function() { | 89 onLoad_: function() { |
49 cr.ui.decorate('command', cr.ui.Command); | 90 cr.ui.decorate('command', cr.ui.Command); |
50 document.addEventListener('canExecute', this.onCanExecute_.bind(this)); | 91 document.addEventListener('canExecute', this.onCanExecute_.bind(this)); |
51 document.addEventListener('command', this.onCommand_.bind(this)); | 92 document.addEventListener('command', this.onCommand_.bind(this)); |
52 | 93 |
53 // Shows all downloads. | 94 // Shows all downloads. |
54 downloads.ActionService.getInstance().search(''); | 95 downloads.ActionService.getInstance().search(''); |
55 }, | 96 }, |
56 | 97 |
57 /** | 98 /** |
99 * @param {number} index | |
100 * @private | |
101 */ | |
102 removeItem_: function(index) { | |
103 this.splice('items_', index, 1); | |
104 this.updateHideDates_(index, index); | |
105 }, | |
106 | |
107 /** | |
58 * @return {number} The number of downloads shown on the page. | 108 * @return {number} The number of downloads shown on the page. |
59 * @private | 109 * @private |
60 */ | 110 */ |
61 size_: function() { | 111 size_: function() { |
62 return this.items_.length; | 112 return this.items_.length; |
63 }, | 113 }, |
64 | 114 |
65 /** | 115 /** |
66 * Called when all items need to be updated. | 116 * @param {number} start |
67 * @param {!Array<!downloads.Data>} list A list of new download data. | 117 * @param {number} end |
68 * @private | 118 * @private |
69 */ | 119 */ |
70 updateAll_: function(list) { | 120 updateHideDates_: function(start, end) { |
71 /** @private {!Object<number>} */ | 121 for (var i = start; i <= end; ++i) { |
72 this.idToIndex_ = {}; | 122 var current = this.items_[i]; |
73 | 123 if (!current) |
74 for (var i = 0; i < list.length; ++i) { | 124 continue; |
75 var data = list[i]; | 125 var prev = this.items_[i - 1]; |
76 | 126 current.hideDate = !!prev && prev.date_string == current.date_string; |
77 this.idToIndex_[data.id] = data.index = i; | |
78 | |
79 var prev = list[i - 1]; | |
80 data.hideDate = !!prev && prev.date_string == data.date_string; | |
81 } | 127 } |
82 | |
83 // TODO(dbeam): this resets the scroll position, which is a huge bummer. | |
84 // Removing something from the bottom of the list should not scroll you | |
85 // back to the top. The grand plan is to restructure how the C++ sends the | |
86 // JS data so that it only gets updates (rather than the most recent set | |
87 // of items). TL;DR - we can't ship with this bug. | |
88 this.items_ = list; | |
89 | |
90 var hasDownloads = this.size_() > 0; | |
91 if (!hasDownloads) { | |
92 var isSearching = downloads.ActionService.getInstance().isSearching(); | |
93 var messageToShow = isSearching ? 'noSearchResults' : 'noDownloads'; | |
94 this.$['no-downloads'].querySelector('span').textContent = | |
95 loadTimeData.getString(messageToShow); | |
96 } | |
97 this.hasDownloads_ = hasDownloads; | |
98 | |
99 if (loadTimeData.getBoolean('allowDeletingHistory')) | |
100 this.$.toolbar.downloadsShowing = this.hasDownloads_; | |
101 | |
102 this.$.panel.classList.remove('loading'); | |
103 }, | 128 }, |
104 | 129 |
105 /** | 130 /** |
131 * @param {number} index | |
106 * @param {!downloads.Data} data | 132 * @param {!downloads.Data} data |
107 * @private | 133 * @private |
108 */ | 134 */ |
109 updateItem_: function(data) { | 135 updateItem_: function(index, data) { |
110 var index = this.idToIndex_[data.id]; | |
111 this.set('items_.' + index, data); | 136 this.set('items_.' + index, data); |
112 this.$['downloads-list'].updateSizeForItem(index); | 137 this.$['downloads-list'].updateSizeForItem(index); |
113 }, | 138 }, |
114 }); | 139 }); |
115 | 140 |
116 Manager.size = function() { | 141 Manager.clearAll = function() { |
117 return document.querySelector('downloads-manager').size_(); | 142 Manager.get().clearAll_(); |
118 }; | 143 }; |
119 | 144 |
120 Manager.updateAll = function(list) { | 145 /** @return {downloads.Manager} */ |
dpapad
2015/11/20 21:55:15
@return {!downloads.Manager}
Dan Beam
2015/11/21 01:40:43
technically this can return null if the component
| |
121 document.querySelector('downloads-manager').updateAll_(list); | 146 Manager.get = function() { |
147 return document.querySelector('downloads-manager'); | |
dpapad
2015/11/20 21:55:15
Can the result of this call be cached, to avoid se
Dan Beam
2015/11/21 01:40:43
this seems like a premature optimization that may
| |
122 }; | 148 }; |
123 | 149 |
124 Manager.updateItem = function(item) { | 150 Manager.insertItems = function(index, list) { |
125 document.querySelector('downloads-manager').updateItem_(item); | 151 Manager.get().insertItems_(index, list); |
126 }; | 152 }; |
127 | 153 |
128 Manager.onLoad = function() { | 154 Manager.onLoad = function() { |
129 document.querySelector('downloads-manager').onLoad_(); | 155 Manager.get().onLoad_(); |
156 }; | |
157 | |
158 Manager.removeItem = function(index) { | |
159 Manager.get().removeItem_(index); | |
160 }; | |
161 | |
162 Manager.size = function() { | |
dpapad
2015/11/20 23:00:19
I couldn't find any uses of Manager.size(). Does i
Dan Beam
2015/11/21 01:40:43
Done.
| |
163 return Manager.get().size_(); | |
164 }; | |
165 | |
166 Manager.updateItem = function(index, data) { | |
167 Manager.get().updateItem_(index, data); | |
130 }; | 168 }; |
131 | 169 |
132 return {Manager: Manager}; | 170 return {Manager: Manager}; |
133 }); | 171 }); |
OLD | NEW |