summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/content/customizeToolbar.js
blob: dd2b28bdab98a830c498ef2bef2e27c4ffe4bfcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var gToolboxDocument = null;
var gToolbox = null;
var gCurrentDragOverItem = null;
var gToolboxChanged = false;
var gToolboxSheet = false;
var gPaletteBox = null;

var { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);

function onLoad() {
  if ("arguments" in window && window.arguments[0]) {
    InitWithToolbox(window.arguments[0]);
    repositionDialog(window);
  } else if (window.frameElement && "toolbox" in window.frameElement) {
    gToolboxSheet = true;
    InitWithToolbox(window.frameElement.toolbox);
    repositionDialog(window.frameElement.panel);
  }
}

function InitWithToolbox(aToolbox) {
  gToolbox = aToolbox;
  dispatchCustomizationEvent("beforecustomization");
  gToolboxDocument = gToolbox.ownerDocument;
  gToolbox.customizing = true;
  forEachCustomizableToolbar(function (toolbar) {
    toolbar.setAttribute("customizing", "true");
  });
  gPaletteBox = document.getElementById("palette-box");

  var elts = getRootElements();
  for (let i = 0; i < elts.length; i++) {
    elts[i].addEventListener("dragstart", onToolbarDragStart, true);
    elts[i].addEventListener("dragover", onToolbarDragOver, true);
    elts[i].addEventListener("dragleave", onToolbarDragLeave, true);
    elts[i].addEventListener("drop", onToolbarDrop, true);
  }

  initDialog();
}

function onClose() {
  if (!gToolboxSheet) {
    window.close();
  } else {
    finishToolbarCustomization();
  }
}

function onUnload() {
  if (!gToolboxSheet) {
    finishToolbarCustomization();
  }
}

function finishToolbarCustomization() {
  removeToolboxListeners();
  unwrapToolbarItems();
  persistCurrentSets();
  gToolbox.customizing = false;
  forEachCustomizableToolbar(function (toolbar) {
    toolbar.removeAttribute("customizing");
  });

  notifyParentComplete();
}

function initDialog() {
  var mode = gToolbox.getAttribute("mode");
  document.getElementById("modelist").value = mode;
  var smallIconsCheckbox = document.getElementById("smallicons");
  smallIconsCheckbox.checked = gToolbox.getAttribute("iconsize") == "small";
  if (mode == "text") {
    smallIconsCheckbox.disabled = true;
  }

  if (AppConstants.MOZ_APP_NAME == "thunderbird") {
    document.getElementById("showTitlebar").checked =
      !Services.prefs.getBoolPref("mail.tabs.drawInTitlebar");
    if (
      window.opener &&
      window.opener.document.documentElement.getAttribute("windowtype") ==
        "mail:3pane"
    ) {
      document.getElementById("titlebarSettings").hidden = false;
    }
  }

  // Build up the palette of other items.
  buildPalette();

  // Wrap all the items on the toolbar in toolbarpaletteitems.
  wrapToolbarItems();
}

function repositionDialog(aWindow) {
  // Position the dialog touching the bottom of the toolbox and centered with
  // it.
  if (!aWindow) {
    return;
  }

  var width;
  if (aWindow != window) {
    width = aWindow.getBoundingClientRect().width;
  } else if (document.documentElement.hasAttribute("width")) {
    width = document.documentElement.getAttribute("width");
  } else {
    width = parseInt(document.documentElement.style.width);
  }
  var boundingRect = gToolbox.getBoundingClientRect();
  var screenX = gToolbox.screenX + (boundingRect.width - width) / 2;
  var screenY = gToolbox.screenY + boundingRect.height;

  aWindow.moveTo(screenX, screenY);
}

function removeToolboxListeners() {
  var elts = getRootElements();
  for (let i = 0; i < elts.length; i++) {
    elts[i].removeEventListener("dragstart", onToolbarDragStart, true);
    elts[i].removeEventListener("dragover", onToolbarDragOver, true);
    elts[i].removeEventListener("dragleave", onToolbarDragLeave, true);
    elts[i].removeEventListener("drop", onToolbarDrop, true);
  }
}

/**
 * Invoke a callback on the toolbox to notify it that the dialog is done
 * and going away.
 */
function notifyParentComplete() {
  if ("customizeDone" in gToolbox) {
    gToolbox.customizeDone(gToolboxChanged);
  }
  dispatchCustomizationEvent("aftercustomization");
}

