Debian Codegraph @

closure-library/closure/goog/tweak/entries.js

raw
1 // Copyright 2009 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Definitions for all tweak entries.
17 * The class hierarchy is as follows (abstract entries are denoted with a *):
18 * BaseEntry(id, description) *
19 * -> ButtonAction(buttons in the UI)
20 * -> BaseSetting(query parameter) *
21 * -> BooleanGroup(child booleans)
22 * -> BasePrimitiveSetting(value, defaultValue) *
23 * -> BooleanSetting
24 * -> StringSetting
25 * -> NumericSetting
26 * -> BooleanInGroupSetting(token)
27 * Most clients should not use these classes directly, but instead use the API
28 * defined in tweak.js. One possible use case for directly using them is to
29 * register tweaks that are not known at compile time.
30 *
31 * @author [email protected] (Andrew Grieve)
32 */
33
34 goog.provide('goog.tweak.BaseEntry');
35 goog.provide('goog.tweak.BasePrimitiveSetting');
36 goog.provide('goog.tweak.BaseSetting');
37 goog.provide('goog.tweak.BooleanGroup');
38 goog.provide('goog.tweak.BooleanInGroupSetting');
39 goog.provide('goog.tweak.BooleanSetting');
40 goog.provide('goog.tweak.ButtonAction');
41 goog.provide('goog.tweak.NumericSetting');
42 goog.provide('goog.tweak.StringSetting');
43
44 goog.require('goog.array');
45 goog.require('goog.asserts');
46 goog.require('goog.log');
47 goog.require('goog.object');
48
49
50
51 /**
52 * Base class for all Registry entries.
53 * @param {string} id The ID for the entry. Must contain only letters,
54 * numbers, underscores and periods.
55 * @param {string} description A description of what the entry does.
56 * @constructor
57 */
58 goog.tweak.BaseEntry = function(id, description) {
59 /**
60 * An ID to uniquely identify the entry.
61 * @type {string}
62 * @private
63 */
64 this.id_ = id;
65
66 /**
67 * A descriptive label for the entry.
68 * @type {string}
69 */
70 this.label = id;
71
72 /**
73 * A description of what this entry does.
74 * @type {string}
75 */
76 this.description = description;
77
78 /**
79 * Functions to be called whenever a setting is changed or a button is
80 * clicked.
81 * @type {!Array<!Function>}
82 * @private
83 */
84 this.callbacks_ = [];
85 };
86
87
88 /**
89 * The logger for this class.
90 * @type {goog.log.Logger}
91 * @protected
92 */
93 goog.tweak.BaseEntry.prototype.logger =
94 goog.log.getLogger('goog.tweak.BaseEntry');
95
96
97 /**
98 * Whether a restart is required for changes to the setting to take effect.
99 * @type {boolean}
100 * @private
101 */
102 goog.tweak.BaseEntry.prototype.restartRequired_ = true;
103
104
105 /**
106 * @return {string} Returns the entry's ID.
107 */
108 goog.tweak.BaseEntry.prototype.getId = function() {
109 return this.id_;
110 };
111
112
113 /**
114 * Returns whether a restart is required for changes to the setting to take
115 * effect.
116 * @return {boolean} The value.
117 */
118 goog.tweak.BaseEntry.prototype.isRestartRequired = function() {
119 return this.restartRequired_;
120 };
121
122
123 /**
124 * Sets whether a restart is required for changes to the setting to take
125 * effect.
126 * @param {boolean} value The new value.
127 */
128 goog.tweak.BaseEntry.prototype.setRestartRequired = function(value) {
129 this.restartRequired_ = value;
130 };
131
132
133 /**
134 * Adds a callback that should be called when the setting has changed (or when
135 * an action has been clicked).
136 * @param {!Function} callback The callback to add.
137 */
138 goog.tweak.BaseEntry.prototype.addCallback = function(callback) {
139 this.callbacks_.push(callback);
140 };
141
142
143 /**
144 * Removes a callback that was added by addCallback.
145 * @param {!Function} callback The callback to add.
146 */
147 goog.tweak.BaseEntry.prototype.removeCallback = function(callback) {
148 goog.array.remove(this.callbacks_, callback);
149 };
150
151
152 /**
153 * Calls all registered callbacks.
154 */
155 goog.tweak.BaseEntry.prototype.fireCallbacks = function() {
156 for (var i = 0, callback; callback = this.callbacks_[i]; ++i) {
157 callback(this);
158 }
159 };
160
161
162
163 /**
164 * Base class for all tweak entries that are settings. Settings are entries
165 * that are associated with a query parameter.
166 * @param {string} id The ID for the setting.
167 * @param {string} description A description of what the setting does.
168 * @constructor
169 * @extends {goog.tweak.BaseEntry}
170 */
171 goog.tweak.BaseSetting = function(id, description) {
172 goog.tweak.BaseEntry.call(this, id, description);
173 // Apply this restriction for settings since they turn in to query
174 // parameters. For buttons, it's not really important.
175 goog.asserts.assert(
176 !/[^A-Za-z0-9._]/.test(id), 'Tweak id contains illegal characters: ', id);
177
178 /**
179 * The value of this setting's query parameter.
180 * @type {string|undefined}
181 * @protected
182 */
183 this.initialQueryParamValue;
184
185 /**
186 * The query parameter that controls this setting.
187 * @type {?string}
188 * @private
189 */
190 this.paramName_ = this.getId().toLowerCase();
191 };
192 goog.inherits(goog.tweak.BaseSetting, goog.tweak.BaseEntry);
193
194
195 /**
196 * States of initialization. Entries are initialized lazily in order to allow
197 * their initialization to happen in multiple statements.
198 * @enum {number}
199 * @private
200 */
201 goog.tweak.BaseSetting.InitializeState_ = {
202 // The start state for all settings.
203 NOT_INITIALIZED: 0,
204 // This is used to allow concrete classes to call assertNotInitialized()
205 // during their initialize() function.
206 INITIALIZING: 1,
207 // One a setting is initialized, it may no longer change its configuration
208 // settings (associated query parameter, token, etc).
209 INITIALIZED: 2
210 };
211
212
213 /**
214 * The logger for this class.
215 * @type {goog.log.Logger}
216 * @protected
217 * @override
218 */
219 goog.tweak.BaseSetting.prototype.logger =
220 goog.log.getLogger('goog.tweak.BaseSetting');
221
222
223 /**
224 * Whether initialize() has been called (or is in the middle of being called).
225 * @type {goog.tweak.BaseSetting.InitializeState_}
226 * @private
227 */
228 goog.tweak.BaseSetting.prototype.initializeState_ =
229 goog.tweak.BaseSetting.InitializeState_.NOT_INITIALIZED;
230
231
232 /**
233 * Sets the value of the entry based on the value of the query parameter. Once
234 * this is called, configuration settings (associated query parameter, token,
235 * etc) may not be changed.
236 * @param {?string} value The part of the query param for this setting after
237 * the '='. Null if it is not present.
238 * @protected
239 */
240 goog.tweak.BaseSetting.prototype.initialize = goog.abstractMethod;
241
242
243 /**
244 * Returns the value to be used in the query parameter for this tweak.
245 * @return {?string} The encoded value. Null if the value is set to its
246 * default.
247 */
248 goog.tweak.BaseSetting.prototype.getNewValueEncoded = goog.abstractMethod;
249
250
251 /**
252 * Asserts that this tweak has not been initialized yet.
253 * @param {string} funcName Function name to use in the assertion message.
254 * @protected
255 */
256 goog.tweak.BaseSetting.prototype.assertNotInitialized = function(funcName) {
257 goog.asserts.assert(
258 this.initializeState_ !=
259 goog.tweak.BaseSetting.InitializeState_.INITIALIZED,
260 'Cannot call ' + funcName + ' after the tweak as been initialized.');
261 };
262
263
264 /**
265 * Returns whether the setting is currently being initialized.
266 * @return {boolean} Whether the setting is currently being initialized.
267 * @protected
268 */
269 goog.tweak.BaseSetting.prototype.isInitializing = function() {
270 return this.initializeState_ ==
271 goog.tweak.BaseSetting.InitializeState_.INITIALIZING;
272 };
273
274
275 /**
276 * Sets the initial query parameter value for this setting. May not be called
277 * after the setting has been initialized.
278 * @param {string} value The initial query parameter value for this setting.
279 */
280 goog.tweak.BaseSetting.prototype.setInitialQueryParamValue = function(value) {
281 this.assertNotInitialized('setInitialQueryParamValue');
282 this.initialQueryParamValue = value;
283 };
284
285
286 /**
287 * Returns the name of the query parameter used for this setting.
288 * @return {?string} The param name. Null if no query parameter is directly
289 * associated with the setting.
290 */
291 goog.tweak.BaseSetting.prototype.getParamName = function() {
292 return this.paramName_;
293 };
294
295
296 /**
297 * Sets the name of the query parameter used for this setting. If null is
298 * passed the the setting will not appear in the top-level query string.
299 * @param {?string} value The new value.
300 */
301 goog.tweak.BaseSetting.prototype.setParamName = function(value) {
302 this.assertNotInitialized('setParamName');
303 this.paramName_ = value;
304 };
305
306
307 /**
308 * Applies the default value or query param value if this is the first time
309 * that the function has been called.
310 * @protected
311 */
312 goog.tweak.BaseSetting.prototype.ensureInitialized = function() {
313 if (this.initializeState_ ==
314 goog.tweak.BaseSetting.InitializeState_.NOT_INITIALIZED) {
315 // Instead of having only initialized / not initialized, there is a
316 // separate in-between state so that functions that call
317 // assertNotInitialized() will not fail when called inside of the
318 // initialize().
319 this.initializeState_ =
320 goog.tweak.BaseSetting.InitializeState_.INITIALIZING;
321 var value = this.initialQueryParamValue == undefined ?
322 null :
323 this.initialQueryParamValue;
324 this.initialize(value);
325 this.initializeState_ = goog.tweak.BaseSetting.InitializeState_.INITIALIZED;
326 }
327 };
328
329
330
331 /**
332 * Base class for all settings that wrap primitive values.
333 * @param {string} id The ID for the setting.
334 * @param {string} description A description of what the setting does.
335 * @param {*} defaultValue The default value for this setting.
336 * @constructor
337 * @extends {goog.tweak.BaseSetting}
338 */
339 goog.tweak.BasePrimitiveSetting = function(id, description, defaultValue) {
340 goog.tweak.BaseSetting.call(this, id, description);
341 /**
342 * The default value of the setting.
343 * @type {?}
344 * @private
345 */
346 this.defaultValue_ = defaultValue;
347
348 /**
349 * The value of the tweak.
350 * @type {?}
351 * @private
352 */
353 this.value_;
354
355 /**
356 * The value of the tweak once "Apply Tweaks" is pressed.
357 * @type {?}
358 * @private
359 */
360 this.newValue_;
361 };
362 goog.inherits(goog.tweak.BasePrimitiveSetting, goog.tweak.BaseSetting);
363
364
365 /**
366 * The logger for this class.
367 * @type {goog.log.Logger}
368 * @protected
369 * @override
370 */
371 goog.tweak.BasePrimitiveSetting.prototype.logger =
372 goog.log.getLogger('goog.tweak.BasePrimitiveSetting');
373
374
375 /**
376 * Returns the query param encoded representation of the setting's value.
377 * @return {string} The encoded value.
378 * @protected
379 */
380 goog.tweak.BasePrimitiveSetting.prototype.encodeNewValue = goog.abstractMethod;
381
382
383 /**
384 * If the setting has the restartRequired option, then returns its initial
385 * value. Otherwise, returns its current value.
386 * @return {?} The value.
387 */
388 goog.tweak.BasePrimitiveSetting.prototype.getValue = function() {
389 this.ensureInitialized();
390 return this.value_;
391 };
392
393
394 /**
395 * Returns the value of the setting to use once "Apply Tweaks" is clicked.
396 * @return {?} The value.
397 */
398 goog.tweak.BasePrimitiveSetting.prototype.getNewValue = function() {
399 this.ensureInitialized();
400 return this.newValue_;
401 };
402
403
404 /**
405 * Sets the value of the setting. If the setting has the restartRequired
406 * option, then the value will not be changed until the "Apply Tweaks" button
407 * is clicked. If it does not have the option, the value will be update
408 * immediately and all registered callbacks will be called.
409 * @param {?} value The value.
410 */
411 goog.tweak.BasePrimitiveSetting.prototype.setValue = function(value) {
412 this.ensureInitialized();
413 var changed = this.newValue_ != value;
414 this.newValue_ = value;
415 // Don't fire callbacks if we are currently in the initialize() method.
416 if (this.isInitializing()) {
417 this.value_ = value;
418 } else {
419 if (!this.isRestartRequired()) {
420 // Update the current value only if the tweak has been marked as not
421 // needing a restart.
422 this.value_ = value;
423 }
424 if (changed) {
425 this.fireCallbacks();
426 }
427 }
428 };
429
430
431 /**
432 * Returns the default value for this setting.
433 * @return {?} The default value.
434 */
435 goog.tweak.BasePrimitiveSetting.prototype.getDefaultValue = function() {
436 return this.defaultValue_;
437 };
438
439
440 /**
441 * Sets the default value for the tweak.
442 * @param {?} value The new value.
443 */
444 goog.tweak.BasePrimitiveSetting.prototype.setDefaultValue = function(value) {
445 this.assertNotInitialized('setDefaultValue');
446 this.defaultValue_ = value;
447 };
448
449
450 /**
451 * @override
452 */
453 goog.tweak.BasePrimitiveSetting.prototype.getNewValueEncoded = function() {
454 this.ensureInitialized();
455 return this.newValue_ == this.defaultValue_ ? null : this.encodeNewValue();
456 };
457
458
459
460 /**
461 * A registry setting for string values.
462 * @param {string} id The ID for the setting.
463 * @param {string} description A description of what the setting does.
464 * @constructor
465 * @extends {goog.tweak.BasePrimitiveSetting}
466 * @final
467 */
468 goog.tweak.StringSetting = function(id, description) {
469 goog.tweak.BasePrimitiveSetting.call(this, id, description, '');
470 /**
471 * Valid values for the setting.
472 * @type {Array<string>|undefined}
473 */
474 this.validValues_;
475 };
476 goog.inherits(goog.tweak.StringSetting, goog.tweak.BasePrimitiveSetting);
477
478
479 /**
480 * The logger for this class.
481 * @type {goog.log.Logger}
482 * @protected
483 * @override
484 */
485 goog.tweak.StringSetting.prototype.logger =
486 goog.log.getLogger('goog.tweak.StringSetting');
487
488
489 /**
490 * @override
491 * @return {string} The tweaks's value.
492 */
493 goog.tweak.StringSetting.prototype.getValue;
494
495
496 /**
497 * @override
498 * @return {string} The tweaks's new value.
499 */
500 goog.tweak.StringSetting.prototype.getNewValue;
501
502
503 /**
504 * @override
505 * @param {string} value The tweaks's value.
506 */
507 goog.tweak.StringSetting.prototype.setValue;
508
509
510 /**
511 * @override
512 * @param {string} value The default value.
513 */
514 goog.tweak.StringSetting.prototype.setDefaultValue;
515
516
517 /**
518 * @override
519 * @return {string} The default value.
520 */
521 goog.tweak.StringSetting.prototype.getDefaultValue;
522
523
524 /**
525 * @override
526 */
527 goog.tweak.StringSetting.prototype.encodeNewValue = function() {
528 return this.getNewValue();
529 };
530
531
532 /**
533 * Sets the valid values for the setting.
534 * @param {Array<string>|undefined} values Valid values.
535 */
536 goog.tweak.StringSetting.prototype.setValidValues = function(values) {
537 this.assertNotInitialized('setValidValues');
538 this.validValues_ = values;
539 // Set the default value to the first value in the list if the current
540 // default value is not within it.
541 if (values && !goog.array.contains(values, this.getDefaultValue())) {
542 this.setDefaultValue(values[0]);
543 }
544 };
545
546
547 /**
548 * Returns the valid values for the setting.
549 * @return {Array<string>|undefined} Valid values.
550 */
551 goog.tweak.StringSetting.prototype.getValidValues = function() {
552 return this.validValues_;
553 };
554
555
556 /**
557 * @override
558 */
559 goog.tweak.StringSetting.prototype.initialize = function(value) {
560 if (value == null) {
561 this.setValue(this.getDefaultValue());
562 } else {
563 var validValues = this.validValues_;
564 if (validValues) {
565 // Make the query parameter values case-insensitive since users might
566 // type them by hand. Make the capitalization that is actual used come
567 // from the list of valid values.
568 value = value.toLowerCase();
569 for (var i = 0, il = validValues.length; i < il; ++i) {
570 if (value == validValues[i].toLowerCase()) {
571 this.setValue(validValues[i]);
572 return;
573 }
574 }
575 // Warn if the value is not in the list of allowed values.
576 goog.log.warning(
577 this.logger, 'Tweak ' + this.getId() +
578 ' has value outside of expected range:' + value);
579 }
580 this.setValue(value);
581 }
582 };
583
584
585
586 /**
587 * A registry setting for numeric values.
588 * @param {string} id The ID for the setting.
589 * @param {string} description A description of what the setting does.
590 * @constructor
591 * @extends {goog.tweak.BasePrimitiveSetting}
592 * @final
593 */
594 goog.tweak.NumericSetting = function(id, description) {
595 goog.tweak.BasePrimitiveSetting.call(this, id, description, 0);
596 /**
597 * Valid values for the setting.
598 * @type {Array<number>|undefined}
599 */
600 this.validValues_;
601 };
602 goog.inherits(goog.tweak.NumericSetting, goog.tweak.BasePrimitiveSetting);
603
604
605 /**
606 * The logger for this class.
607 * @type {goog.log.Logger}
608 * @protected
609 * @override
610 */
611 goog.tweak.NumericSetting.prototype.logger =
612 goog.log.getLogger('goog.tweak.NumericSetting');
613
614
615 /**
616 * @override
617 * @return {number} The tweaks's value.
618 */
619 goog.tweak.NumericSetting.prototype.getValue;
620
621
622 /**
623 * @override
624 * @return {number} The tweaks's new value.
625 */
626 goog.tweak.NumericSetting.prototype.getNewValue;
627
628
629 /**
630 * @override
631 * @param {number} value The tweaks's value.
632 */
633 goog.tweak.NumericSetting.prototype.setValue;
634
635
636 /**
637 * @override
638 * @param {number} value The default value.
639 */
640 goog.tweak.NumericSetting.prototype.setDefaultValue;
641
642
643 /**
644 * @override
645 * @return {number} The default value.
646 */
647 goog.tweak.NumericSetting.prototype.getDefaultValue;
648
649
650 /**
651 * @override
652 */
653 goog.tweak.NumericSetting.prototype.encodeNewValue = function() {
654 return '' + this.getNewValue();
655 };
656
657
658 /**
659 * Sets the valid values for the setting.
660 * @param {Array<number>|undefined} values Valid values.
661 */
662 goog.tweak.NumericSetting.prototype.setValidValues = function(values) {
663 this.assertNotInitialized('setValidValues');
664 this.validValues_ = values;
665 // Set the default value to the first value in the list if the current
666 // default value is not within it.
667 if (values && !goog.array.contains(values, this.getDefaultValue())) {
668 this.setDefaultValue(values[0]);
669 }
670 };
671
672
673 /**
674 * Returns the valid values for the setting.
675 * @return {Array<number>|undefined} Valid values.
676 */
677 goog.tweak.NumericSetting.prototype.getValidValues = function() {
678 return this.validValues_;
679 };
680
681
682 /**
683 * @override
684 */
685 goog.tweak.NumericSetting.prototype.initialize = function(value) {
686 if (value == null) {
687 this.setValue(this.getDefaultValue());
688 } else {
689 var coercedValue = +value;
690 // Warn if the value is not in the list of allowed values.
691 if (this.validValues_ &&
692 !goog.array.contains(this.validValues_, coercedValue)) {
693 goog.log.warning(
694 this.logger, 'Tweak ' + this.getId() +
695 ' has value outside of expected range: ' + value);
696 }
697
698 if (isNaN(coercedValue)) {
699 goog.log.warning(
700 this.logger, 'Tweak ' + this.getId() +
701 ' has value of NaN, resetting to ' + this.getDefaultValue());
702 this.setValue(this.getDefaultValue());
703 } else {
704 this.setValue(coercedValue);
705 }
706 }
707 };
708
709
710
711 /**
712 * A registry setting that can be either true of false.
713 * @param {string} id The ID for the setting.
714 * @param {string} description A description of what the setting does.
715 * @constructor
716 * @extends {goog.tweak.BasePrimitiveSetting}
717 */
718 goog.tweak.BooleanSetting = function(id, description) {
719 goog.tweak.BasePrimitiveSetting.call(this, id, description, false);
720 };
721 goog.inherits(goog.tweak.BooleanSetting, goog.tweak.BasePrimitiveSetting);
722
723
724 /**
725 * The logger for this class.
726 * @type {goog.log.Logger}
727 * @protected
728 * @override
729 */
730 goog.tweak.BooleanSetting.prototype.logger =
731 goog.log.getLogger('goog.tweak.BooleanSetting');
732
733
734 /**
735 * @override
736 * @return {boolean} The tweaks's value.
737 */
738 goog.tweak.BooleanSetting.prototype.getValue;
739
740
741 /**
742 * @override
743 * @return {boolean} The tweaks's new value.
744 */
745 goog.tweak.BooleanSetting.prototype.getNewValue;
746
747
748 /**
749 * @override
750 * @param {boolean} value The tweaks's value.
751 */
752 goog.tweak.BooleanSetting.prototype.setValue;
753
754
755 /**
756 * @override
757 * @param {boolean} value The default value.
758 */
759 goog.tweak.BooleanSetting.prototype.setDefaultValue;
760
761
762 /**
763 * @override
764 * @return {boolean} The default value.
765 */
766 goog.tweak.BooleanSetting.prototype.getDefaultValue;
767
768
769 /**
770 * @override
771 */
772 goog.tweak.BooleanSetting.prototype.encodeNewValue = function() {
773 return this.getNewValue() ? '1' : '0';
774 };
775
776
777 /**
778 * @override
779 */
780 goog.tweak.BooleanSetting.prototype.initialize = function(value) {
781 if (value == null) {
782 this.setValue(this.getDefaultValue());
783 } else {
784 value = value.toLowerCase();
785 this.setValue(value == 'true' || value == '1');
786 }
787 };
788
789
790
791 /**
792 * An entry in a BooleanGroup.
793 * @param {string} id The ID for the setting.
794 * @param {string} description A description of what the setting does.
795 * @param {!goog.tweak.BooleanGroup} group The group that this entry belongs
796 * to.
797 * @constructor
798 * @extends {goog.tweak.BooleanSetting}
799 * @final
800 */
801 goog.tweak.BooleanInGroupSetting = function(id, description, group) {
802 goog.tweak.BooleanSetting.call(this, id, description);
803
804 /**
805 * The token to use in the query parameter.
806 * @type {string}
807 * @private
808 */
809 this.token_ = this.getId().toLowerCase();
810
811 /**
812 * The BooleanGroup that this setting belongs to.
813 * @type {!goog.tweak.BooleanGroup}
814 * @private
815 */
816 this.group_ = group;
817
818 // Take setting out of top-level query parameter list.
819 goog.tweak.BooleanInGroupSetting.superClass_.setParamName.call(this, null);
820 };
821 goog.inherits(goog.tweak.BooleanInGroupSetting, goog.tweak.BooleanSetting);
822
823
824 /**
825 * The logger for this class.
826 * @type {goog.log.Logger}
827 * @protected
828 * @override
829 */
830 goog.tweak.BooleanInGroupSetting.prototype.logger =
831 goog.log.getLogger('goog.tweak.BooleanInGroupSetting');
832
833
834 /**
835 * @override
836 */
837 goog.tweak.BooleanInGroupSetting.prototype.setParamName = function(value) {
838 goog.asserts.fail('Use setToken() for BooleanInGroupSetting.');
839 };
840
841
842 /**
843 * Sets the token to use in the query parameter.
844 * @param {string} value The value.
845 */
846 goog.tweak.BooleanInGroupSetting.prototype.setToken = function(value) {
847 this.token_ = value;
848 };
849
850
851 /**
852 * Returns the token to use in the query parameter.
853 * @return {string} The value.
854 */
855 goog.tweak.BooleanInGroupSetting.prototype.getToken = function() {
856 return this.token_;
857 };
858
859
860 /**
861 * Returns the BooleanGroup that this setting belongs to.
862 * @return {!goog.tweak.BooleanGroup} The BooleanGroup that this setting
863 * belongs to.
864 */
865 goog.tweak.BooleanInGroupSetting.prototype.getGroup = function() {
866 return this.group_;
867 };
868
869
870
871 /**
872 * A registry setting that contains a group of boolean subfield, where all
873 * entries modify the same query parameter. For example:
874 * ?foo=setting1,-setting2
875 * @param {string} id The ID for the setting.
876 * @param {string} description A description of what the setting does.
877 * @constructor
878 * @extends {goog.tweak.BaseSetting}
879 * @final
880 */
881 goog.tweak.BooleanGroup = function(id, description) {
882 goog.tweak.BaseSetting.call(this, id, description);
883
884 /**
885 * A map of token->child entry.
886 * @type {!Object<!goog.tweak.BooleanSetting>}
887 * @private
888 */
889 this.entriesByToken_ = {};
890
891
892 /**
893 * A map of token->true/false for all tokens that appeared in the query
894 * parameter.
895 * @type {!Object<boolean>}
896 * @private
897 */
898 this.queryParamValues_ = {};
899
900 };
901 goog.inherits(goog.tweak.BooleanGroup, goog.tweak.BaseSetting);
902
903
904 /**
905 * The logger for this class.
906 * @type {goog.log.Logger}
907 * @protected
908 * @override
909 */
910 goog.tweak.BooleanGroup.prototype.logger =
911 goog.log.getLogger('goog.tweak.BooleanGroup');
912
913
914 /**
915 * Returns the map of token->boolean settings.
916 * @return {!Object<!goog.tweak.BooleanSetting>} The child settings.
917 */
918 goog.tweak.BooleanGroup.prototype.getChildEntries = function() {
919 return this.entriesByToken_;
920 };
921
922
923 /**
924 * Adds the given BooleanSetting to the group.
925 * @param {goog.tweak.BooleanInGroupSetting} boolEntry The entry.
926 */
927 goog.tweak.BooleanGroup.prototype.addChild = function(boolEntry) {
928 this.ensureInitialized();
929
930 var token = boolEntry.getToken();
931 var lcToken = token.toLowerCase();
932 goog.asserts.assert(
933 !this.entriesByToken_[lcToken],
934 'Multiple bools registered with token "%s" in group: %s', token,
935 this.getId());
936 this.entriesByToken_[lcToken] = boolEntry;
937
938 // Initialize from query param.
939 var value = this.queryParamValues_[lcToken];
940 if (value != undefined) {
941 boolEntry.initialQueryParamValue = value ? '1' : '0';
942 }
943 };
944
945
946 /**
947 * @override
948 */
949 goog.tweak.BooleanGroup.prototype.initialize = function(value) {
950 var queryParamValues = {};
951
952 if (value) {
953 var tokens = value.split(/\s*,\s*/);
954 for (var i = 0; i < tokens.length; ++i) {
955 var token = tokens[i].toLowerCase();
956 var negative = token.charAt(0) == '-';
957 if (negative) {
958 token = token.substr(1);
959 }
960 queryParamValues[token] = !negative;
961 }
962 }
963 this.queryParamValues_ = queryParamValues;
964 };
965
966
967 /**
968 * @override
969 */
970 goog.tweak.BooleanGroup.prototype.getNewValueEncoded = function() {
971 this.ensureInitialized();
972 var nonDefaultValues = [];
973 // Sort the keys so that the generate value is stable.
974 var keys = goog.object.getKeys(this.entriesByToken_);
975 keys.sort();
976 for (var i = 0, entry; entry = this.entriesByToken_[keys[i]]; ++i) {
977 var encodedValue = entry.getNewValueEncoded();
978 if (encodedValue != null) {
979 nonDefaultValues.push(
980 (entry.getNewValue() ? '' : '-') + entry.getToken());
981 }
982 }
983 return nonDefaultValues.length ? nonDefaultValues.join(',') : null;
984 };
985
986
987
988 /**
989 * A registry action (a button).
990 * @param {string} id The ID for the setting.
991 * @param {string} description A description of what the setting does.
992 * @param {!Function} callback Function to call when the button is clicked.
993 * @constructor
994 * @extends {goog.tweak.BaseEntry}
995 * @final
996 */
997 goog.tweak.ButtonAction = function(id, description, callback) {
998 goog.tweak.BaseEntry.call(this, id, description);
999 this.addCallback(callback);
1000 this.setRestartRequired(false);
1001 };
1002 goog.inherits(goog.tweak.ButtonAction, goog.tweak.BaseEntry);