OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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('ntp4', function() { | 5 cr.define('ntp4', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 var TilePage = ntp4.TilePage; | 8 var TilePage = ntp4.TilePage; |
9 | 9 |
10 /** | 10 /** |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 | 98 |
99 this.updatePinnedState_(); | 99 this.updatePinnedState_(); |
100 }, | 100 }, |
101 | 101 |
102 /** | 102 /** |
103 * Handles a click on the tile. | 103 * Handles a click on the tile. |
104 * @param {Event} e The click event. | 104 * @param {Event} e The click event. |
105 */ | 105 */ |
106 handleClick_: function(e) { | 106 handleClick_: function(e) { |
107 var target = e.target; | 107 var target = e.target; |
| 108 |
| 109 // Don't navigate on edit bar clicks. |
| 110 if (this.querySelector('.edit-bar').contains(target)) |
| 111 e.preventDefault(); |
| 112 |
108 if (target.classList.contains('pin')) { | 113 if (target.classList.contains('pin')) { |
109 this.togglePinned_(); | 114 this.setPinned_(!this.data_.pinned); |
110 e.preventDefault(); | |
111 } else if (target.classList.contains('remove')) { | 115 } else if (target.classList.contains('remove')) { |
112 this.blacklist_(); | 116 this.blacklist_(); |
113 e.preventDefault(); | |
114 } else { | 117 } else { |
115 chrome.send('metrics', ['NTP_MostVisited' + this.index]); | 118 chrome.send('metrics', ['NTP_MostVisited' + this.index]); |
116 } | 119 } |
117 }, | 120 }, |
118 | 121 |
119 /** | 122 /** |
120 * Allow blacklisting most visited site using the keyboard. | 123 * Allow blacklisting most visited site using the keyboard. |
121 */ | 124 */ |
122 handleKeyDown_: function(e) { | 125 handleKeyDown_: function(e) { |
123 if (!IS_MAC && e.keyCode == 46 || // Del | 126 if (!IS_MAC && e.keyCode == 46 || // Del |
124 IS_MAC && e.metaKey && e.keyCode == 8) { // Cmd + Backspace | 127 IS_MAC && e.metaKey && e.keyCode == 8) { // Cmd + Backspace |
125 this.blacklist_(); | 128 this.blacklist_(); |
126 } | 129 } |
127 }, | 130 }, |
128 | 131 |
129 /** | 132 /** |
130 * Changes the visual state of the page and updates the model. | 133 * Changes the visual state of the page and updates the model. |
131 */ | 134 */ |
132 togglePinned_: function() { | 135 setPinned_: function(pinned) { |
133 var data = this.data_; | 136 var data = this.data_; |
134 data.pinned = !data.pinned; | 137 data.pinned = pinned; |
135 if (data.pinned) { | 138 if (data.pinned) { |
136 chrome.send('addPinnedURL', [ | 139 chrome.send('addPinnedURL', [ |
137 data.url, | 140 data.url, |
138 data.title, | 141 data.title, |
139 data.faviconUrl || '', | 142 data.faviconUrl || '', |
140 data.thumbnailUrl || '', | 143 data.thumbnailUrl || '', |
141 // TODO(estade): should not need to convert index to string. | 144 // TODO(estade): should not need to convert index to string. |
142 String(this.index) | 145 String(this.index) |
143 ]); | 146 ]); |
144 } else { | 147 } else { |
(...skipping 13 matching lines...) Expand all Loading... |
158 } else { | 161 } else { |
159 this.classList.remove('pinned'); | 162 this.classList.remove('pinned'); |
160 this.querySelector('.pin').title = templateData.pinthumbnailtooltip; | 163 this.querySelector('.pin').title = templateData.pinthumbnailtooltip; |
161 } | 164 } |
162 }, | 165 }, |
163 | 166 |
164 /** | 167 /** |
165 * Permanently removes a page from Most Visited. | 168 * Permanently removes a page from Most Visited. |
166 */ | 169 */ |
167 blacklist_: function() { | 170 blacklist_: function() { |
| 171 this.showUndoNotification_(); |
168 chrome.send('blacklistURLFromMostVisited', [this.data_.url]); | 172 chrome.send('blacklistURLFromMostVisited', [this.data_.url]); |
169 this.reset(); | 173 this.reset(); |
170 chrome.send('getMostVisited'); | 174 chrome.send('getMostVisited'); |
171 }, | 175 }, |
172 | 176 |
| 177 showUndoNotification_: function() { |
| 178 var data = this.data_; |
| 179 var pinned = data.pinned; |
| 180 var self = this; |
| 181 var doUndo = function () { |
| 182 chrome.send('removeURLsFromMostVisitedBlacklist', [data.url]); |
| 183 self.updateForData(data); |
| 184 self.setPinned_(data.pinned); |
| 185 // chrome.send('getMostVisited'); |
| 186 } |
| 187 |
| 188 var undo = { |
| 189 action: doUndo, |
| 190 text: templateData.undothumbnailremove, |
| 191 } |
| 192 |
| 193 var undoAll = { |
| 194 action: function() { |
| 195 chrome.send('clearMostVisitedURLsBlacklist', []); |
| 196 }, |
| 197 text: templateData.restoreThumbnailsShort, |
| 198 } |
| 199 |
| 200 ntp4.showNotification(templateData.thumbnailremovednotification, |
| 201 [undo, undoAll]); |
| 202 }, |
| 203 |
173 /** | 204 /** |
174 * Set the size and position of the most visited tile. | 205 * Set the size and position of the most visited tile. |
175 * @param {number} size The total size of |this|. | 206 * @param {number} size The total size of |this|. |
176 * @param {number} x The x-position. | 207 * @param {number} x The x-position. |
177 * @param {number} y The y-position. | 208 * @param {number} y The y-position. |
178 * animate. | 209 * animate. |
179 */ | 210 */ |
180 setBounds: function(size, x, y) { | 211 setBounds: function(size, x, y) { |
181 this.style.width = size + 'px'; | 212 this.style.width = size + 'px'; |
182 this.style.height = heightForWidth(size) + 'px'; | 213 this.style.height = heightForWidth(size) + 'px'; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 // Mark the entry as 'updated' so we don't try to update again. | 338 // Mark the entry as 'updated' so we don't try to update again. |
308 oldData[j].updated = true; | 339 oldData[j].updated = true; |
309 // Mark the newData page as 'used' so we don't try to re-use it. | 340 // Mark the newData page as 'used' so we don't try to re-use it. |
310 newData[j].used = true; | 341 newData[j].used = true; |
311 } | 342 } |
312 } | 343 } |
313 | 344 |
314 // Look through old pages; if they exist in the newData list, keep them | 345 // Look through old pages; if they exist in the newData list, keep them |
315 // where they are. | 346 // where they are. |
316 for (var i = 0; i < oldData.length; i++) { | 347 for (var i = 0; i < oldData.length; i++) { |
317 if (oldData[i].updated) | 348 if (!oldData[i] || oldData[i].updated) |
318 continue; | 349 continue; |
319 | 350 |
320 for (var j = 0; j < newData.length; j++) { | 351 for (var j = 0; j < newData.length; j++) { |
321 if (newData[j].used) | 352 if (newData[j].used) |
322 continue; | 353 continue; |
323 | 354 |
324 if (newData[j].url == oldData[i].url) { | 355 if (newData[j].url == oldData[i].url) { |
325 // The background image and other data may have changed. | 356 // The background image and other data may have changed. |
326 oldData[i] = newData[j]; | 357 oldData[i] = newData[j]; |
327 oldData[i].updated = true; | 358 oldData[i].updated = true; |
328 newData[j].used = true; | 359 newData[j].used = true; |
329 break; | 360 break; |
330 } | 361 } |
331 } | 362 } |
332 } | 363 } |
333 | 364 |
334 // Look through old pages that haven't been updated yet; replace them. | 365 // Look through old pages that haven't been updated yet; replace them. |
335 for (var i = 0; i < oldData.length; i++) { | 366 for (var i = 0; i < oldData.length; i++) { |
336 if (oldData[i].updated) | 367 if (oldData[i] && oldData[i].updated) |
337 continue; | 368 continue; |
338 | 369 |
339 for (var j = 0; j < newData.length; j++) { | 370 for (var j = 0; j < newData.length; j++) { |
340 if (newData[j].used) | 371 if (newData[j].used) |
341 continue; | 372 continue; |
342 | 373 |
343 oldData[i] = newData[j]; | 374 oldData[i] = newData[j]; |
344 oldData[i].updated = true; | 375 oldData[i].updated = true; |
345 newData[j].used = true; | 376 newData[j].used = true; |
346 break; | 377 break; |
347 } | 378 } |
348 | 379 |
349 if (!oldData[i].updated) | 380 if (oldData[i] && !oldData[i].updated) |
350 oldData[i] = null; | 381 oldData[i] = null; |
351 } | 382 } |
352 | 383 |
353 // Clear 'updated' flags so this function will work next time it's called. | 384 // Clear 'updated' flags so this function will work next time it's called. |
354 for (var i = 0; i < THUMBNAIL_COUNT; i++) { | 385 for (var i = 0; i < THUMBNAIL_COUNT; i++) { |
355 if (oldData[i]) | 386 if (oldData[i]) |
356 oldData[i].updated = false; | 387 oldData[i].updated = false; |
357 } | 388 } |
358 | 389 |
359 return oldData; | 390 return oldData; |
360 }; | 391 }; |
361 | 392 |
362 return { | 393 return { |
363 MostVisitedPage: MostVisitedPage, | 394 MostVisitedPage: MostVisitedPage, |
364 refreshData: refreshData, | 395 refreshData: refreshData, |
365 }; | 396 }; |
366 }); | 397 }); |
OLD | NEW |