function toolboxChanged(aType) {
  gToolboxChanged = true;
  if ("customizeChange" in gToolbox) {
    gToolbox.customizeChange(aType);
  }
  dispatchCustomizationEvent("customizationchange");
}

function dispatchCustomizationEvent(aEventName) {
  var evt = document.createEvent("Events");
  evt.initEvent(aEventName, true, true);
  gToolbox.dispatchEvent(evt);
}

/**
 * Persist the current set of buttons in all customizable toolbars to
 * localstore.
 */
function persistCurrentSets() {
  if (!gToolboxChanged || gToolboxDocument.defaultView.closed) {
    return;
  }

  forEachCustomizableToolbar(function (toolbar) {
    // Calculate currentset and store it in the attribute.
    var currentSet = toolbar.currentSet;
    toolbar.setAttribute("currentset", currentSet);
    Services.xulStore.persist(toolbar, "currentset");
  });
}

/**
 * Wraps all items in all customizable toolbars in a toolbox.
 */
function wrapToolbarItems() {
  forEachCustomizableToolbar(function (toolbar) {
    for (let item of toolbar.children) {
      if (AppConstants.platform == "macosx") {
        if (
          item.firstElementChild &&
          item.firstElementChild.localName == "menubar"
        ) {
          return;
        }
      }
      if (isToolbarItem(item)) {
        let wrapper = wrapToolbarItem(item);
        cleanupItemForToolbar(item, wrapper);
      }
    }
  });
}

function getRootElements() {
  if (window.frameElement && "externalToolbars" in window.frameElement) {
    return [gToolbox].concat(window.frameElement.externalToolbars);
  }
  if ("arguments" in window && window.arguments[1].length > 0) {
    return [gToolbox].concat(window.arguments[1]);
  }
  return [gToolbox];
}

/**
 * Unwraps all items in all customizable toolbars in a toolbox.
 */
function unwrapToolbarItems() {
  let elts = getRootElements();
  for (let i = 0; i < elts.length; i++) {
    let paletteItems = elts[i].getElementsByTagName("toolbarpaletteitem");
    let paletteItem;
    while ((paletteItem = paletteItems.item(0)) != null) {
      let toolbarItem = paletteItem.firstElementChild;
      restoreItemForToolbar(toolbarItem, paletteItem);
      paletteItem.parentNode.replaceChild(toolbarItem, paletteItem);
    }
  }
}

/**
 * Creates a wrapper that can be used to contain a toolbaritem and prevent
 * it from receiving UI events.
 */
function createWrapper(aId, aDocument) {
  let wrapper = aDocument.createXULElement("toolbarpaletteitem");

  wrapper.id = "wrapper-" + aId;
  return wrapper;
}

/**
 * Wraps an item that has been cloned from a template and adds
 * it to the end of the palette.
 */
function wrapPaletteItem(aPaletteItem) {
  var wrapper = createWrapper(aPaletteItem.id, document);

  wrapper.appendChild(aPaletteItem);

  // XXX We need to call this AFTER the palette item has been appended
  // to the wrapper or else we crash dropping certain buttons on the
  // palette due to removal of the command and disabled attributes - JRH
  cleanUpItemForPalette(aPaletteItem, wrapper);

  gPaletteBox.appendChild(wrapper);
}

/**
 * Wraps an item that is currently on a toolbar and replaces the item
 * with the wrapper. This is not used when dropping items from the palette,
 * only when first starting the dialog and wrapping everything on the toolbars.
 */
function wrapToolbarItem(aToolbarItem) {
  var wrapper = createWrapper(aToolbarItem.id, gToolboxDocument);

  wrapper.flex = aToolbarItem.flex;

  aToolbarItem.parentNode.replaceChild(wrapper, aToolbarItem);

  wrapper.appendChild(aToolbarItem);

  return wrapper;
}

/**
 * Get the list of ids for the current set of items on each toolbar.
 */
function getCurrentItemIds() {
  var currentItems = {};
  forEachCustomizableToolbar(function (toolbar) {
    var child = toolbar.firstElementChild;
    while (child) {
      if (isToolbarItem(child)) {
        currentItems[child.id] = 1;
      }
      child = child.nextElementSibling;
    }
  });
  return currentItems;
}

/**
 * Builds the palette of draggable items that are not yet in a toolbar.
 */
