OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 /** @fileoverview Utility functions to help use prefs in Polymer controls. */ | 5 /** @fileoverview Utility functions to help use prefs in Polymer controls. */ |
6 | 6 |
7 // TODO(michaelpg): converge with other WebUI on capitalization. This is | 7 // TODO(michaelpg): converge with other WebUI on capitalization. This is |
8 // consistent with Settings, but WebUI uses lower.underscore_case. | 8 // consistent with Settings, but WebUI uses lower.underscore_case. |
9 cr.define('Settings.PrefUtil', function() { | 9 cr.define('Settings.PrefUtil', function() { |
10 /** | 10 /** |
11 * Converts a string value to a type corresponding to the given preference. | 11 * Converts a string value to a type corresponding to the given preference. |
12 * @param {string} value | 12 * @param {string} value |
13 * @param {!chrome.settingsPrivate.PrefObject} pref | 13 * @param {!chrome.settingsPrivate.PrefObject} pref |
14 * @return {boolean|number|string|undefined} | 14 * @return {boolean|number|string|undefined} |
15 */ | 15 */ |
16 function stringToPrefValue(value, pref) { | 16 function stringToPrefValue(value, pref) { |
17 switch (pref.type) { | 17 switch (pref.type) { |
18 case chrome.settingsPrivate.PrefType.BOOLEAN: | 18 case chrome.settingsPrivate.PrefType.BOOLEAN: |
19 return value == 'true'; | 19 return value == 'true'; |
20 case chrome.settingsPrivate.PrefType.NUMBER: | 20 case chrome.settingsPrivate.PrefType.NUMBER: |
21 var n = parseInt(value, 10); | 21 var n = parseInt(value, 10); |
22 if (isNaN(n)) { | 22 if (isNaN(n)) { |
23 console.error('Argument to stringToPrefValue for number pref ' + | 23 console.error( |
24 'was unparsable: ' + value); | 24 'Argument to stringToPrefValue for number pref ' + |
| 25 'was unparsable: ' + value); |
25 return undefined; | 26 return undefined; |
26 } | 27 } |
27 return n; | 28 return n; |
28 case chrome.settingsPrivate.PrefType.STRING: | 29 case chrome.settingsPrivate.PrefType.STRING: |
29 case chrome.settingsPrivate.PrefType.URL: | 30 case chrome.settingsPrivate.PrefType.URL: |
30 return value; | 31 return value; |
31 default: | 32 default: |
32 assertNotReached('No conversion from string to ' + pref.type + ' pref'); | 33 assertNotReached('No conversion from string to ' + pref.type + ' pref'); |
33 } | 34 } |
34 } | 35 } |
35 | 36 |
36 /** | 37 /** |
37 * Returns the value of the pref as a string. | 38 * Returns the value of the pref as a string. |
38 * @param {!chrome.settingsPrivate.PrefObject} pref | 39 * @param {!chrome.settingsPrivate.PrefObject} pref |
39 * @return {string} | 40 * @return {string} |
40 */ | 41 */ |
41 function prefToString(pref) { | 42 function prefToString(pref) { |
42 switch (pref.type) { | 43 switch (pref.type) { |
43 case chrome.settingsPrivate.PrefType.BOOLEAN: | 44 case chrome.settingsPrivate.PrefType.BOOLEAN: |
44 case chrome.settingsPrivate.PrefType.NUMBER: | 45 case chrome.settingsPrivate.PrefType.NUMBER: |
45 return pref.value.toString(); | 46 return pref.value.toString(); |
46 case chrome.settingsPrivate.PrefType.STRING: | 47 case chrome.settingsPrivate.PrefType.STRING: |
47 case chrome.settingsPrivate.PrefType.URL: | 48 case chrome.settingsPrivate.PrefType.URL: |
48 return /** @type {string} */(pref.value); | 49 return /** @type {string} */ (pref.value); |
49 default: | 50 default: |
50 assertNotReached('No conversion from ' + pref.type + ' pref to string'); | 51 assertNotReached('No conversion from ' + pref.type + ' pref to string'); |
51 } | 52 } |
52 } | 53 } |
53 return { | 54 return { |
54 stringToPrefValue: stringToPrefValue, | 55 stringToPrefValue: stringToPrefValue, |
55 prefToString: prefToString, | 56 prefToString: prefToString, |
56 }; | 57 }; |
57 }); | 58 }); |
OLD | NEW |