| 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 A UI for editing tweak settings / clicking tweak actions. |
| 17 |
* |
| 18 |
* @author [email protected] (Andrew Grieve) |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.tweak.EntriesPanel'); |
| 22 |
goog.provide('goog.tweak.TweakUi'); |
| 23 |
|
| 24 |
goog.require('goog.array'); |
| 25 |
goog.require('goog.asserts'); |
| 26 |
goog.require('goog.dom'); |
| 27 |
goog.require('goog.dom.TagName'); |
| 28 |
goog.require('goog.dom.safe'); |
| 29 |
goog.require('goog.html.SafeHtml'); |
| 30 |
goog.require('goog.html.SafeStyleSheet'); |
| 31 |
goog.require('goog.object'); |
| 32 |
goog.require('goog.string.Const'); |
| 33 |
goog.require('goog.style'); |
| 34 |
goog.require('goog.tweak'); |
| 35 |
goog.require('goog.tweak.BaseEntry'); |
| 36 |
goog.require('goog.tweak.BooleanGroup'); |
| 37 |
goog.require('goog.tweak.BooleanInGroupSetting'); |
| 38 |
goog.require('goog.tweak.BooleanSetting'); |
| 39 |
goog.require('goog.tweak.ButtonAction'); |
| 40 |
goog.require('goog.tweak.NumericSetting'); |
| 41 |
goog.require('goog.tweak.StringSetting'); |
| 42 |
goog.require('goog.ui.Zippy'); |
| 43 |
goog.require('goog.userAgent'); |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* A UI for editing tweak settings / clicking tweak actions. |
| 49 |
* @param {!goog.tweak.Registry} registry The registry to render. |
| 50 |
* @param {goog.dom.DomHelper=} opt_domHelper The DomHelper to render with. |
| 51 |
* @constructor |
| 52 |
* @final |
| 53 |
*/ |
| 54 |
goog.tweak.TweakUi = function(registry, opt_domHelper) { |
| 55 |
/** |
| 56 |
* The registry to create a UI from. |
| 57 |
* @type {!goog.tweak.Registry} |
| 58 |
* @private |
| 59 |
*/ |
| 60 |
this.registry_ = registry; |
| 61 |
|
| 62 |
/** |
| 63 |
* The element to display when the UI is visible. |
| 64 |
* @type {goog.tweak.EntriesPanel|undefined} |
| 65 |
* @private |
| 66 |
*/ |
| 67 |
this.entriesPanel_; |
| 68 |
|
| 69 |
/** |
| 70 |
* The DomHelper to render with. |
| 71 |
* @type {!goog.dom.DomHelper} |
| 72 |
* @private |
| 73 |
*/ |
| 74 |
this.domHelper_ = opt_domHelper || goog.dom.getDomHelper(); |
| 75 |
|
| 76 |
// Listen for newly registered entries (happens with lazy-loaded modules). |
| 77 |
registry.addOnRegisterListener(goog.bind(this.onNewRegisteredEntry_, this)); |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* The CSS class name unique to the root tweak panel div. |
| 83 |
* @type {string} |
| 84 |
* @private |
| 85 |
*/ |
| 86 |
goog.tweak.TweakUi.ROOT_PANEL_CLASS_ = goog.getCssName('goog-tweak-root'); |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* The CSS class name unique to the tweak entry div. |
| 91 |
* @type {string} |
| 92 |
* @private |
| 93 |
*/ |
| 94 |
goog.tweak.TweakUi.ENTRY_CSS_CLASS_ = goog.getCssName('goog-tweak-entry'); |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* The CSS classes for each tweak entry div. |
| 99 |
* @type {string} |
| 100 |
* @private |
| 101 |
*/ |
| 102 |
goog.tweak.TweakUi.ENTRY_CSS_CLASSES_ = goog.tweak.TweakUi.ENTRY_CSS_CLASS_ + |
| 103 |
' ' + goog.getCssName('goog-inline-block'); |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* The CSS classes for each namespace tweak entry div. |
| 108 |
* @type {string} |
| 109 |
* @private |
| 110 |
*/ |
| 111 |
goog.tweak.TweakUi.ENTRY_GROUP_CSS_CLASSES_ = |
| 112 |
goog.tweak.TweakUi.ENTRY_CSS_CLASS_; |
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* Marker that the style sheet has already been installed. |
| 117 |
* @type {string} |
| 118 |
* @private |
| 119 |
*/ |
| 120 |
goog.tweak.TweakUi.STYLE_SHEET_INSTALLED_MARKER_ = '__closure_tweak_installed_'; |
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* CSS used by TweakUI. |
| 125 |
* @type {!goog.html.SafeStyleSheet} |
| 126 |
* @private |
| 127 |
*/ |
| 128 |
goog.tweak.TweakUi.CSS_STYLES_ = (function() { |
| 129 |
var MOBILE = goog.userAgent.MOBILE; |
| 130 |
var IE = goog.userAgent.IE; |
| 131 |
var ROOT_PANEL_CLASS = '.' + goog.tweak.TweakUi.ROOT_PANEL_CLASS_; |
| 132 |
var GOOG_INLINE_BLOCK_CLASS = '.' + goog.getCssName('goog-inline-block'); |
| 133 |
var ret = [goog.html.SafeStyleSheet.createRule( |
| 134 |
ROOT_PANEL_CLASS, {'background': '#ffc', 'padding': '0 4px'})]; |
| 135 |
// Make this work even if the user hasn't included common.css. |
| 136 |
if (!IE) { |
| 137 |
ret.push(goog.html.SafeStyleSheet.createRule( |
| 138 |
GOOG_INLINE_BLOCK_CLASS, {'display': 'inline-block'})); |
| 139 |
} |
| 140 |
// Space things out vertically for touch UIs. |
| 141 |
if (MOBILE) { |
| 142 |
ret.push(goog.html.SafeStyleSheet.createRule( |
| 143 |
ROOT_PANEL_CLASS + ',' + ROOT_PANEL_CLASS + ' fieldset', |
| 144 |
{'line-height': '2em'})); |
| 145 |
} |
| 146 |
return goog.html.SafeStyleSheet.concat(ret); |
| 147 |
})(); |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* Creates a TweakUi if tweaks are enabled. |
| 152 |
* @param {goog.dom.DomHelper=} opt_domHelper The DomHelper to render with. |
| 153 |
* @return {!Element|undefined} The root UI element or undefined if tweaks are |
| 154 |
* not enabled. |
| 155 |
*/ |
| 156 |
goog.tweak.TweakUi.create = function(opt_domHelper) { |
| 157 |
var registry = goog.tweak.getRegistry(); |
| 158 |
if (registry) { |
| 159 |
var ui = new goog.tweak.TweakUi(registry, opt_domHelper); |
| 160 |
ui.render(); |
| 161 |
return ui.getRootElement(); |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
/** |
| 167 |
* Creates a TweakUi inside of a show/hide link. |
| 168 |
* @param {goog.dom.DomHelper=} opt_domHelper The DomHelper to render with. |
| 169 |
* @return {!Element|undefined} The root UI element or undefined if tweaks are |
| 170 |
* not enabled. |
| 171 |
*/ |
| 172 |
goog.tweak.TweakUi.createCollapsible = function(opt_domHelper) { |
| 173 |
var registry = goog.tweak.getRegistry(); |
| 174 |
if (registry) { |
| 175 |
var dh = opt_domHelper || goog.dom.getDomHelper(); |
| 176 |
|
| 177 |
// The following strings are for internal debugging only. No translation |
| 178 |
// necessary. Do NOT wrap goog.getMsg() around these strings. |
| 179 |
var showLink = |
| 180 |
dh.createDom(goog.dom.TagName.A, {href: 'javascript:;'}, 'Show Tweaks'); |
| 181 |
var hideLink = |
| 182 |
dh.createDom(goog.dom.TagName.A, {href: 'javascript:;'}, 'Hide Tweaks'); |
| 183 |
var ret = dh.createDom(goog.dom.TagName.DIV, null, showLink); |
| 184 |
|
| 185 |
var lazyCreate = function() { |
| 186 |
// Lazily render the UI. |
| 187 |
var ui = new goog.tweak.TweakUi( |
| 188 |
/** @type {!goog.tweak.Registry} */ (registry), dh); |
| 189 |
ui.render(); |
| 190 |
// Put the hide link on the same line as the "Show Descriptions" link. |
| 191 |
// Set the style lazily because we can. |
| 192 |
hideLink.style.marginRight = '10px'; |
| 193 |
var tweakElem = ui.getRootElement(); |
| 194 |
tweakElem.insertBefore(hideLink, tweakElem.firstChild); |
| 195 |
ret.appendChild(tweakElem); |
| 196 |
return tweakElem; |
| 197 |
}; |
| 198 |
new goog.ui.Zippy(showLink, lazyCreate, false /* expanded */, hideLink); |
| 199 |
return ret; |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
/** |
| 205 |
* Compares the given entries. Orders alphabetically and groups buttons and |
| 206 |
* expandable groups. |
| 207 |
* @param {!goog.tweak.BaseEntry} a The first entry to compare. |
| 208 |
* @param {!goog.tweak.BaseEntry} b The second entry to compare. |
| 209 |
* @return {number} Refer to goog.array.defaultCompare. |
| 210 |
* @private |
| 211 |
*/ |
| 212 |
goog.tweak.TweakUi.entryCompare_ = function(a, b) { |
| 213 |
return ( |
| 214 |
goog.array.defaultCompare( |
| 215 |
a instanceof goog.tweak.NamespaceEntry_, |
| 216 |
b instanceof goog.tweak.NamespaceEntry_) || |
| 217 |
goog.array.defaultCompare( |
| 218 |
a instanceof goog.tweak.BooleanGroup, |
| 219 |
b instanceof goog.tweak.BooleanGroup) || |
| 220 |
goog.array.defaultCompare( |
| 221 |
a instanceof goog.tweak.ButtonAction, |
| 222 |
b instanceof goog.tweak.ButtonAction) || |
| 223 |
goog.array.defaultCompare(a.label, b.label) || |
| 224 |
goog.array.defaultCompare(a.getId(), b.getId())); |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
/** |
| 229 |
* @param {!goog.tweak.BaseEntry} entry The entry. |
| 230 |
* @return {boolean} Returns whether the given entry contains sub-entries. |
| 231 |
* @private |
| 232 |
*/ |
| 233 |
goog.tweak.TweakUi.isGroupEntry_ = function(entry) { |
| 234 |
return entry instanceof goog.tweak.NamespaceEntry_ || |
| 235 |
entry instanceof goog.tweak.BooleanGroup; |
| 236 |
}; |
| 237 |
|
| 238 |
|
| 239 |
/** |
| 240 |
* Returns the list of entries from the given boolean group. |
| 241 |
* @param {!goog.tweak.BooleanGroup} group The group to get the entries from. |
| 242 |
* @return {!Array<!goog.tweak.BaseEntry>} The sorted entries. |
| 243 |
* @private |
| 244 |
*/ |
| 245 |
goog.tweak.TweakUi.extractBooleanGroupEntries_ = function(group) { |
| 246 |
var ret = goog.object.getValues(group.getChildEntries()); |
| 247 |
ret.sort(goog.tweak.TweakUi.entryCompare_); |
| 248 |
return ret; |
| 249 |
}; |
| 250 |
|
| 251 |
|
| 252 |
/** |
| 253 |
* @param {!goog.tweak.BaseEntry} entry The entry. |
| 254 |
* @return {string} Returns the namespace for the entry, or '' if it is not |
| 255 |
* namespaced. |
| 256 |
* @private |
| 257 |
*/ |
| 258 |
goog.tweak.TweakUi.extractNamespace_ = function(entry) { |
| 259 |
var namespaceMatch = /.+(?=\.)/.exec(entry.getId()); |
| 260 |
return namespaceMatch ? namespaceMatch[0] : ''; |
| 261 |
}; |
| 262 |
|
| 263 |
|
| 264 |
/** |
| 265 |
* @param {!goog.tweak.BaseEntry} entry The entry. |
| 266 |
* @return {string} Returns the part of the label after the last period, unless |
| 267 |
* the label has been explicly set (it is different from the ID). |
| 268 |
* @private |
| 269 |
*/ |
| 270 |
goog.tweak.TweakUi.getNamespacedLabel_ = function(entry) { |
| 271 |
var label = entry.label; |
| 272 |
if (label == entry.getId()) { |
| 273 |
label = label.substr(label.lastIndexOf('.') + 1); |
| 274 |
} |
| 275 |
return label; |
| 276 |
}; |
| 277 |
|
| 278 |
|
| 279 |
/** |
| 280 |
* @return {!Element} The root element. Must not be called before render(). |
| 281 |
*/ |
| 282 |
goog.tweak.TweakUi.prototype.getRootElement = function() { |
| 283 |
goog.asserts.assert( |
| 284 |
this.entriesPanel_, 'TweakUi.getRootElement called before render().'); |
| 285 |
return this.entriesPanel_.getRootElement(); |
| 286 |
}; |
| 287 |
|
| 288 |
|
| 289 |
/** |
| 290 |
* Reloads the page with query parameters set by the UI. |
| 291 |
* @private |
| 292 |
*/ |
| 293 |
goog.tweak.TweakUi.prototype.restartWithAppliedTweaks_ = function() { |
| 294 |
var queryString = this.registry_.makeUrlQuery(); |
| 295 |
var wnd = this.domHelper_.getWindow(); |
| 296 |
if (queryString != wnd.location.search) { |
| 297 |
wnd.location.search = queryString; |
| 298 |
} else { |
| 299 |
wnd.location.reload(); |
| 300 |
} |
| 301 |
}; |
| 302 |
|
| 303 |
|
| 304 |
/** |
| 305 |
* Installs the required CSS styles. |
| 306 |
* @private |
| 307 |
*/ |
| 308 |
goog.tweak.TweakUi.prototype.installStyles_ = function() { |
| 309 |
// Use an marker to install the styles only once per document. |
| 310 |
// Styles are injected via JS instead of in a separate style sheet so that |
| 311 |
// they are automatically excluded when tweaks are stripped out. |
| 312 |
var doc = this.domHelper_.getDocument(); |
| 313 |
if (!(goog.tweak.TweakUi.STYLE_SHEET_INSTALLED_MARKER_ in doc)) { |
| 314 |
goog.style.installSafeStyleSheet(goog.tweak.TweakUi.CSS_STYLES_, doc); |
| 315 |
doc[goog.tweak.TweakUi.STYLE_SHEET_INSTALLED_MARKER_] = true; |
| 316 |
} |
| 317 |
}; |
| 318 |
|
| 319 |
|
| 320 |
/** |
| 321 |
* Creates the element to display when the UI is visible. |
| 322 |
* @return {!Element} The root element. |
| 323 |
*/ |
| 324 |
goog.tweak.TweakUi.prototype.render = function() { |
| 325 |
this.installStyles_(); |
| 326 |
var dh = this.domHelper_; |
| 327 |
// The submit button |
| 328 |
var submitButton = dh.createDom( |
| 329 |
goog.dom.TagName.BUTTON, {style: 'font-weight:bold'}, 'Apply Tweaks'); |
| 330 |
submitButton.onclick = goog.bind(this.restartWithAppliedTweaks_, this); |
| 331 |
|
| 332 |
var rootPanel = new goog.tweak.EntriesPanel([], dh); |
| 333 |
var rootPanelDiv = rootPanel.render(submitButton); |
| 334 |
rootPanelDiv.className += ' ' + goog.tweak.TweakUi.ROOT_PANEL_CLASS_; |
| 335 |
this.entriesPanel_ = rootPanel; |
| 336 |
|
| 337 |
var entries = this.registry_.extractEntries( |
| 338 |
true /* excludeChildEntries */, false /* excludeNonSettings */); |
| 339 |
for (var i = 0, entry; entry = entries[i]; i++) { |
| 340 |
this.insertEntry_(entry); |
| 341 |
} |
| 342 |
|
| 343 |
return rootPanelDiv; |
| 344 |
}; |
| 345 |
|
| 346 |
|
| 347 |
/** |
| 348 |
* Updates the UI with the given entry. |
| 349 |
* @param {!goog.tweak.BaseEntry} entry The newly registered entry. |
| 350 |
* @private |
| 351 |
*/ |
| 352 |
goog.tweak.TweakUi.prototype.onNewRegisteredEntry_ = function(entry) { |
| 353 |
if (this.entriesPanel_) { |
| 354 |
this.insertEntry_(entry); |
| 355 |
} |
| 356 |
}; |
| 357 |
|
| 358 |
|
| 359 |
/** |
| 360 |
* Updates the UI with the given entry. |
| 361 |
* @param {!goog.tweak.BaseEntry} entry The newly registered entry. |
| 362 |
* @private |
| 363 |
*/ |
| 364 |
goog.tweak.TweakUi.prototype.insertEntry_ = function(entry) { |
| 365 |
var panel = this.entriesPanel_; |
| 366 |
var namespace = goog.tweak.TweakUi.extractNamespace_(entry); |
| 367 |
|
| 368 |
if (namespace) { |
| 369 |
// Find the NamespaceEntry that the entry belongs to. |
| 370 |
var namespaceEntryId = goog.tweak.NamespaceEntry_.ID_PREFIX + namespace; |
| 371 |
var nsPanel = panel.childPanels[namespaceEntryId]; |
| 372 |
if (nsPanel) { |
| 373 |
panel = nsPanel; |
| 374 |
} else { |
| 375 |
entry = new goog.tweak.NamespaceEntry_(namespace, [entry]); |
| 376 |
} |
| 377 |
} |
| 378 |
if (entry instanceof goog.tweak.BooleanInGroupSetting) { |
| 379 |
var group = entry.getGroup(); |
| 380 |
// BooleanGroup entries are always registered before their |
| 381 |
// BooleanInGroupSettings. |
| 382 |
panel = panel.childPanels[group.getId()]; |
| 383 |
} |
| 384 |
goog.asserts.assert(panel, 'Missing panel for entry %s', entry.getId()); |
| 385 |
panel.insertEntry(entry); |
| 386 |
}; |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
/** |
| 391 |
* The body of the tweaks UI and also used for BooleanGroup. |
| 392 |
* @param {!Array<!goog.tweak.BaseEntry>} entries The entries to show in the |
| 393 |
* panel. |
| 394 |
* @param {goog.dom.DomHelper=} opt_domHelper The DomHelper to render with. |
| 395 |
* @constructor |
| 396 |
* @final |
| 397 |
*/ |
| 398 |
goog.tweak.EntriesPanel = function(entries, opt_domHelper) { |
| 399 |
/** |
| 400 |
* The entries to show in the panel. |
| 401 |
* @type {!Array<!goog.tweak.BaseEntry>} entries |
| 402 |
* @private |
| 403 |
*/ |
| 404 |
this.entries_ = entries; |
| 405 |
|
| 406 |
var self = this; |
| 407 |
/** |
| 408 |
* The bound onclick handler for the help question marks. |
| 409 |
* @this {Element} |
| 410 |
* @private |
| 411 |
*/ |
| 412 |
this.boundHelpOnClickHandler_ = function() { |
| 413 |
self.onHelpClick_(this.parentNode); |
| 414 |
}; |
| 415 |
|
| 416 |
/** |
| 417 |
* The element that contains the UI. |
| 418 |
* @type {Element} |
| 419 |
* @private |
| 420 |
*/ |
| 421 |
this.rootElem_; |
| 422 |
|
| 423 |
/** |
| 424 |
* The element that contains all of the settings and the endElement. |
| 425 |
* @type {Element} |
| 426 |
* @private |
| 427 |
*/ |
| 428 |
this.mainPanel_; |
| 429 |
|
| 430 |
/** |
| 431 |
* Flips between true/false each time the "Toggle Descriptions" link is |
| 432 |
* clicked. |
| 433 |
* @type {boolean} |
| 434 |
* @private |
| 435 |
*/ |
| 436 |
this.showAllDescriptionsState_; |
| 437 |
|
| 438 |
/** |
| 439 |
* The DomHelper to render with. |
| 440 |
* @type {!goog.dom.DomHelper} |
| 441 |
* @private |
| 442 |
*/ |
| 443 |
this.domHelper_ = opt_domHelper || goog.dom.getDomHelper(); |
| 444 |
|
| 445 |
/** |
| 446 |
* Map of tweak ID -> EntriesPanel for child panels (BooleanGroups). |
| 447 |
* @type {!Object<!goog.tweak.EntriesPanel>} |
| 448 |
*/ |
| 449 |
this.childPanels = {}; |
| 450 |
}; |
| 451 |
|
| 452 |
|
| 453 |
/** |
| 454 |
* @return {!Element} Returns the expanded element. Must not be called before |
| 455 |
* render(). |
| 456 |
*/ |
| 457 |
goog.tweak.EntriesPanel.prototype.getRootElement = function() { |
| 458 |
goog.asserts.assert( |
| 459 |
this.rootElem_, 'EntriesPanel.getRootElement called before render().'); |
| 460 |
return /** @type {!Element} */ (this.rootElem_); |
| 461 |
}; |
| 462 |
|
| 463 |
|
| 464 |
/** |
| 465 |
* Creates and returns the expanded element. |
| 466 |
* The markup looks like: |
| 467 |
* |
| 468 |
* <div> |
| 469 |
* <a>Show Descriptions</a> |
| 470 |
* <div> |
| 471 |
* ... |
| 472 |
* {endElement} |
| 473 |
* </div> |
| 474 |
* </div> |
| 475 |
* |
| 476 |
* @param {Element|DocumentFragment=} opt_endElement Element to insert after all |
| 477 |
* tweak entries. |
| 478 |
* @return {!Element} The root element for the panel. |
| 479 |
*/ |
| 480 |
goog.tweak.EntriesPanel.prototype.render = function(opt_endElement) { |
| 481 |
var dh = this.domHelper_; |
| 482 |
var entries = this.entries_; |
| 483 |
var ret = dh.createDom(goog.dom.TagName.DIV); |
| 484 |
|
| 485 |
var showAllDescriptionsLink = dh.createDom( |
| 486 |
goog.dom.TagName.A, { |
| 487 |
href: 'javascript:;', |
| 488 |
onclick: goog.bind(this.toggleAllDescriptions, this) |
| 489 |
}, |
| 490 |
'Toggle all Descriptions'); |
| 491 |
ret.appendChild(showAllDescriptionsLink); |
| 492 |
|
| 493 |
// Add all of the entries. |
| 494 |
var mainPanel = dh.createElement(goog.dom.TagName.DIV); |
| 495 |
this.mainPanel_ = mainPanel; |
| 496 |
for (var i = 0, entry; entry = entries[i]; i++) { |
| 497 |
mainPanel.appendChild(this.createEntryElem_(entry)); |
| 498 |
} |
| 499 |
|
| 500 |
if (opt_endElement) { |
| 501 |
mainPanel.appendChild(opt_endElement); |
| 502 |
} |
| 503 |
ret.appendChild(mainPanel); |
| 504 |
this.rootElem_ = ret; |
| 505 |
return /** @type {!Element} */ (ret); |
| 506 |
}; |
| 507 |
|
| 508 |
|
| 509 |
/** |
| 510 |
* Inserts the given entry into the panel. |
| 511 |
* @param {!goog.tweak.BaseEntry} entry The entry to insert. |
| 512 |
*/ |
| 513 |
goog.tweak.EntriesPanel.prototype.insertEntry = function(entry) { |
| 514 |
var insertIndex = |
| 515 |
-goog.array.binarySearch( |
| 516 |
this.entries_, entry, goog.tweak.TweakUi.entryCompare_) - |
| 517 |
1; |
| 518 |
goog.asserts.assert( |
| 519 |
insertIndex >= 0, 'insertEntry failed for %s', entry.getId()); |
| 520 |
goog.array.insertAt(this.entries_, entry, insertIndex); |
| 521 |
this.mainPanel_.insertBefore( |
| 522 |
this.createEntryElem_(entry), |
| 523 |
// IE doesn't like 'undefined' here. |
| 524 |
this.mainPanel_.childNodes[insertIndex] || null); |
| 525 |
}; |
| 526 |
|
| 527 |
|
| 528 |
/** |
| 529 |
* Creates and returns a form element for the given entry. |
| 530 |
* @param {!goog.tweak.BaseEntry} entry The entry. |
| 531 |
* @return {!Element} The root DOM element for the entry. |
| 532 |
* @private |
| 533 |
*/ |
| 534 |
goog.tweak.EntriesPanel.prototype.createEntryElem_ = function(entry) { |
| 535 |
var dh = this.domHelper_; |
| 536 |
var isGroupEntry = goog.tweak.TweakUi.isGroupEntry_(entry); |
| 537 |
var classes = isGroupEntry ? goog.tweak.TweakUi.ENTRY_GROUP_CSS_CLASSES_ : |
| 538 |
goog.tweak.TweakUi.ENTRY_CSS_CLASSES_; |
| 539 |
// Containers should not use label tags or else all descendent inputs will be |
| 540 |
// connected on desktop browsers. |
| 541 |
var containerNodeName = |
| 542 |
isGroupEntry ? goog.dom.TagName.SPAN : goog.dom.TagName.LABEL; |
| 543 |
var ret = dh.createDom( |
| 544 |
goog.dom.TagName.DIV, classes, |
| 545 |
dh.createDom( |
| 546 |
containerNodeName, { |
| 547 |
// Make the hover text the description. |
| 548 |
title: entry.description, |
| 549 |
style: 'color:' + (entry.isRestartRequired() ? '' : 'blue') |
| 550 |
}, |
| 551 |
this.createTweakEntryDom_(entry)), |
| 552 |
// Add the expandable help question mark. |
| 553 |
this.createHelpElem_(entry)); |
| 554 |
return ret; |
| 555 |
}; |
| 556 |
|
| 557 |
|
| 558 |
/** |
| 559 |
* Click handler for the help link. |
| 560 |
* @param {Node} entryDiv The div that contains the tweak. |
| 561 |
* @private |
| 562 |
*/ |
| 563 |
goog.tweak.EntriesPanel.prototype.onHelpClick_ = function(entryDiv) { |
| 564 |
this.showDescription_(entryDiv, !entryDiv.style.display); |
| 565 |
}; |
| 566 |
|
| 567 |
|
| 568 |
/** |
| 569 |
* Twiddle the DOM so that the entry within the given span is shown/hidden. |
| 570 |
* @param {Node} entryDiv The div that contains the tweak. |
| 571 |
* @param {boolean} show True to show, false to hide. |
| 572 |
* @private |
| 573 |
*/ |
| 574 |
goog.tweak.EntriesPanel.prototype.showDescription_ = function(entryDiv, show) { |
| 575 |
var descriptionElem = entryDiv.lastChild.lastChild; |
| 576 |
goog.style.setElementShown(/** @type {Element} */ (descriptionElem), show); |
| 577 |
entryDiv.style.display = show ? 'block' : ''; |
| 578 |
}; |
| 579 |
|
| 580 |
|
| 581 |
/** |
| 582 |
* Creates and returns a help element for the given entry. |
| 583 |
* @param {goog.tweak.BaseEntry} entry The entry. |
| 584 |
* @return {!Element} The root element of the created DOM. |
| 585 |
* @private |
| 586 |
*/ |
| 587 |
goog.tweak.EntriesPanel.prototype.createHelpElem_ = function(entry) { |
| 588 |
// The markup looks like: |
| 589 |
// <span onclick=...><b>?</b><span>{description}</span></span> |
| 590 |
var ret = this.domHelper_.createElement(goog.dom.TagName.SPAN); |
| 591 |
goog.dom.safe.setInnerHtml( |
| 592 |
ret, |
| 593 |
goog.html.SafeHtml.concat( |
| 594 |
goog.html.SafeHtml.create( |
| 595 |
'b', {'style': goog.string.Const.from('padding:0 1em 0 .5em')}, |
| 596 |
'?'), |
| 597 |
goog.html.SafeHtml.create( |
| 598 |
'span', |
| 599 |
{'style': goog.string.Const.from('display:none;color:#666')}))); |
| 600 |
ret.onclick = this.boundHelpOnClickHandler_; |
| 601 |
// IE<9 doesn't support lastElementChild. |
| 602 |
var descriptionElem = /** @type {!Element} */ (ret.lastChild); |
| 603 |
if (entry.isRestartRequired()) { |
| 604 |
goog.dom.setTextContent(descriptionElem, entry.description); |
| 605 |
} else { |
| 606 |
goog.dom.safe.setInnerHtml( |
| 607 |
descriptionElem, |
| 608 |
goog.html.SafeHtml.concat( |
| 609 |
goog.html.SafeHtml.htmlEscape(entry.description), |
| 610 |
goog.html.SafeHtml.create( |
| 611 |
'span', {'style': goog.string.Const.from('color: blue')}, |
| 612 |
'(no restart required)'))); |
| 613 |
} |
| 614 |
return ret; |
| 615 |
}; |
| 616 |
|
| 617 |
|
| 618 |
/** |
| 619 |
* Show all entry descriptions (has the same effect as clicking on all ?'s). |
| 620 |
*/ |
| 621 |
goog.tweak.EntriesPanel.prototype.toggleAllDescriptions = function() { |
| 622 |
var show = !this.showAllDescriptionsState_; |
| 623 |
this.showAllDescriptionsState_ = show; |
| 624 |
var entryDivs = this.domHelper_.getElementsByTagNameAndClass( |
| 625 |
goog.dom.TagName.DIV, goog.tweak.TweakUi.ENTRY_CSS_CLASS_, |
| 626 |
this.rootElem_); |
| 627 |
for (var i = 0, div; div = entryDivs[i]; i++) { |
| 628 |
this.showDescription_(div, show); |
| 629 |
} |
| 630 |
}; |
| 631 |
|
| 632 |
|
| 633 |
/** |
| 634 |
* Creates the DOM element to control the given enum setting. |
| 635 |
* @param {!goog.tweak.StringSetting|!goog.tweak.NumericSetting} tweak The |
| 636 |
* setting. |
| 637 |
* @param {string} label The label for the entry. |
| 638 |
* @param {!Function} onchangeFunc onchange event handler. |
| 639 |
* @return {!DocumentFragment} The DOM element. |
| 640 |
* @private |
| 641 |
*/ |
| 642 |
goog.tweak.EntriesPanel.prototype.createComboBoxDom_ = function( |
| 643 |
tweak, label, onchangeFunc) { |
| 644 |
// The markup looks like: |
| 645 |
// Label: <select><option></option></select> |
| 646 |
var dh = this.domHelper_; |
| 647 |
var ret = dh.getDocument().createDocumentFragment(); |
| 648 |
ret.appendChild(dh.createTextNode(label + ': ')); |
| 649 |
var selectElem = dh.createElement(goog.dom.TagName.SELECT); |
| 650 |
var values = tweak.getValidValues(); |
| 651 |
for (var i = 0, il = values.length; i < il; ++i) { |
| 652 |
var optionElem = dh.createElement(goog.dom.TagName.OPTION); |
| 653 |
optionElem.text = String(values[i]); |
| 654 |
// Setting the option tag's value is required for selectElem.value to work |
| 655 |
// properly. |
| 656 |
optionElem.value = String(values[i]); |
| 657 |
selectElem.appendChild(optionElem); |
| 658 |
} |
| 659 |
ret.appendChild(selectElem); |
| 660 |
|
| 661 |
// Set the value and add a callback. |
| 662 |
selectElem.value = String(tweak.getNewValue()); |
| 663 |
selectElem.onchange = onchangeFunc; |
| 664 |
tweak.addCallback(function() { |
| 665 |
selectElem.value = String(tweak.getNewValue()); |
| 666 |
}); |
| 667 |
return ret; |
| 668 |
}; |
| 669 |
|
| 670 |
|
| 671 |
/** |
| 672 |
* Creates the DOM element to control the given boolean setting. |
| 673 |
* @param {!goog.tweak.BooleanSetting} tweak The setting. |
| 674 |
* @param {string} label The label for the entry. |
| 675 |
* @return {!DocumentFragment} The DOM elements. |
| 676 |
* @private |
| 677 |
*/ |
| 678 |
goog.tweak.EntriesPanel.prototype.createBooleanSettingDom_ = function( |
| 679 |
tweak, label) { |
| 680 |
var dh = this.domHelper_; |
| 681 |
var ret = dh.getDocument().createDocumentFragment(); |
| 682 |
var checkbox = dh.createDom(goog.dom.TagName.INPUT, {type: 'checkbox'}); |
| 683 |
ret.appendChild(checkbox); |
| 684 |
ret.appendChild(dh.createTextNode(label)); |
| 685 |
|
| 686 |
// Needed on IE6 to ensure the textbox doesn't get cleared |
| 687 |
// when added to the DOM. |
| 688 |
checkbox.defaultChecked = tweak.getNewValue(); |
| 689 |
|
| 690 |
checkbox.checked = tweak.getNewValue(); |
| 691 |
checkbox.onchange = function() { tweak.setValue(checkbox.checked); }; |
| 692 |
tweak.addCallback(function() { checkbox.checked = tweak.getNewValue(); }); |
| 693 |
return ret; |
| 694 |
}; |
| 695 |
|
| 696 |
|
| 697 |
/** |
| 698 |
* Creates the DOM for a BooleanGroup or NamespaceEntry. |
| 699 |
* @param {!goog.tweak.BooleanGroup|!goog.tweak.NamespaceEntry_} entry The |
| 700 |
* entry. |
| 701 |
* @param {string} label The label for the entry. |
| 702 |
* @param {!Array<goog.tweak.BaseEntry>} childEntries The child entries. |
| 703 |
* @return {!DocumentFragment} The DOM element. |
| 704 |
* @private |
| 705 |
*/ |
| 706 |
goog.tweak.EntriesPanel.prototype.createSubPanelDom_ = function( |
| 707 |
entry, label, childEntries) { |
| 708 |
var dh = this.domHelper_; |
| 709 |
var toggleLink = |
| 710 |
dh.createDom(goog.dom.TagName.A, {href: 'javascript:;'}, label + ' \xBB'); |
| 711 |
var toggleLink2 = |
| 712 |
dh.createDom(goog.dom.TagName.A, {href: 'javascript:;'}, '\xAB ' + label); |
| 713 |
toggleLink2.style.marginRight = '10px'; |
| 714 |
|
| 715 |
var innerUi = new goog.tweak.EntriesPanel(childEntries, dh); |
| 716 |
this.childPanels[entry.getId()] = innerUi; |
| 717 |
|
| 718 |
var elem = innerUi.render(); |
| 719 |
// Move the toggle descriptions link into the legend. |
| 720 |
var descriptionsLink = elem.firstChild; |
| 721 |
var childrenElem = dh.createDom( |
| 722 |
goog.dom.TagName.FIELDSET, goog.getCssName('goog-inline-block'), |
| 723 |
dh.createDom( |
| 724 |
goog.dom.TagName.LEGEND, null, toggleLink2, descriptionsLink), |
| 725 |
elem); |
| 726 |
|
| 727 |
new goog.ui.Zippy( |
| 728 |
toggleLink, childrenElem, false /* expanded */, toggleLink2); |
| 729 |
|
| 730 |
var ret = dh.getDocument().createDocumentFragment(); |
| 731 |
ret.appendChild(toggleLink); |
| 732 |
ret.appendChild(childrenElem); |
| 733 |
return ret; |
| 734 |
}; |
| 735 |
|
| 736 |
|
| 737 |
/** |
| 738 |
* Creates the DOM element to control the given string setting. |
| 739 |
* @param {!goog.tweak.StringSetting|!goog.tweak.NumericSetting} tweak The |
| 740 |
* setting. |
| 741 |
* @param {string} label The label for the entry. |
| 742 |
* @param {!Function} onchangeFunc onchange event handler. |
| 743 |
* @return {!DocumentFragment} The DOM element. |
| 744 |
* @private |
| 745 |
*/ |
| 746 |
goog.tweak.EntriesPanel.prototype.createTextBoxDom_ = function( |
| 747 |
tweak, label, onchangeFunc) { |
| 748 |
var dh = this.domHelper_; |
| 749 |
var ret = dh.getDocument().createDocumentFragment(); |
| 750 |
ret.appendChild(dh.createTextNode(label + ': ')); |
| 751 |
var textBox = dh.createDom(goog.dom.TagName.INPUT, { |
| 752 |
value: String(tweak.getNewValue()), |
| 753 |
// TODO(agrieve): Make size configurable or autogrow. |
| 754 |
size: 5, |
| 755 |
onblur: onchangeFunc |
| 756 |
}); |
| 757 |
ret.appendChild(textBox); |
| 758 |
tweak.addCallback(function() { |
| 759 |
textBox.value = String(tweak.getNewValue()); |
| 760 |
}); |
| 761 |
return ret; |
| 762 |
}; |
| 763 |
|
| 764 |
|
| 765 |
/** |
| 766 |
* Creates the DOM element to control the given button action. |
| 767 |
* @param {!goog.tweak.ButtonAction} tweak The action. |
| 768 |
* @param {string} label The label for the entry. |
| 769 |
* @return {!Element} The DOM element. |
| 770 |
* @private |
| 771 |
*/ |
| 772 |
goog.tweak.EntriesPanel.prototype.createButtonActionDom_ = function( |
| 773 |
tweak, label) { |
| 774 |
return this.domHelper_.createDom( |
| 775 |
goog.dom.TagName.BUTTON, {onclick: goog.bind(tweak.fireCallbacks, tweak)}, |
| 776 |
label); |
| 777 |
}; |
| 778 |
|
| 779 |
|
| 780 |
/** |
| 781 |
* Creates the DOM element to control the given entry. |
| 782 |
* @param {!goog.tweak.BaseEntry} entry The entry. |
| 783 |
* @return {!Element|!DocumentFragment} The DOM element. |
| 784 |
* @private |
| 785 |
*/ |
| 786 |
goog.tweak.EntriesPanel.prototype.createTweakEntryDom_ = function(entry) { |
| 787 |
var label = goog.tweak.TweakUi.getNamespacedLabel_(entry); |
| 788 |
if (entry instanceof goog.tweak.BooleanSetting) { |
| 789 |
return this.createBooleanSettingDom_(entry, label); |
| 790 |
} else if (entry instanceof goog.tweak.BooleanGroup) { |
| 791 |
var childEntries = goog.tweak.TweakUi.extractBooleanGroupEntries_(entry); |
| 792 |
return this.createSubPanelDom_(entry, label, childEntries); |
| 793 |
} else if (entry instanceof goog.tweak.StringSetting) { |
| 794 |
/** @this {Element} */ |
| 795 |
var setValueFunc = function() { entry.setValue(this.value); }; |
| 796 |
return entry.getValidValues() ? |
| 797 |
this.createComboBoxDom_(entry, label, setValueFunc) : |
| 798 |
this.createTextBoxDom_(entry, label, setValueFunc); |
| 799 |
} else if (entry instanceof goog.tweak.NumericSetting) { |
| 800 |
setValueFunc = function() { |
| 801 |
// Reset the value if it's not a number. |
| 802 |
if (isNaN(this.value)) { |
| 803 |
this.value = entry.getNewValue(); |
| 804 |
} else { |
| 805 |
entry.setValue(+this.value); |
| 806 |
} |
| 807 |
}; |
| 808 |
return entry.getValidValues() ? |
| 809 |
this.createComboBoxDom_(entry, label, setValueFunc) : |
| 810 |
this.createTextBoxDom_(entry, label, setValueFunc); |
| 811 |
} else if (entry instanceof goog.tweak.NamespaceEntry_) { |
| 812 |
return this.createSubPanelDom_(entry, entry.label, entry.entries); |
| 813 |
} |
| 814 |
goog.asserts.assertInstanceof( |
| 815 |
entry, goog.tweak.ButtonAction, 'invalid entry: %s', entry); |
| 816 |
return this.createButtonActionDom_( |
| 817 |
/** @type {!goog.tweak.ButtonAction} */ (entry), label); |
| 818 |
}; |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
/** |
| 823 |
* Entries used to represent the collapsible namespace links. These entries are |
| 824 |
* never registered with the TweakRegistry, but are contained within the |
| 825 |
* collection of entries within TweakPanels. |
| 826 |
* @param {string} namespace The namespace for the entry. |
| 827 |
* @param {!Array<!goog.tweak.BaseEntry>} entries Entries within the namespace. |
| 828 |
* @constructor |
| 829 |
* @extends {goog.tweak.BaseEntry} |
| 830 |
* @private |
| 831 |
*/ |
| 832 |
goog.tweak.NamespaceEntry_ = function(namespace, entries) { |
| 833 |
goog.tweak.BaseEntry.call( |
| 834 |
this, goog.tweak.NamespaceEntry_.ID_PREFIX + namespace, |
| 835 |
'Tweaks within the ' + namespace + ' namespace.'); |
| 836 |
|
| 837 |
/** |
| 838 |
* Entries within this namespace. |
| 839 |
* @type {!Array<!goog.tweak.BaseEntry>} |
| 840 |
*/ |
| 841 |
this.entries = entries; |
| 842 |
|
| 843 |
this.label = namespace; |
| 844 |
}; |
| 845 |
goog.inherits(goog.tweak.NamespaceEntry_, goog.tweak.BaseEntry); |
| 846 |
|
| 847 |
|
| 848 |
/** |
| 849 |
* Prefix for the IDs of namespace entries used to ensure that they do not |
| 850 |
* conflict with regular entries. |
| 851 |
* @type {string} |
| 852 |
*/ |
| 853 |
goog.tweak.NamespaceEntry_.ID_PREFIX = '!'; |