function buildPalette() {
  // Empty the palette first.
  while (gPaletteBox.lastElementChild) {
    gPaletteBox.lastChild.remove();
  }

  // Add the toolbar separator item.
  var templateNode = document.createXULElement("toolbarseparator");
  templateNode.id = "separator";
  wrapPaletteItem(templateNode);

  // Add the toolbar spring item.
  templateNode = document.createXULElement("toolbarspring");
  templateNode.id = "spring";
  templateNode.flex = 1;
  wrapPaletteItem(templateNode);

  // Add the toolbar spacer item.
  templateNode = document.createXULElement("toolbarspacer");
  templateNode.id = "spacer";
  templateNode.flex = 1;
  wrapPaletteItem(templateNode);

  var currentItems = getCurrentItemIds();
  templateNode = gToolbox.palette.firstElementChild;
  while (templateNode) {
    // Check if the item is already in a toolbar before adding it to the
    // palette, but do not add back separators, springs and spacers - we do
    // not want them duplicated.
    if (!isSpecialItem(templateNode) && !(templateNode.id in currentItems)) {
      var paletteItem = document.importNode(templateNode, true);
      wrapPaletteItem(paletteItem);
    }

    templateNode = templateNode.nextElementSibling;
  }
}

/**
 * Makes sure that an item that has been cloned from a template
 * is stripped of any attributes that may adversely affect its
 * appearance in the palette.
 */
function cleanUpItemForPalette(aItem, aWrapper) {
  aWrapper.setAttribute("place", "palette");
  setWrapperType(aItem, aWrapper);

  if (aItem.hasAttribute("title")) {
    aWrapper.setAttribute("title", aItem.getAttribute("title"));
  } else if (aItem.hasAttribute("label")) {
    aWrapper.setAttribute("title", aItem.getAttribute("label"));
  } else if (isSpecialItem(aItem)) {
    var stringBundle = document.getElementById("stringBundle");
    // Remove the common "toolbar" prefix to generate the string name.
    var title = stringBundle.getString(aItem.localName.slice(7) + "Title");
    aWrapper.setAttribute("title", title);
  }
  aWrapper.setAttribute("tooltiptext", aWrapper.getAttribute("title"));

  // Remove attributes that screw up our appearance.
  aItem.removeAttribute("command");
  aItem.removeAttribute("observes");
  aItem.removeAttribute("type");
  aItem.removeAttribute("width");
  aItem.removeAttribute("checked");
  aItem.removeAttribute("collapsed");

  aWrapper.querySelectorAll("[disabled]").forEach(function (aNode) {
    aNode.removeAttribute("disabled");
  });
}

/**
 * Makes sure that an item that has been cloned from a template
 * is stripped of all properties that may adversely affect its
 * appearance in the toolbar.  Store critical properties on the
 * wrapper so they can be put back on the item when we're done.
 */
function cleanupItemForToolbar(aItem, aWrapper) {
  setWrapperType(aItem, aWrapper);
  aWrapper.setAttribute("place", "toolbar");

  if (aItem.hasAttribute("command")) {
    aWrapper.setAttribute("itemcommand", aItem.getAttribute("command"));
    aItem.removeAttribute("command");
  }

  if (aItem.hasAttribute("collapsed")) {
    aWrapper.setAttribute("itemcollapsed", aItem.getAttribute("collapsed"));
    aItem.removeAttribute("collapsed");
  }

  if (aItem.checked) {
    aWrapper.setAttribute("itemchecked", "true");
    aItem.checked = false;
  }

  if (aItem.disabled) {
    aWrapper.setAttribute("itemdisabled", "true");
    aItem.disabled = false;
  }
}

/**
 * Restore all the properties that we stripped off above.
 */
function restoreItemForToolbar(aItem, aWrapper) {
  if (aWrapper.hasAttribute("itemdisabled")) {
    aItem.disabled = true;
  }

  if (aWrapper.hasAttribute("itemchecked")) {
    aItem.checked = true;
  }

  if (aWrapper.hasAttribute("itemcollapsed")) {
    let collapsed = aWrapper.getAttribute("itemcollapsed");
    aItem.setAttribute("collapsed", collapsed);
  }

  if (aWrapper.hasAttribute("itemcommand")) {
    let commandID = aWrapper.getAttribute("itemcommand");
    aItem.setAttribute("command", commandID);

    // XXX Bug 309953 - toolbarbuttons aren't in sync with their commands after customizing
    let command = gToolboxDocument.getElementById(commandID);
    if (command && command.hasAttribute("disabled")) {
      aItem.setAttribute("disabled", command.getAttribute("disabled"));
    }
  }
}

