123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853 |
- goog.provide('goog.tweak.EntriesPanel');
- goog.provide('goog.tweak.TweakUi');
- goog.require('goog.array');
- goog.require('goog.asserts');
- goog.require('goog.dom');
- goog.require('goog.dom.TagName');
- goog.require('goog.dom.safe');
- goog.require('goog.html.SafeHtml');
- goog.require('goog.html.SafeStyleSheet');
- goog.require('goog.object');
- goog.require('goog.string.Const');
- goog.require('goog.style');
- goog.require('goog.tweak');
- goog.require('goog.tweak.BaseEntry');
- 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.StringSetting');
- goog.require('goog.ui.Zippy');
- goog.require('goog.userAgent');
- goog.tweak.TweakUi = function(registry, opt_domHelper) {
-
- this.registry_ = registry;
-
- this.entriesPanel_;
-
- this.domHelper_ = opt_domHelper || goog.dom.getDomHelper();
-
- registry.addOnRegisterListener(goog.bind(this.onNewRegisteredEntry_, this));
- };
- goog.tweak.TweakUi.ROOT_PANEL_CLASS_ = goog.getCssName('goog-tweak-root');
- goog.tweak.TweakUi.ENTRY_CSS_CLASS_ = goog.getCssName('goog-tweak-entry');
- goog.tweak.TweakUi.ENTRY_CSS_CLASSES_ = goog.tweak.TweakUi.ENTRY_CSS_CLASS_ +
- ' ' + goog.getCssName('goog-inline-block');
- goog.tweak.TweakUi.ENTRY_GROUP_CSS_CLASSES_ =
- goog.tweak.TweakUi.ENTRY_CSS_CLASS_;
- goog.tweak.TweakUi.STYLE_SHEET_INSTALLED_MARKER_ = '__closure_tweak_installed_';
- goog.tweak.TweakUi.CSS_STYLES_ = (function() {
- var MOBILE = goog.userAgent.MOBILE;
- var IE = goog.userAgent.IE;
- var ROOT_PANEL_CLASS = '.' + goog.tweak.TweakUi.ROOT_PANEL_CLASS_;
- var GOOG_INLINE_BLOCK_CLASS = '.' + goog.getCssName('goog-inline-block');
- var ret = [goog.html.SafeStyleSheet.createRule(
- ROOT_PANEL_CLASS, {'background': '#ffc', 'padding': '0 4px'})];
-
- if (!IE) {
- ret.push(goog.html.SafeStyleSheet.createRule(
- GOOG_INLINE_BLOCK_CLASS, {'display': 'inline-block'}));
- }
-
- if (MOBILE) {
- ret.push(goog.html.SafeStyleSheet.createRule(
- ROOT_PANEL_CLASS + ',' + ROOT_PANEL_CLASS + ' fieldset',
- {'line-height': '2em'}));
- }
- return goog.html.SafeStyleSheet.concat(ret);
- })();
- goog.tweak.TweakUi.create = function(opt_domHelper) {
- var registry = goog.tweak.getRegistry();
- if (registry) {
- var ui = new goog.tweak.TweakUi(registry, opt_domHelper);
- ui.render();
- return ui.getRootElement();
- }
- };
- goog.tweak.TweakUi.createCollapsible = function(opt_domHelper) {
- var registry = goog.tweak.getRegistry();
- if (registry) {
- var dh = opt_domHelper || goog.dom.getDomHelper();
-
-
- var showLink =
- dh.createDom(goog.dom.TagName.A, {href: 'javascript:;'}, 'Show Tweaks');
- var hideLink =
- dh.createDom(goog.dom.TagName.A, {href: 'javascript:;'}, 'Hide Tweaks');
- var ret = dh.createDom(goog.dom.TagName.DIV, null, showLink);
- var lazyCreate = function() {
-
- var ui = new goog.tweak.TweakUi(
- (registry), dh);
- ui.render();
-
-
- hideLink.style.marginRight = '10px';
- var tweakElem = ui.getRootElement();
- tweakElem.insertBefore(hideLink, tweakElem.firstChild);
- ret.appendChild(tweakElem);
- return tweakElem;
- };
- new goog.ui.Zippy(showLink, lazyCreate, false , hideLink);
- return ret;
- }
- };
- goog.tweak.TweakUi.entryCompare_ = function(a, b) {
- return (
- goog.array.defaultCompare(
- a instanceof goog.tweak.NamespaceEntry_,
- b instanceof goog.tweak.NamespaceEntry_) ||
- goog.array.defaultCompare(
- a instanceof goog.tweak.BooleanGroup,
- b instanceof goog.tweak.BooleanGroup) ||
- goog.array.defaultCompare(
- a instanceof goog.tweak.ButtonAction,
- b instanceof goog.tweak.ButtonAction) ||
- goog.array.defaultCompare(a.label, b.label) ||
- goog.array.defaultCompare(a.getId(), b.getId()));
- };
- goog.tweak.TweakUi.isGroupEntry_ = function(entry) {
- return entry instanceof goog.tweak.NamespaceEntry_ ||
- entry instanceof goog.tweak.BooleanGroup;
- };
- goog.tweak.TweakUi.extractBooleanGroupEntries_ = function(group) {
- var ret = goog.object.getValues(group.getChildEntries());
- ret.sort(goog.tweak.TweakUi.entryCompare_);
- return ret;
- };
- goog.tweak.TweakUi.extractNamespace_ = function(entry) {
- var namespaceMatch = /.+(?=\.)/.exec(entry.getId());
- return namespaceMatch ? namespaceMatch[0] : '';
- };
- goog.tweak.TweakUi.getNamespacedLabel_ = function(entry) {
- var label = entry.label;
- if (label == entry.getId()) {
- label = label.substr(label.lastIndexOf('.') + 1);
- }
- return label;
- };
- goog.tweak.TweakUi.prototype.getRootElement = function() {
- goog.asserts.assert(
- this.entriesPanel_, 'TweakUi.getRootElement called before render().');
- return this.entriesPanel_.getRootElement();
- };
- goog.tweak.TweakUi.prototype.restartWithAppliedTweaks_ = function() {
- var queryString = this.registry_.makeUrlQuery();
- var wnd = this.domHelper_.getWindow();
- if (queryString != wnd.location.search) {
- wnd.location.search = queryString;
- } else {
- wnd.location.reload();
- }
- };
- goog.tweak.TweakUi.prototype.installStyles_ = function() {
-
-
-
- var doc = this.domHelper_.getDocument();
- if (!(goog.tweak.TweakUi.STYLE_SHEET_INSTALLED_MARKER_ in doc)) {
- goog.style.installSafeStyleSheet(goog.tweak.TweakUi.CSS_STYLES_, doc);
- doc[goog.tweak.TweakUi.STYLE_SHEET_INSTALLED_MARKER_] = true;
- }
- };
- goog.tweak.TweakUi.prototype.render = function() {
- this.installStyles_();
- var dh = this.domHelper_;
-
- var submitButton = dh.createDom(
- goog.dom.TagName.BUTTON, {style: 'font-weight:bold'}, 'Apply Tweaks');
- submitButton.onclick = goog.bind(this.restartWithAppliedTweaks_, this);
- var rootPanel = new goog.tweak.EntriesPanel([], dh);
- var rootPanelDiv = rootPanel.render(submitButton);
- rootPanelDiv.className += ' ' + goog.tweak.TweakUi.ROOT_PANEL_CLASS_;
- this.entriesPanel_ = rootPanel;
- var entries = this.registry_.extractEntries(
- true , false );
- for (var i = 0, entry; entry = entries[i]; i++) {
- this.insertEntry_(entry);
- }
- return rootPanelDiv;
- };
- goog.tweak.TweakUi.prototype.onNewRegisteredEntry_ = function(entry) {
- if (this.entriesPanel_) {
- this.insertEntry_(entry);
- }
- };
- goog.tweak.TweakUi.prototype.insertEntry_ = function(entry) {
- var panel = this.entriesPanel_;
- var namespace = goog.tweak.TweakUi.extractNamespace_(entry);
- if (namespace) {
-
- var namespaceEntryId = goog.tweak.NamespaceEntry_.ID_PREFIX + namespace;
- var nsPanel = panel.childPanels[namespaceEntryId];
- if (nsPanel) {
- panel = nsPanel;
- } else {
- entry = new goog.tweak.NamespaceEntry_(namespace, [entry]);
- }
- }
- if (entry instanceof goog.tweak.BooleanInGroupSetting) {
- var group = entry.getGroup();
-
-
- panel = panel.childPanels[group.getId()];
- }
- goog.asserts.assert(panel, 'Missing panel for entry %s', entry.getId());
- panel.insertEntry(entry);
- };
- goog.tweak.EntriesPanel = function(entries, opt_domHelper) {
-
- this.entries_ = entries;
- var self = this;
-
- this.boundHelpOnClickHandler_ = function() {
- self.onHelpClick_(this.parentNode);
- };
-
- this.rootElem_;
-
- this.mainPanel_;
-
- this.showAllDescriptionsState_;
-
- this.domHelper_ = opt_domHelper || goog.dom.getDomHelper();
-
- this.childPanels = {};
- };
- goog.tweak.EntriesPanel.prototype.getRootElement = function() {
- goog.asserts.assert(
- this.rootElem_, 'EntriesPanel.getRootElement called before render().');
- return (this.rootElem_);
- };
- goog.tweak.EntriesPanel.prototype.render = function(opt_endElement) {
- var dh = this.domHelper_;
- var entries = this.entries_;
- var ret = dh.createDom(goog.dom.TagName.DIV);
- var showAllDescriptionsLink = dh.createDom(
- goog.dom.TagName.A, {
- href: 'javascript:;',
- onclick: goog.bind(this.toggleAllDescriptions, this)
- },
- 'Toggle all Descriptions');
- ret.appendChild(showAllDescriptionsLink);
-
- var mainPanel = dh.createElement(goog.dom.TagName.DIV);
- this.mainPanel_ = mainPanel;
- for (var i = 0, entry; entry = entries[i]; i++) {
- mainPanel.appendChild(this.createEntryElem_(entry));
- }
- if (opt_endElement) {
- mainPanel.appendChild(opt_endElement);
- }
- ret.appendChild(mainPanel);
- this.rootElem_ = ret;
- return (ret);
- };
- goog.tweak.EntriesPanel.prototype.insertEntry = function(entry) {
- var insertIndex =
- -goog.array.binarySearch(
- this.entries_, entry, goog.tweak.TweakUi.entryCompare_) -
- 1;
- goog.asserts.assert(
- insertIndex >= 0, 'insertEntry failed for %s', entry.getId());
- goog.array.insertAt(this.entries_, entry, insertIndex);
- this.mainPanel_.insertBefore(
- this.createEntryElem_(entry),
-
- this.mainPanel_.childNodes[insertIndex] || null);
- };
- goog.tweak.EntriesPanel.prototype.createEntryElem_ = function(entry) {
- var dh = this.domHelper_;
- var isGroupEntry = goog.tweak.TweakUi.isGroupEntry_(entry);
- var classes = isGroupEntry ? goog.tweak.TweakUi.ENTRY_GROUP_CSS_CLASSES_ :
- goog.tweak.TweakUi.ENTRY_CSS_CLASSES_;
-
-
- var containerNodeName =
- isGroupEntry ? goog.dom.TagName.SPAN : goog.dom.TagName.LABEL;
- var ret = dh.createDom(
- goog.dom.TagName.DIV, classes,
- dh.createDom(
- containerNodeName, {
-
- title: entry.description,
- style: 'color:' + (entry.isRestartRequired() ? '' : 'blue')
- },
- this.createTweakEntryDom_(entry)),
-
- this.createHelpElem_(entry));
- return ret;
- };
- goog.tweak.EntriesPanel.prototype.onHelpClick_ = function(entryDiv) {
- this.showDescription_(entryDiv, !entryDiv.style.display);
- };
- goog.tweak.EntriesPanel.prototype.showDescription_ = function(entryDiv, show) {
- var descriptionElem = entryDiv.lastChild.lastChild;
- goog.style.setElementShown( (descriptionElem), show);
- entryDiv.style.display = show ? 'block' : '';
- };
- goog.tweak.EntriesPanel.prototype.createHelpElem_ = function(entry) {
-
-
- var ret = this.domHelper_.createElement(goog.dom.TagName.SPAN);
- goog.dom.safe.setInnerHtml(
- ret,
- goog.html.SafeHtml.concat(
- goog.html.SafeHtml.create(
- 'b', {'style': goog.string.Const.from('padding:0 1em 0 .5em')},
- '?'),
- goog.html.SafeHtml.create(
- 'span',
- {'style': goog.string.Const.from('display:none;color:#666')})));
- ret.onclick = this.boundHelpOnClickHandler_;
-
- var descriptionElem = (ret.lastChild);
- if (entry.isRestartRequired()) {
- goog.dom.setTextContent(descriptionElem, entry.description);
- } else {
- goog.dom.safe.setInnerHtml(
- descriptionElem,
- goog.html.SafeHtml.concat(
- goog.html.SafeHtml.htmlEscape(entry.description),
- goog.html.SafeHtml.create(
- 'span', {'style': goog.string.Const.from('color: blue')},
- '(no restart required)')));
- }
- return ret;
- };
- goog.tweak.EntriesPanel.prototype.toggleAllDescriptions = function() {
- var show = !this.showAllDescriptionsState_;
- this.showAllDescriptionsState_ = show;
- var entryDivs = this.domHelper_.getElementsByTagNameAndClass(
- goog.dom.TagName.DIV, goog.tweak.TweakUi.ENTRY_CSS_CLASS_,
- this.rootElem_);
- for (var i = 0, div; div = entryDivs[i]; i++) {
- this.showDescription_(div, show);
- }
- };
- goog.tweak.EntriesPanel.prototype.createComboBoxDom_ = function(
- tweak, label, onchangeFunc) {
-
-
- var dh = this.domHelper_;
- var ret = dh.getDocument().createDocumentFragment();
- ret.appendChild(dh.createTextNode(label + ': '));
- var selectElem = dh.createElement(goog.dom.TagName.SELECT);
- var values = tweak.getValidValues();
- for (var i = 0, il = values.length; i < il; ++i) {
- var optionElem = dh.createElement(goog.dom.TagName.OPTION);
- optionElem.text = String(values[i]);
-
-
- optionElem.value = String(values[i]);
- selectElem.appendChild(optionElem);
- }
- ret.appendChild(selectElem);
-
- selectElem.value = String(tweak.getNewValue());
- selectElem.onchange = onchangeFunc;
- tweak.addCallback(function() {
- selectElem.value = String(tweak.getNewValue());
- });
- return ret;
- };
- goog.tweak.EntriesPanel.prototype.createBooleanSettingDom_ = function(
- tweak, label) {
- var dh = this.domHelper_;
- var ret = dh.getDocument().createDocumentFragment();
- var checkbox = dh.createDom(goog.dom.TagName.INPUT, {type: 'checkbox'});
- ret.appendChild(checkbox);
- ret.appendChild(dh.createTextNode(label));
-
-
- checkbox.defaultChecked = tweak.getNewValue();
- checkbox.checked = tweak.getNewValue();
- checkbox.onchange = function() { tweak.setValue(checkbox.checked); };
- tweak.addCallback(function() { checkbox.checked = tweak.getNewValue(); });
- return ret;
- };
- goog.tweak.EntriesPanel.prototype.createSubPanelDom_ = function(
- entry, label, childEntries) {
- var dh = this.domHelper_;
- var toggleLink =
- dh.createDom(goog.dom.TagName.A, {href: 'javascript:;'}, label + ' \xBB');
- var toggleLink2 =
- dh.createDom(goog.dom.TagName.A, {href: 'javascript:;'}, '\xAB ' + label);
- toggleLink2.style.marginRight = '10px';
- var innerUi = new goog.tweak.EntriesPanel(childEntries, dh);
- this.childPanels[entry.getId()] = innerUi;
- var elem = innerUi.render();
-
- var descriptionsLink = elem.firstChild;
- var childrenElem = dh.createDom(
- goog.dom.TagName.FIELDSET, goog.getCssName('goog-inline-block'),
- dh.createDom(
- goog.dom.TagName.LEGEND, null, toggleLink2, descriptionsLink),
- elem);
- new goog.ui.Zippy(
- toggleLink, childrenElem, false , toggleLink2);
- var ret = dh.getDocument().createDocumentFragment();
- ret.appendChild(toggleLink);
- ret.appendChild(childrenElem);
- return ret;
- };
- goog.tweak.EntriesPanel.prototype.createTextBoxDom_ = function(
- tweak, label, onchangeFunc) {
- var dh = this.domHelper_;
- var ret = dh.getDocument().createDocumentFragment();
- ret.appendChild(dh.createTextNode(label + ': '));
- var textBox = dh.createDom(goog.dom.TagName.INPUT, {
- value: String(tweak.getNewValue()),
-
- size: 5,
- onblur: onchangeFunc
- });
- ret.appendChild(textBox);
- tweak.addCallback(function() {
- textBox.value = String(tweak.getNewValue());
- });
- return ret;
- };
- goog.tweak.EntriesPanel.prototype.createButtonActionDom_ = function(
- tweak, label) {
- return this.domHelper_.createDom(
- goog.dom.TagName.BUTTON, {onclick: goog.bind(tweak.fireCallbacks, tweak)},
- label);
- };
- goog.tweak.EntriesPanel.prototype.createTweakEntryDom_ = function(entry) {
- var label = goog.tweak.TweakUi.getNamespacedLabel_(entry);
- if (entry instanceof goog.tweak.BooleanSetting) {
- return this.createBooleanSettingDom_(entry, label);
- } else if (entry instanceof goog.tweak.BooleanGroup) {
- var childEntries = goog.tweak.TweakUi.extractBooleanGroupEntries_(entry);
- return this.createSubPanelDom_(entry, label, childEntries);
- } else if (entry instanceof goog.tweak.StringSetting) {
-
- var setValueFunc = function() { entry.setValue(this.value); };
- return entry.getValidValues() ?
- this.createComboBoxDom_(entry, label, setValueFunc) :
- this.createTextBoxDom_(entry, label, setValueFunc);
- } else if (entry instanceof goog.tweak.NumericSetting) {
- setValueFunc = function() {
-
- if (isNaN(this.value)) {
- this.value = entry.getNewValue();
- } else {
- entry.setValue(+this.value);
- }
- };
- return entry.getValidValues() ?
- this.createComboBoxDom_(entry, label, setValueFunc) :
- this.createTextBoxDom_(entry, label, setValueFunc);
- } else if (entry instanceof goog.tweak.NamespaceEntry_) {
- return this.createSubPanelDom_(entry, entry.label, entry.entries);
- }
- goog.asserts.assertInstanceof(
- entry, goog.tweak.ButtonAction, 'invalid entry: %s', entry);
- return this.createButtonActionDom_(
- (entry), label);
- };
- goog.tweak.NamespaceEntry_ = function(namespace, entries) {
- goog.tweak.BaseEntry.call(
- this, goog.tweak.NamespaceEntry_.ID_PREFIX + namespace,
- 'Tweaks within the ' + namespace + ' namespace.');
-
- this.entries = entries;
- this.label = namespace;
- };
- goog.inherits(goog.tweak.NamespaceEntry_, goog.tweak.BaseEntry);
- goog.tweak.NamespaceEntry_.ID_PREFIX = '!';
|