OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <include src="assert.js"> | 5 <include src="assert.js"> |
6 | 6 |
7 /** | 7 /** |
8 * The global object. | |
9 * @type {!Object} | |
10 * @const | |
11 */ | |
12 var global = this; | |
13 | |
14 /** | |
15 * Alias for document.getElementById. | 8 * Alias for document.getElementById. |
16 * @param {string} id The ID of the element to find. | 9 * @param {string} id The ID of the element to find. |
17 * @return {HTMLElement} The found element or null if not found. | 10 * @return {HTMLElement} The found element or null if not found. |
18 */ | 11 */ |
19 function $(id) { | 12 function $(id) { |
20 return document.getElementById(id); | 13 return document.getElementById(id); |
21 } | 14 } |
22 | 15 |
23 /** | 16 /** |
24 * Add an accessible message to the page that will be announced to | 17 * Add an accessible message to the page that will be announced to |
(...skipping 28 matching lines...) Expand all Loading... |
53 global[callbackName] = old; | 46 global[callbackName] = old; |
54 | 47 |
55 var args = Array.prototype.slice.call(arguments); | 48 var args = Array.prototype.slice.call(arguments); |
56 return callback.apply(global, args); | 49 return callback.apply(global, args); |
57 }; | 50 }; |
58 chrome.send(name, params); | 51 chrome.send(name, params); |
59 } | 52 } |
60 | 53 |
61 /** | 54 /** |
62 * Returns the scale factors supported by this platform. | 55 * Returns the scale factors supported by this platform. |
63 * @return {array} The supported scale factors. | 56 * @return {Array} The supported scale factors. |
64 */ | 57 */ |
65 function getSupportedScaleFactors() { | 58 function getSupportedScaleFactors() { |
66 var supportedScaleFactors = []; | 59 var supportedScaleFactors = []; |
67 if (cr.isMac || cr.isChromeOS) { | 60 if (cr.isMac || cr.isChromeOS) { |
68 supportedScaleFactors.push(1); | 61 supportedScaleFactors.push(1); |
69 supportedScaleFactors.push(2); | 62 supportedScaleFactors.push(2); |
70 } else { | 63 } else { |
71 // Windows must be restarted to display at a different scale factor. | 64 // Windows must be restarted to display at a different scale factor. |
72 supportedScaleFactors.push(window.devicePixelRatio); | 65 supportedScaleFactors.push(window.devicePixelRatio); |
73 } | 66 } |
(...skipping 16 matching lines...) Expand all Loading... |
90 // Add a space to work around the WebKit bug. | 83 // Add a space to work around the WebKit bug. |
91 s2 += ' '; | 84 s2 += ' '; |
92 } | 85 } |
93 return 'url("' + s2 + '")'; | 86 return 'url("' + s2 + '")'; |
94 } | 87 } |
95 | 88 |
96 /** | 89 /** |
97 * Returns the URL of the image, or an image set of URLs for the profile avatar. | 90 * Returns the URL of the image, or an image set of URLs for the profile avatar. |
98 * Default avatars have resources available for multiple scalefactors, whereas | 91 * Default avatars have resources available for multiple scalefactors, whereas |
99 * the GAIA profile image only comes in one size. | 92 * the GAIA profile image only comes in one size. |
100 | 93 * |
101 * @param {string} url The path of the image. | 94 * @param {string} path The path of the image. |
102 * @return {string} The url, or an image set of URLs of the avatar image. | 95 * @return {string} The url, or an image set of URLs of the avatar image. |
103 */ | 96 */ |
104 function getProfileAvatarIcon(path) { | 97 function getProfileAvatarIcon(path) { |
105 var chromeThemePath = 'chrome://theme'; | 98 var chromeThemePath = 'chrome://theme'; |
106 var isDefaultAvatar = | 99 var isDefaultAvatar = |
107 (path.slice(0, chromeThemePath.length) == chromeThemePath); | 100 (path.slice(0, chromeThemePath.length) == chromeThemePath); |
108 return isDefaultAvatar ? imageset(path + '@scalefactorx'): url(path); | 101 return isDefaultAvatar ? imageset(path + '@scalefactorx'): url(path); |
109 } | 102 } |
110 | 103 |
111 /** | 104 /** |
(...skipping 21 matching lines...) Expand all Loading... |
133 s += url(pathWithScaleFactor) + ' ' + scaleFactor + 'x'; | 126 s += url(pathWithScaleFactor) + ' ' + scaleFactor + 'x'; |
134 | 127 |
135 if (i != supportedScaleFactors.length - 1) | 128 if (i != supportedScaleFactors.length - 1) |
136 s += ', '; | 129 s += ', '; |
137 } | 130 } |
138 return '-webkit-image-set(' + s + ')'; | 131 return '-webkit-image-set(' + s + ')'; |
139 } | 132 } |
140 | 133 |
141 /** | 134 /** |
142 * Parses query parameters from Location. | 135 * Parses query parameters from Location. |
143 * @param {string} location The URL to generate the CSS url for. | 136 * @param {Location} location The URL to generate the CSS url for. |
144 * @return {object} Dictionary containing name value pairs for URL | 137 * @return {Object} Dictionary containing name value pairs for URL |
145 */ | 138 */ |
146 function parseQueryParams(location) { | 139 function parseQueryParams(location) { |
147 var params = {}; | 140 var params = {}; |
148 var query = unescape(location.search.substring(1)); | 141 var query = unescape(location.search.substring(1)); |
149 var vars = query.split('&'); | 142 var vars = query.split('&'); |
150 for (var i = 0; i < vars.length; i++) { | 143 for (var i = 0; i < vars.length; i++) { |
151 var pair = vars[i].split('='); | 144 var pair = vars[i].split('='); |
152 params[pair[0]] = pair[1]; | 145 params[pair[0]] = pair[1]; |
153 } | 146 } |
154 return params; | 147 return params; |
155 } | 148 } |
156 | 149 |
157 /** | 150 /** |
158 * Creates a new URL by appending or replacing the given query key and value. | 151 * Creates a new URL by appending or replacing the given query key and value. |
159 * Not supporting URL with username and password. | 152 * Not supporting URL with username and password. |
160 * @param {object} location The original URL. | 153 * @param {Location} location The original URL. |
161 * @param {string} key The query parameter name. | 154 * @param {string} key The query parameter name. |
162 * @param {string} value The query parameter value. | 155 * @param {string} value The query parameter value. |
163 * @return {string} The constructed new URL. | 156 * @return {string} The constructed new URL. |
164 */ | 157 */ |
165 function setQueryParam(location, key, value) { | 158 function setQueryParam(location, key, value) { |
166 var query = parseQueryParams(location); | 159 var query = parseQueryParams(location); |
167 query[encodeURIComponent(key)] = encodeURIComponent(value); | 160 query[encodeURIComponent(key)] = encodeURIComponent(value); |
168 | 161 |
169 var newQuery = ''; | 162 var newQuery = ''; |
170 for (var q in query) { | 163 for (var q in query) { |
171 newQuery += (newQuery ? '&' : '?') + q + '=' + query[q]; | 164 newQuery += (newQuery ? '&' : '?') + q + '=' + query[q]; |
172 } | 165 } |
173 | 166 |
174 return location.origin + location.pathname + newQuery + location.hash; | 167 return location.origin + location.pathname + newQuery + location.hash; |
175 } | 168 } |
176 | 169 |
| 170 /** |
| 171 * @param {Node} el An element to search for ancestors with |className|. |
| 172 * @param {string} className A class to search for. |
| 173 * @return {Node} A node with class of |className| or null if none is found. |
| 174 */ |
177 function findAncestorByClass(el, className) { | 175 function findAncestorByClass(el, className) { |
178 return findAncestor(el, function(el) { | 176 return findAncestor(el, function(el) { |
179 if (el.classList) | 177 return el.classList && el.classList.contains(className); |
180 return el.classList.contains(className); | |
181 return null; | |
182 }); | 178 }); |
183 } | 179 } |
184 | 180 |
185 /** | 181 /** |
186 * Return the first ancestor for which the {@code predicate} returns true. | 182 * Return the first ancestor for which the {@code predicate} returns true. |
187 * @param {Node} node The node to check. | 183 * @param {Node} node The node to check. |
188 * @param {function(Node) : boolean} predicate The function that tests the | 184 * @param {function(Node):boolean} predicate The function that tests the |
189 * nodes. | 185 * nodes. |
190 * @return {Node} The found ancestor or null if not found. | 186 * @return {Node} The found ancestor or null if not found. |
191 */ | 187 */ |
192 function findAncestor(node, predicate) { | 188 function findAncestor(node, predicate) { |
193 var last = false; | 189 var last = false; |
194 while (node != null && !(last = predicate(node))) { | 190 while (node != null && !(last = predicate(node))) { |
195 node = node.parentNode; | 191 node = node.parentNode; |
196 } | 192 } |
197 return last ? node : null; | 193 return last ? node : null; |
198 } | 194 } |
(...skipping 30 matching lines...) Expand all Loading... |
229 e.preventDefault(); | 225 e.preventDefault(); |
230 }; | 226 }; |
231 } | 227 } |
232 | 228 |
233 /** | 229 /** |
234 * Call this to stop clicks on <a href="#"> links from scrolling to the top of | 230 * Call this to stop clicks on <a href="#"> links from scrolling to the top of |
235 * the page (and possibly showing a # in the link). | 231 * the page (and possibly showing a # in the link). |
236 */ | 232 */ |
237 function preventDefaultOnPoundLinkClicks() { | 233 function preventDefaultOnPoundLinkClicks() { |
238 document.addEventListener('click', function(e) { | 234 document.addEventListener('click', function(e) { |
239 var anchor = findAncestor(e.target, function(el) { | 235 var anchor = findAncestor(/** @type {Node} */(e.target), function(el) { |
240 return el.tagName == 'A'; | 236 return el.tagName == 'A'; |
241 }); | 237 }); |
242 // Use getAttribute() to prevent URL normalization. | 238 // Use getAttribute() to prevent URL normalization. |
243 if (anchor && anchor.getAttribute('href') == '#') | 239 if (anchor && anchor.getAttribute('href') == '#') |
244 e.preventDefault(); | 240 e.preventDefault(); |
245 }); | 241 }); |
246 } | 242 } |
247 | 243 |
248 /** | 244 /** |
249 * Check the directionality of the page. | 245 * Check the directionality of the page. |
250 * @return {boolean} True if Chrome is running an RTL UI. | 246 * @return {boolean} True if Chrome is running an RTL UI. |
251 */ | 247 */ |
252 function isRTL() { | 248 function isRTL() { |
253 return document.documentElement.dir == 'rtl'; | 249 return document.documentElement.dir == 'rtl'; |
254 } | 250 } |
255 | 251 |
256 /** | 252 /** |
257 * Get an element that's known to exist by its ID. We use this instead of just | 253 * Get an element that's known to exist by its ID. We use this instead of just |
258 * calling getElementById and not checking the result because this lets us | 254 * calling getElementById and not checking the result because this lets us |
259 * satisfy the JSCompiler type system. | 255 * satisfy the JSCompiler type system. |
260 * @param {string} id The identifier name. | 256 * @param {string} id The identifier name. |
261 * @return {!Element} the Element. | 257 * @return {!Element} the Element. |
262 */ | 258 */ |
263 function getRequiredElement(id) { | 259 function getRequiredElement(id) { |
264 var element = $(id); | 260 var element = $(id); |
265 assert(element, 'Missing required element: ' + id); | 261 assert(element, 'Missing required element: ' + id); |
266 return element; | 262 return /** @type {!Element} */(element); |
267 } | 263 } |
268 | 264 |
269 // Handle click on a link. If the link points to a chrome: or file: url, then | 265 // Handle click on a link. If the link points to a chrome: or file: url, then |
270 // call into the browser to do the navigation. | 266 // call into the browser to do the navigation. |
271 document.addEventListener('click', function(e) { | 267 document.addEventListener('click', function(e) { |
272 if (e.defaultPrevented) | 268 if (e.defaultPrevented) |
273 return; | 269 return; |
274 | 270 |
275 var el = e.target; | 271 var el = e.target; |
276 if (el.nodeType == Node.ELEMENT_NODE && | 272 if (el.nodeType == Node.ELEMENT_NODE && |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 * @param {number} maxLength The maximum length allowed for the string. | 432 * @param {number} maxLength The maximum length allowed for the string. |
437 * @return {string} The original string if its length does not exceed | 433 * @return {string} The original string if its length does not exceed |
438 * |maxLength|. Otherwise the first |maxLength| - 1 characters with '...' | 434 * |maxLength|. Otherwise the first |maxLength| - 1 characters with '...' |
439 * appended. | 435 * appended. |
440 */ | 436 */ |
441 function elide(original, maxLength) { | 437 function elide(original, maxLength) { |
442 if (original.length <= maxLength) | 438 if (original.length <= maxLength) |
443 return original; | 439 return original; |
444 return original.substring(0, maxLength - 1) + '\u2026'; | 440 return original.substring(0, maxLength - 1) + '\u2026'; |
445 } | 441 } |
OLD | NEW |