function setWrapperType(aItem, aWrapper) {
  if (aItem.localName == "toolbarseparator") {
    aWrapper.setAttribute("type", "separator");
  } else if (aItem.localName == "toolbarspring") {
    aWrapper.setAttribute("type", "spring");
  } else if (aItem.localName == "toolbarspacer") {
    aWrapper.setAttribute("type", "spacer");
  } else if (aItem.localName == "toolbaritem" && aItem.firstElementChild) {
    aWrapper.setAttribute("type", aItem.firstElementChild.localName);
  }
}

function setDragActive(aItem, aValue) {
  var node = aItem;
  var direction = window.getComputedStyle(aItem).direction;
  var value = direction == "ltr" ? "left" : "right";
  if (aItem.localName == "toolbar") {
    node = aItem.lastElementChild;
    value = direction == "ltr" ? "right" : "left";
  }

  if (!node) {
    return;
  }

  if (aValue) {
    if (!node.hasAttribute("dragover")) {
      node.setAttribute("dragover", value);
    }
  } else {
    node.removeAttribute("dragover");
  }
}

/**
 * Restore the default set of buttons to fixed toolbars,
 * remove all custom toolbars, and rebuild the palette.
 */
function restoreDefaultSet() {
  // Unwrap the items on the toolbar.
  unwrapToolbarItems();

  // Remove all of the customized toolbars.
  var child = gToolbox.lastElementChild;
  while (child) {
    if (child.hasAttribute("customindex")) {
      var thisChild = child;
      child = child.previousElementSibling;
      thisChild.currentSet = "__empty";
      gToolbox.removeChild(thisChild);
    } else {
      child = child.previousElementSibling;
    }
  }

  // Restore the defaultset for fixed toolbars.
  forEachCustomizableToolbar(function (toolbar) {
    var defaultSet = toolbar.getAttribute("defaultset");
    if (defaultSet) {
      toolbar.currentSet = defaultSet;
    }
  });

  // Restore the default icon size and mode.
  document.getElementById("smallicons").checked = updateIconSize() == "small";
  document.getElementById("modelist").value = updateToolbarMode();

  // Now rebuild the palette.
  buildPalette();

  // Now re-wrap the items on the toolbar.
  wrapToolbarItems();

  toolboxChanged("reset");
}

function updateIconSize(aSize) {
  return updateToolboxProperty("iconsize", aSize, "large");
}

function updateTitlebar() {
  let titlebarCheckbox = document.getElementById("showTitlebar");
  Services.prefs.setBoolPref(
    "mail.tabs.drawInTitlebar",
    !titlebarCheckbox.checked
  );

  // Bring the customizeToolbar window to front (on linux it's behind the main
  // window). Otherwise the customization window gets left in the background.
  setTimeout(() => window.focus(), 100);
}

function updateToolbarMode(aModeValue) {
  var mode = updateToolboxProperty("mode", aModeValue, "icons");

  var iconSizeCheckbox = document.getElementById("smallicons");
  iconSizeCheckbox.disabled = mode == "text";

  return mode;
}

function updateToolboxProperty(aProp, aValue, aToolkitDefault) {
  var toolboxDefault =
    gToolbox.getAttribute("default" + aProp) || aToolkitDefault;

  gToolbox.setAttribute(aProp, aValue || toolboxDefault);
  Services.xulStore.persist(gToolbox, aProp);

  forEachCustomizableToolbar(function (toolbar) {
    var toolbarDefault =
      toolbar.getAttribute("default" + aProp) || toolboxDefault;
    if (
      toolbar.getAttribute("lock" + aProp) == "true" &&
      toolbar.getAttribute(aProp) == toolbarDefault
    ) {
      return;
    }

    toolbar.setAttribute(aProp, aValue || toolbarDefault);
    Services.xulStore.persist(toolbar, aProp);
  });

  toolboxChanged(aProp);

  return aValue || toolboxDefault;
}

