Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(483)

Side by Side Diff: chrome/browser/resources/md_downloads/item_view.js

Issue 1224623013: Port downloads.ItemView to a Polymer component. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@p-dl-rough-draft2
Patch Set: merge Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 cr.define('downloads', function() {
6 var ItemView = Polymer({
7 is: 'item-view',
8
9 /** @param {!downloads.ThrottledIconLoader} iconLoader */
10 factoryImpl: function(iconLoader) {
11 /** @private {!downloads.ThrottledIconLoader} */
12 this.iconLoader_ = iconLoader;
13 },
14
15 properties: {
16 hideDate: {type: Boolean, value: false},
17 isDangerous: {type: Boolean, value: false},
18 // Only use |isMalware| if |isDangerous| is true.
19 isMalware: Boolean,
20 },
21
22 ready: function() {
23 this.$.safe.ondragstart = this.onSafeDragstart_.bind(this);
24 this.$['file-link'].onclick = this.onFileLinkClick_.bind(this);
25 this.$.show.onclick = this.onShowClick_.bind(this);
26 this.$.pause.onclick = this.onPauseClick_.bind(this);
27 this.$.resume.onclick = this.onResumeClick_.bind(this);
28 this.$['safe-remove'].onclick = this.onSafeRemoveClick_.bind(this);
29 this.$.cancel.onclick = this.onCancelClick_.bind(this);
30 this.$.restore.onclick = this.onRestoreClick_.bind(this);
31 this.$.save.onclick = this.onSaveClick_.bind(this);
32 this.$['dangerous-remove'].onclick = this.onDangerRemoveClick_.bind(this);
33 this.$.discard.onclick = this.onDiscardClick_.bind(this);
34 },
35
36 /** @param {!downloads.Data} data */
37 update: function(data) {
38 assert(!this.id_ || data.id == this.id_);
39 this.id_ = data.id; // This is the only thing saved from |data|.
40
41 this.classList.toggle('otr', data.otr);
42
43 this.ensureTextIs_(this.$.since, data.since_string);
44 this.ensureTextIs_(this.$.date, data.date_string);
45
46 var dangerText = this.getDangerText_(data);
47 this.isDangerous = !!dangerText;
48
49 if (dangerText) {
50 this.ensureTextIs_(this.$.description, dangerText);
51
52 var dangerType = data.danger_type;
53 var dangerousFile = dangerType == downloads.DangerType.DANGEROUS_FILE;
54 this.$.description.classList.toggle('malware', !dangerousFile);
55
56 var idr = dangerousFile ? 'IDR_WARNING' : 'IDR_SAFEBROWSING_WARNING';
57 var iconUrl = 'chrome://theme/' + idr;
58 this.iconLoader_.loadScaledIcon(this.$['dangerous-icon'], iconUrl);
59
60 this.isMalware =
61 dangerType == downloads.DangerType.DANGEROUS_CONTENT ||
62 dangerType == downloads.DangerType.DANGEROUS_HOST ||
63 dangerType == downloads.DangerType.DANGEROUS_URL ||
64 dangerType == downloads.DangerType.POTENTIALLY_UNWANTED;
65 } else {
66 var iconUrl = 'chrome://fileicon/' + encodeURIComponent(data.file_path);
67 this.iconLoader_.loadScaledIcon(this.$['safe-icon'], iconUrl);
68
69 /** @const */ var isInProgress =
70 data.state == downloads.States.IN_PROGRESS;
71 this.classList.toggle('in-progress', isInProgress);
72
73 /** @const */ var completelyOnDisk =
74 data.state == downloads.States.COMPLETE &&
75 !data.file_externally_removed;
76
77 this.$['file-link'].href = data.url;
78 this.ensureTextIs_(this.$['file-link'], data.file_name);
79 this.$['file-link'].hidden = !completelyOnDisk;
80
81 /** @const */ var isInterrupted =
82 data.state == downloads.States.INTERRUPTED;
83 this.$.name.classList.toggle('interrupted', isInterrupted);
84 this.ensureTextIs_(this.$.name, data.file_name);
85 this.$.name.hidden = completelyOnDisk;
86
87 this.$.show.hidden = !completelyOnDisk;
88
89 this.$.retry.href = data.url;
90 this.$.retry.hidden = !data.retry;
91
92 this.$.pause.hidden = !isInProgress;
93
94 this.$.resume.hidden = !data.resume;
95
96 /** @const */ var isPaused = data.state == downloads.States.PAUSED;
97 /** @const */ var showCancel = isPaused || isInProgress;
98 this.$.cancel.hidden = !showCancel;
99
100 this.$['safe-remove'].hidden = showCancel ||
101 !loadTimeData.getBoolean('allow_deleting_history');
102
103 /** @const */ var controlledByExtension = data.by_ext_id &&
104 data.by_ext_name;
105 this.$['controlled-by'].hidden = !controlledByExtension;
106 if (controlledByExtension) {
107 var link = this.$['controlled-by'].querySelector('a');
108 link.href = 'chrome://extensions#' + data.by_ext_id;
109 link.setAttribute('column-type', 'controlled-by');
110 link.textContent = data.by_ext_name;
111 }
112
113 this.ensureTextIs_(this.$['src-url'], data.url);
114 this.$['src-url'].href = data.url;
115 this.ensureTextIs_(this.$.status, this.getStatusText_(data));
116
117 this.$.progress.hidden = !isInProgress;
118
119 // TODO(dbeam): implement progress.
120 }
121 },
122
123 /**
124 * Overwrite |el|'s textContent if it differs from |text|.
125 * @param {!Element} el
126 * @param {string} text
127 * @private
128 */
129 ensureTextIs_: function(el, text) {
130 if (el.textContent != text)
131 el.textContent = text;
132 },
133
134 /**
135 * @param {!downloads.Data} data
136 * @return {string} Text describing the danger of a download. Empty if not
137 * dangerous.
138 */
139 getDangerText_: function(data) {
140 switch (data.danger_type) {
141 case downloads.DangerType.DANGEROUS_FILE:
142 return loadTimeData.getStringF('danger_file_desc', data.file_name);
143 case downloads.DangerType.DANGEROUS_URL:
144 return loadTimeData.getString('danger_url_desc');
145 case downloads.DangerType.DANGEROUS_CONTENT: // Fall through.
146 case downloads.DangerType.DANGEROUS_HOST:
147 return loadTimeData.getStringF('danger_content_desc', data.file_name);
148 case downloads.DangerType.UNCOMMON_CONTENT:
149 return loadTimeData.getStringF('danger_uncommon_desc',
150 data.file_name);
151 case downloads.DangerType.POTENTIALLY_UNWANTED:
152 return loadTimeData.getStringF('danger_settings_desc',
153 data.file_name);
154 default:
155 return '';
156 }
157 },
158
159 /**
160 * @param {!downloads.Data} data
161 * @return {string} User-visible status update text.
162 * @private
163 */
164 getStatusText_: function(data) {
165 switch (data.state) {
166 case downloads.States.IN_PROGRESS:
167 case downloads.States.PAUSED: // Fallthrough.
168 assert(typeof data.progress_status_text == 'string');
169 return data.progress_status_text;
170 case downloads.States.CANCELLED:
171 return loadTimeData.getString('status_cancelled');
172 case downloads.States.DANGEROUS:
173 break; // Intentionally hit assertNotReached(); at bottom.
174 case downloads.States.INTERRUPTED:
175 assert(typeof data.last_reason_text == 'string');
176 return data.last_reason_text;
177 case downloads.States.COMPLETE:
178 return data.file_externally_removed ?
179 loadTimeData.getString('status_removed') : '';
180 }
181 assertNotReached();
182 return '';
183 },
184
185 /**
186 * @private
187 * @param {Event} e
188 */
189 onSafeDragstart_: function(e) {
190 e.preventDefault();
191 chrome.send('drag', [this.id_]);
192 },
193
194 /**
195 * @param {Event} e
196 * @private
197 */
198 onFileLinkClick_: function(e) {
199 e.preventDefault();
200 chrome.send('openFile', [this.id_]);
201 },
202
203 /** @private */
204 onShowClick_: function() {
205 chrome.send('show', [this.id_]);
206 },
207
208 /** @private */
209 onPauseClick_: function() {
210 chrome.send('pause', [this.id_]);
211 },
212
213 /** @private */
214 onResumeClick_: function() {
215 chrome.send('resume', [this.id_]);
216 },
217
218 /** @private */
219 onSafeRemoveClick_: function() {
220 chrome.send('remove', [this.id_]);
221 },
222
223 /** @private */
224 onCancelClick_: function() {
225 chrome.send('cancel', [this.id_]);
226 },
227
228 /** @private */
229 onRestoreClick_: function() {
230 this.onSaveClick_();
231 },
232
233 /** @private */
234 onSaveClick_: function() {
235 chrome.send('saveDangerous', [this.id_]);
236 },
237
238 /** @private */
239 onDangerRemoveClick_: function() {
240 this.onDiscardClick_();
241 },
242
243 /** @private */
244 onDiscardClick_: function() {
245 chrome.send('discardDangerous', [this.id_]);
246 },
247 });
248
249 return {ItemView: ItemView};
250 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/md_downloads/item_view.html ('k') | chrome/browser/resources/md_downloads/manager.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698