summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/unifiedtoolbar/content/items/global-search-bar.mjs
blob: 924955c8959f48d9c7db39d651d90175794d527b (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
/* 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/. */

import { SearchBar } from "chrome://messenger/content/unifiedtoolbar/search-bar.mjs";

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

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  GlodaIMSearcher: "resource:///modules/GlodaIMSearcher.sys.mjs",
});
ChromeUtils.defineModuleGetter(
  lazy,
  "GlodaMsgSearcher",
  "resource:///modules/gloda/GlodaMsgSearcher.jsm"
);
ChromeUtils.defineModuleGetter(
  lazy,
  "GlodaConstants",
  "resource:///modules/gloda/GlodaConstants.jsm"
);
ChromeUtils.defineModuleGetter(
  lazy,
  "Gloda",
  "resource:///modules/gloda/GlodaPublic.jsm"
);
XPCOMUtils.defineLazyGetter(
  lazy,
  "glodaCompleter",
  () =>
    Cc["@mozilla.org/autocomplete/search;1?name=gloda"].getService(
      Ci.nsIAutoCompleteSearch
    ).wrappedJSObject
);

/**
 * Unified toolbar global search bar.
 */
class GlobalSearchBar extends SearchBar {
  // Fields required for the auto complete popup to work.

  get popup() {
    return document.getElementById("PopupGlodaAutocomplete");
  }

  controller = {
    matchCount: 0,
    searchString: "",
    stopSearch() {
      lazy.glodaCompleter.stopSearch();
    },
    handleEnter: (isAutocomplete, event) => {
      if (!isAutocomplete) {
        return;
      }
      this.#handleSearch({ detail: this.controller.searchString });
      this.reset();
    },
  };

  _focus() {
    this.focus();
  }

  #searchResultListener = {
    onSearchResult: (result, search) => {
      this.controller.matchCount = search.matchCount;
      if (this.controller.matchCount < 1) {
        this.popup.closePopup();
        return;
      }
      if (!this.popup.mPopupOpen) {
        this.popup.openAutocompletePopup(
          this,
          this.shadowRoot.querySelector("input")
        );
        return;
      }
      this.popup.invalidate();
    },
  };

  // Normal custom element stuff

  connectedCallback() {
    if (this.shadowRoot) {
      return;
    }
    if (
      !Services.prefs.getBoolPref(
        "mailnews.database.global.indexer.enabled",
        true
      )
    ) {
      return;
    }
    // Need to call this after the shadow root test, since this will always set
    // up a shadow root.
    super.connectedCallback();
    this.addEventListener("search", this.#handleSearch);
    this.addEventListener("autocomplete", this.#handleAutocomplete);
    // Capturing to avoid the default cursor movements inside the input.
    this.addEventListener("keydown", this.#handleKeydown, {
      capture: true,
    });
    this.addEventListener("focus", this.#handleFocus);
    this.addEventListener("blur", this);
    this.addEventListener("drop", this.#handleDrop, { capture: true });
  }

  handleEvent(event) {
    switch (event.type) {
      case "blur":
        if (this.popup.mPopupOpen) {
          this.popup.closePopup();
        }
        break;
    }
  }

  #handleSearch = event => {
    let tabmail = document.getElementById("tabmail");
    let args;
    // Build the query from the autocomplete result.
    const selectedIndex = this.popup.selectedIndex;
    if (selectedIndex > -1) {
      const curResult = lazy.glodaCompleter.curResult;
      if (curResult) {
        const row = curResult.getObjectAt(selectedIndex);
        if (row && !row.fullText && row.nounDef) {
          let query = lazy.Gloda.newQuery(lazy.GlodaConstants.NOUN_MESSAGE);
          switch (row.nounDef.name) {
            case "tag":
              query = query.tags(row.item);
              break;
            case "identity":
              query = query.involves(row.item);
              break;
          }
          query.orderBy("-date");
          args = { query };
        }
      }
    }
    // Or just do a normal full text search.
    if (!args) {
      let searchString = event.detail;
      args = {
        searcher: new lazy.GlodaMsgSearcher(null, searchString),
      };
      if (Services.prefs.getBoolPref("mail.chat.enabled")) {
        args.IMSearcher = new lazy.GlodaIMSearcher(null, searchString);
      }
    }
    tabmail.openTab("glodaFacet", args);
    this.popup.closePopup();
    this.controller.matchCount = 0;
    this.controller.searchString = "";
  };

  #handleAutocomplete = event => {
    this.controller.searchString = event.detail;
    if (!event.detail) {
      this.popup.closePopup();
      this.controller.matchCount = 0;
      return;
    }
    lazy.glodaCompleter.startSearch(
      this.controller.searchString,
      "global",
      null,
      this.#searchResultListener
    );
  };

  #handleKeydown = event => {
    if (event.ctrlKey) {
      return;
    }
    if (event.key == "ArrowDown") {
      if (this.popup.selectedIndex < this.controller.matchCount - 1) {
        ++this.popup.selectedIndex;
        event.preventDefault();
        return;
      }
      this.popup.selectedIndex = -1;
      event.preventDefault();
      return;
    }
    if (event.key == "ArrowUp") {
      if (this.popup.selectedIndex > -1) {
        --this.popup.selectedIndex;
        event.preventDefault();
        return;
      }
      this.popup.selectedIndex = this.controller.matchCount - 1;
      event.preventDefault();
    }
  };

  #handleFocus = event => {
    if (this.controller.searchString && this.controller.matchCount >= 1) {
      this.popup.openAutocompletePopup(
        this,
        this.shadowRoot.querySelector("input")
      );
    }
  };

  #handleDrop = event => {
    if (event.dataTransfer.types.includes("text/x-moz-address")) {
      const searchTerm = event.dataTransfer.getData("text/plain");
      this.#handleSearch({ detail: searchTerm });
    }
    event.stopPropagation();
    event.preventDefault();
  };
}
customElements.define("global-search-bar", GlobalSearchBar);