function forEachCustomizableToolbar(callback) {
  if (window.frameElement && "externalToolbars" in window.frameElement) {
    Array.from(window.frameElement.externalToolbars)
      .filter(isCustomizableToolbar)
      .forEach(callback);
  } else if ("arguments" in window && window.arguments[1].length > 0) {
    Array.from(window.arguments[1])
      .filter(isCustomizableToolbar)
      .forEach(callback);
  }
  Array.from(gToolbox.children).filter(isCustomizableToolbar).forEach(callback);
}

function isCustomizableToolbar(aElt) {
  return (
    aElt.localName == "toolbar" && aElt.getAttribute("customizable") == "true"
  );
}

function isSpecialItem(aElt) {
  return (
    aElt.localName == "toolbarseparator" ||
    aElt.localName == "toolbarspring" ||
    aElt.localName == "toolbarspacer"
  );
}

function isToolbarItem(aElt) {
  return (
    aElt.localName == "toolbarbutton" ||
    aElt.localName == "toolbaritem" ||
    aElt.localName == "toolbarseparator" ||
    aElt.localName == "toolbarspring" ||
    aElt.localName == "toolbarspacer"
  );
}

// Drag and Drop observers

function onToolbarDragLeave(aEvent) {
  if (isUnwantedDragEvent(aEvent)) {
    return;
  }

  if (gCurrentDragOverItem) {
    setDragActive(gCurrentDragOverItem, false);
  }
}

function onToolbarDragStart(aEvent) {
  var item = aEvent.target;
  while (item && item.localName != "toolbarpaletteitem") {
    if (item.localName == "toolbar") {
      return;
    }
    item = item.parentNode;
  }

  item.setAttribute("dragactive", "true");

  var dt = aEvent.dataTransfer;
  var documentId = gToolboxDocument.documentElement.id;
  dt.setData("text/toolbarwrapper-id/" + documentId, item.firstElementChild.id);
  dt.effectAllowed = "move";
}

function onToolbarDragOver(aEvent) {
  if (isUnwantedDragEvent(aEvent)) {
    return;
  }

  var documentId = gToolboxDocument.documentElement.id;
  if (
    !aEvent.dataTransfer.types.includes(
      "text/toolbarwrapper-id/" + documentId.toLowerCase()
    )
  ) {
    return;
  }

  var toolbar = aEvent.target;
  var dropTarget = aEvent.target;
  while (toolbar && toolbar.localName != "toolbar") {
    dropTarget = toolbar;
    toolbar = toolbar.parentNode;
  }

  // Make sure we are dragging over a customizable toolbar.
  if (!toolbar || !isCustomizableToolbar(toolbar)) {
    gCurrentDragOverItem = null;
    return;
  }

  var previousDragItem = gCurrentDragOverItem;

  if (dropTarget.localName == "toolbar") {
    gCurrentDragOverItem = dropTarget;
  } else {
    gCurrentDragOverItem = null;

    var direction = window.getComputedStyle(dropTarget.parentNode).direction;
    var boundingRect = dropTarget.getBoundingClientRect();
    var dropTargetCenter = boundingRect.x + boundingRect.width / 2;
    var dragAfter;
    if (direction == "ltr") {
      dragAfter = aEvent.clientX > dropTargetCenter;
    } else {
      dragAfter = aEvent.clientX < dropTargetCenter;
    }

    if (dragAfter) {
      gCurrentDragOverItem = dropTarget.nextElementSibling;
      if (!gCurrentDragOverItem) {
        gCurrentDragOverItem = toolbar;
      }
    } else {
      gCurrentDragOverItem = dropTarget;
    }
  }

  if (previousDragItem && gCurrentDragOverItem != previousDragItem) {
    setDragActive(previousDragItem, false);
  }

  setDragActive(gCurrentDragOverItem, true);

  aEvent.preventDefault();
  aEvent.stopPropagation();
}

