summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/content/glodaFacetTab.js
blob: f194b2c24b34a9a13241716f34d8e40cb1c7706f (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
/* 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/. */

ChromeUtils.defineModuleGetter(
  this,
  "GlodaMsgSearcher",
  "resource:///modules/gloda/GlodaMsgSearcher.jsm"
);

var glodaFacetTabType = {
  name: "glodaFacet",
  perTabPanel: "vbox",
  lastTabId: 0,
  strings: Services.strings.createBundle(
    "chrome://messenger/locale/glodaFacetView.properties"
  ),
  modes: {
    glodaFacet: {
      // this is what get exposed on the tab for icon purposes
      type: "glodaSearch",
    },
  },
  openTab(aTab, aArgs) {
    // If aArgs is empty, default to a blank user search.
    if (!Object.keys(aArgs).length) {
      aArgs = { searcher: new GlodaMsgSearcher(null, "") };
    }
    // we have no browser until our XUL document loads
    aTab.browser = null;

    aTab.tabNode.setIcon(
      "chrome://messenger/skin/icons/new/compact/search.svg"
    );

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

    aTab.panel.setAttribute("id", "glodaTab" + this.lastTabId);
    aTab.panel.appendChild(clone);
    aTab.iframe = aTab.panel.querySelector("iframe");

    if ("query" in aArgs) {
      aTab.query = aArgs.query;
      aTab.collection = aTab.query.getCollection();

      aTab.title = this.strings.GetStringFromName(
        "glodaFacetView.tab.query.label"
      );
      aTab.searchString = null;
    } else if ("searcher" in aArgs) {
      aTab.searcher = aArgs.searcher;
      aTab.collection = aTab.searcher.getCollection();
      aTab.query = aTab.searcher.query;
      if ("IMSearcher" in aArgs) {
        aTab.IMSearcher = aArgs.IMSearcher;
        aTab.IMCollection = aArgs.IMSearcher.getCollection();
        aTab.IMQuery = aTab.IMSearcher.query;
      }

      let searchString = aTab.searcher.searchString;
      aTab.searchInputValue = aTab.searchString = searchString;
      aTab.title = searchString
        ? searchString
        : this.strings.GetStringFromName("glodaFacetView.tab.search.label");
    } else if ("collection" in aArgs) {
      aTab.collection = aArgs.collection;

      aTab.title = this.strings.GetStringFromName(
        "glodaFacetView.tab.query.label"
      );
      aTab.searchString = null;
    }

    function xulLoadHandler() {
      aTab.iframe.contentWindow.tab = aTab;
      aTab.browser = aTab.iframe.contentDocument.getElementById("browser");
      aTab.browser.setAttribute(
        "src",
        "chrome://messenger/content/glodaFacetView.xhtml"
      );

      // Wire up the search input icon click event
      let searchInput = aTab.panel.querySelector(".remote-gloda-search");
      searchInput.focus();
    }

    aTab.iframe.contentWindow.addEventListener("load", xulLoadHandler, {
      capture: false,
      once: true,
    });
    aTab.iframe.setAttribute(
      "src",
      "chrome://messenger/content/glodaFacetViewWrapper.xhtml"
    );

    this.lastTabId++;
  },
  closeTab(aTab) {},
  saveTabState(aTab) {
    // nothing to do; we are not multiplexed
  },
  showTab(aTab) {
    // nothing to do; we are not multiplexed
  },
  getBrowser(aTab) {
    return aTab.browser;
  },
};