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;
8 var gDefaultAutoCompleteSearch = null;
11 * Register 'test-a11y-search' AutoCompleteSearch.
13 * @param aValues [in] set of possible results values
14 * @param aComments [in] set of possible results descriptions
16 function initAutoComplete(aValues, aComments) {
17 var allResults = new ResultsHeap(aValues, aComments);
18 gDefaultAutoCompleteSearch = new AutoCompleteSearch(
22 registerAutoCompleteSearch(
23 gDefaultAutoCompleteSearch,
24 "Accessibility Test AutoCompleteSearch"
29 * Unregister 'test-a11y-search' AutoCompleteSearch.
31 function shutdownAutoComplete() {
32 unregisterAutoCompleteSearch(gDefaultAutoCompleteSearch);
33 gDefaultAutoCompleteSearch.cid = null;
34 gDefaultAutoCompleteSearch = null;
38 * Register the given AutoCompleteSearch.
40 * @param aSearch [in] AutoCompleteSearch object
41 * @param aDescription [in] description of the search object
43 function registerAutoCompleteSearch(aSearch, aDescription) {
44 var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name;
47 Cc["@mozilla.org/uuid-generator;1"].getService(nsIUUIDGenerator);
48 var cid = uuidGenerator.generateUUID();
50 var componentManager = Components.manager.QueryInterface(
53 componentManager.registerFactory(cid, aDescription, name, aSearch);
55 // Keep the id on the object so we can unregister later.
60 * Unregister the given AutoCompleteSearch.
62 function unregisterAutoCompleteSearch(aSearch) {
63 var componentManager = Components.manager.QueryInterface(
66 componentManager.unregisterFactory(aSearch.cid, aSearch);
70 * A container to keep all possible results of autocomplete search.
72 function ResultsHeap(aValues, aComments) {
73 this.values = aValues;
74 this.comments = aComments;
77 ResultsHeap.prototype = {
78 constructor: ResultsHeap,
81 * Return AutoCompleteResult for the given search string.
83 getAutoCompleteResultFor(aSearchString) {
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]);
92 return new AutoCompleteResult(values, comments);
97 * nsIAutoCompleteSearch implementation.
99 * @param aName [in] the name of autocomplete search
100 * @param aAllResults [in] ResultsHeap object
102 function AutoCompleteSearch(aName, aAllResults) {
104 this.allResults = aAllResults;
107 AutoCompleteSearch.prototype = {
108 constructor: AutoCompleteSearch,
110 // nsIAutoCompleteSearch implementation
111 startSearch(aSearchString, aSearchParam, aPreviousResult, aListener) {
112 var result = this.allResults.getAutoCompleteResultFor(aSearchString);
113 aListener.onSearchResult(this, result);
118 // nsISupports implementation
119 QueryInterface: ChromeUtils.generateQI([
121 "nsIAutoCompleteSearch",
124 // nsIFactory implementation
125 createInstance(iid) {
126 return this.QueryInterface(iid);
129 // Search name. Used by AutoCompleteController.
137 * nsIAutoCompleteResult implementation.
139 function AutoCompleteResult(aValues, aComments) {
140 this.values = aValues;
141 this.comments = aComments;
143 if (this.values.length) {
144 this.searchResult = nsIAutoCompleteResult.RESULT_SUCCESS;
146 this.searchResult = nsIAutoCompleteResult.NOMATCH;
150 AutoCompleteResult.prototype = {
151 constructor: AutoCompleteResult,
159 return this.values.length;
163 return this.values[aIndex];
167 return this.getValueAt(aIndex);
170 getCommentAt(aIndex) {
171 return this.comments[aIndex];
182 getFinalCompleteValueAt(aIndex) {
183 return this.getValueAt(aIndex);
192 // nsISupports implementation
193 QueryInterface: ChromeUtils.generateQI(["nsIAutoCompleteResult"]),