OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 | 5 |
6 /** | 6 /** |
7 * @fileoverview Utilities for rendering most visited thumbnails and titles. | 7 * @fileoverview Utilities for rendering most visited thumbnails and titles. |
8 */ | 8 */ |
9 | 9 |
10 <include src="instant_iframe_validation.js"> | 10 <include src="instant_iframe_validation.js"> |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 ntpApiHandle.navigateContentWindow(href, getDispositionFromEvent(e)); | 155 ntpApiHandle.navigateContentWindow(href, getDispositionFromEvent(e)); |
156 } | 156 } |
157 // Else follow <a> normally, so transition type would be LINK. | 157 // Else follow <a> normally, so transition type would be LINK. |
158 }); | 158 }); |
159 | 159 |
160 return link; | 160 return link; |
161 } | 161 } |
162 | 162 |
163 | 163 |
164 /** | 164 /** |
| 165 * Returns the color to display string with, depending on whether title is |
| 166 * displayed, the current theme, and URL parameters. |
| 167 * @param {Object.<string, string>} params URL parameters specifying style. |
| 168 * @param {boolean} isTitle if the style is for the Most Visited Title. |
| 169 * @return {string} The color to use, in "rgba(#,#,#,#)" format. |
| 170 */ |
| 171 function getTextColor(params, isTitle) { |
| 172 // 'RRGGBBAA' color format overrides everything. |
| 173 if ('c' in params && params.c.match(/^[0-9A-Fa-f]{8}$/)) { |
| 174 // Extract the 4 pairs of hex digits, map to number, then form rgba(). |
| 175 var t = params.c.match(/(..)(..)(..)(..)/).slice(1).map(function(s) { |
| 176 return parseInt(s, 16); |
| 177 }); |
| 178 return 'rgba(' + t[0] + ',' + t[1] + ',' + t[2] + ',' + t[3] / 255 + ')'; |
| 179 } |
| 180 |
| 181 // For backward compatibility with server-side NTP, look at themes directly |
| 182 // and use param.c for non-title or as fallback. |
| 183 var apiHandle = chrome.embeddedSearch.newTabPage; |
| 184 var themeInfo = apiHandle.themeBackgroundInfo; |
| 185 var c = '#777'; |
| 186 if (isTitle && themeInfo && !themeInfo.usingDefaultTheme) { |
| 187 // read from theme directly |
| 188 c = convertArrayToRGBAColor(themeInfo.textColorRgba) || c; |
| 189 } else if ('c' in params) { |
| 190 c = convertToHexColor(parseInt(params.c, 16)) || c; |
| 191 } |
| 192 return c; |
| 193 } |
| 194 |
| 195 |
| 196 /** |
165 * Decodes most visited styles from URL parameters. | 197 * Decodes most visited styles from URL parameters. |
166 * - c: A hexadecimal number interpreted as a hex color code. | 198 * - c: A hexadecimal number interpreted as a hex color code. |
167 * - f: font-family. | 199 * - f: font-family. |
168 * - fs: font-size as a number in pixels. | 200 * - fs: font-size as a number in pixels. |
169 * - ta: text-align property, as a string. | 201 * - ta: text-align property, as a string. |
170 * - tf: specifying a text fade starting position, in pixels. | 202 * - tf: specifying a text fade starting position, in pixels. |
171 * @param {Object.<string, string>} params URL parameters specifying style. | 203 * @param {Object.<string, string>} params URL parameters specifying style. |
172 * @param {boolean} isTitle if the style is for the Most Visited Title. | 204 * @param {boolean} isTitle if the style is for the Most Visited Title. |
173 * @return {Object} Styles suitable for CSS interpolation. | 205 * @return {Object} Styles suitable for CSS interpolation. |
174 */ | 206 */ |
175 function getMostVisitedStyles(params, isTitle) { | 207 function getMostVisitedStyles(params, isTitle) { |
176 var styles = { | 208 var styles = { |
177 color: '#777', | 209 color: getTextColor(params, isTitle), // Handles 'c' in params. |
178 fontFamily: '', | 210 fontFamily: '', |
179 fontSize: 11 | 211 fontSize: 11 |
180 }; | 212 }; |
181 var apiHandle = chrome.embeddedSearch.newTabPage; | |
182 var themeInfo = apiHandle.themeBackgroundInfo; | |
183 if (isTitle && themeInfo && !themeInfo.usingDefaultTheme) { | |
184 styles.color = convertArrayToRGBAColor(themeInfo.textColorRgba) || | |
185 styles.color; | |
186 } else if ('c' in params) { | |
187 styles.color = convertToHexColor(parseInt(params.c, 16)) || styles.color; | |
188 } | |
189 if ('f' in params && /^[-0-9a-zA-Z ,]+$/.test(params.f)) | 213 if ('f' in params && /^[-0-9a-zA-Z ,]+$/.test(params.f)) |
190 styles.fontFamily = params.f; | 214 styles.fontFamily = params.f; |
191 if ('fs' in params && isFinite(parseInt(params.fs, 10))) | 215 if ('fs' in params && isFinite(parseInt(params.fs, 10))) |
192 styles.fontSize = parseInt(params.fs, 10); | 216 styles.fontSize = parseInt(params.fs, 10); |
193 if ('ta' in params && /^[-0-9a-zA-Z ,]+$/.test(params.ta)) | 217 if ('ta' in params && /^[-0-9a-zA-Z ,]+$/.test(params.ta)) |
194 styles.textAlign = params.ta; | 218 styles.textAlign = params.ta; |
195 if ('tf' in params) { | 219 if ('tf' in params) { |
196 var tf = parseInt(params.tf, 10); | 220 var tf = parseInt(params.tf, 10); |
197 if (isFinite(tf)) | 221 if (isFinite(tf)) |
198 styles.textFadePos = tf; | 222 styles.textFadePos = tf; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 if (navigator.sendBeacon) { | 278 if (navigator.sendBeacon) { |
255 navigator.sendBeacon(url); | 279 navigator.sendBeacon(url); |
256 } else { | 280 } else { |
257 // if sendBeacon is not enabled, we fallback for "a ping". | 281 // if sendBeacon is not enabled, we fallback for "a ping". |
258 var a = document.createElement('a'); | 282 var a = document.createElement('a'); |
259 a.href = '#'; | 283 a.href = '#'; |
260 a.ping = url; | 284 a.ping = url; |
261 a.click(); | 285 a.click(); |
262 } | 286 } |
263 } | 287 } |
OLD | NEW |