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 /** | 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). |
(...skipping 21 matching lines...) Expand all Loading... |
32 */ | 32 */ |
33 set data(value) { | 33 set data(value) { |
34 expect(!this.data_, 'Re-setting data.'); | 34 expect(!this.data_, 'Re-setting data.'); |
35 this.data_ = value; | 35 this.data_ = value; |
36 }, | 36 }, |
37 | 37 |
38 /** | 38 /** |
39 * Returns a JsEvalContext for |data_|. | 39 * Returns a JsEvalContext for |data_|. |
40 * @returns {JsEvalContext} | 40 * @returns {JsEvalContext} |
41 */ | 41 */ |
42 createJsEvalContext: function() { return new JsEvalContext(this.data_); }, | 42 createJsEvalContext: function() { |
| 43 return new JsEvalContext(this.data_); |
| 44 }, |
43 | 45 |
44 /** | 46 /** |
45 * @param {string} id An ID of a value that might exist. | 47 * @param {string} id An ID of a value that might exist. |
46 * @return {boolean} True if |id| is a key in the dictionary. | 48 * @return {boolean} True if |id| is a key in the dictionary. |
47 */ | 49 */ |
48 valueExists: function(id) { return id in this.data_; }, | 50 valueExists: function(id) { |
| 51 return id in this.data_; |
| 52 }, |
49 | 53 |
50 /** | 54 /** |
51 * Fetches a value, expecting that it exists. | 55 * Fetches a value, expecting that it exists. |
52 * @param {string} id The key that identifies the desired value. | 56 * @param {string} id The key that identifies the desired value. |
53 * @return {*} The corresponding value. | 57 * @return {*} The corresponding value. |
54 */ | 58 */ |
55 getValue: function(id) { | 59 getValue: function(id) { |
56 expect(this.data_, 'No data. Did you remember to include strings.js?'); | 60 expect(this.data_, 'No data. Did you remember to include strings.js?'); |
57 var value = this.data_[id]; | 61 var value = this.data_[id]; |
58 expect(typeof value != 'undefined', 'Could not find value for ' + id); | 62 expect(typeof value != 'undefined', 'Could not find value for ' + id); |
(...skipping 18 matching lines...) Expand all Loading... |
77 * @param {...(string|number)} var_args The extra values to include in the | 81 * @param {...(string|number)} var_args The extra values to include in the |
78 * formatted output. | 82 * formatted output. |
79 * @return {string} The formatted string. | 83 * @return {string} The formatted string. |
80 */ | 84 */ |
81 getStringF: function(id, var_args) { | 85 getStringF: function(id, var_args) { |
82 var value = this.getString(id); | 86 var value = this.getString(id); |
83 if (!value) | 87 if (!value) |
84 return ''; | 88 return ''; |
85 | 89 |
86 var varArgs = arguments; | 90 var varArgs = arguments; |
87 return value.replace( | 91 return value.replace(/\$[$1-9]/g, function(m) { |
88 /\$[$1-9]/g, function(m) { return m == '$$' ? '$' : varArgs[m[1]]; }); | 92 return m == '$$' ? '$' : varArgs[m[1]]; |
| 93 }); |
89 }, | 94 }, |
90 | 95 |
91 /** | 96 /** |
92 * As above, but also makes sure that the value is a boolean. | 97 * As above, but also makes sure that the value is a boolean. |
93 * @param {string} id The key that identifies the desired boolean. | 98 * @param {string} id The key that identifies the desired boolean. |
94 * @return {boolean} The corresponding boolean value. | 99 * @return {boolean} The corresponding boolean value. |
95 */ | 100 */ |
96 getBoolean: function(id) { | 101 getBoolean: function(id) { |
97 var value = this.getValue(id); | 102 var value = this.getValue(id); |
98 expectIsType(id, value, 'boolean'); | 103 expectIsType(id, value, 'boolean'); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 * @param {string} type The type we expect |value| to be. | 149 * @param {string} type The type we expect |value| to be. |
145 */ | 150 */ |
146 function expectIsType(id, value, type) { | 151 function expectIsType(id, value, type) { |
147 expect( | 152 expect( |
148 typeof value == type, '[' + value + '] (' + id + ') is not a ' + type); | 153 typeof value == type, '[' + value + '] (' + id + ') is not a ' + type); |
149 } | 154 } |
150 | 155 |
151 expect(!loadTimeData, 'should only include this file once'); | 156 expect(!loadTimeData, 'should only include this file once'); |
152 loadTimeData = new LoadTimeData; | 157 loadTimeData = new LoadTimeData; |
153 })(); | 158 })(); |
OLD | NEW |