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.items_.length > 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 /** |
58 * @return {number} The number of downloads shown on the page. | 99 * @param {number} index |
59 * @private | 100 * @private |
60 */ | 101 */ |
61 size_: function() { | 102 removeItem_: function(index) { |
62 return this.items_.length; | 103 this.splice('items_', index, 1); |
| 104 this.updateHideDates_(index, index); |
63 }, | 105 }, |
64 | 106 |
65 /** | 107 /** |
66 * Called when all items need to be updated. | 108 * @param {number} start |
67 * @param {!Array<!downloads.Data>} list A list of new download data. | 109 * @param {number} end |
68 * @private | 110 * @private |
69 */ | 111 */ |
70 updateAll_: function(list) { | 112 updateHideDates_: function(start, end) { |
71 /** @private {!Object<number>} */ | 113 for (var i = start; i <= end; ++i) { |
72 this.idToIndex_ = {}; | 114 var current = this.items_[i]; |
73 | 115 if (!current) |
74 for (var i = 0; i < list.length; ++i) { | 116 continue; |
75 var data = list[i]; | 117 var prev = this.items_[i - 1]; |
76 | 118 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 } | 119 } |
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 }, | 120 }, |
104 | 121 |
105 /** | 122 /** |
| 123 * @param {number} index |
106 * @param {!downloads.Data} data | 124 * @param {!downloads.Data} data |
107 * @private | 125 * @private |
108 */ | 126 */ |
109 updateItem_: function(data) { | 127 updateItem_: function(index, data) { |
110 var index = this.idToIndex_[data.id]; | |
111 this.set('items_.' + index, data); | 128 this.set('items_.' + index, data); |
| 129 this.updateHideDates_(index, index); |
112 this.$['downloads-list'].updateSizeForItem(index); | 130 this.$['downloads-list'].updateSizeForItem(index); |
113 }, | 131 }, |
114 }); | 132 }); |
115 | 133 |
116 Manager.size = function() { | 134 Manager.clearAll = function() { |
117 return document.querySelector('downloads-manager').size_(); | 135 Manager.get().clearAll_(); |
118 }; | 136 }; |
119 | 137 |
120 Manager.updateAll = function(list) { | 138 /** @return {!downloads.Manager} */ |
121 document.querySelector('downloads-manager').updateAll_(list); | 139 Manager.get = function() { |
| 140 return queryRequiredElement('downloads-manager'); |
122 }; | 141 }; |
123 | 142 |
124 Manager.updateItem = function(item) { | 143 Manager.insertItems = function(index, list) { |
125 document.querySelector('downloads-manager').updateItem_(item); | 144 Manager.get().insertItems_(index, list); |
126 }; | 145 }; |
127 | 146 |
128 Manager.onLoad = function() { | 147 Manager.onLoad = function() { |
129 document.querySelector('downloads-manager').onLoad_(); | 148 Manager.get().onLoad_(); |
| 149 }; |
| 150 |
| 151 Manager.removeItem = function(index) { |
| 152 Manager.get().removeItem_(index); |
| 153 }; |
| 154 |
| 155 Manager.updateItem = function(index, data) { |
| 156 Manager.get().updateItem_(index, data); |
130 }; | 157 }; |
131 | 158 |
132 return {Manager: Manager}; | 159 return {Manager: Manager}; |
133 }); | 160 }); |
OLD | NEW |