|
1 |
|
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; |
|
8 |
|
9 var gDefaultAutoCompleteSearch = null; |
|
10 |
|
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 } |
|
25 |
|
26 /** |
|
27 * Unregister 'test-a11y-search' AutoCompleteSearch. |
|
28 */ |
|
29 function shutdownAutoComplete() |
|
30 { |
|
31 unregisterAutoCompleteSearch(gDefaultAutoCompleteSearch); |
|
32 gDefaultAutoCompleteSearch.cid = null; |
|
33 gDefaultAutoCompleteSearch = null; |
|
34 } |
|
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 { |
|
45 var name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name; |
|
46 |
|
47 var uuidGenerator = Components.classes["@mozilla.org/uuid-generator;1"]. |
|
48 getService(nsIUUIDGenerator); |
|
49 var cid = uuidGenerator.generateUUID(); |
|
50 |
|
51 var componentManager = Components.manager.QueryInterface(nsIComponentRegistrar); |
|
52 componentManager.registerFactory(cid, aDescription, name, aSearch); |
|
53 |
|
54 // Keep the id on the object so we can unregister later. |
|
55 aSearch.cid = cid; |
|
56 } |
|
57 |
|
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 } |
|
66 |
|
67 |
|
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 } |
|
76 |
|
77 ResultsHeap.prototype = |
|
78 { |
|
79 constructor: ResultsHeap, |
|
80 |
|
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 } |
|
96 |
|
97 |
|
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 } |
|
109 |
|
110 AutoCompleteSearch.prototype = |
|
111 { |
|
112 constructor: AutoCompleteSearch, |
|
113 |
|
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 }, |
|
121 |
|
122 stopSearch: function() {}, |
|
123 |
|
124 // nsISupports implementation |
|
125 QueryInterface: function(iid) |
|
126 { |
|
127 if (iid.equals(nsISupports) || |
|
128 iid.equals(nsIFactory) || |
|
129 iid.equals(nsIAutoCompleteSearch)) |
|
130 return this; |
|
131 |
|
132 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
133 }, |
|
134 |
|
135 // nsIFactory implementation |
|
136 createInstance: function(outer, iid) |
|
137 { |
|
138 return this.QueryInterface(iid); |
|
139 }, |
|
140 |
|
141 // Search name. Used by AutoCompleteController. |
|
142 name: null, |
|
143 |
|
144 // Results heap. |
|
145 allResults: null |
|
146 } |
|
147 |
|
148 |
|
149 /** |
|
150 * nsIAutoCompleteResult implementation. |
|
151 */ |
|
152 function AutoCompleteResult(aValues, aComments) |
|
153 { |
|
154 this.values = aValues; |
|
155 this.comments = aComments; |
|
156 |
|
157 if (this.values.length > 0) |
|
158 this.searchResult = nsIAutoCompleteResult.RESULT_SUCCESS; |
|
159 else |
|
160 this.searchResult = nsIAutoCompleteResult.NOMATCH; |
|
161 } |
|
162 |
|
163 AutoCompleteResult.prototype = |
|
164 { |
|
165 constructor: AutoCompleteResult, |
|
166 |
|
167 searchString: "", |
|
168 searchResult: null, |
|
169 |
|
170 defaultIndex: 0, |
|
171 |
|
172 get matchCount() |
|
173 { |
|
174 return this.values.length; |
|
175 }, |
|
176 |
|
177 getValueAt: function(aIndex) |
|
178 { |
|
179 return this.values[aIndex]; |
|
180 }, |
|
181 |
|
182 getLabelAt: function(aIndex) |
|
183 { |
|
184 return this.getValueAt(aIndex); |
|
185 }, |
|
186 |
|
187 getCommentAt: function(aIndex) |
|
188 { |
|
189 return this.comments[aIndex]; |
|
190 }, |
|
191 |
|
192 getStyleAt: function(aIndex) |
|
193 { |
|
194 return null; |
|
195 }, |
|
196 |
|
197 getImageAt: function(aIndex) |
|
198 { |
|
199 return ""; |
|
200 }, |
|
201 |
|
202 getFinalCompleteValueAt: function(aIndex) |
|
203 { |
|
204 return this.getValueAt(aIndex); |
|
205 }, |
|
206 |
|
207 removeValueAt: function (aRowIndex, aRemoveFromDb) {}, |
|
208 |
|
209 // nsISupports implementation |
|
210 QueryInterface: function(iid) { |
|
211 if (iid.equals(nsISupports) || |
|
212 iid.equals(nsIAutoCompleteResult)) |
|
213 return this; |
|
214 |
|
215 throw Components.results.NS_ERROR_NO_INTERFACE; |
|
216 }, |
|
217 |
|
218 // Data |
|
219 values: null, |
|
220 comments: null |
|
221 } |