123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002 |
- // Copyright 2009 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- /**
- * @fileoverview Definitions for all tweak entries.
- * The class hierarchy is as follows (abstract entries are denoted with a *):
- * BaseEntry(id, description) *
- * -> ButtonAction(buttons in the UI)
- * -> BaseSetting(query parameter) *
- * -> BooleanGroup(child booleans)
- * -> BasePrimitiveSetting(value, defaultValue) *
- * -> BooleanSetting
- * -> StringSetting
- * -> NumericSetting
- * -> BooleanInGroupSetting(token)
- * Most clients should not use these classes directly, but instead use the API
- * defined in tweak.js. One possible use case for directly using them is to
- * register tweaks that are not known at compile time.
- *
- * @author agrieve@google.com (Andrew Grieve)
- */
- goog.provide('goog.tweak.BaseEntry');
- goog.provide('goog.tweak.BasePrimitiveSetting');
- goog.provide('goog.tweak.BaseSetting');
- goog.provide('goog.tweak.BooleanGroup');
- goog.provide('goog.tweak.BooleanInGroupSetting');
- goog.provide('goog.tweak.BooleanSetting');
- goog.provide('goog.tweak.ButtonAction');
- goog.provide('goog.tweak.NumericSetting');
- goog.provide('goog.tweak.StringSetting');
- goog.require('goog.array');
- goog.require('goog.asserts');
- goog.require('goog.log');
- goog.require('goog.object');
- /**
- * Base class for all Registry entries.
- * @param {string} id The ID for the entry. Must contain only letters,
- * numbers, underscores and periods.
- * @param {string} description A description of what the entry does.
- * @constructor
- */
- goog.tweak.BaseEntry = function(id, description) {
- /**
- * An ID to uniquely identify the entry.
- * @type {string}
- * @private
- */
- this.id_ = id;
- /**
- * A descriptive label for the entry.
- * @type {string}
- */
- this.label = id;
- /**
- * A description of what this entry does.
- * @type {string}
- */
- this.description = description;
- /**
- * Functions to be called whenever a setting is changed or a button is
- * clicked.
- * @type {!Array<!Function>}
- * @private
- */
- this.callbacks_ = [];
- };
- /**
- * The logger for this class.
- * @type {goog.log.Logger}
- * @protected
- */
- goog.tweak.BaseEntry.prototype.logger =
- goog.log.getLogger('goog.tweak.BaseEntry');
- /**
- * Whether a restart is required for changes to the setting to take effect.
- * @type {boolean}
- * @private
- */
- goog.tweak.BaseEntry.prototype.restartRequired_ = true;
- /**
- * @return {string} Returns the entry's ID.
- */
- goog.tweak.BaseEntry.prototype.getId = function() {
- return this.id_;
- };
- /**
- * Returns whether a restart is required for changes to the setting to take
- * effect.
- * @return {boolean} The value.
- */
- goog.tweak.BaseEntry.prototype.isRestartRequired = function() {
- return this.restartRequired_;
- };
- /**
- * Sets whether a restart is required for changes to the setting to take
- * effect.
- * @param {boolean} value The new value.
- */
- goog.tweak.BaseEntry.prototype.setRestartRequired = function(value) {
- this.restartRequired_ = value;
- };
- /**
- * Adds a callback that should be called when the setting has changed (or when
- * an action has been clicked).
- * @param {!Function} callback The callback to add.
- */
- goog.tweak.BaseEntry.prototype.addCallback = function(callback) {
- this.callbacks_.push(callback);
- };
- /**
- * Removes a callback that was added by addCallback.
- * @param {!Function} callback The callback to add.
- */
- goog.tweak.BaseEntry.prototype.removeCallback = function(callback) {
- goog.array.remove(this.callbacks_, callback);
- };
- /**
- * Calls all registered callbacks.
- */
- goog.tweak.BaseEntry.prototype.fireCallbacks = function() {
- for (var i = 0, callback; callback = this.callbacks_[i]; ++i) {
- callback(this);
- }
- };
- /**
- * Base class for all tweak entries that are settings. Settings are entries
- * that are associated with a query parameter.
- * @param {string} id The ID for the setting.
- * @param {string} description A description of what the setting does.
- * @constructor
- * @extends {goog.tweak.BaseEntry}
- */
- goog.tweak.BaseSetting = function(id, description) {
- goog.tweak.BaseEntry.call(this, id, description);
- // Apply this restriction for settings since they turn in to query
- // parameters. For buttons, it's not really important.
- goog.asserts.assert(
- !/[^A-Za-z0-9._]/.test(id), 'Tweak id contains illegal characters: ', id);
- /**
- * The value of this setting's query parameter.
- * @type {string|undefined}
- * @protected
- */
- this.initialQueryParamValue;
- /**
- * The query parameter that controls this setting.
- * @type {?string}
- * @private
- */
- this.paramName_ = this.getId().toLowerCase();
- };
- goog.inherits(goog.tweak.BaseSetting, goog.tweak.BaseEntry);
- /**
- * States of initialization. Entries are initialized lazily in order to allow
- * their initialization to happen in multiple statements.
- * @enum {number}
- * @private
- */
- goog.tweak.BaseSetting.InitializeState_ = {
- // The start state for all settings.
- NOT_INITIALIZED: 0,
- // This is used to allow concrete classes to call assertNotInitialized()
- // during their initialize() function.
- INITIALIZING: 1,
- // One a setting is initialized, it may no longer change its configuration
- // settings (associated query parameter, token, etc).
- INITIALIZED: 2
- };
- /**
- * The logger for this class.
- * @type {goog.log.Logger}
- * @protected
- * @override
- */
- goog.tweak.BaseSetting.prototype.logger =
- goog.log.getLogger('goog.tweak.BaseSetting');
- /**
- * Whether initialize() has been called (or is in the middle of being called).
- * @type {goog.tweak.BaseSetting.InitializeState_}
- * @private
- */
- goog.tweak.BaseSetting.prototype.initializeState_ =
- goog.tweak.BaseSetting.InitializeState_.NOT_INITIALIZED;
- /**
- * Sets the value of the entry based on the value of the query parameter. Once
- * this is called, configuration settings (associated query parameter, token,
- * etc) may not be changed.
- * @param {?string} value The part of the query param for this setting after
- * the '='. Null if it is not present.
- * @protected
- */
- goog.tweak.BaseSetting.prototype.initialize = goog.abstractMethod;
- /**
- * Returns the value to be used in the query parameter for this tweak.
- * @return {?string} The encoded value. Null if the value is set to its
- * default.
- */
- goog.tweak.BaseSetting.prototype.getNewValueEncoded = goog.abstractMethod;
- /**
- * Asserts that this tweak has not been initialized yet.
- * @param {string} funcName Function name to use in the assertion message.
- * @protected
- */
- goog.tweak.BaseSetting.prototype.assertNotInitialized = function(funcName) {
- goog.asserts.assert(
- this.initializeState_ !=
- goog.tweak.BaseSetting.InitializeState_.INITIALIZED,
- 'Cannot call ' + funcName + ' after the tweak as been initialized.');
- };
- /**
- * Returns whether the setting is currently being initialized.
- * @return {boolean} Whether the setting is currently being initialized.
- * @protected
- */
- goog.tweak.BaseSetting.prototype.isInitializing = function() {
- return this.initializeState_ ==
- goog.tweak.BaseSetting.InitializeState_.INITIALIZING;
- };
- /**
- * Sets the initial query parameter value for this setting. May not be called
- * after the setting has been initialized.
- * @param {string} value The initial query parameter value for this setting.
- */
- goog.tweak.BaseSetting.prototype.setInitialQueryParamValue = function(value) {
- this.assertNotInitialized('setInitialQueryParamValue');
- this.initialQueryParamValue = value;
- };
- /**
- * Returns the name of the query parameter used for this setting.
- * @return {?string} The param name. Null if no query parameter is directly
- * associated with the setting.
- */
- goog.tweak.BaseSetting.prototype.getParamName = function() {
- return this.paramName_;
- };
- /**
- * Sets the name of the query parameter used for this setting. If null is
- * passed the the setting will not appear in the top-level query string.
- * @param {?string} value The new value.
- */
- goog.tweak.BaseSetting.prototype.setParamName = function(value) {
- this.assertNotInitialized('setParamName');
- this.paramName_ = value;
- };
- /**
- * Applies the default value or query param value if this is the first time
- * that the function has been called.
- * @protected
- */
- goog.tweak.BaseSetting.prototype.ensureInitialized = function() {
- if (this.initializeState_ ==
- goog.tweak.BaseSetting.InitializeState_.NOT_INITIALIZED) {
- // Instead of having only initialized / not initialized, there is a
- // separate in-between state so that functions that call
- // assertNotInitialized() will not fail when called inside of the
- // initialize().
- this.initializeState_ =
- goog.tweak.BaseSetting.InitializeState_.INITIALIZING;
- var value = this.initialQueryParamValue == undefined ?
- null :
- this.initialQueryParamValue;
- this.initialize(value);
- this.initializeState_ = goog.tweak.BaseSetting.InitializeState_.INITIALIZED;
- }
- };
- /**
- * Base class for all settings that wrap primitive values.
- * @param {string} id The ID for the setting.
- * @param {string} description A description of what the setting does.
- * @param {*} defaultValue The default value for this setting.
- * @constructor
- * @extends {goog.tweak.BaseSetting}
- */
- goog.tweak.BasePrimitiveSetting = function(id, description, defaultValue) {
- goog.tweak.BaseSetting.call(this, id, description);
- /**
- * The default value of the setting.
- * @type {*}
- * @private
- */
- this.defaultValue_ = defaultValue;
- /**
- * The value of the tweak.
- * @type {*}
- * @private
- */
- this.value_;
- /**
- * The value of the tweak once "Apply Tweaks" is pressed.
- * @type {*}
- * @private
- */
- this.newValue_;
- };
- goog.inherits(goog.tweak.BasePrimitiveSetting, goog.tweak.BaseSetting);
- /**
- * The logger for this class.
- * @type {goog.log.Logger}
- * @protected
- * @override
- */
- goog.tweak.BasePrimitiveSetting.prototype.logger =
- goog.log.getLogger('goog.tweak.BasePrimitiveSetting');
- /**
- * Returns the query param encoded representation of the setting's value.
- * @return {string} The encoded value.
- * @protected
- */
- goog.tweak.BasePrimitiveSetting.prototype.encodeNewValue = goog.abstractMethod;
- /**
- * If the setting has the restartRequired option, then returns its initial
- * value. Otherwise, returns its current value.
- * @return {*} The value.
- */
- goog.tweak.BasePrimitiveSetting.prototype.getValue = function() {
- this.ensureInitialized();
- return this.value_;
- };
- /**
- * Returns the value of the setting to use once "Apply Tweaks" is clicked.
- * @return {*} The value.
- */
- goog.tweak.BasePrimitiveSetting.prototype.getNewValue = function() {
- this.ensureInitialized();
- return this.newValue_;
- };
- /**
- * Sets the value of the setting. If the setting has the restartRequired
- * option, then the value will not be changed until the "Apply Tweaks" button
- * is clicked. If it does not have the option, the value will be update
- * immediately and all registered callbacks will be called.
- * @param {*} value The value.
- */
- goog.tweak.BasePrimitiveSetting.prototype.setValue = function(value) {
- this.ensureInitialized();
- var changed = this.newValue_ != value;
- this.newValue_ = value;
- // Don't fire callbacks if we are currently in the initialize() method.
- if (this.isInitializing()) {
- this.value_ = value;
- } else {
- if (!this.isRestartRequired()) {
- // Update the current value only if the tweak has been marked as not
- // needing a restart.
- this.value_ = value;
- }
- if (changed) {
- this.fireCallbacks();
- }
- }
- };
- /**
- * Returns the default value for this setting.
- * @return {*} The default value.
- */
- goog.tweak.BasePrimitiveSetting.prototype.getDefaultValue = function() {
- return this.defaultValue_;
- };
- /**
- * Sets the default value for the tweak.
- * @param {*} value The new value.
- */
- goog.tweak.BasePrimitiveSetting.prototype.setDefaultValue = function(value) {
- this.assertNotInitialized('setDefaultValue');
- this.defaultValue_ = value;
- };
- /**
- * @override
- */
- goog.tweak.BasePrimitiveSetting.prototype.getNewValueEncoded = function() {
- this.ensureInitialized();
- return this.newValue_ == this.defaultValue_ ? null : this.encodeNewValue();
- };
- /**
- * A registry setting for string values.
- * @param {string} id The ID for the setting.
- * @param {string} description A description of what the setting does.
- * @constructor
- * @extends {goog.tweak.BasePrimitiveSetting}
- * @final
- */
- goog.tweak.StringSetting = function(id, description) {
- goog.tweak.BasePrimitiveSetting.call(this, id, description, '');
- /**
- * Valid values for the setting.
- * @type {Array<string>|undefined}
- */
- this.validValues_;
- };
- goog.inherits(goog.tweak.StringSetting, goog.tweak.BasePrimitiveSetting);
- /**
- * The logger for this class.
- * @type {goog.log.Logger}
- * @protected
- * @override
- */
- goog.tweak.StringSetting.prototype.logger =
- goog.log.getLogger('goog.tweak.StringSetting');
- /**
- * @override
- * @return {string} The tweaks's value.
- */
- goog.tweak.StringSetting.prototype.getValue;
- /**
- * @override
- * @return {string} The tweaks's new value.
- */
- goog.tweak.StringSetting.prototype.getNewValue;
- /**
- * @override
- * @param {string} value The tweaks's value.
- */
- goog.tweak.StringSetting.prototype.setValue;
- /**
- * @override
- * @param {string} value The default value.
- */
- goog.tweak.StringSetting.prototype.setDefaultValue;
- /**
- * @override
- * @return {string} The default value.
- */
- goog.tweak.StringSetting.prototype.getDefaultValue;
- /**
- * @override
- */
- goog.tweak.StringSetting.prototype.encodeNewValue = function() {
- return this.getNewValue();
- };
- /**
- * Sets the valid values for the setting.
- * @param {Array<string>|undefined} values Valid values.
- */
- goog.tweak.StringSetting.prototype.setValidValues = function(values) {
- this.assertNotInitialized('setValidValues');
- this.validValues_ = values;
- // Set the default value to the first value in the list if the current
- // default value is not within it.
- if (values && !goog.array.contains(values, this.getDefaultValue())) {
- this.setDefaultValue(values[0]);
- }
- };
- /**
- * Returns the valid values for the setting.
- * @return {Array<string>|undefined} Valid values.
- */
- goog.tweak.StringSetting.prototype.getValidValues = function() {
- return this.validValues_;
- };
- /**
- * @override
- */
- goog.tweak.StringSetting.prototype.initialize = function(value) {
- if (value == null) {
- this.setValue(this.getDefaultValue());
- } else {
- var validValues = this.validValues_;
- if (validValues) {
- // Make the query parameter values case-insensitive since users might
- // type them by hand. Make the capitalization that is actual used come
- // from the list of valid values.
- value = value.toLowerCase();
- for (var i = 0, il = validValues.length; i < il; ++i) {
- if (value == validValues[i].toLowerCase()) {
- this.setValue(validValues[i]);
- return;
- }
- }
- // Warn if the value is not in the list of allowed values.
- goog.log.warning(
- this.logger, 'Tweak ' + this.getId() +
- ' has value outside of expected range:' + value);
- }
- this.setValue(value);
- }
- };
- /**
- * A registry setting for numeric values.
- * @param {string} id The ID for the setting.
- * @param {string} description A description of what the setting does.
- * @constructor
- * @extends {goog.tweak.BasePrimitiveSetting}
- * @final
- */
- goog.tweak.NumericSetting = function(id, description) {
- goog.tweak.BasePrimitiveSetting.call(this, id, description, 0);
- /**
- * Valid values for the setting.
- * @type {Array<number>|undefined}
- */
- this.validValues_;
- };
- goog.inherits(goog.tweak.NumericSetting, goog.tweak.BasePrimitiveSetting);
- /**
- * The logger for this class.
- * @type {goog.log.Logger}
- * @protected
- * @override
- */
- goog.tweak.NumericSetting.prototype.logger =
- goog.log.getLogger('goog.tweak.NumericSetting');
- /**
- * @override
- * @return {number} The tweaks's value.
- */
- goog.tweak.NumericSetting.prototype.getValue;
- /**
- * @override
- * @return {number} The tweaks's new value.
- */
- goog.tweak.NumericSetting.prototype.getNewValue;
- /**
- * @override
- * @param {number} value The tweaks's value.
- */
- goog.tweak.NumericSetting.prototype.setValue;
- /**
- * @override
- * @param {number} value The default value.
- */
- goog.tweak.NumericSetting.prototype.setDefaultValue;
- /**
- * @override
- * @return {number} The default value.
- */
- goog.tweak.NumericSetting.prototype.getDefaultValue;
- /**
- * @override
- */
- goog.tweak.NumericSetting.prototype.encodeNewValue = function() {
- return '' + this.getNewValue();
- };
- /**
- * Sets the valid values for the setting.
- * @param {Array<number>|undefined} values Valid values.
- */
- goog.tweak.NumericSetting.prototype.setValidValues = function(values) {
- this.assertNotInitialized('setValidValues');
- this.validValues_ = values;
- // Set the default value to the first value in the list if the current
- // default value is not within it.
- if (values && !goog.array.contains(values, this.getDefaultValue())) {
- this.setDefaultValue(values[0]);
- }
- };
- /**
- * Returns the valid values for the setting.
- * @return {Array<number>|undefined} Valid values.
- */
- goog.tweak.NumericSetting.prototype.getValidValues = function() {
- return this.validValues_;
- };
- /**
- * @override
- */
- goog.tweak.NumericSetting.prototype.initialize = function(value) {
- if (value == null) {
- this.setValue(this.getDefaultValue());
- } else {
- var coercedValue = +value;
- // Warn if the value is not in the list of allowed values.
- if (this.validValues_ &&
- !goog.array.contains(this.validValues_, coercedValue)) {
- goog.log.warning(
- this.logger, 'Tweak ' + this.getId() +
- ' has value outside of expected range: ' + value);
- }
- if (isNaN(coercedValue)) {
- goog.log.warning(
- this.logger, 'Tweak ' + this.getId() +
- ' has value of NaN, resetting to ' + this.getDefaultValue());
- this.setValue(this.getDefaultValue());
- } else {
- this.setValue(coercedValue);
- }
- }
- };
- /**
- * A registry setting that can be either true of false.
- * @param {string} id The ID for the setting.
- * @param {string} description A description of what the setting does.
- * @constructor
- * @extends {goog.tweak.BasePrimitiveSetting}
- */
- goog.tweak.BooleanSetting = function(id, description) {
- goog.tweak.BasePrimitiveSetting.call(this, id, description, false);
- };
- goog.inherits(goog.tweak.BooleanSetting, goog.tweak.BasePrimitiveSetting);
- /**
- * The logger for this class.
- * @type {goog.log.Logger}
- * @protected
- * @override
- */
- goog.tweak.BooleanSetting.prototype.logger =
- goog.log.getLogger('goog.tweak.BooleanSetting');
- /**
- * @override
- * @return {boolean} The tweaks's value.
- */
- goog.tweak.BooleanSetting.prototype.getValue;
- /**
- * @override
- * @return {boolean} The tweaks's new value.
- */
- goog.tweak.BooleanSetting.prototype.getNewValue;
- /**
- * @override
- * @param {boolean} value The tweaks's value.
- */
- goog.tweak.BooleanSetting.prototype.setValue;
- /**
- * @override
- * @param {boolean} value The default value.
- */
- goog.tweak.BooleanSetting.prototype.setDefaultValue;
- /**
- * @override
- * @return {boolean} The default value.
- */
- goog.tweak.BooleanSetting.prototype.getDefaultValue;
- /**
- * @override
- */
- goog.tweak.BooleanSetting.prototype.encodeNewValue = function() {
- return this.getNewValue() ? '1' : '0';
- };
- /**
- * @override
- */
- goog.tweak.BooleanSetting.prototype.initialize = function(value) {
- if (value == null) {
- this.setValue(this.getDefaultValue());
- } else {
- value = value.toLowerCase();
- this.setValue(value == 'true' || value == '1');
- }
- };
- /**
- * An entry in a BooleanGroup.
- * @param {string} id The ID for the setting.
- * @param {string} description A description of what the setting does.
- * @param {!goog.tweak.BooleanGroup} group The group that this entry belongs
- * to.
- * @constructor
- * @extends {goog.tweak.BooleanSetting}
- * @final
- */
- goog.tweak.BooleanInGroupSetting = function(id, description, group) {
- goog.tweak.BooleanSetting.call(this, id, description);
- /**
- * The token to use in the query parameter.
- * @type {string}
- * @private
- */
- this.token_ = this.getId().toLowerCase();
- /**
- * The BooleanGroup that this setting belongs to.
- * @type {!goog.tweak.BooleanGroup}
- * @private
- */
- this.group_ = group;
- // Take setting out of top-level query parameter list.
- goog.tweak.BooleanInGroupSetting.superClass_.setParamName.call(this, null);
- };
- goog.inherits(goog.tweak.BooleanInGroupSetting, goog.tweak.BooleanSetting);
- /**
- * The logger for this class.
- * @type {goog.log.Logger}
- * @protected
- * @override
- */
- goog.tweak.BooleanInGroupSetting.prototype.logger =
- goog.log.getLogger('goog.tweak.BooleanInGroupSetting');
- /**
- * @override
- */
- goog.tweak.BooleanInGroupSetting.prototype.setParamName = function(value) {
- goog.asserts.fail('Use setToken() for BooleanInGroupSetting.');
- };
- /**
- * Sets the token to use in the query parameter.
- * @param {string} value The value.
- */
- goog.tweak.BooleanInGroupSetting.prototype.setToken = function(value) {
- this.token_ = value;
- };
- /**
- * Returns the token to use in the query parameter.
- * @return {string} The value.
- */
- goog.tweak.BooleanInGroupSetting.prototype.getToken = function() {
- return this.token_;
- };
- /**
- * Returns the BooleanGroup that this setting belongs to.
- * @return {!goog.tweak.BooleanGroup} The BooleanGroup that this setting
- * belongs to.
- */
- goog.tweak.BooleanInGroupSetting.prototype.getGroup = function() {
- return this.group_;
- };
- /**
- * A registry setting that contains a group of boolean subfield, where all
- * entries modify the same query parameter. For example:
- * ?foo=setting1,-setting2
- * @param {string} id The ID for the setting.
- * @param {string} description A description of what the setting does.
- * @constructor
- * @extends {goog.tweak.BaseSetting}
- * @final
- */
- goog.tweak.BooleanGroup = function(id, description) {
- goog.tweak.BaseSetting.call(this, id, description);
- /**
- * A map of token->child entry.
- * @type {!Object<!goog.tweak.BooleanSetting>}
- * @private
- */
- this.entriesByToken_ = {};
- /**
- * A map of token->true/false for all tokens that appeared in the query
- * parameter.
- * @type {!Object<boolean>}
- * @private
- */
- this.queryParamValues_ = {};
- };
- goog.inherits(goog.tweak.BooleanGroup, goog.tweak.BaseSetting);
- /**
- * The logger for this class.
- * @type {goog.log.Logger}
- * @protected
- * @override
- */
- goog.tweak.BooleanGroup.prototype.logger =
- goog.log.getLogger('goog.tweak.BooleanGroup');
- /**
- * Returns the map of token->boolean settings.
- * @return {!Object<!goog.tweak.BooleanSetting>} The child settings.
- */
- goog.tweak.BooleanGroup.prototype.getChildEntries = function() {
- return this.entriesByToken_;
- };
- /**
- * Adds the given BooleanSetting to the group.
- * @param {goog.tweak.BooleanInGroupSetting} boolEntry The entry.
- */
- goog.tweak.BooleanGroup.prototype.addChild = function(boolEntry) {
- this.ensureInitialized();
- var token = boolEntry.getToken();
- var lcToken = token.toLowerCase();
- goog.asserts.assert(
- !this.entriesByToken_[lcToken],
- 'Multiple bools registered with token "%s" in group: %s', token,
- this.getId());
- this.entriesByToken_[lcToken] = boolEntry;
- // Initialize from query param.
- var value = this.queryParamValues_[lcToken];
- if (value != undefined) {
- boolEntry.initialQueryParamValue = value ? '1' : '0';
- }
- };
- /**
- * @override
- */
- goog.tweak.BooleanGroup.prototype.initialize = function(value) {
- var queryParamValues = {};
- if (value) {
- var tokens = value.split(/\s*,\s*/);
- for (var i = 0; i < tokens.length; ++i) {
- var token = tokens[i].toLowerCase();
- var negative = token.charAt(0) == '-';
- if (negative) {
- token = token.substr(1);
- }
- queryParamValues[token] = !negative;
- }
- }
- this.queryParamValues_ = queryParamValues;
- };
- /**
- * @override
- */
- goog.tweak.BooleanGroup.prototype.getNewValueEncoded = function() {
- this.ensureInitialized();
- var nonDefaultValues = [];
- // Sort the keys so that the generate value is stable.
- var keys = goog.object.getKeys(this.entriesByToken_);
- keys.sort();
- for (var i = 0, entry; entry = this.entriesByToken_[keys[i]]; ++i) {
- var encodedValue = entry.getNewValueEncoded();
- if (encodedValue != null) {
- nonDefaultValues.push(
- (entry.getNewValue() ? '' : '-') + entry.getToken());
- }
- }
- return nonDefaultValues.length ? nonDefaultValues.join(',') : null;
- };
- /**
- * A registry action (a button).
- * @param {string} id The ID for the setting.
- * @param {string} description A description of what the setting does.
- * @param {!Function} callback Function to call when the button is clicked.
- * @constructor
- * @extends {goog.tweak.BaseEntry}
- * @final
- */
- goog.tweak.ButtonAction = function(id, description, callback) {
- goog.tweak.BaseEntry.call(this, id, description);
- this.addCallback(callback);
- this.setRestartRequired(false);
- };
- goog.inherits(goog.tweak.ButtonAction, goog.tweak.BaseEntry);
|