toolkit/content/customizeToolbar.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 4
michael@0 5 var gToolboxDocument = null;
michael@0 6 var gToolbox = null;
michael@0 7 var gCurrentDragOverItem = null;
michael@0 8 var gToolboxChanged = false;
michael@0 9 var gToolboxSheet = false;
michael@0 10 var gPaletteBox = null;
michael@0 11
michael@0 12 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 13
michael@0 14 function onLoad()
michael@0 15 {
michael@0 16 if ("arguments" in window && window.arguments[0]) {
michael@0 17 InitWithToolbox(window.arguments[0]);
michael@0 18 repositionDialog(window);
michael@0 19 }
michael@0 20 else if (window.frameElement &&
michael@0 21 "toolbox" in window.frameElement) {
michael@0 22 gToolboxSheet = true;
michael@0 23 InitWithToolbox(window.frameElement.toolbox);
michael@0 24 repositionDialog(window.frameElement.panel);
michael@0 25 }
michael@0 26 }
michael@0 27
michael@0 28 function InitWithToolbox(aToolbox)
michael@0 29 {
michael@0 30 gToolbox = aToolbox;
michael@0 31 dispatchCustomizationEvent("beforecustomization");
michael@0 32 gToolboxDocument = gToolbox.ownerDocument;
michael@0 33 gToolbox.customizing = true;
michael@0 34 forEachCustomizableToolbar(function (toolbar) {
michael@0 35 toolbar.setAttribute("customizing", "true");
michael@0 36 });
michael@0 37 gPaletteBox = document.getElementById("palette-box");
michael@0 38
michael@0 39 var elts = getRootElements();
michael@0 40 for (let i=0; i < elts.length; i++) {
michael@0 41 elts[i].addEventListener("dragstart", onToolbarDragStart, true);
michael@0 42 elts[i].addEventListener("dragover", onToolbarDragOver, true);
michael@0 43 elts[i].addEventListener("dragexit", onToolbarDragExit, true);
michael@0 44 elts[i].addEventListener("drop", onToolbarDrop, true);
michael@0 45 }
michael@0 46
michael@0 47 initDialog();
michael@0 48 }
michael@0 49
michael@0 50 function onClose()
michael@0 51 {
michael@0 52 if (!gToolboxSheet)
michael@0 53 window.close();
michael@0 54 else
michael@0 55 finishToolbarCustomization();
michael@0 56 }
michael@0 57
michael@0 58 function onUnload()
michael@0 59 {
michael@0 60 if (!gToolboxSheet)
michael@0 61 finishToolbarCustomization();
michael@0 62 }
michael@0 63
michael@0 64 function finishToolbarCustomization()
michael@0 65 {
michael@0 66 removeToolboxListeners();
michael@0 67 unwrapToolbarItems();
michael@0 68 persistCurrentSets();
michael@0 69 gToolbox.customizing = false;
michael@0 70 forEachCustomizableToolbar(function (toolbar) {
michael@0 71 toolbar.removeAttribute("customizing");
michael@0 72 });
michael@0 73
michael@0 74 notifyParentComplete();
michael@0 75 }
michael@0 76
michael@0 77 function initDialog()
michael@0 78 {
michael@0 79 if (!gToolbox.toolbarset) {
michael@0 80 document.getElementById("newtoolbar").hidden = true;
michael@0 81 }
michael@0 82
michael@0 83 var mode = gToolbox.getAttribute("mode");
michael@0 84 document.getElementById("modelist").value = mode;
michael@0 85 var smallIconsCheckbox = document.getElementById("smallicons");
michael@0 86 smallIconsCheckbox.checked = gToolbox.getAttribute("iconsize") == "small";
michael@0 87 if (mode == "text")
michael@0 88 smallIconsCheckbox.disabled = true;
michael@0 89
michael@0 90 // Build up the palette of other items.
michael@0 91 buildPalette();
michael@0 92
michael@0 93 // Wrap all the items on the toolbar in toolbarpaletteitems.
michael@0 94 wrapToolbarItems();
michael@0 95 }
michael@0 96
michael@0 97 function repositionDialog(aWindow)
michael@0 98 {
michael@0 99 // Position the dialog touching the bottom of the toolbox and centered with
michael@0 100 // it.
michael@0 101 if (!aWindow)
michael@0 102 return;
michael@0 103
michael@0 104 var width;
michael@0 105 if (aWindow != window)
michael@0 106 width = aWindow.getBoundingClientRect().width;
michael@0 107 else if (document.documentElement.hasAttribute("width"))
michael@0 108 width = document.documentElement.getAttribute("width");
michael@0 109 else
michael@0 110 width = parseInt(document.documentElement.style.width);
michael@0 111 var screenX = gToolbox.boxObject.screenX
michael@0 112 + ((gToolbox.boxObject.width - width) / 2);
michael@0 113 var screenY = gToolbox.boxObject.screenY + gToolbox.boxObject.height;
michael@0 114
michael@0 115 aWindow.moveTo(screenX, screenY);
michael@0 116 }
michael@0 117
michael@0 118 function removeToolboxListeners()
michael@0 119 {
michael@0 120 var elts = getRootElements();
michael@0 121 for (let i=0; i < elts.length; i++) {
michael@0 122 elts[i].removeEventListener("dragstart", onToolbarDragStart, true);
michael@0 123 elts[i].removeEventListener("dragover", onToolbarDragOver, true);
michael@0 124 elts[i].removeEventListener("dragexit", onToolbarDragExit, true);
michael@0 125 elts[i].removeEventListener("drop", onToolbarDrop, true);
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 /**
michael@0 130 * Invoke a callback on the toolbox to notify it that the dialog is done
michael@0 131 * and going away.
michael@0 132 */
michael@0 133 function notifyParentComplete()
michael@0 134 {
michael@0 135 if ("customizeDone" in gToolbox)
michael@0 136 gToolbox.customizeDone(gToolboxChanged);
michael@0 137 dispatchCustomizationEvent("aftercustomization");
michael@0 138 }
michael@0 139
michael@0 140 function toolboxChanged(aType)
michael@0 141 {
michael@0 142 gToolboxChanged = true;
michael@0 143 if ("customizeChange" in gToolbox)
michael@0 144 gToolbox.customizeChange(aType);
michael@0 145 dispatchCustomizationEvent("customizationchange");
michael@0 146 }
michael@0 147
michael@0 148 function dispatchCustomizationEvent(aEventName) {
michael@0 149 var evt = document.createEvent("Events");
michael@0 150 evt.initEvent(aEventName, true, true);
michael@0 151 gToolbox.dispatchEvent(evt);
michael@0 152 }
michael@0 153
michael@0 154 /**
michael@0 155 * Persist the current set of buttons in all customizable toolbars to
michael@0 156 * localstore.
michael@0 157 */
michael@0 158 function persistCurrentSets()
michael@0 159 {
michael@0 160 if (!gToolboxChanged || gToolboxDocument.defaultView.closed)
michael@0 161 return;
michael@0 162
michael@0 163 var customCount = 0;
michael@0 164 forEachCustomizableToolbar(function (toolbar) {
michael@0 165 // Calculate currentset and store it in the attribute.
michael@0 166 var currentSet = toolbar.currentSet;
michael@0 167 toolbar.setAttribute("currentset", currentSet);
michael@0 168
michael@0 169 var customIndex = toolbar.hasAttribute("customindex");
michael@0 170 if (customIndex) {
michael@0 171 if (!toolbar.hasChildNodes()) {
michael@0 172 // Remove custom toolbars whose contents have been removed.
michael@0 173 gToolbox.removeChild(toolbar);
michael@0 174 } else if (gToolbox.toolbarset) {
michael@0 175 // Persist custom toolbar info on the <toolbarset/>
michael@0 176 gToolbox.toolbarset.setAttribute("toolbar"+(++customCount),
michael@0 177 toolbar.toolbarName + ":" + currentSet);
michael@0 178 gToolboxDocument.persist(gToolbox.toolbarset.id, "toolbar"+customCount);
michael@0 179 }
michael@0 180 }
michael@0 181
michael@0 182 if (!customIndex) {
michael@0 183 // Persist the currentset attribute directly on hardcoded toolbars.
michael@0 184 gToolboxDocument.persist(toolbar.id, "currentset");
michael@0 185 }
michael@0 186 });
michael@0 187
michael@0 188 // Remove toolbarX attributes for removed toolbars.
michael@0 189 while (gToolbox.toolbarset && gToolbox.toolbarset.hasAttribute("toolbar"+(++customCount))) {
michael@0 190 gToolbox.toolbarset.removeAttribute("toolbar"+customCount);
michael@0 191 gToolboxDocument.persist(gToolbox.toolbarset.id, "toolbar"+customCount);
michael@0 192 }
michael@0 193 }
michael@0 194
michael@0 195 /**
michael@0 196 * Wraps all items in all customizable toolbars in a toolbox.
michael@0 197 */
michael@0 198 function wrapToolbarItems()
michael@0 199 {
michael@0 200 forEachCustomizableToolbar(function (toolbar) {
michael@0 201 Array.forEach(toolbar.childNodes, function (item) {
michael@0 202 #ifdef XP_MACOSX
michael@0 203 if (item.firstChild && item.firstChild.localName == "menubar")
michael@0 204 return;
michael@0 205 #endif
michael@0 206 if (isToolbarItem(item)) {
michael@0 207 let wrapper = wrapToolbarItem(item);
michael@0 208 cleanupItemForToolbar(item, wrapper);
michael@0 209 }
michael@0 210 });
michael@0 211 });
michael@0 212 }
michael@0 213
michael@0 214 function getRootElements()
michael@0 215 {
michael@0 216 return [gToolbox].concat(gToolbox.externalToolbars);
michael@0 217 }
michael@0 218
michael@0 219 /**
michael@0 220 * Unwraps all items in all customizable toolbars in a toolbox.
michael@0 221 */
michael@0 222 function unwrapToolbarItems()
michael@0 223 {
michael@0 224 let elts = getRootElements();
michael@0 225 for (let i=0; i < elts.length; i++) {
michael@0 226 let paletteItems = elts[i].getElementsByTagName("toolbarpaletteitem");
michael@0 227 let paletteItem;
michael@0 228 while ((paletteItem = paletteItems.item(0)) != null) {
michael@0 229 let toolbarItem = paletteItem.firstChild;
michael@0 230 restoreItemForToolbar(toolbarItem, paletteItem);
michael@0 231 paletteItem.parentNode.replaceChild(toolbarItem, paletteItem);
michael@0 232 }
michael@0 233 }
michael@0 234 }
michael@0 235
michael@0 236 /**
michael@0 237 * Creates a wrapper that can be used to contain a toolbaritem and prevent
michael@0 238 * it from receiving UI events.
michael@0 239 */
michael@0 240 function createWrapper(aId, aDocument)
michael@0 241 {
michael@0 242 var wrapper = aDocument.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
michael@0 243 "toolbarpaletteitem");
michael@0 244
michael@0 245 wrapper.id = "wrapper-"+aId;
michael@0 246 return wrapper;
michael@0 247 }
michael@0 248
michael@0 249 /**
michael@0 250 * Wraps an item that has been cloned from a template and adds
michael@0 251 * it to the end of the palette.
michael@0 252 */
michael@0 253 function wrapPaletteItem(aPaletteItem)
michael@0 254 {
michael@0 255 var wrapper = createWrapper(aPaletteItem.id, document);
michael@0 256
michael@0 257 wrapper.appendChild(aPaletteItem);
michael@0 258
michael@0 259 // XXX We need to call this AFTER the palette item has been appended
michael@0 260 // to the wrapper or else we crash dropping certain buttons on the
michael@0 261 // palette due to removal of the command and disabled attributes - JRH
michael@0 262 cleanUpItemForPalette(aPaletteItem, wrapper);
michael@0 263
michael@0 264 gPaletteBox.appendChild(wrapper);
michael@0 265 }
michael@0 266
michael@0 267 /**
michael@0 268 * Wraps an item that is currently on a toolbar and replaces the item
michael@0 269 * with the wrapper. This is not used when dropping items from the palette,
michael@0 270 * only when first starting the dialog and wrapping everything on the toolbars.
michael@0 271 */
michael@0 272 function wrapToolbarItem(aToolbarItem)
michael@0 273 {
michael@0 274 var wrapper = createWrapper(aToolbarItem.id, gToolboxDocument);
michael@0 275
michael@0 276 wrapper.flex = aToolbarItem.flex;
michael@0 277
michael@0 278 aToolbarItem.parentNode.replaceChild(wrapper, aToolbarItem);
michael@0 279
michael@0 280 wrapper.appendChild(aToolbarItem);
michael@0 281
michael@0 282 return wrapper;
michael@0 283 }
michael@0 284
michael@0 285 /**
michael@0 286 * Get the list of ids for the current set of items on each toolbar.
michael@0 287 */
michael@0 288 function getCurrentItemIds()
michael@0 289 {
michael@0 290 var currentItems = {};
michael@0 291 forEachCustomizableToolbar(function (toolbar) {
michael@0 292 var child = toolbar.firstChild;
michael@0 293 while (child) {
michael@0 294 if (isToolbarItem(child))
michael@0 295 currentItems[child.id] = 1;
michael@0 296 child = child.nextSibling;
michael@0 297 }
michael@0 298 });
michael@0 299 return currentItems;
michael@0 300 }
michael@0 301
michael@0 302 /**
michael@0 303 * Builds the palette of draggable items that are not yet in a toolbar.
michael@0 304 */
michael@0 305 function buildPalette()
michael@0 306 {
michael@0 307 // Empty the palette first.
michael@0 308 while (gPaletteBox.lastChild)
michael@0 309 gPaletteBox.removeChild(gPaletteBox.lastChild);
michael@0 310
michael@0 311 // Add the toolbar separator item.
michael@0 312 var templateNode = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
michael@0 313 "toolbarseparator");
michael@0 314 templateNode.id = "separator";
michael@0 315 wrapPaletteItem(templateNode);
michael@0 316
michael@0 317 // Add the toolbar spring item.
michael@0 318 templateNode = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
michael@0 319 "toolbarspring");
michael@0 320 templateNode.id = "spring";
michael@0 321 templateNode.flex = 1;
michael@0 322 wrapPaletteItem(templateNode);
michael@0 323
michael@0 324 // Add the toolbar spacer item.
michael@0 325 templateNode = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
michael@0 326 "toolbarspacer");
michael@0 327 templateNode.id = "spacer";
michael@0 328 templateNode.flex = 1;
michael@0 329 wrapPaletteItem(templateNode);
michael@0 330
michael@0 331 var currentItems = getCurrentItemIds();
michael@0 332 templateNode = gToolbox.palette.firstChild;
michael@0 333 while (templateNode) {
michael@0 334 // Check if the item is already in a toolbar before adding it to the palette.
michael@0 335 if (!(templateNode.id in currentItems)) {
michael@0 336 var paletteItem = document.importNode(templateNode, true);
michael@0 337 wrapPaletteItem(paletteItem);
michael@0 338 }
michael@0 339
michael@0 340 templateNode = templateNode.nextSibling;
michael@0 341 }
michael@0 342 }
michael@0 343
michael@0 344 /**
michael@0 345 * Makes sure that an item that has been cloned from a template
michael@0 346 * is stripped of any attributes that may adversely affect its
michael@0 347 * appearance in the palette.
michael@0 348 */
michael@0 349 function cleanUpItemForPalette(aItem, aWrapper)
michael@0 350 {
michael@0 351 aWrapper.setAttribute("place", "palette");
michael@0 352 setWrapperType(aItem, aWrapper);
michael@0 353
michael@0 354 if (aItem.hasAttribute("title"))
michael@0 355 aWrapper.setAttribute("title", aItem.getAttribute("title"));
michael@0 356 else if (aItem.hasAttribute("label"))
michael@0 357 aWrapper.setAttribute("title", aItem.getAttribute("label"));
michael@0 358 else if (isSpecialItem(aItem)) {
michael@0 359 var stringBundle = document.getElementById("stringBundle");
michael@0 360 // Remove the common "toolbar" prefix to generate the string name.
michael@0 361 var title = stringBundle.getString(aItem.localName.slice(7) + "Title");
michael@0 362 aWrapper.setAttribute("title", title);
michael@0 363 }
michael@0 364 aWrapper.setAttribute("tooltiptext", aWrapper.getAttribute("title"));
michael@0 365
michael@0 366 // Remove attributes that screw up our appearance.
michael@0 367 aItem.removeAttribute("command");
michael@0 368 aItem.removeAttribute("observes");
michael@0 369 aItem.removeAttribute("type");
michael@0 370 aItem.removeAttribute("width");
michael@0 371
michael@0 372 Array.forEach(aWrapper.querySelectorAll("[disabled]"), function(aNode) {
michael@0 373 aNode.removeAttribute("disabled");
michael@0 374 });
michael@0 375 }
michael@0 376
michael@0 377 /**
michael@0 378 * Makes sure that an item that has been cloned from a template
michael@0 379 * is stripped of all properties that may adversely affect its
michael@0 380 * appearance in the toolbar. Store critical properties on the
michael@0 381 * wrapper so they can be put back on the item when we're done.
michael@0 382 */
michael@0 383 function cleanupItemForToolbar(aItem, aWrapper)
michael@0 384 {
michael@0 385 setWrapperType(aItem, aWrapper);
michael@0 386 aWrapper.setAttribute("place", "toolbar");
michael@0 387
michael@0 388 if (aItem.hasAttribute("command")) {
michael@0 389 aWrapper.setAttribute("itemcommand", aItem.getAttribute("command"));
michael@0 390 aItem.removeAttribute("command");
michael@0 391 }
michael@0 392
michael@0 393 if (aItem.checked) {
michael@0 394 aWrapper.setAttribute("itemchecked", "true");
michael@0 395 aItem.checked = false;
michael@0 396 }
michael@0 397
michael@0 398 if (aItem.disabled) {
michael@0 399 aWrapper.setAttribute("itemdisabled", "true");
michael@0 400 aItem.disabled = false;
michael@0 401 }
michael@0 402 }
michael@0 403
michael@0 404 /**
michael@0 405 * Restore all the properties that we stripped off above.
michael@0 406 */
michael@0 407 function restoreItemForToolbar(aItem, aWrapper)
michael@0 408 {
michael@0 409 if (aWrapper.hasAttribute("itemdisabled"))
michael@0 410 aItem.disabled = true;
michael@0 411
michael@0 412 if (aWrapper.hasAttribute("itemchecked"))
michael@0 413 aItem.checked = true;
michael@0 414
michael@0 415 if (aWrapper.hasAttribute("itemcommand")) {
michael@0 416 let commandID = aWrapper.getAttribute("itemcommand");
michael@0 417 aItem.setAttribute("command", commandID);
michael@0 418
michael@0 419 //XXX Bug 309953 - toolbarbuttons aren't in sync with their commands after customizing
michael@0 420 let command = gToolboxDocument.getElementById(commandID);
michael@0 421 if (command && command.hasAttribute("disabled"))
michael@0 422 aItem.setAttribute("disabled", command.getAttribute("disabled"));
michael@0 423 }
michael@0 424 }
michael@0 425
michael@0 426 function setWrapperType(aItem, aWrapper)
michael@0 427 {
michael@0 428 if (aItem.localName == "toolbarseparator") {
michael@0 429 aWrapper.setAttribute("type", "separator");
michael@0 430 } else if (aItem.localName == "toolbarspring") {
michael@0 431 aWrapper.setAttribute("type", "spring");
michael@0 432 } else if (aItem.localName == "toolbarspacer") {
michael@0 433 aWrapper.setAttribute("type", "spacer");
michael@0 434 } else if (aItem.localName == "toolbaritem" && aItem.firstChild) {
michael@0 435 aWrapper.setAttribute("type", aItem.firstChild.localName);
michael@0 436 }
michael@0 437 }
michael@0 438
michael@0 439 function setDragActive(aItem, aValue)
michael@0 440 {
michael@0 441 var node = aItem;
michael@0 442 var direction = window.getComputedStyle(aItem, null).direction;
michael@0 443 var value = direction == "ltr"? "left" : "right";
michael@0 444 if (aItem.localName == "toolbar") {
michael@0 445 node = aItem.lastChild;
michael@0 446 value = direction == "ltr"? "right" : "left";
michael@0 447 }
michael@0 448
michael@0 449 if (!node)
michael@0 450 return;
michael@0 451
michael@0 452 if (aValue) {
michael@0 453 if (!node.hasAttribute("dragover"))
michael@0 454 node.setAttribute("dragover", value);
michael@0 455 } else {
michael@0 456 node.removeAttribute("dragover");
michael@0 457 }
michael@0 458 }
michael@0 459
michael@0 460 function addNewToolbar()
michael@0 461 {
michael@0 462 var promptService = Services.prompt;
michael@0 463 var stringBundle = document.getElementById("stringBundle");
michael@0 464 var message = stringBundle.getString("enterToolbarName");
michael@0 465 var title = stringBundle.getString("enterToolbarTitle");
michael@0 466
michael@0 467 var name = {};
michael@0 468
michael@0 469 // Quitting from the toolbar dialog while the new toolbar prompt is up
michael@0 470 // can cause things to become unresponsive on the Mac. Until dialog modality
michael@0 471 // is fixed (395465), disable the "Done" button explicitly.
michael@0 472 var doneButton = document.getElementById("donebutton");
michael@0 473 doneButton.disabled = true;
michael@0 474
michael@0 475 while (true) {
michael@0 476
michael@0 477 if (!promptService.prompt(window, title, message, name, null, {})) {
michael@0 478 doneButton.disabled = false;
michael@0 479 return;
michael@0 480 }
michael@0 481
michael@0 482 if (!name.value) {
michael@0 483 message = stringBundle.getFormattedString("enterToolbarBlank", [name.value]);
michael@0 484 continue;
michael@0 485 }
michael@0 486
michael@0 487 var dupeFound = false;
michael@0 488
michael@0 489 // Check for an existing toolbar with the same display name
michael@0 490 for (let i = 0; i < gToolbox.childNodes.length; ++i) {
michael@0 491 var toolbar = gToolbox.childNodes[i];
michael@0 492 var toolbarName = toolbar.getAttribute("toolbarname");
michael@0 493
michael@0 494 if (toolbarName == name.value &&
michael@0 495 toolbar.getAttribute("type") != "menubar" &&
michael@0 496 toolbar.nodeName == 'toolbar') {
michael@0 497 dupeFound = true;
michael@0 498 break;
michael@0 499 }
michael@0 500 }
michael@0 501
michael@0 502 if (!dupeFound)
michael@0 503 break;
michael@0 504
michael@0 505 message = stringBundle.getFormattedString("enterToolbarDup", [name.value]);
michael@0 506 }
michael@0 507
michael@0 508 gToolbox.appendCustomToolbar(name.value, "");
michael@0 509
michael@0 510 toolboxChanged();
michael@0 511
michael@0 512 doneButton.disabled = false;
michael@0 513 }
michael@0 514
michael@0 515 /**
michael@0 516 * Restore the default set of buttons to fixed toolbars,
michael@0 517 * remove all custom toolbars, and rebuild the palette.
michael@0 518 */
michael@0 519 function restoreDefaultSet()
michael@0 520 {
michael@0 521 // Unwrap the items on the toolbar.
michael@0 522 unwrapToolbarItems();
michael@0 523
michael@0 524 // Remove all of the customized toolbars.
michael@0 525 var child = gToolbox.lastChild;
michael@0 526 while (child) {
michael@0 527 if (child.hasAttribute("customindex")) {
michael@0 528 var thisChild = child;
michael@0 529 child = child.previousSibling;
michael@0 530 thisChild.currentSet = "__empty";
michael@0 531 gToolbox.removeChild(thisChild);
michael@0 532 } else {
michael@0 533 child = child.previousSibling;
michael@0 534 }
michael@0 535 }
michael@0 536
michael@0 537 // Restore the defaultset for fixed toolbars.
michael@0 538 forEachCustomizableToolbar(function (toolbar) {
michael@0 539 var defaultSet = toolbar.getAttribute("defaultset");
michael@0 540 if (defaultSet)
michael@0 541 toolbar.currentSet = defaultSet;
michael@0 542 });
michael@0 543
michael@0 544 // Restore the default icon size and mode.
michael@0 545 document.getElementById("smallicons").checked = (updateIconSize() == "small");
michael@0 546 document.getElementById("modelist").value = updateToolbarMode();
michael@0 547
michael@0 548 // Now rebuild the palette.
michael@0 549 buildPalette();
michael@0 550
michael@0 551 // Now re-wrap the items on the toolbar.
michael@0 552 wrapToolbarItems();
michael@0 553
michael@0 554 toolboxChanged("reset");
michael@0 555 }
michael@0 556
michael@0 557 function updateIconSize(aSize) {
michael@0 558 return updateToolboxProperty("iconsize", aSize, "large");
michael@0 559 }
michael@0 560
michael@0 561 function updateToolbarMode(aModeValue) {
michael@0 562 var mode = updateToolboxProperty("mode", aModeValue, "icons");
michael@0 563
michael@0 564 var iconSizeCheckbox = document.getElementById("smallicons");
michael@0 565 iconSizeCheckbox.disabled = mode == "text";
michael@0 566
michael@0 567 return mode;
michael@0 568 }
michael@0 569
michael@0 570 function updateToolboxProperty(aProp, aValue, aToolkitDefault) {
michael@0 571 var toolboxDefault = gToolbox.getAttribute("default" + aProp) ||
michael@0 572 aToolkitDefault;
michael@0 573
michael@0 574 gToolbox.setAttribute(aProp, aValue || toolboxDefault);
michael@0 575 gToolboxDocument.persist(gToolbox.id, aProp);
michael@0 576
michael@0 577 forEachCustomizableToolbar(function (toolbar) {
michael@0 578 var toolbarDefault = toolbar.getAttribute("default" + aProp) ||
michael@0 579 toolboxDefault;
michael@0 580 if (toolbar.getAttribute("lock" + aProp) == "true" &&
michael@0 581 toolbar.getAttribute(aProp) == toolbarDefault)
michael@0 582 return;
michael@0 583
michael@0 584 toolbar.setAttribute(aProp, aValue || toolbarDefault);
michael@0 585 gToolboxDocument.persist(toolbar.id, aProp);
michael@0 586 });
michael@0 587
michael@0 588 toolboxChanged(aProp);
michael@0 589
michael@0 590 return aValue || toolboxDefault;
michael@0 591 }
michael@0 592
michael@0 593 function forEachCustomizableToolbar(callback) {
michael@0 594 Array.filter(gToolbox.childNodes, isCustomizableToolbar).forEach(callback);
michael@0 595 Array.filter(gToolbox.externalToolbars, isCustomizableToolbar).forEach(callback);
michael@0 596 }
michael@0 597
michael@0 598 function isCustomizableToolbar(aElt)
michael@0 599 {
michael@0 600 return aElt.localName == "toolbar" &&
michael@0 601 aElt.getAttribute("customizable") == "true";
michael@0 602 }
michael@0 603
michael@0 604 function isSpecialItem(aElt)
michael@0 605 {
michael@0 606 return aElt.localName == "toolbarseparator" ||
michael@0 607 aElt.localName == "toolbarspring" ||
michael@0 608 aElt.localName == "toolbarspacer";
michael@0 609 }
michael@0 610
michael@0 611 function isToolbarItem(aElt)
michael@0 612 {
michael@0 613 return aElt.localName == "toolbarbutton" ||
michael@0 614 aElt.localName == "toolbaritem" ||
michael@0 615 aElt.localName == "toolbarseparator" ||
michael@0 616 aElt.localName == "toolbarspring" ||
michael@0 617 aElt.localName == "toolbarspacer";
michael@0 618 }
michael@0 619
michael@0 620 ///////////////////////////////////////////////////////////////////////////
michael@0 621 //// Drag and Drop observers
michael@0 622
michael@0 623 function onToolbarDragExit(aEvent)
michael@0 624 {
michael@0 625 if (isUnwantedDragEvent(aEvent)) {
michael@0 626 return;
michael@0 627 }
michael@0 628
michael@0 629 if (gCurrentDragOverItem)
michael@0 630 setDragActive(gCurrentDragOverItem, false);
michael@0 631 }
michael@0 632
michael@0 633 function onToolbarDragStart(aEvent)
michael@0 634 {
michael@0 635 var item = aEvent.target;
michael@0 636 while (item && item.localName != "toolbarpaletteitem") {
michael@0 637 if (item.localName == "toolbar")
michael@0 638 return;
michael@0 639 item = item.parentNode;
michael@0 640 }
michael@0 641
michael@0 642 item.setAttribute("dragactive", "true");
michael@0 643
michael@0 644 var dt = aEvent.dataTransfer;
michael@0 645 var documentId = gToolboxDocument.documentElement.id;
michael@0 646 dt.setData("text/toolbarwrapper-id/" + documentId, item.firstChild.id);
michael@0 647 dt.effectAllowed = "move";
michael@0 648 }
michael@0 649
michael@0 650 function onToolbarDragOver(aEvent)
michael@0 651 {
michael@0 652 if (isUnwantedDragEvent(aEvent)) {
michael@0 653 return;
michael@0 654 }
michael@0 655
michael@0 656 var documentId = gToolboxDocument.documentElement.id;
michael@0 657 if (!aEvent.dataTransfer.types.contains("text/toolbarwrapper-id/" + documentId.toLowerCase()))
michael@0 658 return;
michael@0 659
michael@0 660 var toolbar = aEvent.target;
michael@0 661 var dropTarget = aEvent.target;
michael@0 662 while (toolbar && toolbar.localName != "toolbar") {
michael@0 663 dropTarget = toolbar;
michael@0 664 toolbar = toolbar.parentNode;
michael@0 665 }
michael@0 666
michael@0 667 // Make sure we are dragging over a customizable toolbar.
michael@0 668 if (!toolbar || !isCustomizableToolbar(toolbar)) {
michael@0 669 gCurrentDragOverItem = null;
michael@0 670 return;
michael@0 671 }
michael@0 672
michael@0 673 var previousDragItem = gCurrentDragOverItem;
michael@0 674
michael@0 675 if (dropTarget.localName == "toolbar") {
michael@0 676 gCurrentDragOverItem = dropTarget;
michael@0 677 } else {
michael@0 678 gCurrentDragOverItem = null;
michael@0 679
michael@0 680 var direction = window.getComputedStyle(dropTarget.parentNode, null).direction;
michael@0 681 var dropTargetCenter = dropTarget.boxObject.x + (dropTarget.boxObject.width / 2);
michael@0 682 var dragAfter;
michael@0 683 if (direction == "ltr")
michael@0 684 dragAfter = aEvent.clientX > dropTargetCenter;
michael@0 685 else
michael@0 686 dragAfter = aEvent.clientX < dropTargetCenter;
michael@0 687
michael@0 688 if (dragAfter) {
michael@0 689 gCurrentDragOverItem = dropTarget.nextSibling;
michael@0 690 if (!gCurrentDragOverItem)
michael@0 691 gCurrentDragOverItem = toolbar;
michael@0 692 } else
michael@0 693 gCurrentDragOverItem = dropTarget;
michael@0 694 }
michael@0 695
michael@0 696 if (previousDragItem && gCurrentDragOverItem != previousDragItem) {
michael@0 697 setDragActive(previousDragItem, false);
michael@0 698 }
michael@0 699
michael@0 700 setDragActive(gCurrentDragOverItem, true);
michael@0 701
michael@0 702 aEvent.preventDefault();
michael@0 703 aEvent.stopPropagation();
michael@0 704 }
michael@0 705
michael@0 706 function onToolbarDrop(aEvent)
michael@0 707 {
michael@0 708 if (isUnwantedDragEvent(aEvent)) {
michael@0 709 return;
michael@0 710 }
michael@0 711
michael@0 712 if (!gCurrentDragOverItem)
michael@0 713 return;
michael@0 714
michael@0 715 setDragActive(gCurrentDragOverItem, false);
michael@0 716
michael@0 717 var documentId = gToolboxDocument.documentElement.id;
michael@0 718 var draggedItemId = aEvent.dataTransfer.getData("text/toolbarwrapper-id/" + documentId);
michael@0 719 if (gCurrentDragOverItem.id == draggedItemId)
michael@0 720 return;
michael@0 721
michael@0 722 var toolbar = aEvent.target;
michael@0 723 while (toolbar.localName != "toolbar")
michael@0 724 toolbar = toolbar.parentNode;
michael@0 725
michael@0 726 var draggedPaletteWrapper = document.getElementById("wrapper-"+draggedItemId);
michael@0 727 if (!draggedPaletteWrapper) {
michael@0 728 // The wrapper has been dragged from the toolbar.
michael@0 729 // Get the wrapper from the toolbar document and make sure that
michael@0 730 // it isn't being dropped on itself.
michael@0 731 var wrapper = gToolboxDocument.getElementById("wrapper-"+draggedItemId);
michael@0 732 if (wrapper == gCurrentDragOverItem)
michael@0 733 return;
michael@0 734
michael@0 735 // Don't allow non-removable kids (e.g., the menubar) to move.
michael@0 736 if (wrapper.firstChild.getAttribute("removable") != "true")
michael@0 737 return;
michael@0 738
michael@0 739 // Remove the item from its place in the toolbar.
michael@0 740 wrapper.parentNode.removeChild(wrapper);
michael@0 741
michael@0 742 // Determine which toolbar we are dropping on.
michael@0 743 var dropToolbar = null;
michael@0 744 if (gCurrentDragOverItem.localName == "toolbar")
michael@0 745 dropToolbar = gCurrentDragOverItem;
michael@0 746 else
michael@0 747 dropToolbar = gCurrentDragOverItem.parentNode;
michael@0 748
michael@0 749 // Insert the item into the toolbar.
michael@0 750 if (gCurrentDragOverItem != dropToolbar)
michael@0 751 dropToolbar.insertBefore(wrapper, gCurrentDragOverItem);
michael@0 752 else
michael@0 753 dropToolbar.appendChild(wrapper);
michael@0 754 } else {
michael@0 755 // The item has been dragged from the palette
michael@0 756
michael@0 757 // Create a new wrapper for the item. We don't know the id yet.
michael@0 758 var wrapper = createWrapper("", gToolboxDocument);
michael@0 759
michael@0 760 // Ask the toolbar to clone the item's template, place it inside the wrapper, and insert it in the toolbar.
michael@0 761 var newItem = toolbar.insertItem(draggedItemId, gCurrentDragOverItem == toolbar ? null : gCurrentDragOverItem, wrapper);
michael@0 762
michael@0 763 // Prepare the item and wrapper to look good on the toolbar.
michael@0 764 cleanupItemForToolbar(newItem, wrapper);
michael@0 765 wrapper.id = "wrapper-"+newItem.id;
michael@0 766 wrapper.flex = newItem.flex;
michael@0 767
michael@0 768 // Remove the wrapper from the palette.
michael@0 769 if (draggedItemId != "separator" &&
michael@0 770 draggedItemId != "spring" &&
michael@0 771 draggedItemId != "spacer")
michael@0 772 gPaletteBox.removeChild(draggedPaletteWrapper);
michael@0 773 }
michael@0 774
michael@0 775 gCurrentDragOverItem = null;
michael@0 776
michael@0 777 toolboxChanged();
michael@0 778 };
michael@0 779
michael@0 780 function onPaletteDragOver(aEvent)
michael@0 781 {
michael@0 782 if (isUnwantedDragEvent(aEvent)) {
michael@0 783 return;
michael@0 784 }
michael@0 785 var documentId = gToolboxDocument.documentElement.id;
michael@0 786 if (aEvent.dataTransfer.types.contains("text/toolbarwrapper-id/" + documentId.toLowerCase()))
michael@0 787 aEvent.preventDefault();
michael@0 788 }
michael@0 789
michael@0 790 function onPaletteDrop(aEvent)
michael@0 791 {
michael@0 792 if (isUnwantedDragEvent(aEvent)) {
michael@0 793 return;
michael@0 794 }
michael@0 795 var documentId = gToolboxDocument.documentElement.id;
michael@0 796 var itemId = aEvent.dataTransfer.getData("text/toolbarwrapper-id/" + documentId);
michael@0 797
michael@0 798 var wrapper = gToolboxDocument.getElementById("wrapper-"+itemId);
michael@0 799 if (wrapper) {
michael@0 800 // Don't allow non-removable kids (e.g., the menubar) to move.
michael@0 801 if (wrapper.firstChild.getAttribute("removable") != "true")
michael@0 802 return;
michael@0 803
michael@0 804 var wrapperType = wrapper.getAttribute("type");
michael@0 805 if (wrapperType != "separator" &&
michael@0 806 wrapperType != "spacer" &&
michael@0 807 wrapperType != "spring") {
michael@0 808 restoreItemForToolbar(wrapper.firstChild, wrapper);
michael@0 809 wrapPaletteItem(document.importNode(wrapper.firstChild, true));
michael@0 810 gToolbox.palette.appendChild(wrapper.firstChild);
michael@0 811 }
michael@0 812
michael@0 813 // The item was dragged out of the toolbar.
michael@0 814 wrapper.parentNode.removeChild(wrapper);
michael@0 815 }
michael@0 816
michael@0 817 toolboxChanged();
michael@0 818 }
michael@0 819
michael@0 820
michael@0 821 function isUnwantedDragEvent(aEvent) {
michael@0 822 try {
michael@0 823 if (Services.prefs.getBoolPref("toolkit.customization.unsafe_drag_events")) {
michael@0 824 return false;
michael@0 825 }
michael@0 826 } catch (ex) {}
michael@0 827
michael@0 828 /* Discard drag events that originated from a separate window to
michael@0 829 prevent content->chrome privilege escalations. */
michael@0 830 let mozSourceNode = aEvent.dataTransfer.mozSourceNode;
michael@0 831 // mozSourceNode is null in the dragStart event handler or if
michael@0 832 // the drag event originated in an external application.
michael@0 833 if (!mozSourceNode) {
michael@0 834 return true;
michael@0 835 }
michael@0 836 let sourceWindow = mozSourceNode.ownerDocument.defaultView;
michael@0 837 return sourceWindow != window && sourceWindow != gToolboxDocument.defaultView;
michael@0 838 }
michael@0 839

mercurial