summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/addrbook/content/addressBookTab.js
blob: 5605612daf4341535772eee4aa29951327e08ff4 (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
/* 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/. */

// mail/base/content/specialTabs.js
/* globals contentTabBaseType, DOMLinkHandler */

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

/**
 * A tab to show the Address Book.
 */
var addressBookTabType = {
  __proto__: contentTabBaseType,
  name: "addressBookTab",
  perTabPanel: "vbox",
  lastBrowserId: 0,
  bundle: Services.strings.createBundle(
    "chrome://messenger/locale/messenger.properties"
  ),
  protoSvc: Cc["@mozilla.org/uriloader/external-protocol-service;1"].getService(
    Ci.nsIExternalProtocolService
  ),

  get loadingTabString() {
    delete this.loadingTabString;
    return (this.loadingTabString = document
      .getElementById("bundle_messenger")
      .getString("loadingTab"));
  },

  modes: {
    addressBookTab: {
      type: "addressBookTab",
    },
  },

  shouldSwitchTo(aArgs) {
    if (!this.tab) {
      return -1;
    }

    if ("onLoad" in aArgs) {
      if (this.tab.browser.contentDocument.readyState != "complete") {
        this.tab.browser.addEventListener(
          "about-addressbook-ready",
          event => aArgs.onLoad(event, this.tab.browser),
          {
            capture: true,
            once: true,
          }
        );
      } else {
        aArgs.onLoad(null, this.tab.browser);
      }
    }
    return document.getElementById("tabmail").tabInfo.indexOf(this.tab);
  },

  closeTab(aTab) {
    this.tab = null;
  },

  openTab(aTab, aArgs) {
    aTab.tabNode.setIcon(
      "chrome://messenger/skin/icons/new/compact/address-book.svg"
    );

    // First clone the page and set up the basics.
    let clone = document
      .getElementById("preferencesTab")
      .firstElementChild.cloneNode(true);

    clone.setAttribute("id", "addressBookTab" + this.lastBrowserId);
    clone.setAttribute("collapsed", false);

    aTab.panel.setAttribute("id", "addressBookTabWrapper" + this.lastBrowserId);
    aTab.panel.appendChild(clone);

    // Start setting up the browser.
    aTab.browser = aTab.panel.querySelector("browser");
    aTab.browser.setAttribute(
      "id",
      "addressBookTabBrowser" + this.lastBrowserId
    );
    aTab.browser.setAttribute("autocompletepopup", "PopupAutoComplete");
    aTab.browser.addEventListener("DOMLinkAdded", DOMLinkHandler);

    aTab.findbar = document.createXULElement("findbar");
    aTab.findbar.setAttribute(
      "browserid",
      "addressBookTabBrowser" + this.lastBrowserId
    );
    aTab.panel.appendChild(aTab.findbar);

    // Default to reload being disabled.
    aTab.reloadEnabled = false;

    aTab.url = "about:addressbook";
    aTab.paneID = aArgs.paneID;
    aTab.scrollPaneTo = aArgs.scrollPaneTo;
    aTab.otherArgs = aArgs.otherArgs;

    // Now set up the listeners.
    this._setUpTitleListener(aTab);
    this._setUpCloseWindowListener(aTab);

    // Wait for full loading of the tab and the automatic selecting of last tab.
    // Then run the given onload code.
    aTab.browser.addEventListener(
      "about-addressbook-ready",
      function (event) {
        aTab.pageLoading = false;
        aTab.pageLoaded = true;

        if ("onLoad" in aArgs) {
          // Let selection of the initial pane complete before selecting another.
          // Otherwise we can end up with two panes selected at once.
          aTab.browser.contentWindow.setTimeout(() => {
            // By now, the tab could already be closed. Check that it isn't.
            if (aTab.panel) {
              aArgs.onLoad(event, aTab.browser);
            }
          });
        }
      },
      {
        capture: true,
        once: true,
      }
    );

    // Initialize our unit testing variables.
    aTab.pageLoading = true;
    aTab.pageLoaded = false;

    // Now start loading the content.
    aTab.title = this.loadingTabString;

    ExtensionParent.apiManager.emit("extension-browser-inserted", aTab.browser);
    let params = {
      triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(),
      postData: aArgs.postData || null,
    };
    aTab.browser.loadURI(Services.io.newURI("about:addressbook"), params);

    this.tab = aTab;
    this.lastBrowserId++;
  },

  persistTab(aTab) {
    if (aTab.browser.currentURI.spec == "about:blank") {
      return null;
    }

    return {};
  },

  restoreTab(aTabmail, aPersistedState) {
    aTabmail.openTab("addressBookTab", {});
  },

  doCommand(aCommand, aTab) {
    if (aCommand == "cmd_print") {
      aTab.browser.contentWindow.externalAction({ action: "print" });
      return;
    }
    this.__proto__.doCommand(aCommand, aTab);
  },
};