1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
// 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 Provides facilities for creating and querying tweaks.
* @see http://code.google.com/p/closure-library/wiki/UsingTweaks
*
* @author agrieve@google.com (Andrew Grieve)
*/
goog.provide('goog.tweak');
goog.provide('goog.tweak.ConfigParams');
goog.require('goog.asserts');
goog.require('goog.tweak.BaseSetting');
goog.require('goog.tweak.BooleanGroup');
goog.require('goog.tweak.BooleanInGroupSetting');
goog.require('goog.tweak.BooleanSetting');
goog.require('goog.tweak.ButtonAction');
goog.require('goog.tweak.NumericSetting');
goog.require('goog.tweak.Registry');
goog.require('goog.tweak.StringSetting');
/**
* Calls to this function are overridden by the compiler by the processTweaks
* pass. It returns the overrides to default values for tweaks set by compiler
* options.
* @return {!Object<number|string|boolean>} A map of tweakId -> defaultValue.
* @private
*/
goog.tweak.getCompilerOverrides_ = function() {
return {};
};
/**
* The global reference to the registry, if it exists.
* @type {goog.tweak.Registry}
* @private
*/
goog.tweak.registry_ = null;
/**
* The boolean group set by beginBooleanGroup and cleared by endBooleanGroup.
* @type {goog.tweak.BooleanGroup}
* @private
*/
goog.tweak.activeBooleanGroup_ = null;
/**
* Returns/creates the registry singleton.
* @return {!goog.tweak.Registry} The tweak registry.
*/
goog.tweak.getRegistry = function() {
if (!goog.tweak.registry_) {
var queryString = window.location.search;
var overrides = goog.tweak.getCompilerOverrides_();
goog.tweak.registry_ = new goog.tweak.Registry(queryString, overrides);
}
return goog.tweak.registry_;
};
/**
* Type for configParams.
* TODO(agrieve): Remove |Object when optional fields in struct types are
* implemented.
* @typedef {{
* label:(string|undefined),
* validValues:(!Array<string>|!Array<number>|undefined),
* paramName:(string|undefined),
* restartRequired:(boolean|undefined),
* callback:(Function|undefined),
* token:(string|undefined)
* }|!Object}
*/
goog.tweak.ConfigParams;
/**
* Applies all extra configuration parameters in configParams.
* @param {!goog.tweak.BaseEntry} entry The entry to apply them to.
* @param {!goog.tweak.ConfigParams} configParams Extra configuration
* parameters.
* @private
*/
goog.tweak.applyConfigParams_ = function(entry, configParams) {
if (configParams.label) {
entry.label = configParams.label;
delete configParams.label;
}
if (configParams.validValues) {
goog.asserts.assert(
entry instanceof goog.tweak.StringSetting ||
entry instanceof goog.tweak.NumericSetting,
'Cannot set validValues on tweak: %s', entry.getId());
entry.setValidValues(configParams.validValues);
delete configParams.validValues;
}
if (goog.isDef(configParams.paramName)) {
goog.asserts.assertInstanceof(
entry, goog.tweak.BaseSetting, 'Cannot set paramName on tweak: %s',
entry.getId());
entry.setParamName(configParams.paramName);
delete configParams.paramName;
}
if (goog.isDef(configParams.restartRequired)) {
entry.setRestartRequired(configParams.restartRequired);
delete configParams.restartRequired;
}
if (configParams.callback) {
entry.addCallback(configParams.callback);
delete configParams.callback;
goog.asserts.assert(
!entry.isRestartRequired() || (configParams.restartRequired == false),
'Tweak %s should set restartRequired: false, when adding a callback.',
entry.getId());
}
if (configParams.token) {
goog.asserts.assertInstanceof(
entry, goog.tweak.BooleanInGroupSetting,
'Cannot set token on tweak: %s', entry.getId());
entry.setToken(configParams.token);
delete configParams.token;
}
for (var key in configParams) {
goog.asserts.fail(
'Unknown config options (' + key + '=' + configParams[key] +
') for tweak ' + entry.getId());
}
};
/**
* Registers a tweak using the given factoryFunc.
* @param {!goog.tweak.BaseEntry} entry The entry to register.
* @param {boolean|string|number=} opt_defaultValue Default value.
* @param {goog.tweak.ConfigParams=} opt_configParams Extra
* configuration parameters.
* @private
*/
goog.tweak.doRegister_ = function(entry, opt_defaultValue, opt_configParams) {
if (opt_configParams) {
goog.tweak.applyConfigParams_(entry, opt_configParams);
}
if (opt_defaultValue != undefined) {
entry.setDefaultValue(opt_defaultValue);
}
if (goog.tweak.activeBooleanGroup_) {
goog.asserts.assertInstanceof(
entry, goog.tweak.BooleanInGroupSetting,
'Forgot to end Boolean Group: %s',
goog.tweak.activeBooleanGroup_.getId());
goog.tweak.activeBooleanGroup_.addChild(
/** @type {!goog.tweak.BooleanInGroupSetting} */ (entry));
}
goog.tweak.getRegistry().register(entry);
};
/**
* Creates and registers a group of BooleanSettings that are all set by a
* single query parameter. A call to goog.tweak.endBooleanGroup() must be used
* to close this group. Only goog.tweak.registerBoolean() calls are allowed with
* the beginBooleanGroup()/endBooleanGroup().
* @param {string} id The unique ID for the setting.
* @param {string} description A description of what the setting does.
* @param {goog.tweak.ConfigParams=} opt_configParams Extra configuration
* parameters.
*/
goog.tweak.beginBooleanGroup = function(id, description, opt_configParams) {
var entry = new goog.tweak.BooleanGroup(id, description);
goog.tweak.doRegister_(entry, undefined, opt_configParams);
goog.tweak.activeBooleanGroup_ = entry;
};
/**
* Stops adding boolean entries to the active boolean group.
*/
goog.tweak.endBooleanGroup = function() {
goog.tweak.activeBooleanGroup_ = null;
};
/**
* Creates and registers a BooleanSetting.
* @param {string} id The unique ID for the setting.
* @param {string} description A description of what the setting does.
* @param {boolean=} opt_defaultValue The default value for the setting.
* @param {goog.tweak.ConfigParams=} opt_configParams Extra configuration
* parameters.
*/
goog.tweak.registerBoolean = function(
id, description, opt_defaultValue, opt_configParams) {
// TODO(agrieve): There is a bug in the compiler that causes these calls not
// to be stripped without this outer if. Might be Issue #90.
if (goog.tweak.activeBooleanGroup_) {
var entry = new goog.tweak.BooleanInGroupSetting(
id, description, goog.tweak.activeBooleanGroup_);
} else {
entry = new goog.tweak.BooleanSetting(id, description);
}
goog.tweak.doRegister_(entry, opt_defaultValue, opt_configParams);
};
/**
* Creates and registers a StringSetting.
* @param {string} id The unique ID for the setting.
* @param {string} description A description of what the setting does.
* @param {string=} opt_defaultValue The default value for the setting.
* @param {goog.tweak.ConfigParams=} opt_configParams Extra configuration
* parameters.
*/
goog.tweak.registerString = function(
id, description, opt_defaultValue, opt_configParams) {
goog.tweak.doRegister_(
new goog.tweak.StringSetting(id, description), opt_defaultValue,
opt_configParams);
};
/**
* Creates and registers a NumericSetting.
* @param {string} id The unique ID for the setting.
* @param {string} description A description of what the setting does.
* @param {number=} opt_defaultValue The default value for the setting.
* @param {goog.tweak.ConfigParams=} opt_configParams Extra configuration
* parameters.
*/
goog.tweak.registerNumber = function(
id, description, opt_defaultValue, opt_configParams) {
goog.tweak.doRegister_(
new goog.tweak.NumericSetting(id, description), opt_defaultValue,
opt_configParams);
};
/**
* Creates and registers a ButtonAction.
* @param {string} id The unique ID for the setting.
* @param {string} description A description of what the action does.
* @param {!Function} callback Function to call when the button is clicked.
* @param {string=} opt_label The button text (instead of the ID).
*/
goog.tweak.registerButton = function(id, description, callback, opt_label) {
var tweak = new goog.tweak.ButtonAction(id, description, callback);
tweak.label = opt_label || tweak.label;
goog.tweak.doRegister_(tweak);
};
/**
* Sets a default value to use for the given tweak instead of the one passed
* to the register* function. This function must be called before the tweak is
* registered.
* @param {string} id The unique string that identifies the entry.
* @param {string|number|boolean} value The new default value for the tweak.
*/
goog.tweak.overrideDefaultValue = function(id, value) {
goog.tweak.getRegistry().overrideDefaultValue(id, value);
};
/**
* Returns the value of the boolean setting with the given ID.
* @param {string} id The unique string that identifies this entry.
* @return {boolean} The value of the tweak.
*/
goog.tweak.getBoolean = function(id) {
return goog.tweak.getRegistry().getBooleanSetting(id).getValue();
};
/**
* Returns the value of the string setting with the given ID,
* @param {string} id The unique string that identifies this entry.
* @return {string} The value of the tweak.
*/
goog.tweak.getString = function(id) {
return goog.tweak.getRegistry().getStringSetting(id).getValue();
};
/**
* Returns the value of the numeric setting with the given ID.
* @param {string} id The unique string that identifies this entry.
* @return {number} The value of the tweak.
*/
goog.tweak.getNumber = function(id) {
return goog.tweak.getRegistry().getNumericSetting(id).getValue();
};
|