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
|
const nsISupports = Ci.nsISupports;
const nsIAutoCompleteResult = Ci.nsIAutoCompleteResult;
const nsIAutoCompleteSearch = Ci.nsIAutoCompleteSearch;
const nsIFactory = Ci.nsIFactory;
const nsIUUIDGenerator = Ci.nsIUUIDGenerator;
const nsIComponentRegistrar = Ci.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 =
Cc["@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(aSearchString) {
var values = [],
comments = [];
for (var idx = 0; idx < this.values.length; idx++) {
if (this.values[idx].includes(aSearchString)) {
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(aSearchString, aSearchParam, aPreviousResult, aListener) {
var result = this.allResults.getAutoCompleteResultFor(aSearchString);
aListener.onSearchResult(this, result);
},
stopSearch() {},
// nsISupports implementation
QueryInterface: ChromeUtils.generateQI([
"nsIFactory",
"nsIAutoCompleteSearch",
]),
// nsIFactory implementation
createInstance(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) {
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(aIndex) {
return this.values[aIndex];
},
getLabelAt(aIndex) {
return this.getValueAt(aIndex);
},
getCommentAt(aIndex) {
return this.comments[aIndex];
},
getStyleAt() {
return null;
},
getImageAt() {
return "";
},
getFinalCompleteValueAt(aIndex) {
return this.getValueAt(aIndex);
},
isRemovableAt() {
return true;
},
removeValueAt() {},
// nsISupports implementation
QueryInterface: ChromeUtils.generateQI(["nsIAutoCompleteResult"]),
// Data
values: null,
comments: null,
};
|