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