accessible/tests/mochitest/autocomplete.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     2 const nsISupports = Components.interfaces.nsISupports;
     3 const nsIAutoCompleteResult = Components.interfaces.nsIAutoCompleteResult;
     4 const nsIAutoCompleteSearch = Components.interfaces.nsIAutoCompleteSearch;
     5 const nsIFactory = Components.interfaces.nsIFactory;
     6 const nsIUUIDGenerator = Components.interfaces.nsIUUIDGenerator;
     7 const nsIComponentRegistrar = Components.interfaces.nsIComponentRegistrar;
     9 var gDefaultAutoCompleteSearch = null;
    11 /**
    12  * Register 'test-a11y-search' AutoCompleteSearch.
    13  *
    14  * @param aValues [in] set of possible results values
    15  * @param aComments [in] set of possible results descriptions
    16  */
    17 function initAutoComplete(aValues, aComments)
    18 {
    19   var allResults = new ResultsHeap(aValues, aComments);
    20   gDefaultAutoCompleteSearch =
    21     new AutoCompleteSearch("test-a11y-search", allResults);
    22   registerAutoCompleteSearch(gDefaultAutoCompleteSearch,
    23                              "Accessibility Test AutoCompleteSearch");
    24 }
    26 /**
    27  * Unregister 'test-a11y-search' AutoCompleteSearch.
    28  */
    29 function shutdownAutoComplete()
    30 {
    31   unregisterAutoCompleteSearch(gDefaultAutoCompleteSearch);
    32   gDefaultAutoCompleteSearch.cid = null;
    33   gDefaultAutoCompleteSearch = null;
    34 }
    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 {
    45   var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name;
    47   var uuidGenerator = Components.classes["@mozilla.org/uuid-generator;1"].
    48     getService(nsIUUIDGenerator);
    49   var cid = uuidGenerator.generateUUID();
    51   var componentManager = Components.manager.QueryInterface(nsIComponentRegistrar);
    52   componentManager.registerFactory(cid, aDescription, name, aSearch);
    54   // Keep the id on the object so we can unregister later.
    55   aSearch.cid = cid;
    56 }
    58 /**
    59  * Unregister the given AutoCompleteSearch.
    60  */
    61 function unregisterAutoCompleteSearch(aSearch)
    62 {
    63   var componentManager = Components.manager.QueryInterface(nsIComponentRegistrar);
    64   componentManager.unregisterFactory(aSearch.cid, aSearch);
    65 }
    68 /**
    69  * A container to keep all possible results of autocomplete search.
    70  */
    71 function ResultsHeap(aValues, aComments)
    72 {
    73   this.values = aValues;
    74   this.comments = aComments;
    75 }
    77 ResultsHeap.prototype =
    78 {
    79   constructor: ResultsHeap,
    81   /**
    82    * Return AutoCompleteResult for the given search string.
    83    */
    84   getAutoCompleteResultFor: function(aSearchString)
    85   {
    86     var values = [], comments = [];
    87     for (var idx = 0; idx < this.values.length; idx++) {
    88       if (this.values[idx].indexOf(aSearchString) != -1) {
    89         values.push(this.values[idx]);
    90         comments.push(this.comments[idx]);
    91       }
    92     }
    93     return new AutoCompleteResult(values, comments);
    94   }
    95 }
    98 /**
    99  * nsIAutoCompleteSearch implementation.
   100  *
   101  * @param aName       [in] the name of autocomplete search
   102  * @param aAllResults [in] ResultsHeap object
   103  */
   104 function AutoCompleteSearch(aName, aAllResults)
   105 {
   106   this.name = aName;
   107   this.allResults = aAllResults;
   108 }
   110 AutoCompleteSearch.prototype =
   111 {
   112   constructor: AutoCompleteSearch,
   114   // nsIAutoCompleteSearch implementation
   115   startSearch: function(aSearchString, aSearchParam, aPreviousResult,
   116                         aListener)
   117   {
   118     var result = this.allResults.getAutoCompleteResultFor(aSearchString);
   119     aListener.onSearchResult(this, result);
   120   },
   122   stopSearch: function() {},
   124   // nsISupports implementation
   125   QueryInterface: function(iid)
   126   {
   127     if (iid.equals(nsISupports) ||
   128         iid.equals(nsIFactory) ||
   129         iid.equals(nsIAutoCompleteSearch))
   130       return this;
   132     throw Components.results.NS_ERROR_NO_INTERFACE;
   133   },
   135   // nsIFactory implementation
   136   createInstance: function(outer, iid)
   137   {
   138     return this.QueryInterface(iid);
   139   },
   141   // Search name. Used by AutoCompleteController.
   142   name: null,
   144   // Results heap.
   145   allResults: null
   146 }
   149 /**
   150  * nsIAutoCompleteResult implementation.
   151  */
   152 function AutoCompleteResult(aValues, aComments)
   153 {
   154   this.values = aValues;
   155   this.comments = aComments;
   157   if (this.values.length > 0)
   158     this.searchResult = nsIAutoCompleteResult.RESULT_SUCCESS;
   159   else
   160     this.searchResult = nsIAutoCompleteResult.NOMATCH;
   161 }
   163 AutoCompleteResult.prototype =
   164 {
   165   constructor: AutoCompleteResult,
   167   searchString: "",
   168   searchResult: null,
   170   defaultIndex: 0,
   172   get matchCount()
   173   {
   174     return this.values.length;
   175   },
   177   getValueAt: function(aIndex)
   178   {
   179     return this.values[aIndex];
   180   },
   182   getLabelAt: function(aIndex)
   183   {
   184     return this.getValueAt(aIndex);
   185   },
   187   getCommentAt: function(aIndex)
   188   {
   189     return this.comments[aIndex];
   190   },
   192   getStyleAt: function(aIndex)
   193   {
   194     return null;
   195   },
   197   getImageAt: function(aIndex)
   198   {
   199     return "";
   200   },
   202   getFinalCompleteValueAt: function(aIndex)
   203   {
   204     return this.getValueAt(aIndex);
   205   },
   207   removeValueAt: function (aRowIndex, aRemoveFromDb) {},
   209   // nsISupports implementation
   210   QueryInterface: function(iid) {
   211     if (iid.equals(nsISupports) ||
   212         iid.equals(nsIAutoCompleteResult))
   213       return this;
   215     throw Components.results.NS_ERROR_NO_INTERFACE;
   216   },
   218   // Data
   219   values: null,
   220   comments: null
   221 }

mercurial