tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

autocomplete.js (4649B)


      1 const nsISupports = Ci.nsISupports;
      2 const nsIAutoCompleteResult = Ci.nsIAutoCompleteResult;
      3 const nsIAutoCompleteSearch = Ci.nsIAutoCompleteSearch;
      4 const nsIFactory = Ci.nsIFactory;
      5 const nsIUUIDGenerator = Ci.nsIUUIDGenerator;
      6 const nsIComponentRegistrar = Ci.nsIComponentRegistrar;
      7 
      8 var gDefaultAutoCompleteSearch = null;
      9 
     10 /**
     11 * Register 'test-a11y-search' AutoCompleteSearch.
     12 *
     13 * @param aValues [in] set of possible results values
     14 * @param aComments [in] set of possible results descriptions
     15 */
     16 function initAutoComplete(aValues, aComments) {
     17  var allResults = new ResultsHeap(aValues, aComments);
     18  gDefaultAutoCompleteSearch = new AutoCompleteSearch(
     19    "test-a11y-search",
     20    allResults
     21  );
     22  registerAutoCompleteSearch(
     23    gDefaultAutoCompleteSearch,
     24    "Accessibility Test AutoCompleteSearch"
     25  );
     26 }
     27 
     28 /**
     29 * Unregister 'test-a11y-search' AutoCompleteSearch.
     30 */
     31 function shutdownAutoComplete() {
     32  unregisterAutoCompleteSearch(gDefaultAutoCompleteSearch);
     33  gDefaultAutoCompleteSearch.cid = null;
     34  gDefaultAutoCompleteSearch = null;
     35 }
     36 
     37 /**
     38 * Register the given AutoCompleteSearch.
     39 *
     40 * @param aSearch       [in] AutoCompleteSearch object
     41 * @param aDescription  [in] description of the search object
     42 */
     43 function registerAutoCompleteSearch(aSearch, aDescription) {
     44  var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name;
     45 
     46  var uuidGenerator =
     47    Cc["@mozilla.org/uuid-generator;1"].getService(nsIUUIDGenerator);
     48  var cid = uuidGenerator.generateUUID();
     49 
     50  var componentManager = Components.manager.QueryInterface(
     51    nsIComponentRegistrar
     52  );
     53  componentManager.registerFactory(cid, aDescription, name, aSearch);
     54 
     55  // Keep the id on the object so we can unregister later.
     56  aSearch.cid = cid;
     57 }
     58 
     59 /**
     60 * Unregister the given AutoCompleteSearch.
     61 */
     62 function unregisterAutoCompleteSearch(aSearch) {
     63  var componentManager = Components.manager.QueryInterface(
     64    nsIComponentRegistrar
     65  );
     66  componentManager.unregisterFactory(aSearch.cid, aSearch);
     67 }
     68 
     69 /**
     70 * A container to keep all possible results of autocomplete search.
     71 */
     72 function ResultsHeap(aValues, aComments) {
     73  this.values = aValues;
     74  this.comments = aComments;
     75 }
     76 
     77 ResultsHeap.prototype = {
     78  constructor: ResultsHeap,
     79 
     80  /**
     81   * Return AutoCompleteResult for the given search string.
     82   */
     83  getAutoCompleteResultFor(aSearchString) {
     84    var values = [],
     85      comments = [];
     86    for (var idx = 0; idx < this.values.length; idx++) {
     87      if (this.values[idx].includes(aSearchString)) {
     88        values.push(this.values[idx]);
     89        comments.push(this.comments[idx]);
     90      }
     91    }
     92    return new AutoCompleteResult(values, comments);
     93  },
     94 };
     95 
     96 /**
     97 * nsIAutoCompleteSearch implementation.
     98 *
     99 * @param aName       [in] the name of autocomplete search
    100 * @param aAllResults [in] ResultsHeap object
    101 */
    102 function AutoCompleteSearch(aName, aAllResults) {
    103  this.name = aName;
    104  this.allResults = aAllResults;
    105 }
    106 
    107 AutoCompleteSearch.prototype = {
    108  constructor: AutoCompleteSearch,
    109 
    110  // nsIAutoCompleteSearch implementation
    111  startSearch(aSearchString, aSearchParam, aPreviousResult, aListener) {
    112    var result = this.allResults.getAutoCompleteResultFor(aSearchString);
    113    aListener.onSearchResult(this, result);
    114  },
    115 
    116  stopSearch() {},
    117 
    118  // nsISupports implementation
    119  QueryInterface: ChromeUtils.generateQI([
    120    "nsIFactory",
    121    "nsIAutoCompleteSearch",
    122  ]),
    123 
    124  // nsIFactory implementation
    125  createInstance(iid) {
    126    return this.QueryInterface(iid);
    127  },
    128 
    129  // Search name. Used by AutoCompleteController.
    130  name: null,
    131 
    132  // Results heap.
    133  allResults: null,
    134 };
    135 
    136 /**
    137 * nsIAutoCompleteResult implementation.
    138 */
    139 function AutoCompleteResult(aValues, aComments) {
    140  this.values = aValues;
    141  this.comments = aComments;
    142 
    143  if (this.values.length) {
    144    this.searchResult = nsIAutoCompleteResult.RESULT_SUCCESS;
    145  } else {
    146    this.searchResult = nsIAutoCompleteResult.NOMATCH;
    147  }
    148 }
    149 
    150 AutoCompleteResult.prototype = {
    151  constructor: AutoCompleteResult,
    152 
    153  searchString: "",
    154  searchResult: null,
    155 
    156  defaultIndex: 0,
    157 
    158  get matchCount() {
    159    return this.values.length;
    160  },
    161 
    162  getValueAt(aIndex) {
    163    return this.values[aIndex];
    164  },
    165 
    166  getLabelAt(aIndex) {
    167    return this.getValueAt(aIndex);
    168  },
    169 
    170  getCommentAt(aIndex) {
    171    return this.comments[aIndex];
    172  },
    173 
    174  getStyleAt() {
    175    return null;
    176  },
    177 
    178  getImageAt() {
    179    return "";
    180  },
    181 
    182  getFinalCompleteValueAt(aIndex) {
    183    return this.getValueAt(aIndex);
    184  },
    185 
    186  isRemovableAt() {
    187    return true;
    188  },
    189 
    190  removeValueAt() {},
    191 
    192  // nsISupports implementation
    193  QueryInterface: ChromeUtils.generateQI(["nsIAutoCompleteResult"]),
    194 
    195  // Data
    196  values: null,
    197  comments: null,
    198 };