toolkit/components/satchel/nsFormAutoCompleteResult.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/satchel/nsFormAutoCompleteResult.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,184 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +this.EXPORTED_SYMBOLS = [ "FormAutoCompleteResult" ];
     1.9 +
    1.10 +const Ci = Components.interfaces;
    1.11 +const Cr = Components.results;
    1.12 +
    1.13 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.14 +
    1.15 +this.FormAutoCompleteResult =
    1.16 + function FormAutoCompleteResult(searchString,
    1.17 +                                 searchResult,
    1.18 +                                 defaultIndex,
    1.19 +                                 errorDescription,
    1.20 +                                 values,
    1.21 +                                 labels,
    1.22 +                                 comments,
    1.23 +                                 prevResult) {
    1.24 +  this.searchString = searchString;
    1.25 +  this._searchResult = searchResult;
    1.26 +  this._defaultIndex = defaultIndex;
    1.27 +  this._errorDescription = errorDescription;
    1.28 +  this._values = values;
    1.29 +  this._labels = labels;
    1.30 +  this._comments = comments;
    1.31 +  this._formHistResult = prevResult;
    1.32 +
    1.33 +  if (prevResult) {
    1.34 +    this.entries = prevResult.wrappedJSObject.entries;
    1.35 +  } else {
    1.36 +    this.entries = [];
    1.37 +  }
    1.38 +}
    1.39 +
    1.40 +FormAutoCompleteResult.prototype = {
    1.41 +
    1.42 +  // The user's query string
    1.43 +  searchString: "",
    1.44 +
    1.45 +  // The result code of this result object, see |get searchResult| for possible values.
    1.46 +  _searchResult: 0,
    1.47 +
    1.48 +  // The default item that should be entered if none is selected
    1.49 +  _defaultIndex: 0,
    1.50 +
    1.51 +  //The reason the search failed
    1.52 +  _errorDescription: "",
    1.53 +
    1.54 +  /**
    1.55 +   * A reference to the form history nsIAutocompleteResult that we're wrapping.
    1.56 +   * We use this to forward removeEntryAt calls as needed.
    1.57 +   */
    1.58 +  _formHistResult: null,
    1.59 +
    1.60 +  entries: null,
    1.61 +
    1.62 +  get wrappedJSObject() {
    1.63 +    return this;
    1.64 +  },
    1.65 +
    1.66 +  /**
    1.67 +   * @return the result code of this result object, either:
    1.68 +   *         RESULT_IGNORED   (invalid searchString)
    1.69 +   *         RESULT_FAILURE   (failure)
    1.70 +   *         RESULT_NOMATCH   (no matches found)
    1.71 +   *         RESULT_SUCCESS   (matches found)
    1.72 +   */
    1.73 +  get searchResult() {
    1.74 +    return this._searchResult;
    1.75 +  },
    1.76 +
    1.77 +  /**
    1.78 +   * @return the default item that should be entered if none is selected
    1.79 +   */
    1.80 +  get defaultIndex() {
    1.81 +    return this._defaultIndex;
    1.82 +  },
    1.83 +
    1.84 +  /**
    1.85 +   * @return the reason the search failed
    1.86 +   */
    1.87 +  get errorDescription() {
    1.88 +    return this._errorDescription;
    1.89 +  },
    1.90 +
    1.91 +  /**
    1.92 +   * @return the number of results
    1.93 +   */
    1.94 +  get matchCount() {
    1.95 +    return this._values.length;
    1.96 +  },
    1.97 +
    1.98 +  _checkIndexBounds : function (index) {
    1.99 +    if (index < 0 || index >= this._values.length) {
   1.100 +      throw Components.Exception("Index out of range.", Cr.NS_ERROR_ILLEGAL_VALUE);
   1.101 +    }
   1.102 +  },
   1.103 +
   1.104 +  /**
   1.105 +   * Retrieves a result
   1.106 +   * @param  index    the index of the result requested
   1.107 +   * @return          the result at the specified index
   1.108 +   */
   1.109 +  getValueAt: function(index) {
   1.110 +    this._checkIndexBounds(index);
   1.111 +    return this._values[index];
   1.112 +  },
   1.113 +
   1.114 +  getLabelAt: function(index) {
   1.115 +    this._checkIndexBounds(index);
   1.116 +    return this._labels[index];
   1.117 +  },
   1.118 +
   1.119 +  /**
   1.120 +   * Retrieves a comment (metadata instance)
   1.121 +   * @param  index    the index of the comment requested
   1.122 +   * @return          the comment at the specified index
   1.123 +   */
   1.124 +  getCommentAt: function(index) {
   1.125 +    this._checkIndexBounds(index);
   1.126 +    return this._comments[index];
   1.127 +  },
   1.128 +
   1.129 +  /**
   1.130 +   * Retrieves a style hint specific to a particular index.
   1.131 +   * @param  index    the index of the style hint requested
   1.132 +   * @return          the style hint at the specified index
   1.133 +   */
   1.134 +  getStyleAt: function(index) {
   1.135 +    this._checkIndexBounds(index);
   1.136 +    if (!this._comments[index]) {
   1.137 +      return null;  // not a category label, so no special styling
   1.138 +    }
   1.139 +
   1.140 +    if (index == 0) {
   1.141 +      return "suggestfirst";  // category label on first line of results
   1.142 +    }
   1.143 +
   1.144 +    return "suggesthint";   // category label on any other line of results
   1.145 +  },
   1.146 +
   1.147 +  /**
   1.148 +   * Retrieves an image url.
   1.149 +   * @param  index    the index of the image url requested
   1.150 +   * @return          the image url at the specified index
   1.151 +   */
   1.152 +  getImageAt: function(index) {
   1.153 +    this._checkIndexBounds(index);
   1.154 +    return "";
   1.155 +  },
   1.156 +
   1.157 +  /**
   1.158 +   * Retrieves a result
   1.159 +   * @param  index    the index of the result requested
   1.160 +   * @return          the result at the specified index
   1.161 +   */
   1.162 +  getFinalCompleteValueAt: function(index) {
   1.163 +    return this.getValueAt(index);
   1.164 +  },
   1.165 +
   1.166 +  /**
   1.167 +   * Removes a result from the resultset
   1.168 +   * @param  index    the index of the result to remove
   1.169 +   */
   1.170 +  removeValueAt: function(index, removeFromDatabase) {
   1.171 +    this._checkIndexBounds(index);
   1.172 +    // Forward the removeValueAt call to the underlying result if we have one
   1.173 +    // Note: this assumes that the form history results were added to the top
   1.174 +    // of our arrays.
   1.175 +    if (removeFromDatabase && this._formHistResult &&
   1.176 +        index < this._formHistResult.matchCount) {
   1.177 +      // Delete the history result from the DB
   1.178 +      this._formHistResult.removeValueAt(index, true);
   1.179 +    }
   1.180 +    this._values.splice(index, 1);
   1.181 +    this._labels.splice(index, 1);
   1.182 +    this._comments.splice(index, 1);
   1.183 +  },
   1.184 +
   1.185 +  // nsISupports
   1.186 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteResult])
   1.187 +};

mercurial