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

Side by Side Diff: chrome/browser/resources/ntp_search/most_visited_page.js

Issue 10823052: Refactoring the NTP. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 4 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 (c) 2012 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('ntp', function() {
6 'use strict';
7
8 var Thumbnail = ntp.Thumbnail;
9 var ThumbnailPage = ntp.ThumbnailPage;
10
11 /**
12 * Creates a new Most Visited object for tiling.
13 * @constructor
14 * @extends {HTMLAnchorElement}
15 */
16 function MostVisited() {
jeremycho_google 2012/07/31 03:09:16 Pass in gridValues?
jeremycho_google 2012/07/31 03:09:16 This doesn't seem to get called.
pedrosimonetti2 2012/08/03 18:14:01 Done. See comments on the new CL.
pedrosimonetti2 2012/08/03 18:14:01 Done.
17 var el = cr.doc.createElement('a');
18 el.__proto__ = MostVisited.prototype;
19 el.initialize();
20
21 return el;
22 }
23
24 MostVisited.prototype = {
25 __proto__: Thumbnail.prototype,
26 };
27
28 var THUMBNAIL_COUNT = 8;
29
30 /**
31 * Creates a new MostVisitedPage object.
32 * @constructor
33 * @extends {TilePage}
34 */
35 function MostVisitedPage() {
36 var el = new ThumbnailPage();
37 el.__proto__ = MostVisitedPage.prototype;
38 el.initialize();
39
40 return el;
41 }
42
43 MostVisitedPage.prototype = {
44 __proto__: ThumbnailPage.prototype,
45
46 set data(data) {
jeremycho_google 2012/07/31 03:09:16 Is it possible to avoid duplicating this from Thum
pedrosimonetti2 2012/08/03 18:14:01 I replaced the code from ThumbnailPage.prototype.s
47 var startTime = Date.now();
48
49 // The first time data is set, create the tiles.
50 if (!this.data_) {
51 this.createTiles_();
52 this.data_ = data.slice(0, THUMBNAIL_COUNT);
53 } else {
54 this.data_ = refreshData(this.data_, data);
55 }
56
57 this.updateTiles_();
58 logEvent('mostVisited.layout: ' + (Date.now() - startTime));
59 },
60 };
61
62 /**
63 * Executed once the NTP has loaded. Checks if the Most Visited pane is
64 * shown or not. If it is shown, the 'mostVisitedSelected' message is sent
65 * to the C++ code, to record the fact that the user has seen this pane.
66 */
67 MostVisitedPage.onLoaded = function() {
68 if (ntp.getCardSlider() &&
69 ntp.getCardSlider().currentCardValue &&
70 ntp.getCardSlider().currentCardValue.classList
71 .contains('most-visited-page')) {
72 chrome.send('mostVisitedSelected');
73 }
74 }
75
76 /**
77 * We've gotten additional Most Visited data. Update our old data with the
78 * new data. The ordering of the new data is not important, except when a
79 * page is pinned. Thus we try to minimize re-ordering.
80 * @param {Array} oldData The current Most Visited page list.
81 * @param {Array} newData The new Most Visited page list.
82 * @return {Array} The merged page list that should replace the current page
83 * list.
84 */
85 function refreshData(oldData, newData) {
86 oldData = oldData.slice(0, THUMBNAIL_COUNT);
87 newData = newData.slice(0, THUMBNAIL_COUNT);
88
89 // Copy over pinned sites directly.
90 for (var j = 0; j < newData.length; j++) {
91 if (newData[j].pinned) {
92 oldData[j] = newData[j];
93 // Mark the entry as 'updated' so we don't try to update again.
94 oldData[j].updated = true;
95 // Mark the newData page as 'used' so we don't try to re-use it.
96 newData[j].used = true;
97 }
98 }
99
100 // Look through old pages; if they exist in the newData list, keep them
101 // where they are.
102 for (var i = 0; i < oldData.length; i++) {
103 if (!oldData[i] || oldData[i].updated)
104 continue;
105
106 for (var j = 0; j < newData.length; j++) {
107 if (newData[j].used)
108 continue;
109
110 if (newData[j].url == oldData[i].url) {
111 // The background image and other data may have changed.
112 oldData[i] = newData[j];
113 oldData[i].updated = true;
114 newData[j].used = true;
115 break;
116 }
117 }
118 }
119
120 // Look through old pages that haven't been updated yet; replace them.
121 for (var i = 0; i < oldData.length; i++) {
122 if (oldData[i] && oldData[i].updated)
123 continue;
124
125 for (var j = 0; j < newData.length; j++) {
126 if (newData[j].used)
127 continue;
128
129 oldData[i] = newData[j];
130 oldData[i].updated = true;
131 newData[j].used = true;
132 break;
133 }
134
135 if (oldData[i] && !oldData[i].updated)
136 oldData[i] = null;
137 }
138
139 // Clear 'updated' flags so this function will work next time it's called.
140 for (var i = 0; i < THUMBNAIL_COUNT; i++) {
141 if (oldData[i])
142 oldData[i].updated = false;
143 }
144
145 return oldData;
146 };
147
148 return {
149 MostVisitedPage: MostVisitedPage,
150 refreshData: refreshData,
151 };
152 });
153
154 document.addEventListener('ntpLoaded', ntp.MostVisitedPage.onLoaded);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698