summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/content/widgets/toolbarbutton-menu-button.js
blob: c514aa73577b9230f67b3ab5ae62b2be3b1fdbdf (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
/* 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/. */

"use strict";

/* global MozXULElement */

// Wrap in a block to prevent leaking to window scope.
{
  /**
   * The MozToolbarButtonMenuButton widget is a toolbarbutton with
   * type="menu". Place a menupopup element inside the button to create
   * the menu popup. When the dropmarker in the toobarbutton is pressed the
   * menupopup will open. When clicking the main area of the button it works
   * like a normal toolbarbutton.
   *
   * @augments MozToolbarbutton
   */
  class MozToolbarButtonMenuButton extends customElements.get("toolbarbutton") {
    static get inheritedAttributes() {
      return {
        ...super.inheritedAttributes,
        ".toolbarbutton-menubutton-button":
          "command,hidden,disabled,align,dir,pack,orient,label,wrap,tooltiptext=buttontooltiptext",
        ".toolbarbutton-menubutton-dropmarker": "open,disabled",
      };
    }
    static get menubuttonFragment() {
      let frag = document.importNode(
        MozXULElement.parseXULToFragment(`
          <toolbarbutton class="box-inherit toolbarbutton-menubutton-button"
                         flex="1"
                         allowevents="true"></toolbarbutton>
          <dropmarker type="menu"
                      class="toolbarbutton-menubutton-dropmarker"></dropmarker>
        `),
        true
      );
      Object.defineProperty(this, "menubuttonFragment", { value: frag });
      return frag;
    }

    /** @override */
    get _hasConnected() {
      return (
        this.querySelector(":scope > toolbarbutton > .toolbarbutton-text") !=
        null
      );
    }

    /** @override */
    render() {
      this.appendChild(this.constructor.menubuttonFragment.cloneNode(true));
      this.initializeAttributeInheritance();
    }

    connectedCallback() {
      if (this.delayConnectedCallback() || this._hasConnected) {
        return;
      }

      // Defer creating DOM elements for content inside popups.
      // These will be added in the popupshown handler above.
      let panel = this.closest("panel");
      if (panel && !panel.hasAttribute("hasbeenopened")) {
        return;
      }
      this.setAttribute("is", "toolbarbutton-menu-button");
      this.setAttribute("type", "menu");

      this.render();
    }
  }
  customElements.define(
    "toolbarbutton-menu-button",
    MozToolbarButtonMenuButton,
    { extends: "toolbarbutton" }
  );
}