| 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_', | |
| 12 type: Boolean, | 11 type: Boolean, |
| 12 value: false, |
| 13 }, | 13 }, |
| 14 | 14 |
| 15 items_: { | 15 items_: { |
| 16 type: Array, | 16 type: Array, |
| 17 value: function() { return []; }, | |
| 18 }, | 17 }, |
| 19 }, | 18 }, |
| 20 | 19 |
| 21 hostAttributes: { | 20 hostAttributes: { |
| 22 loading: true, | 21 loading: true, |
| 23 }, | 22 }, |
| 24 | 23 |
| 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 /** | 24 /** |
| 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 | |
| 65 /** | |
| 66 * @param {Event} e | 25 * @param {Event} e |
| 67 * @private | 26 * @private |
| 68 */ | 27 */ |
| 69 onCanExecute_: function(e) { | 28 onCanExecute_: function(e) { |
| 70 e = /** @type {cr.ui.CanExecuteEvent} */(e); | 29 e = /** @type {cr.ui.CanExecuteEvent} */(e); |
| 71 switch (e.command.id) { | 30 switch (e.command.id) { |
| 72 case 'undo-command': | 31 case 'undo-command': |
| 73 e.canExecute = this.$.toolbar.canUndo(); | 32 e.canExecute = this.$.toolbar.canUndo(); |
| 74 break; | 33 break; |
| 75 case 'clear-all-command': | 34 case 'clear-all-command': |
| (...skipping 17 matching lines...) Expand all Loading... |
| 93 onLoad_: function() { | 52 onLoad_: function() { |
| 94 cr.ui.decorate('command', cr.ui.Command); | 53 cr.ui.decorate('command', cr.ui.Command); |
| 95 document.addEventListener('canExecute', this.onCanExecute_.bind(this)); | 54 document.addEventListener('canExecute', this.onCanExecute_.bind(this)); |
| 96 document.addEventListener('command', this.onCommand_.bind(this)); | 55 document.addEventListener('command', this.onCommand_.bind(this)); |
| 97 | 56 |
| 98 // Shows all downloads. | 57 // Shows all downloads. |
| 99 downloads.ActionService.getInstance().search(''); | 58 downloads.ActionService.getInstance().search(''); |
| 100 }, | 59 }, |
| 101 | 60 |
| 102 /** | 61 /** |
| 103 * @param {number} index | 62 * @return {number} The number of downloads shown on the page. |
| 104 * @private | 63 * @private |
| 105 */ | 64 */ |
| 106 removeItem_: function(index) { | 65 size_: function() { |
| 107 this.splice('items_', index, 1); | 66 return this.items_.length; |
| 108 this.updateHideDates_(index, index); | |
| 109 }, | 67 }, |
| 110 | 68 |
| 111 /** | 69 /** |
| 112 * @param {number} start | 70 * Called when all items need to be updated. |
| 113 * @param {number} end | 71 * @param {!Array<!downloads.Data>} list A list of new download data. |
| 114 * @private | 72 * @private |
| 115 */ | 73 */ |
| 116 updateHideDates_: function(start, end) { | 74 updateAll_: function(list) { |
| 117 for (var i = start; i <= end; ++i) { | 75 /** @private {!Object<number>} */ |
| 118 var current = this.items_[i]; | 76 this.idToIndex_ = {}; |
| 119 if (!current) | 77 |
| 120 continue; | 78 for (var i = 0; i < list.length; ++i) { |
| 121 var prev = this.items_[i - 1]; | 79 var data = list[i]; |
| 122 current.hideDate = !!prev && prev.date_string == current.date_string; | 80 |
| 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; |
| 123 } | 85 } |
| 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'); |
| 124 }, | 107 }, |
| 125 | 108 |
| 126 /** | 109 /** |
| 127 * @param {number} index | |
| 128 * @param {!downloads.Data} data | 110 * @param {!downloads.Data} data |
| 129 * @private | 111 * @private |
| 130 */ | 112 */ |
| 131 updateItem_: function(index, data) { | 113 updateItem_: function(data) { |
| 114 var index = this.idToIndex_[data.id]; |
| 132 this.set('items_.' + index, data); | 115 this.set('items_.' + index, data); |
| 133 this.updateHideDates_(index, index); | |
| 134 this.$['downloads-list'].updateSizeForItem(index); | 116 this.$['downloads-list'].updateSizeForItem(index); |
| 135 }, | 117 }, |
| 136 }); | 118 }); |
| 137 | 119 |
| 138 Manager.clearAll = function() { | 120 Manager.size = function() { |
| 139 Manager.get().clearAll_(); | 121 return document.querySelector('downloads-manager').size_(); |
| 140 }; | 122 }; |
| 141 | 123 |
| 142 /** @return {!downloads.Manager} */ | 124 Manager.updateAll = function(list) { |
| 143 Manager.get = function() { | 125 document.querySelector('downloads-manager').updateAll_(list); |
| 144 return /** @type {!downloads.Manager} */( | |
| 145 queryRequiredElement('downloads-manager')); | |
| 146 }; | 126 }; |
| 147 | 127 |
| 148 Manager.insertItems = function(index, list) { | 128 Manager.updateItem = function(item) { |
| 149 Manager.get().insertItems_(index, list); | 129 document.querySelector('downloads-manager').updateItem_(item); |
| 150 }; | 130 }; |
| 151 | 131 |
| 152 Manager.onLoad = function() { | 132 Manager.onLoad = function() { |
| 153 Manager.get().onLoad_(); | 133 document.querySelector('downloads-manager').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); | |
| 162 }; | 134 }; |
| 163 | 135 |
| 164 return {Manager: Manager}; | 136 return {Manager: Manager}; |
| 165 }); | 137 }); |
| OLD | NEW |