summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/nsFormAutoCompleteResult.sys.mjs
blob: 6c86119a868a624985e0a97e16c3e3e1f9979a5c (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
/* 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/. */

export class FormAutoCompleteResult {
  constructor(
    searchString,
    searchResult,
    defaultIndex,
    errorDescription,
    items,
    prevResult
  ) {
    this.searchString = searchString;
    this._searchResult = searchResult;
    this._defaultIndex = defaultIndex;
    this._errorDescription = errorDescription;
    this._items = items;
    this._formHistResult = prevResult;
    this.entries = prevResult ? prevResult.wrappedJSObject.entries : [];
  }

  // nsISupports
  QueryInterface = ChromeUtils.generateQI(["nsIAutoCompleteResult"]);

  // The user's query string
  searchString = "";

  // The result code of this result object, see |get searchResult| for possible values.
  _searchResult = 0;

  // The default item that should be entered if none is selected
  _defaultIndex = 0;

  // The reason the search failed
  _errorDescription = "";

  /**
   * A reference to the form history nsIAutocompleteResult that we're wrapping.
   * We use this to forward removeEntryAt calls as needed.
   */
  _formHistResult = null;

  entries = null;

  get wrappedJSObject() {
    return this;
  }

  /**
   * @returns {number} the result code of this result object, either:
   *         RESULT_IGNORED   (invalid searchString)
   *         RESULT_FAILURE   (failure)
   *         RESULT_NOMATCH   (no matches found)
   *         RESULT_SUCCESS   (matches found)
   */
  get searchResult() {
    return this._searchResult;
  }

  /**
   * @returns {number} the default item that should be entered if none is selected
   */
  get defaultIndex() {
    return this._defaultIndex;
  }

  /**
   * @returns {string} the reason the search failed
   */
  get errorDescription() {
    return this._errorDescription;
  }

  /**
   * @returns {number} the number of results
   */
  get matchCount() {
    return this._items.length;
  }

  _checkIndexBounds(index) {
    if (index < 0 || index >= this._items.length) {
      throw Components.Exception(
        "Index out of range.",
        Cr.NS_ERROR_ILLEGAL_VALUE
      );
    }
  }

  /**
   * Retrieves a result
   *
   * @param   {number} index   the index of the result requested
   * @returns {string}         the result at the specified index
   */
  getValueAt(index) {
    this._checkIndexBounds(index);
    return this._items[index].value;
  }

  getLabelAt(index) {
    this._checkIndexBounds(index);
    return this._items[index].label || this._items[index].value;
  }

  /**
   * Retrieves a comment (metadata instance)
   *
   * @param {number} index    the index of the comment requested
   * @returns {object}        the comment at the specified index
   */
  getCommentAt(index) {
    this._checkIndexBounds(index);
    return this._items[index].comment;
  }

  /**
   * Retrieves a style hint specific to a particular index.
   *
   * @param   {number} index   the index of the style hint requested
   * @returns {string|null}    the style hint at the specified index
   */
  getStyleAt(index) {
    this._checkIndexBounds(index);

    if (index < this._formHistResult?.matchCount) {
      return "fromhistory";
    }

    if (
      this._formHistResult?.matchCount > 0 &&
      index == this._formHistResult.matchCount
    ) {
      return "datalist-first";
    }

    return null;
  }

  /**
   * Retrieves an image url.
   *
   * @param   {number} index  the index of the image url requested
   * @returns {string}        the image url at the specified index
   */
  getImageAt(index) {
    this._checkIndexBounds(index);
    return "";
  }

  /**
   * Retrieves a result
   *
   * @param   {number} index   the index of the result requested
   * @returns {string}         the result at the specified index
   */
  getFinalCompleteValueAt(index) {
    return this.getValueAt(index);
  }

  /**
   * Returns true if the value at the given index is removable
   *
   * @param   {number}  index  the index of the result to remove
   * @returns {boolean}        True if the value is removable
   */
  isRemovableAt(index) {
    this._checkIndexBounds(index);
    return this._items[index].removable;
  }

  /**
   * Removes a result from the resultset
   *
   * @param {number}  index    the index of the result to remove
   */
  removeValueAt(index) {
    this._checkIndexBounds(index);
    // Forward the removeValueAt call to the underlying result if we have one
    // Note: this assumes that the form history results were added to the top
    // of our arrays.
    if (this._formHistResult && index < this._formHistResult.matchCount) {
      // Delete the history result from the DB
      this._formHistResult.removeValueAt(index);
    }
    this._items.splice(index, 1);
  }
}