function onToolbarDrop(aEvent) {
  if (isUnwantedDragEvent(aEvent)) {
    return;
  }

  if (!gCurrentDragOverItem) {
    return;
  }

  setDragActive(gCurrentDragOverItem, false);

  var documentId = gToolboxDocument.documentElement.id;
  var draggedItemId = aEvent.dataTransfer.getData(
    "text/toolbarwrapper-id/" + documentId
  );
  if (gCurrentDragOverItem.id == draggedItemId) {
    return;
  }

  var toolbar = aEvent.target;
  while (toolbar.localName != "toolbar") {
    toolbar = toolbar.parentNode;
  }

  var draggedPaletteWrapper = document.getElementById(
    "wrapper-" + draggedItemId
  );
  if (!draggedPaletteWrapper) {
    // The wrapper has been dragged from the toolbar.
    // Get the wrapper from the toolbar document and make sure that
    // it isn't being dropped on itself.
    let wrapper = gToolboxDocument.getElementById("wrapper-" + draggedItemId);
    if (wrapper == gCurrentDragOverItem) {
      return;
    }

    // Don't allow non-removable kids (e.g., the menubar) to move.
    if (wrapper.firstElementChild.getAttribute("removable") != "true") {
      return;
    }

    // Remove the item from its place in the toolbar.
    wrapper.remove();

    // Determine which toolbar we are dropping on.
    var dropToolbar = null;
    if (gCurrentDragOverItem.localName == "toolbar") {
      dropToolbar = gCurrentDragOverItem;
    } else {
      dropToolbar = gCurrentDragOverItem.parentNode;
    }

    // Insert the item into the toolbar.
    if (gCurrentDragOverItem != dropToolbar) {
      dropToolbar.insertBefore(wrapper, gCurrentDragOverItem);
    } else {
      dropToolbar.appendChild(wrapper);
    }
  } else {
    // The item has been dragged from the palette

    // Create a new wrapper for the item. We don't know the id yet.
    let wrapper = createWrapper("", gToolboxDocument);

    // Ask the toolbar to clone the item's template, place it inside the wrapper, and insert it in the toolbar.
    var newItem = toolbar.insertItem(
      draggedItemId,
      gCurrentDragOverItem == toolbar ? null : gCurrentDragOverItem,
      wrapper
    );

    // Prepare the item and wrapper to look good on the toolbar.
    cleanupItemForToolbar(newItem, wrapper);
    wrapper.id = "wrapper-" + newItem.id;
    wrapper.flex = newItem.flex;

    // Remove the wrapper from the palette.
    if (
      draggedItemId != "separator" &&
      draggedItemId != "spring" &&
      draggedItemId != "spacer"
    ) {
      gPaletteBox.removeChild(draggedPaletteWrapper);
    }
  }

  gCurrentDragOverItem = null;

  toolboxChanged();
}

function onPaletteDragOver(aEvent) {
  if (isUnwantedDragEvent(aEvent)) {
    return;
  }
  var documentId = gToolboxDocument.documentElement.id;
  if (
    aEvent.dataTransfer.types.includes(
      "text/toolbarwrapper-id/" + documentId.toLowerCase()
    )
  ) {
    aEvent.preventDefault();
  }
}

function onPaletteDrop(aEvent) {
  if (isUnwantedDragEvent(aEvent)) {
    return;
  }
  var documentId = gToolboxDocument.documentElement.id;
  var itemId = aEvent.dataTransfer.getData(
    "text/toolbarwrapper-id/" + documentId
  );

  var wrapper = gToolboxDocument.getElementById("wrapper-" + itemId);
  if (wrapper) {
    // Don't allow non-removable kids (e.g., the menubar) to move.
    if (wrapper.firstElementChild.getAttribute("removable") != "true") {
      return;
    }

    var wrapperType = wrapper.getAttribute("type");
    if (
      wrapperType != "separator" &&
      wrapperType != "spacer" &&
      wrapperType != "spring"
    ) {
      restoreItemForToolbar(wrapper.firstElementChild, wrapper);
      wrapPaletteItem(document.importNode(wrapper.firstElementChild, true));
      gToolbox.palette.appendChild(wrapper.firstElementChild);
    }

    // The item was dragged out of the toolbar.
    wrapper.remove();
  }

  toolboxChanged();
}

function isUnwantedDragEvent(aEvent) {
  try {
    if (
      Services.prefs.getBoolPref("toolkit.customization.unsafe_drag_events")
    ) {
      return false;
    }
  } catch (ex) {}

  // Discard drag events that originated from a separate window to
  // prevent content->chrome privilege escalations.
  let mozSourceNode = aEvent.dataTransfer.mozSourceNode;
  // mozSourceNode is null in the dragStart event handler or if
  // the drag event originated in an external application.
  if (!mozSourceNode) {
    return true;
  }
  let sourceWindow = mozSourceNode.ownerGlobal;
  return sourceWindow != window && sourceWindow != gToolboxDocument.defaultView;
}