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) { |
11 return function(id) { chrome.send(chromeSendName, [id]); }; | 11 return function(id) { chrome.send(chromeSendName, [id]); }; |
12 } | 12 } |
13 | 13 |
14 /** @constructor */ | 14 /** @constructor */ |
15 function ActionService() {} | 15 function ActionService() {} |
16 | 16 |
17 /** | |
18 * @param {string} searchText Input typed by the user into a search box. | |
19 * @return {Array<string>} A list of terms extracted from |searchText|. | |
20 */ | |
21 ActionService.splitTerms = function(searchText) { | |
22 /** | |
23 * @param {string} s | |
24 * @return {string} |s| without whitespace at the beginning or end. | |
25 */ | |
26 function trim(s) { return s.trim(); } | |
27 | |
28 /** | |
29 * @param {string} s | |
30 * @return {boolean} Whether |s| is empty. | |
31 */ | |
32 function notEmpty(s) { return s.length > 0; } | |
dpapad
2015/11/25 17:50:47
Nit: We are re-defining the trim() and notEpmty()
Dan Beam
2015/11/25 20:23:23
Done.
| |
33 | |
34 // Split quoted terms (e.g., 'The "lazy" dog' => ['The', 'lazy', 'dog']). | |
35 return searchText.split(/"([^"]*)"/).map(trim).filter(notEmpty); | |
36 }; | |
37 | |
17 ActionService.prototype = { | 38 ActionService.prototype = { |
18 /** @param {string} id ID of the download to cancel. */ | 39 /** @param {string} id ID of the download to cancel. */ |
19 cancel: chromeSendWithId('cancel'), | 40 cancel: chromeSendWithId('cancel'), |
20 | 41 |
21 /** Instructs the browser to clear all finished downloads. */ | 42 /** Instructs the browser to clear all finished downloads. */ |
22 clearAll: function() { | 43 clearAll: function() { |
23 if (loadTimeData.getBoolean('allowDeletingHistory')) { | 44 if (loadTimeData.getBoolean('allowDeletingHistory')) { |
24 chrome.send('clearAll'); | 45 chrome.send('clearAll'); |
25 this.search(''); | 46 this.search(''); |
26 } | 47 } |
(...skipping 11 matching lines...) Expand all Loading... | |
38 }, | 59 }, |
39 | 60 |
40 /** @param {string} id ID of the download that the user started dragging. */ | 61 /** @param {string} id ID of the download that the user started dragging. */ |
41 drag: chromeSendWithId('drag'), | 62 drag: chromeSendWithId('drag'), |
42 | 63 |
43 /** | 64 /** |
44 * @return {boolean} Whether the user is currently searching for downloads | 65 * @return {boolean} Whether the user is currently searching for downloads |
45 * (i.e. has a non-empty search term). | 66 * (i.e. has a non-empty search term). |
46 */ | 67 */ |
47 isSearching: function() { | 68 isSearching: function() { |
48 return this.searchText_.length > 0; | 69 return !!this.searchText_; |
dpapad
2015/11/25 17:50:47
If you follow the comment above, then you can re-u
Dan Beam
2015/11/25 20:23:23
changed a little, but basically doing this
| |
49 }, | 70 }, |
50 | 71 |
51 /** Opens the current local destination for downloads. */ | 72 /** Opens the current local destination for downloads. */ |
52 openDownloadsFolder: chrome.send.bind(chrome, 'openDownloadsFolder'), | 73 openDownloadsFolder: chrome.send.bind(chrome, 'openDownloadsFolder'), |
53 | 74 |
54 /** | 75 /** |
55 * @param {string} id ID of the download to run locally on the user's box. | 76 * @param {string} id ID of the download to run locally on the user's box. |
56 */ | 77 */ |
57 openFile: chromeSendWithId('openFile'), | 78 openFile: chromeSendWithId('openFile'), |
58 | 79 |
(...skipping 11 matching lines...) Expand all Loading... | |
70 * warnings. | 91 * warnings. |
71 */ | 92 */ |
72 saveDangerous: chromeSendWithId('saveDangerous'), | 93 saveDangerous: chromeSendWithId('saveDangerous'), |
73 | 94 |
74 /** @param {string} searchText What to search for. */ | 95 /** @param {string} searchText What to search for. */ |
75 search: function(searchText) { | 96 search: function(searchText) { |
76 if (this.searchText_ == searchText) | 97 if (this.searchText_ == searchText) |
77 return; | 98 return; |
78 | 99 |
79 this.searchText_ = searchText; | 100 this.searchText_ = searchText; |
80 | 101 chrome.send('getDownloads', ActionService.splitTerms(searchText)); |
81 // Split quoted terms (e.g., 'The "lazy" dog' => ['The', 'lazy', 'dog']). | |
82 function trim(s) { return s.trim(); } | |
83 chrome.send('getDownloads', searchText.split(/"([^"]*)"/).map(trim)); | |
84 }, | 102 }, |
85 | 103 |
86 /** | 104 /** |
87 * Shows the local folder a finished download resides in. | 105 * Shows the local folder a finished download resides in. |
88 * @param {string} id ID of the download to show. | 106 * @param {string} id ID of the download to show. |
89 */ | 107 */ |
90 show: chromeSendWithId('show'), | 108 show: chromeSendWithId('show'), |
91 | 109 |
92 /** Undo download removal. */ | 110 /** Undo download removal. */ |
93 undo: chrome.send.bind(chrome, 'undo'), | 111 undo: chrome.send.bind(chrome, 'undo'), |
94 }; | 112 }; |
95 | 113 |
96 cr.addSingletonGetter(ActionService); | 114 cr.addSingletonGetter(ActionService); |
97 | 115 |
98 return {ActionService: ActionService}; | 116 return {ActionService: ActionService}; |
99 }); | 117 }); |
OLD | NEW |