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