OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @fileoverview Orchestrates loading of suggestion content in several | |
7 * chrome-search://suggestion iframes. | |
8 */ | |
9 | |
10 (function() { | |
11 /** | |
12 * The origin of the embedding page. | |
13 * This string literal must be in double quotes for proper escaping. | |
14 * @type {string} | |
15 * @const | |
16 */ | |
17 var EMBEDDER_ORIGIN = "%s"; | |
18 | |
19 /** | |
20 * Converts an RGB color number to a hex color string if valid. | |
21 * @param {number} color A 6-digit hex RGB color code as a number. | |
22 * @return {?string} A CSS representation of the color or null if invalid. | |
23 */ | |
24 function convertColor(color) { | |
25 if (typeof(color) == 'number' && !isNaN(color) && | |
26 (color % 1) == 0 && color >= 0 && color <= 0xffffff) { | |
27 var hexColor = color.toString(16); | |
28 return '#000000'.substr(0, 7 - hexColor.length) + hexColor; | |
29 } | |
30 return null; | |
31 } | |
32 | |
33 /** | |
34 * Checks and returns suggestion style. | |
35 * @param {!Object} pageStyle Instant page-specified overrides for suggestion | |
36 * styles. | |
37 * @return {!Object} Checked styles or defaults. | |
38 */ | |
39 function getStyle(pageStyle) { | |
40 var apiHandle = chrome.embeddedSearch.searchBox; | |
41 var style = { | |
42 queryColor: '#000000', | |
43 urlColor: '#009933', | |
44 titleColor: '#666666', | |
45 font: apiHandle.font, | |
46 fontSize: apiHandle.fontSize | |
47 }; | |
48 if ('queryColor' in pageStyle) | |
49 style.queryColor = convertColor(pageStyle.queryColor) || style.queryColor; | |
50 if ('urlColor' in pageStyle) | |
51 style.urlColor = convertColor(pageStyle.urlColor) || style.urlColor; | |
52 if ('titleColor' in pageStyle) | |
53 style.titleColor = convertColor(pageStyle.titleColor) || style.titleColor; | |
54 return style; | |
55 } | |
56 | |
57 /** | |
58 * Renders a native history suggestion. | |
59 * @param {!Document} resultDoc The suggestion template document. | |
60 * @param {!Object} suggestion The NativeSuggestion to render. | |
61 * @param {!Object} pageStyle Page-specificed styles. | |
62 */ | |
63 function updateResult(resultDoc, suggestion, pageStyle) { | |
64 var style = getStyle(pageStyle || {}); | |
65 resultDoc.body.style.fontSize = style.fontSize + 'px'; | |
66 resultDoc.body.style.fontFamily = style.font; | |
67 var contentsNode = resultDoc.querySelector('#contents'); | |
68 contentsNode.textContent = suggestion.contents; | |
69 contentsNode.style.color = suggestion.is_search ? | |
70 style.queryColor : style.urlColor; | |
71 var optionalNode = resultDoc.querySelector('#optional'); | |
Dan Beam
2013/04/06 00:18:13
optionalNode.hidden = !suggestion.description;
Jered
2013/04/06 02:45:16
Done.
| |
72 if (suggestion.description) { | |
73 var titleNode = resultDoc.querySelector('#title'); | |
74 titleNode.textContent = suggestion.description; | |
75 optionalNode.style.color = style.titleColor; | |
76 optionalNode.hidden = true; | |
77 } else { | |
78 optionalNode.hidden = false; | |
79 } | |
80 } | |
81 | |
82 /** | |
83 * Handles a postMessage from the embedding page requesting to populate history | |
84 * suggestion iframes. | |
85 * @param {!Object} message The message. | |
86 */ | |
87 function handleMessage(message) { | |
88 // Only allow messages from the embedding page, which should be an Instant | |
89 // search provider or the local omnibox dropdown (and not e.g. a site which | |
90 // it has iframed.) | |
91 if (message.origin != EMBEDDER_ORIGIN) | |
92 return; | |
93 | |
94 var apiHandle = chrome.embeddedSearch.searchBox; | |
95 if ('load' in message.data) { | |
96 var loaded = []; | |
97 for (var id in message.data.load) { | |
98 var restrictedId = message.data.load[id]; | |
99 var suggestion = apiHandle.getSuggestionData(restrictedId); | |
100 var iframe = window.parent.frames[id]; | |
101 if (iframe) { | |
102 updateResult(iframe.document, suggestion, message.data.style || {}); | |
103 loaded.push(id); | |
104 } | |
105 } | |
106 message.source.postMessage( | |
107 {'loaded': message.data.requestId}, message.origin); | |
108 } | |
109 } | |
110 | |
111 window.addEventListener('message', handleMessage, false); | |
112 })(); | |
OLD | NEW |