Chromium Code Reviews| Index: chrome/browser/resources/settings/prefs/selectable_pref_behavior.js |
| diff --git a/chrome/browser/resources/settings/prefs/selectable_pref_behavior.js b/chrome/browser/resources/settings/prefs/selectable_pref_behavior.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..137f019ad02b82058d2f17d36e9bbc504354a408 |
| --- /dev/null |
| +++ b/chrome/browser/resources/settings/prefs/selectable_pref_behavior.js |
| @@ -0,0 +1,47 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * @fileoverview Behavior to convert between a pref value and a string. |
| + */ |
| + |
| +/** @polymerBehavior */ |
| +var SelectablePrefBehavior = { |
| + /** |
| + * @param {string} value |
| + * @return {*} |
| + */ |
| + toPrefValue: function(value) { |
|
stevenjb
2015/11/11 02:04:22
nit: Even tho we're using Closure to type values,
|
| + if (!this.pref) |
| + return undefined; |
|
stevenjb
2015/11/11 02:04:22
I have a slight preference for passing 'this.pref'
michaelpg
2015/11/11 20:29:21
I see your point, but then, should something like
stevenjb
2015/11/11 20:42:28
It certainly could be. In this case maybe that wou
|
| + if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER) { |
| + var n = parseInt(value, 10); |
| + if (isNaN(n)) { |
| + console.error('Bad selected name for numerical pref: ' + value); |
| + return undefined; |
| + } |
| + return n; |
| + } else if (this.pref.type == chrome.settingsPrivate.PrefType.BOOLEAN) { |
| + return value == 'true'; |
| + } else { |
| + assert(this.pref.type == chrome.settingsPrivate.PrefType.STRING); |
| + return value; |
| + } |
| + }, |
| + |
| + /** |
| + * Returns the string value of the pref. |
| + * @return {string} |
| + */ |
| + prefToString: function() { |
| + if (!this.pref) |
| + return ''; |
| + if (this.pref.type == chrome.settingsPrivate.PrefType.NUMBER || |
| + this.pref.type == chrome.settingsPrivate.PrefType.BOOLEAN) { |
| + return this.pref.value.toString(); |
| + } |
| + assert(this.pref.type == chrome.settingsPrivate.PrefType.STRING); |
| + return /** @type {string} */(this.pref.value); |
| + }, |
| +}; |