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. |
| 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() { |
| 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. |
| 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) { |
| 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_); |
| 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'); |
41 | 177 |
42 // TODO(csilv): Remove this placeholder. | 178 var titleWrapper = $('bookmarks-title-wrapper-template').cloneNode(true); |
43 var placeholder = this.ownerDocument.createElement('div'); | 179 titleWrapper.id = 'bookmarks_title_wrapper'; |
44 placeholder.textContent = 'Bookmarks coming soon...'; | 180 this.insertBefore(titleWrapper, this.firstChild); |
45 placeholder.className = 'placeholder'; | 181 }, |
46 this.insertBefore(placeholder, this.firstChild); | 182 |
| 183 /** |
| 184 * Build the bookmark titles bar (ie, navigation hiearchy). |
| 185 * @param {Array} items The parent hiearchy of the current folder. |
| 186 * @private |
| 187 */ |
| 188 updateBookmarkTitles_: function(items) { |
| 189 var wrapper = $('bookmarks_title_wrapper'); |
| 190 var title = wrapper.querySelector('.section-title') |
| 191 title.innerHTML = ''; |
| 192 |
| 193 for (var i = items.length - 1; i > 0; i--) { |
| 194 var titleCrumb = new BookmarkTitle(items[i]); |
| 195 title.appendChild(titleCrumb); |
| 196 |
| 197 var separator = document.createElement('hr'); |
| 198 separator.className = 'bookmark-separator'; |
| 199 title.appendChild(separator); |
| 200 } |
| 201 |
| 202 var titleCrumb = new BookmarkTitle(items[0]); |
| 203 titleCrumb.classList.add('title-crumb-active'); |
| 204 title.appendChild(titleCrumb); |
| 205 }, |
| 206 |
| 207 /** |
| 208 * Build the bookmark tiles. |
| 209 * @param {Array} items The contents of the current folder. |
| 210 * @private |
| 211 */ |
| 212 updateBookmarkTiles_: function(items) { |
| 213 this.removeAllTiles(); |
| 214 for (var i = 0; i < items.length; i++) |
| 215 this.appendTile(new Bookmark(items[i]), false); |
47 }, | 216 }, |
48 | 217 |
49 /** @inheritDoc */ | 218 /** @inheritDoc */ |
50 shouldAcceptDrag: function(dataTransfer) { | 219 shouldAcceptDrag: function(dataTransfer) { |
51 return false; | 220 return false; |
52 } | 221 }, |
| 222 |
| 223 /** |
| 224 * Set the bookmark data that should be displayed, replacing any existing |
| 225 * data. |
| 226 */ |
| 227 set data(data) { |
| 228 this.updateBookmarkTiles_(data.items); |
| 229 this.updateBookmarkTitles_(data.navigationItems); |
| 230 }, |
| 231 }; |
| 232 |
| 233 /** |
| 234 * Initializes and renders the bookmark chevron canvas. This needs to be |
| 235 * performed after the page has been loaded so that we have access to the |
| 236 * style sheet values. |
| 237 */ |
| 238 function initBookmarkChevron() { |
| 239 var wrapperStyle = window.getComputedStyle($('bookmarks_title_wrapper')); |
| 240 var width = 10; |
| 241 var height = parseInt(wrapperStyle.height, 10); |
| 242 var ctx = document.getCSSCanvasContext('2d', 'bookmark-chevron', |
| 243 width, height); |
| 244 ctx.strokeStyle = wrapperStyle.borderBottomColor; |
| 245 ctx.beginPath(); |
| 246 ctx.moveTo(0, 0); |
| 247 ctx.lineTo(width, height / 2); |
| 248 ctx.lineTo(0, height); |
| 249 ctx.stroke(); |
| 250 }; |
| 251 |
| 252 /** |
| 253 * Set the dominant color for a bookmark tile. This is the callback method |
| 254 * from a request made when the tile was created. |
| 255 */ |
| 256 function setBookmarksFaviconDominantColor(id, color) { |
| 257 var tile = $('bookmark_tile_' + id); |
| 258 if (tile) |
| 259 tile.stripeColor = color; |
53 }; | 260 }; |
54 | 261 |
55 return { | 262 return { |
56 BookmarksPage: BookmarksPage | 263 BookmarksPage: BookmarksPage, |
| 264 initBookmarkChevron: initBookmarkChevron, |
| 265 setBookmarksFaviconDominantColor: setBookmarksFaviconDominantColor |
57 }; | 266 }; |
58 }); | 267 }); |
| 268 |
| 269 window.addEventListener('load', ntp4.initBookmarkChevron); |
OLD | NEW |