Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: ui/webui/resources/js/load_time_data.js

Issue 369643002: Lay groudwork to Closure compile JavaScript (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: asdf Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 /** 5 /**
6 * @fileoverview This file defines a singleton which provides access to all data 6 * @fileoverview This file defines a singleton which provides access to all data
7 * that is available as soon as the page's resources are loaded (before DOM 7 * that is available as soon as the page's resources are loaded (before DOM
8 * content has finished loading). This data includes both localized strings and 8 * content has finished loading). This data includes both localized strings and
9 * any data that is important to have ready from a very early stage (e.g. things 9 * any data that is important to have ready from a very early stage (e.g. things
10 * that must be displayed right away). 10 * that must be displayed right away).
11 */ 11 */
12 12
13 var loadTimeData; 13 var loadTimeData;
14 14
15 (function() { 15 (function() {
16 'use strict'; 16 'use strict';
17 17
18 /** @constructor */
18 function LoadTimeData() { 19 function LoadTimeData() {
19 } 20 }
20 21
21 LoadTimeData.prototype = { 22 LoadTimeData.prototype = {
22 /** 23 /**
23 * Sets the backing object. 24 * Sets the backing object.
24 * @param {Object} value The de-serialized page data. 25 * @param {Object} value The de-serialized page data.
25 */ 26 */
26 set data(value) { 27 set data(value) {
27 expect(!this.data_, 'Re-setting data.'); 28 expect(!this.data_, 'Re-setting data.');
28 this.data_ = value; 29 this.data_ = value;
29 }, 30 },
30 31
31 /** 32 /**
33 * @param {string} id An ID of a value that might exist.
32 * @return {boolean} True if |id| is a key in the dictionary. 34 * @return {boolean} True if |id| is a key in the dictionary.
33 */ 35 */
34 valueExists: function(id) { 36 valueExists: function(id) {
35 return id in this.data_; 37 return id in this.data_;
36 }, 38 },
37 39
38 /** 40 /**
39 * Fetches a value, expecting that it exists. 41 * Fetches a value, expecting that it exists.
40 * @param {string} id The key that identifies the desired value. 42 * @param {string} id The key that identifies the desired value.
41 * @return {*} The corresponding value. 43 * @return {*} The corresponding value.
42 */ 44 */
43 getValue: function(id) { 45 getValue: function(id) {
44 expect(this.data_, 'No data. Did you remember to include strings.js?'); 46 expect(this.data_, 'No data. Did you remember to include strings.js?');
45 var value = this.data_[id]; 47 var value = this.data_[id];
46 expect(typeof value != 'undefined', 'Could not find value for ' + id); 48 expect(typeof value != 'undefined', 'Could not find value for ' + id);
47 return value; 49 return value;
48 }, 50 },
49 51
50 /** 52 /**
51 * As above, but also makes sure that the value is a string. 53 * As above, but also makes sure that the value is a string.
52 * @param {string} id The key that identifies the desired string. 54 * @param {string} id The key that identifies the desired string.
53 * @return {string} The corresponding string value. 55 * @return {string} The corresponding string value.
54 */ 56 */
55 getString: function(id) { 57 getString: function(id) {
56 var value = this.getValue(id); 58 var value = this.getValue(id);
57 expectIsType(id, value, 'string'); 59 expectIsType(id, value, 'string');
58 return value; 60 return /** @type {string} */ (value);
59 }, 61 },
60 62
61 /** 63 /**
62 * Returns a formatted localized string where $1 to $9 are replaced by the 64 * Returns a formatted localized string where $1 to $9 are replaced by the
63 * second to the tenth argument. 65 * second to the tenth argument.
64 * @param {string} id The ID of the string we want. 66 * @param {string} id The ID of the string we want.
65 * @param {...string} The extra values to include in the formatted output. 67 * @param {...string} var_args The extra values to include in the formatted
68 * output.
66 * @return {string} The formatted string. 69 * @return {string} The formatted string.
67 */ 70 */
68 getStringF: function(id) { 71 getStringF: function(id, var_args) {
69 var value = this.getString(id); 72 var value = this.getString(id);
70 if (!value) 73 if (!value)
71 return; 74 return '';
72 75
73 var varArgs = arguments; 76 var varArgs = arguments;
74 return value.replace(/\$[$1-9]/g, function(m) { 77 return value.replace(/\$[$1-9]/g, function(m) {
75 return m == '$$' ? '$' : varArgs[m[1]]; 78 return m == '$$' ? '$' : varArgs[m[1]];
76 }); 79 });
77 }, 80 },
78 81
79 /** 82 /**
80 * As above, but also makes sure that the value is a boolean. 83 * As above, but also makes sure that the value is a boolean.
81 * @param {string} id The key that identifies the desired boolean. 84 * @param {string} id The key that identifies the desired boolean.
82 * @return {boolean} The corresponding boolean value. 85 * @return {boolean} The corresponding boolean value.
83 */ 86 */
84 getBoolean: function(id) { 87 getBoolean: function(id) {
85 var value = this.getValue(id); 88 var value = this.getValue(id);
86 expectIsType(id, value, 'boolean'); 89 expectIsType(id, value, 'boolean');
87 return value; 90 return /** @type {boolean} */ (value);
88 }, 91 },
89 92
90 /** 93 /**
91 * As above, but also makes sure that the value is an integer. 94 * As above, but also makes sure that the value is an integer.
92 * @param {string} id The key that identifies the desired number. 95 * @param {string} id The key that identifies the desired number.
93 * @return {number} The corresponding number value. 96 * @return {number} The corresponding number value.
94 */ 97 */
95 getInteger: function(id) { 98 getInteger: function(id) {
96 var value = this.getValue(id); 99 var value = this.getValue(id);
97 expectIsType(id, value, 'number'); 100 expectIsType(id, value, 'number');
98 expect(value == Math.floor(value), 'Number isn\'t integer: ' + value); 101 expect(value == Math.floor(value), 'Number isn\'t integer: ' + value);
99 return value; 102 return /** @type {number} */ (value);
100 }, 103 },
101 104
102 /** 105 /**
103 * Override values in loadTimeData with the values found in |replacements|. 106 * Override values in loadTimeData with the values found in |replacements|.
104 * @param {Object} replacements The dictionary object of keys to replace. 107 * @param {Object} replacements The dictionary object of keys to replace.
105 */ 108 */
106 overrideValues: function(replacements) { 109 overrideValues: function(replacements) {
107 expect(typeof replacements == 'object', 110 expect(typeof replacements == 'object',
108 'Replacements must be a dictionary object.'); 111 'Replacements must be a dictionary object.');
109 for (var key in replacements) { 112 for (var key in replacements) {
(...skipping 19 matching lines...) Expand all
129 * @param {string} type The type we expect |value| to be. 132 * @param {string} type The type we expect |value| to be.
130 */ 133 */
131 function expectIsType(id, value, type) { 134 function expectIsType(id, value, type) {
132 expect(typeof value == type, '[' + value + '] (' + id + 135 expect(typeof value == type, '[' + value + '] (' + id +
133 ') is not a ' + type); 136 ') is not a ' + type);
134 } 137 }
135 138
136 expect(!loadTimeData, 'should only include this file once'); 139 expect(!loadTimeData, 'should only include this file once');
137 loadTimeData = new LoadTimeData; 140 loadTimeData = new LoadTimeData;
138 })(); 141 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698