toolkit/components/satchel/nsFormAutoCompleteResult.jsm

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 this.EXPORTED_SYMBOLS = [ "FormAutoCompleteResult" ];
     7 const Ci = Components.interfaces;
     8 const Cr = Components.results;
    10 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    12 this.FormAutoCompleteResult =
    13  function FormAutoCompleteResult(searchString,
    14                                  searchResult,
    15                                  defaultIndex,
    16                                  errorDescription,
    17                                  values,
    18                                  labels,
    19                                  comments,
    20                                  prevResult) {
    21   this.searchString = searchString;
    22   this._searchResult = searchResult;
    23   this._defaultIndex = defaultIndex;
    24   this._errorDescription = errorDescription;
    25   this._values = values;
    26   this._labels = labels;
    27   this._comments = comments;
    28   this._formHistResult = prevResult;
    30   if (prevResult) {
    31     this.entries = prevResult.wrappedJSObject.entries;
    32   } else {
    33     this.entries = [];
    34   }
    35 }
    37 FormAutoCompleteResult.prototype = {
    39   // The user's query string
    40   searchString: "",
    42   // The result code of this result object, see |get searchResult| for possible values.
    43   _searchResult: 0,
    45   // The default item that should be entered if none is selected
    46   _defaultIndex: 0,
    48   //The reason the search failed
    49   _errorDescription: "",
    51   /**
    52    * A reference to the form history nsIAutocompleteResult that we're wrapping.
    53    * We use this to forward removeEntryAt calls as needed.
    54    */
    55   _formHistResult: null,
    57   entries: null,
    59   get wrappedJSObject() {
    60     return this;
    61   },
    63   /**
    64    * @return the result code of this result object, either:
    65    *         RESULT_IGNORED   (invalid searchString)
    66    *         RESULT_FAILURE   (failure)
    67    *         RESULT_NOMATCH   (no matches found)
    68    *         RESULT_SUCCESS   (matches found)
    69    */
    70   get searchResult() {
    71     return this._searchResult;
    72   },
    74   /**
    75    * @return the default item that should be entered if none is selected
    76    */
    77   get defaultIndex() {
    78     return this._defaultIndex;
    79   },
    81   /**
    82    * @return the reason the search failed
    83    */
    84   get errorDescription() {
    85     return this._errorDescription;
    86   },
    88   /**
    89    * @return the number of results
    90    */
    91   get matchCount() {
    92     return this._values.length;
    93   },
    95   _checkIndexBounds : function (index) {
    96     if (index < 0 || index >= this._values.length) {
    97       throw Components.Exception("Index out of range.", Cr.NS_ERROR_ILLEGAL_VALUE);
    98     }
    99   },
   101   /**
   102    * Retrieves a result
   103    * @param  index    the index of the result requested
   104    * @return          the result at the specified index
   105    */
   106   getValueAt: function(index) {
   107     this._checkIndexBounds(index);
   108     return this._values[index];
   109   },
   111   getLabelAt: function(index) {
   112     this._checkIndexBounds(index);
   113     return this._labels[index];
   114   },
   116   /**
   117    * Retrieves a comment (metadata instance)
   118    * @param  index    the index of the comment requested
   119    * @return          the comment at the specified index
   120    */
   121   getCommentAt: function(index) {
   122     this._checkIndexBounds(index);
   123     return this._comments[index];
   124   },
   126   /**
   127    * Retrieves a style hint specific to a particular index.
   128    * @param  index    the index of the style hint requested
   129    * @return          the style hint at the specified index
   130    */
   131   getStyleAt: function(index) {
   132     this._checkIndexBounds(index);
   133     if (!this._comments[index]) {
   134       return null;  // not a category label, so no special styling
   135     }
   137     if (index == 0) {
   138       return "suggestfirst";  // category label on first line of results
   139     }
   141     return "suggesthint";   // category label on any other line of results
   142   },
   144   /**
   145    * Retrieves an image url.
   146    * @param  index    the index of the image url requested
   147    * @return          the image url at the specified index
   148    */
   149   getImageAt: function(index) {
   150     this._checkIndexBounds(index);
   151     return "";
   152   },
   154   /**
   155    * Retrieves a result
   156    * @param  index    the index of the result requested
   157    * @return          the result at the specified index
   158    */
   159   getFinalCompleteValueAt: function(index) {
   160     return this.getValueAt(index);
   161   },
   163   /**
   164    * Removes a result from the resultset
   165    * @param  index    the index of the result to remove
   166    */
   167   removeValueAt: function(index, removeFromDatabase) {
   168     this._checkIndexBounds(index);
   169     // Forward the removeValueAt call to the underlying result if we have one
   170     // Note: this assumes that the form history results were added to the top
   171     // of our arrays.
   172     if (removeFromDatabase && this._formHistResult &&
   173         index < this._formHistResult.matchCount) {
   174       // Delete the history result from the DB
   175       this._formHistResult.removeValueAt(index, true);
   176     }
   177     this._values.splice(index, 1);
   178     this._labels.splice(index, 1);
   179     this._comments.splice(index, 1);
   180   },
   182   // nsISupports
   183   QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteResult])
   184 };

mercurial