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 var tileID = 0; | |
Evan Stade
2011/08/12 21:13:30
comment this
arv (Not doing code reviews)
2011/08/13 01:02:07
tileId for consistency
csilv
2011/08/13 01:09:21
Done.
csilv
2011/08/13 01:48:17
Done.
| |
10 | |
11 /** | |
12 * Creates a new bookmark object. | |
13 * @param {Object} data The url and title. | |
14 * @constructor | |
15 * @extends {HTMLDivElement} | |
16 */ | |
17 function Bookmark(data) { | |
18 var el = cr.doc.createElement('div'); | |
19 el.__proto__ = Bookmark.prototype; | |
20 el.data = data; | |
21 el.initialize(); | |
22 | |
23 return el; | |
24 } | |
25 | |
26 Bookmark.prototype = { | |
27 __proto__: HTMLDivElement.prototype, | |
28 | |
29 initialize: function() { | |
30 this.className = 'bookmark'; | |
31 this.innerHTML = | |
32 '<a class="button bookmark-fills-parent">' + | |
Evan Stade
2011/08/12 21:13:30
perhaps you could put this html in new_tab.html as
csilv
2011/08/13 01:09:21
Oh, that's a great idea! Done. Also did the same
| |
33 '<div class="close-button"></div>' + | |
34 '<span class="button-frame bookmark-fills-parent"></span>' + | |
35 '<span class="favicon bookmark-fills-parent"></span>' + | |
36 '</a>' + | |
37 '<div class="color-stripe"></div>' + | |
38 '<a class="title"></a>'; | |
39 | |
40 var id = tileID++; | |
41 this.setAttribute('id', 'tile' + id); | |
Evan Stade
2011/08/12 21:13:30
I think you can just do this.id =
(again, regardl
csilv
2011/08/13 01:09:21
Done. I also made updates so that the dominant co
| |
42 | |
43 var title = this.querySelector('.title'); | |
44 title.textContent = this.data.title; | |
45 | |
46 if (this.data.url) { | |
47 var button = this.querySelector('.button'); | |
48 button.href = title.href = this.data.url; | |
49 } | |
50 | |
51 var faviconDiv = this.querySelector('.favicon'); | |
52 var faviconUrl; | |
53 if (this.data.url) { | |
54 faviconUrl = 'chrome://favicon/size/32/' + this.data.url; | |
55 if (this.data.faviconDominantColor) | |
56 this.setStripeColor(this.data.faviconDominantColor); | |
Evan Stade
2011/08/12 21:13:30
add TODOs for the parts of the code that don't act
arv (Not doing code reviews)
2011/08/13 01:02:07
Use a setter whenever you see a setFoo.
csilv
2011/08/13 01:09:21
Done. This is now implemented, all other missing
csilv
2011/08/13 01:48:17
Done.
| |
57 else | |
58 chrome.send('getFaviconDominantColor', [faviconUrl, id]); | |
59 } else { | |
60 // TODO(csilv): We need a large (32px) icon for this URL. | |
61 faviconUrl = 'chrome://theme/IDR_BOOKMARK_BAR_FOLDER'; | |
62 this.setStripeColor('#919191'); | |
arv (Not doing code reviews)
2011/08/13 01:02:07
The icon for the bookmark bar folder is different
csilv
2011/08/13 01:48:17
Perhaps, right now it's just using a neutral color
| |
63 } | |
64 faviconDiv.style.backgroundImage = url(faviconUrl); | |
65 | |
66 this.addEventListener('click', this.handleClick_.bind(this)); | |
67 }, | |
68 | |
69 /** | |
70 * Sets the color of the favicon dominant color bar. | |
71 * @param {string} color The css-parsable value for the color. | |
72 */ | |
73 setStripeColor: function(color) { | |
74 this.querySelector('.color-stripe').style.backgroundColor = color; | |
75 }, | |
76 | |
77 /** | |
78 * Set the size and position of the bookmark tile. | |
79 * @param {number} size The total size of |this|. | |
80 * @param {number} x The x-position. | |
81 * @param {number} y The y-position. | |
82 * animate. | |
83 */ | |
84 setBounds: function(size, x, y) { | |
85 this.style.width = this.style.height = size + 'px'; | |
86 this.style.left = x + 'px'; | |
87 this.style.top = y + 'px'; | |
88 }, | |
89 | |
90 /** | |
91 * Invoked when a bookmark is clicked | |
92 * @param {Event} e The click event. | |
93 * @private | |
94 */ | |
95 handleClick_: function(e) { | |
96 if (e.target.classList.contains('close-button')) { | |
97 this.handleDelete_(); | |
98 e.preventDefault(); | |
99 } else if (!this.data.url) { | |
100 chrome.send('getBookmarksData', [ this.data.id ]); | |
arv (Not doing code reviews)
2011/08/13 01:02:07
no ws after [ nor before ]
csilv
2011/08/13 01:48:17
Done.
| |
101 e.preventDefault(); | |
102 } | |
103 }, | |
104 | |
105 /** | |
106 * Delete a bookmark from the data model. | |
107 * @private | |
108 */ | |
109 handleDelete_: function() { | |
110 // TODO(csilv): add support for deleting bookmarks | |
111 }, | |
112 | |
113 /** | |
114 * The data and preferences for this app. | |
115 * @type {Object} | |
116 */ | |
117 set data(data) { | |
118 this.data_ = data; | |
arv (Not doing code reviews)
2011/08/13 01:02:07
No need for a getter or setter her since there are
csilv
2011/08/13 01:48:17
Done.
| |
119 }, | |
120 get data() { | |
121 return this.data_; | |
122 }, | |
123 }; | |
124 | |
125 /** | |
126 * Creates a new bookmark title object. | |
127 * @param {Object} data The url and title. | |
128 * @constructor | |
129 * @extends {HTMLDivElement} | |
130 */ | |
131 function BookmarkTitle(data) { | |
132 var el = cr.doc.createElement('div'); | |
133 el.__proto__ = BookmarkTitle.prototype; | |
134 el.initialize(data); | |
135 | |
136 return el; | |
137 } | |
138 | |
139 BookmarkTitle.prototype = { | |
140 __proto__: HTMLDivElement.prototype, | |
141 | |
142 initialize: function(data) { | |
143 this.className = 'title-crumb'; | |
144 this.folderId_ = data.id; | |
145 | |
146 if (!data.parentId) | |
147 this.innerText = localStrings.getString('bookmarksPage'); | |
Evan Stade
2011/08/12 21:13:30
ternary operator
arv (Not doing code reviews)
2011/08/13 01:02:07
Use textContent instead of innerText
csilv
2011/08/13 01:09:21
Done.
csilv
2011/08/13 01:48:17
Done.
| |
148 else | |
149 this.innerText = data.title; | |
150 | |
151 this.addEventListener('click', this.handleClick_.bind(this)); | |
arv (Not doing code reviews)
2011/08/13 01:02:07
no need for this bind since you are adding the eve
csilv
2011/08/13 01:48:17
Done.
| |
152 }, | |
153 | |
154 /** | |
155 * Invoked when a bookmark title is clicked | |
156 * @param {Event} e The click event. | |
157 * @private | |
158 */ | |
159 handleClick_: function(e) { | |
160 chrome.send('getBookmarksData', [ this.folderId_ ]); | |
arv (Not doing code reviews)
2011/08/13 01:02:07
ws
csilv
2011/08/13 01:48:17
Done.
| |
161 }, | |
162 }; | |
163 | |
8 var TilePage = ntp4.TilePage; | 164 var TilePage = ntp4.TilePage; |
9 | 165 |
10 var bookmarksPageGridValues = { | 166 var bookmarksPageGridValues = { |
11 // The fewest tiles we will show in a row. | 167 // The fewest tiles we will show in a row. |
12 minColCount: 3, | 168 minColCount: 3, |
13 // The most tiles we will show in a row. | 169 // The most tiles we will show in a row. |
14 maxColCount: 6, | 170 maxColCount: 6, |
15 | 171 |
16 // The smallest a tile can be. | 172 // The smallest a tile can be. |
17 minTileWidth: 100, | 173 minTileWidth: 150, |
18 // The biggest a tile can be. | 174 // The biggest a tile can be. |
19 maxTileWidth: 100, | 175 maxTileWidth: 150, |
20 }; | 176 }; |
21 TilePage.initGridValues(bookmarksPageGridValues); | 177 TilePage.initGridValues(bookmarksPageGridValues); |
22 | 178 |
23 /** | 179 /** |
24 * Creates a new BookmarksPage object. | 180 * Creates a new BookmarksPage object. |
25 * @constructor | 181 * @constructor |
26 * @extends {TilePage} | 182 * @extends {TilePage} |
27 */ | 183 */ |
28 function BookmarksPage() { | 184 function BookmarksPage() { |
29 var el = new TilePage(bookmarksPageGridValues); | 185 var el = new TilePage(bookmarksPageGridValues); |
30 el.__proto__ = BookmarksPage.prototype; | 186 el.__proto__ = BookmarksPage.prototype; |
31 el.initialize(); | 187 el.initialize(); |
32 | 188 |
33 return el; | 189 return el; |
34 } | 190 } |
35 | 191 |
36 BookmarksPage.prototype = { | 192 BookmarksPage.prototype = { |
37 __proto__: TilePage.prototype, | 193 __proto__: TilePage.prototype, |
38 | 194 |
39 initialize: function() { | 195 initialize: function() { |
40 this.classList.add('bookmarks-page'); | 196 this.classList.add('bookmarks-page'); |
41 | 197 |
42 // TODO(csilv): Remove this placeholder. | 198 var titleWrapper = this.ownerDocument.createElement('div'); |
43 var placeholder = this.ownerDocument.createElement('div'); | 199 titleWrapper.className = 'section-title-wrapper'; |
44 placeholder.textContent = 'Bookmarks coming soon...'; | 200 titleWrapper.setAttribute('id', 'bookmarks_title_wrapper'); |
arv (Not doing code reviews)
2011/08/13 01:02:07
Prefer DOM bindings over setAttribute
titleWrappe
csilv
2011/08/13 01:48:17
Done.
| |
45 placeholder.className = 'placeholder'; | 201 this.insertBefore(titleWrapper, this.firstChild); |
46 this.insertBefore(placeholder, this.firstChild); | 202 |
203 var titleMask = this.ownerDocument.createElement('div'); | |
204 titleMask.className = 'section-title-mask'; | |
205 titleWrapper.appendChild(titleMask); | |
206 | |
207 var title = this.ownerDocument.createElement('div'); | |
208 title.className = 'section-title'; | |
209 titleWrapper.appendChild(title); | |
210 }, | |
211 | |
212 /** | |
213 * Build the bookmark titles bar (ie, navigation hiearchy). | |
214 * @param {Array} items The parent hiearchy of the current folder. | |
215 * @private | |
216 */ | |
217 updateBookmarkTitles_: function(items) { | |
218 var wrapper = $('bookmarks_title_wrapper'); | |
219 var title = wrapper.getElementsByClassName('section-title')[0]; | |
arv (Not doing code reviews)
2011/08/13 01:02:07
querySelector('.section-title')
csilv
2011/08/13 01:48:17
Done.
| |
220 title.innerHTML = ''; | |
221 | |
222 for (var i = items.length - 1; i > 0; i--) { | |
223 var titleCrumb = new BookmarkTitle(items[i]); | |
224 title.appendChild(titleCrumb); | |
225 | |
226 var separator = document.createElement('div'); | |
arv (Not doing code reviews)
2011/08/13 01:02:07
maybe use an hr here?
csilv
2011/08/13 01:48:17
Done.
| |
227 separator.className = 'bookmark-separator'; | |
228 title.appendChild(separator); | |
229 } | |
230 | |
231 var titleCrumb = new BookmarkTitle(items[0]); | |
232 titleCrumb.classList.add('title-crumb-active'); | |
233 title.appendChild(titleCrumb); | |
234 }, | |
235 | |
236 /** | |
237 * Build the bookmark tiles. | |
238 * @param {Array} items The contents of the current folder. | |
239 * @private | |
240 */ | |
241 updateBookmarkTiles_: function(items) { | |
242 this.removeAllTiles(); | |
243 for (var i = 0; i < items.length; i++) | |
244 this.appendTile(new Bookmark(items[i]), false); | |
47 }, | 245 }, |
48 | 246 |
49 /** @inheritDoc */ | 247 /** @inheritDoc */ |
50 shouldAcceptDrag: function(dataTransfer) { | 248 shouldAcceptDrag: function(dataTransfer) { |
51 return false; | 249 return false; |
52 } | 250 }, |
251 | |
252 /** | |
253 * Set the bookmark data that should be displayed, replacing any existing | |
254 * data. | |
255 */ | |
256 set data(data) { | |
257 this.updateBookmarkTiles_(data.items); | |
258 this.updateBookmarkTitles_(data.navigationItems); | |
259 }, | |
53 }; | 260 }; |
54 | 261 |
262 /** | |
263 * Initializes and renders the bookmark chevron canvas. This needs to be | |
264 * performed after the page has been loaded so that we have access to the | |
265 * style sheet values. | |
266 */ | |
267 function initBookmarkChevron() { | |
268 var wrapperStyle = window.getComputedStyle($('bookmarks_title_wrapper')); | |
269 var width = 10; | |
270 var height = parseInt(wrapperStyle.height); | |
arv (Not doing code reviews)
2011/08/13 01:02:07
always use two args to parseInt due to its strange
csilv
2011/08/13 01:48:17
Done.
| |
271 var ctx = document.getCSSCanvasContext('2d', 'bookmark-chevron', | |
272 width, height); | |
273 ctx.strokeStyle = wrapperStyle.borderBottomColor; | |
274 ctx.beginPath(); | |
275 ctx.moveTo(0, 0); | |
276 ctx.lineTo(width, height / 2); | |
277 ctx.lineTo(0, height); | |
278 ctx.stroke(); | |
279 } | |
280 | |
55 return { | 281 return { |
56 BookmarksPage: BookmarksPage | 282 BookmarksPage: BookmarksPage, |
283 initBookmarkChevron: initBookmarkChevron | |
57 }; | 284 }; |
58 }); | 285 }); |
286 | |
287 window.addEventListener('load', ntp4.initBookmarkChevron); | |
288 | |
OLD | NEW |