Chromium Code Reviews| 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 /** | 6 /** |
| 7 * @param {string} chromeSendName | 7 * @param {string} chromeSendName |
| 8 * @return {function(string):void} A chrome.send() callback with curried name. | 8 * @return {function(string):void} A chrome.send() callback with curried name. |
| 9 */ | 9 */ |
| 10 function chromeSendWithId(chromeSendName) { | 10 function chromeSendWithId(chromeSendName) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 29 /** | 29 /** |
| 30 * @param {string} searchText Input typed by the user into a search box. | 30 * @param {string} searchText Input typed by the user into a search box. |
| 31 * @return {Array<string>} A list of terms extracted from |searchText|. | 31 * @return {Array<string>} A list of terms extracted from |searchText|. |
| 32 */ | 32 */ |
| 33 ActionService.splitTerms = function(searchText) { | 33 ActionService.splitTerms = function(searchText) { |
| 34 // Split quoted terms (e.g., 'The "lazy" dog' => ['The', 'lazy', 'dog']). | 34 // Split quoted terms (e.g., 'The "lazy" dog' => ['The', 'lazy', 'dog']). |
| 35 return searchText.split(/"([^"]*)"/).map(trim).filter(truthy); | 35 return searchText.split(/"([^"]*)"/).map(trim).filter(truthy); |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 ActionService.prototype = { | 38 ActionService.prototype = { |
| 39 /** @private {Array<string>} */ | |
| 40 searchTerms_: [], | |
|
esprehn
2015/12/10 21:42:59
this is static, the array is shred between all ins
Dan Beam
2015/12/10 21:51:15
yeah, but this class is a singleton. i understand
| |
| 41 | |
| 39 /** @param {string} id ID of the download to cancel. */ | 42 /** @param {string} id ID of the download to cancel. */ |
| 40 cancel: chromeSendWithId('cancel'), | 43 cancel: chromeSendWithId('cancel'), |
| 41 | 44 |
| 42 /** Instructs the browser to clear all finished downloads. */ | 45 /** Instructs the browser to clear all finished downloads. */ |
| 43 clearAll: function() { | 46 clearAll: function() { |
| 44 if (loadTimeData.getBoolean('allowDeletingHistory')) { | 47 if (loadTimeData.getBoolean('allowDeletingHistory')) { |
| 45 chrome.send('clearAll'); | 48 chrome.send('clearAll'); |
| 46 this.search(''); | 49 this.search(''); |
| 47 } | 50 } |
| 48 }, | 51 }, |
| 49 | 52 |
| 50 /** @param {string} id ID of the dangerous download to discard. */ | 53 /** @param {string} id ID of the dangerous download to discard. */ |
| 51 discardDangerous: chromeSendWithId('discardDangerous'), | 54 discardDangerous: chromeSendWithId('discardDangerous'), |
| 52 | 55 |
| 53 /** @param {string} url URL of a file to download. */ | 56 /** @param {string} url URL of a file to download. */ |
| 54 download: function(url) { | 57 download: function(url) { |
| 55 var a = document.createElement('a'); | 58 var a = document.createElement('a'); |
| 56 a.href = url; | 59 a.href = url; |
| 57 a.setAttribute('download', ''); | 60 a.setAttribute('download', ''); |
| 58 a.click(); | 61 a.click(); |
| 59 }, | 62 }, |
| 60 | 63 |
| 61 /** @param {string} id ID of the download that the user started dragging. */ | 64 /** @param {string} id ID of the download that the user started dragging. */ |
| 62 drag: chromeSendWithId('drag'), | 65 drag: chromeSendWithId('drag'), |
| 63 | 66 |
| 64 /** @private {boolean} */ | 67 /** Loads more downloads with the current search terms. */ |
| 65 isSearching_: false, | 68 loadMore: function() { |
| 69 chrome.send('getDownloads', this.searchTerms_); | |
| 70 }, | |
| 66 | 71 |
| 67 /** | 72 /** |
| 68 * @return {boolean} Whether the user is currently searching for downloads | 73 * @return {boolean} Whether the user is currently searching for downloads |
| 69 * (i.e. has a non-empty search term). | 74 * (i.e. has a non-empty search term). |
| 70 */ | 75 */ |
| 71 isSearching: function() { | 76 isSearching: function() { |
| 72 return this.isSearching_; | 77 return this.searchTerms_.length > 0; |
| 73 }, | 78 }, |
| 74 | 79 |
| 75 /** Opens the current local destination for downloads. */ | 80 /** Opens the current local destination for downloads. */ |
| 76 openDownloadsFolder: chrome.send.bind(chrome, 'openDownloadsFolder'), | 81 openDownloadsFolder: chrome.send.bind(chrome, 'openDownloadsFolder'), |
| 77 | 82 |
| 78 /** | 83 /** |
| 79 * @param {string} id ID of the download to run locally on the user's box. | 84 * @param {string} id ID of the download to run locally on the user's box. |
| 80 */ | 85 */ |
| 81 openFile: chromeSendWithId('openFile'), | 86 openFile: chromeSendWithId('openFile'), |
| 82 | 87 |
| 83 /** @param {string} id ID the of the progressing download to pause. */ | 88 /** @param {string} id ID the of the progressing download to pause. */ |
| 84 pause: chromeSendWithId('pause'), | 89 pause: chromeSendWithId('pause'), |
| 85 | 90 |
| 86 /** @param {string} id ID of the finished download to remove. */ | 91 /** @param {string} id ID of the finished download to remove. */ |
| 87 remove: chromeSendWithId('remove'), | 92 remove: chromeSendWithId('remove'), |
| 88 | 93 |
| 89 /** @param {string} id ID of the paused download to resume. */ | 94 /** @param {string} id ID of the paused download to resume. */ |
| 90 resume: chromeSendWithId('resume'), | 95 resume: chromeSendWithId('resume'), |
| 91 | 96 |
| 92 /** | 97 /** |
| 93 * @param {string} id ID of the dangerous download to save despite | 98 * @param {string} id ID of the dangerous download to save despite |
| 94 * warnings. | 99 * warnings. |
| 95 */ | 100 */ |
| 96 saveDangerous: chromeSendWithId('saveDangerous'), | 101 saveDangerous: chromeSendWithId('saveDangerous'), |
| 97 | 102 |
| 98 /** @param {string} searchText What to search for. */ | 103 /** @param {string} searchText What to search for. */ |
| 99 search: function(searchText) { | 104 search: function(searchText) { |
| 100 if (this.searchText_ == searchText) | 105 var searchTerms = ActionService.splitTerms(searchText); |
| 106 var sameTerms = searchTerms.length == this.searchTerms_.length; | |
| 107 | |
| 108 for (var i = 0; sameTerms && i < searchTerms.length; ++i) { | |
|
esprehn
2015/12/10 21:42:59
there's no standard library in WebUI like closure?
Dan Beam
2015/12/10 21:51:15
not currently. why isn't there an Array equals...
| |
| 109 if (searchTerms[i] != this.searchTerms_[i]) | |
| 110 sameTerms = false; | |
| 111 } | |
| 112 | |
| 113 if (sameTerms) | |
| 101 return; | 114 return; |
| 102 | 115 |
| 103 this.searchText_ = searchText; | 116 this.searchTerms_ = searchTerms; |
|
esprehn
2015/12/10 21:42:59
this is actually shadowing the variable on the pro
Dan Beam
2015/12/10 21:51:15
that's fine for our use-case, but will send a foll
Dan Beam
2015/12/10 22:08:41
followup here: https://codereview.chromium.org/151
| |
| 104 | 117 this.loadMore(); |
| 105 var terms = ActionService.splitTerms(searchText); | |
| 106 this.isSearching_ = terms.length > 0; | |
| 107 | |
| 108 chrome.send('getDownloads', terms); | |
| 109 }, | 118 }, |
| 110 | 119 |
| 111 /** | 120 /** |
| 112 * Shows the local folder a finished download resides in. | 121 * Shows the local folder a finished download resides in. |
| 113 * @param {string} id ID of the download to show. | 122 * @param {string} id ID of the download to show. |
| 114 */ | 123 */ |
| 115 show: chromeSendWithId('show'), | 124 show: chromeSendWithId('show'), |
| 116 | 125 |
| 117 /** Undo download removal. */ | 126 /** Undo download removal. */ |
| 118 undo: chrome.send.bind(chrome, 'undo'), | 127 undo: chrome.send.bind(chrome, 'undo'), |
| 119 }; | 128 }; |
| 120 | 129 |
| 121 cr.addSingletonGetter(ActionService); | 130 cr.addSingletonGetter(ActionService); |
| 122 | 131 |
| 123 return {ActionService: ActionService}; | 132 return {ActionService: ActionService}; |
| 124 }); | 133 }); |
| OLD | NEW |