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 localStrings = new LocalStrings; | |
9 | |
10 /** | |
11 * A running count of bookmark tiles that we create so that each will have | |
12 * a unique ID. | |
Sheridan Rawlins
2011/08/16 05:50:28
nit: @type {number}
| |
13 */ | |
14 var tileId = 0; | |
15 | |
16 /** | |
17 * Creates a new bookmark object. | |
18 * @param {Object} data The url and title. | |
19 * @constructor | |
20 * @extends {HTMLDivElement} | |
21 */ | |
22 function Bookmark(data) { | |
23 var el = $('bookmark-template').cloneNode(true); | |
24 el.__proto__ = Bookmark.prototype; | |
25 el.data = data; | |
26 el.initialize(); | |
27 | |
28 return el; | |
29 } | |
30 | |
31 Bookmark.prototype = { | |
32 __proto__: HTMLDivElement.prototype, | |
33 | |
34 initialize: function() { | |
Sheridan Rawlins
2011/08/16 05:50:28
nit: JSDoc (@inheritDoc?)
arv (Not doing code reviews)
2011/08/16 19:36:56
HTMLDivElement does not have initialize so this sh
| |
35 var id = tileId++; | |
36 this.id = 'bookmark_tile_' + id; | |
37 | |
38 var title = this.querySelector('.title'); | |
39 title.textContent = this.data.title; | |
40 | |
41 if (this.data.url) { | |
42 var button = this.querySelector('.button'); | |
43 button.href = title.href = this.data.url; | |
44 } | |
45 | |
46 var faviconDiv = this.querySelector('.favicon'); | |
47 var faviconUrl; | |
48 if (this.data.url) { | |
49 faviconUrl = 'chrome://favicon/size/32/' + this.data.url; | |
50 chrome.send('getFaviconDominantColor', | |
51 [faviconUrl, id, 'ntp4.setBookmarksFaviconDominantColor']); | |
52 } else { | |
53 // TODO(csilv): We need a large (32px) icon for this URL. | |
54 faviconUrl = 'chrome://theme/IDR_BOOKMARK_BAR_FOLDER'; | |
55 // TODO(csilv): Should we vary this color by platform? | |
56 this.stripeColor = '#919191'; | |
57 } | |
58 faviconDiv.style.backgroundImage = url(faviconUrl); | |
59 | |
60 this.addEventListener('click', this.handleClick_.bind(this)); | |
61 }, | |
62 | |
63 /** | |
64 * Sets the color of the favicon dominant color bar. | |
65 * @param {string} color The css-parsable value for the color. | |
66 */ | |
67 set stripeColor(color) { | |
68 this.querySelector('.color-stripe').style.backgroundColor = color; | |
69 }, | |
70 | |
71 /** | |
72 * Set the size and position of the bookmark tile. | |
73 * @param {number} size The total size of |this|. | |
74 * @param {number} x The x-position. | |
75 * @param {number} y The y-position. | |
76 * animate. | |
Sheridan Rawlins
2011/08/16 05:50:28
Sorry, what does "animate" mean?
| |
77 */ | |
78 setBounds: function(size, x, y) { | |
79 this.style.width = this.style.height = size + 'px'; | |
80 this.style.left = x + 'px'; | |
81 this.style.top = y + 'px'; | |
82 }, | |
83 | |
84 /** | |
85 * Invoked when a bookmark is clicked | |
86 * @param {Event} e The click event. | |
87 * @private | |
88 */ | |
89 handleClick_: function(e) { | |
90 if (e.target.classList.contains('close-button')) { | |
91 this.handleDelete_(); | |
92 e.preventDefault(); | |
93 } else if (!this.data.url) { | |
94 chrome.send('getBookmarksData', [this.data.id]); | |
95 e.preventDefault(); | |
96 } | |
97 }, | |
98 | |
99 /** | |
100 * Delete a bookmark from the data model. | |
101 * @private | |
102 */ | |
103 handleDelete_: function() { | |
104 // TODO(csilv): add support for deleting bookmarks | |
105 }, | |
106 }; | |
107 | |
108 /** | |
109 * Creates a new bookmark title object. | |
110 * @param {Object} data The url and title. | |
111 * @constructor | |
112 * @extends {HTMLDivElement} | |
113 */ | |
114 function BookmarkTitle(data) { | |
115 var el = cr.doc.createElement('div'); | |
116 el.__proto__ = BookmarkTitle.prototype; | |
117 el.initialize(data); | |
118 | |
119 return el; | |
120 } | |
121 | |
122 BookmarkTitle.prototype = { | |
123 __proto__: HTMLDivElement.prototype, | |
124 | |
125 initialize: function(data) { | |
Sheridan Rawlins
2011/08/16 05:50:28
nit: JSDoc (@inheritDoc ?)
| |
126 this.className = 'title-crumb'; | |
127 this.folderId_ = data.id; | |
128 this.textContent = data.parentId ? data.title : | |
129 localStrings.getString('bookmarksPage'); | |
130 | |
131 this.addEventListener('click', this.handleClick_); | |
Sheridan Rawlins
2011/08/16 05:50:28
Will this work, or does it need to be "bound"? It
arv (Not doing code reviews)
2011/08/16 19:36:56
It will work since addEventListener is called on t
| |
132 }, | |
133 | |
134 /** | |
135 * Invoked when a bookmark title is clicked | |
136 * @param {Event} e The click event. | |
137 * @private | |
138 */ | |
139 handleClick_: function(e) { | |
140 chrome.send('getBookmarksData', [this.folderId_]); | |
141 }, | |
142 }; | |
143 | |
8 var TilePage = ntp4.TilePage; | 144 var TilePage = ntp4.TilePage; |
9 | 145 |
10 var bookmarksPageGridValues = { | 146 var bookmarksPageGridValues = { |
11 // The fewest tiles we will show in a row. | 147 // The fewest tiles we will show in a row. |
12 minColCount: 3, | 148 minColCount: 3, |
13 // The most tiles we will show in a row. | 149 // The most tiles we will show in a row. |
14 maxColCount: 6, | 150 maxColCount: 6, |
15 | 151 |
16 // The smallest a tile can be. | 152 // The smallest a tile can be. |
17 minTileWidth: 100, | 153 minTileWidth: 150, |
18 // The biggest a tile can be. | 154 // The biggest a tile can be. |
19 maxTileWidth: 100, | 155 maxTileWidth: 150, |
20 }; | 156 }; |
21 TilePage.initGridValues(bookmarksPageGridValues); | 157 TilePage.initGridValues(bookmarksPageGridValues); |
22 | 158 |
23 /** | 159 /** |
24 * Creates a new BookmarksPage object. | 160 * Creates a new BookmarksPage object. |
25 * @constructor | 161 * @constructor |
26 * @extends {TilePage} | 162 * @extends {TilePage} |
27 */ | 163 */ |
28 function BookmarksPage() { | 164 function BookmarksPage() { |
29 var el = new TilePage(bookmarksPageGridValues); | 165 var el = new TilePage(bookmarksPageGridValues); |
30 el.__proto__ = BookmarksPage.prototype; | 166 el.__proto__ = BookmarksPage.prototype; |
31 el.initialize(); | 167 el.initialize(); |
32 | 168 |
33 return el; | 169 return el; |
34 } | 170 } |
35 | 171 |
36 BookmarksPage.prototype = { | 172 BookmarksPage.prototype = { |
37 __proto__: TilePage.prototype, | 173 __proto__: TilePage.prototype, |
38 | 174 |
39 initialize: function() { | 175 initialize: function() { |
40 this.classList.add('bookmarks-page'); | 176 this.classList.add('bookmarks-page'); |
177 this.insertBefore($('bookmarks-title-wrapper'), this.firstChild); | |
178 }, | |
41 | 179 |
42 // TODO(csilv): Remove this placeholder. | 180 /** |
43 var placeholder = this.ownerDocument.createElement('div'); | 181 * Build the bookmark titles bar (ie, navigation hiearchy). |
44 placeholder.textContent = 'Bookmarks coming soon...'; | 182 * @param {Array} items The parent hiearchy of the current folder. |
45 placeholder.className = 'placeholder'; | 183 * @private |
46 this.insertBefore(placeholder, this.firstChild); | 184 */ |
185 updateBookmarkTitles_: function(items) { | |
186 var wrapper = $('bookmarks-title-wrapper'); | |
187 var title = wrapper.querySelector('.section-title'); | |
188 title.innerHTML = ''; | |
189 | |
190 for (var i = items.length - 1; i > 0; i--) { | |
Sheridan Rawlins
2011/08/16 05:50:28
nit: --i
Evan Stade
2011/08/16 06:05:38
why? I don't see any mention of post vs pre increm
Sheridan Rawlins
2011/08/16 06:39:13
I would have thought that getting in the habit of
arv (Not doing code reviews)
2011/08/16 19:36:56
All the Google JS uses post inc/dec for consistenc
| |
191 title.appendChild(new BookmarkTitle(items[i])); | |
192 | |
193 var separator = document.createElement('hr'); | |
194 separator.className = 'bookmark-separator'; | |
195 title.appendChild(separator); | |
196 } | |
197 | |
198 var titleCrumb = new BookmarkTitle(items[0]); | |
199 titleCrumb.classList.add('title-crumb-active'); | |
200 title.appendChild(titleCrumb); | |
201 }, | |
202 | |
203 /** | |
204 * Build the bookmark tiles. | |
205 * @param {Array} items The contents of the current folder. | |
206 * @private | |
207 */ | |
208 updateBookmarkTiles_: function(items) { | |
209 this.removeAllTiles(); | |
210 for (var i = 0; i < items.length; i++) | |
Sheridan Rawlins
2011/08/16 05:50:28
nit: ++i
Sheridan Rawlins
2011/08/16 06:39:13
Please ignore
| |
211 this.appendTile(new Bookmark(items[i]), false); | |
47 }, | 212 }, |
48 | 213 |
49 /** @inheritDoc */ | 214 /** @inheritDoc */ |
50 shouldAcceptDrag: function(dataTransfer) { | 215 shouldAcceptDrag: function(dataTransfer) { |
51 return false; | 216 return false; |
52 } | 217 }, |
218 | |
219 /** | |
220 * Set the bookmark data that should be displayed, replacing any existing | |
221 * data. | |
222 */ | |
223 set data(data) { | |
224 this.updateBookmarkTiles_(data.items); | |
225 this.updateBookmarkTitles_(data.navigationItems); | |
226 }, | |
227 }; | |
228 | |
229 /** | |
230 * Initializes and renders the bookmark chevron canvas. This needs to be | |
231 * performed after the page has been loaded so that we have access to the | |
232 * style sheet values. | |
233 */ | |
234 function initBookmarkChevron() { | |
235 var wrapperStyle = window.getComputedStyle($('bookmarks-title-wrapper')); | |
236 var width = 10; | |
237 var height = parseInt(wrapperStyle.height, 10); | |
238 var ctx = document.getCSSCanvasContext('2d', 'bookmark-chevron', | |
239 width, height); | |
240 ctx.strokeStyle = wrapperStyle.borderBottomColor; | |
241 ctx.beginPath(); | |
242 ctx.moveTo(0, 0); | |
243 ctx.lineTo(width, height / 2); | |
244 ctx.lineTo(0, height); | |
245 ctx.stroke(); | |
246 }; | |
247 | |
248 /** | |
249 * Set the dominant color for a bookmark tile. This is the callback method | |
250 * from a request made when the tile was created. | |
251 * @param {number} id The numeric ID of the bookmark tile. | |
252 * @param {string} color The color represented as a CSS string. | |
253 */ | |
254 function setBookmarksFaviconDominantColor(id, color) { | |
255 var tile = $('bookmark_tile_' + id); | |
256 if (tile) | |
257 tile.stripeColor = color; | |
53 }; | 258 }; |
54 | 259 |
55 return { | 260 return { |
56 BookmarksPage: BookmarksPage | 261 BookmarksPage: BookmarksPage, |
262 initBookmarkChevron: initBookmarkChevron, | |
263 setBookmarksFaviconDominantColor: setBookmarksFaviconDominantColor | |
57 }; | 264 }; |
58 }); | 265 }); |
266 | |
267 window.addEventListener('load', ntp4.initBookmarkChevron); | |
OLD | NEW |