-
Notifications
You must be signed in to change notification settings - Fork 23
/
autosuggest.js
executable file
·268 lines (213 loc) · 7.54 KB
/
autosuggest.js
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
t.module("glow.widgets.AutoSuggest");
t.test("Load DOM", function() {
t.expect(1);
t.stop();
glow.ready(function() {
t.ok(glow.isReady, "Document Ready");
t.start();
});
});
t.test("Set up", function() {
t.expect(1);
window.tempContainer = glow.dom.create('<div><div><input type="text" id="example"/></div></div>').appendTo("#mainOutput");
/*1*/ t.ok(glow.dom.get("#example"), "A test form element can be created.");
});
t.test("glow.widgets.AutoSuggest", function() {
t.expect(3);
/*1*/ t.ok(glow.widgets.AutoSuggest, "AutoSuggest module exists");
/*2*/ t.equals(typeof glow.widgets.AutoSuggest, "function", "glow.widgets.AutoSuggest is a function.");
var myForm = glow.dom.get("#example");
var myAutoSuggest = new glow.widgets.AutoSuggest(myForm, {}, {});
/*3*/ t.ok(myAutoSuggest, "A new form instance can be created.");
});
t.test("index", function() {
t.expect(6);
var myForm = glow.dom.get("#example");
glow.widgets.AutoSuggest.testDataSource = [
{ingredients:["Chilli"], name:"Ray's Firehouse Chilli", chef:"Raymond Blanc"},
{ingredients:["coriander", "Swordfish", "fish"], name:"Coriander spiced swordfish", chef:"Ainsley Harriott"},
{ingredients:["fish", "cherry"], name:"Poached Trout With Cherry Sauce", chef:"Antony Worrall Thompson"},
{ingredients:["apples","pears", "pecans"], name:"Flan", chef:"Delia Smith"}
];
var myAutoSuggest = new glow.widgets.AutoSuggest(
myForm,
glow.widgets.AutoSuggest.testDataSource,
{
index: "ingredients" // can be a single string, the name of a field in your data items
}
);
/*1*/ t.ok(myAutoSuggest.index["=cherry"], "Indexing on a field name should cause an item in that field to appear as a key in the index.");
/*2*/ t.ok(myAutoSuggest.index["=chilli"], "Keys should be indexed in lowercase.");
myAutoSuggest = new glow.widgets.AutoSuggest(
myForm,
glow.widgets.AutoSuggest.testDataSource,
{
index: ["ingredients", "chef"] // or can be multiple field names
}
);
/*3*/ t.ok(myAutoSuggest.index["=delia smith"], "Indexing on an array of field names should cause items in those fields to appear as keys in the index.");
myAutoSuggest = new glow.widgets.AutoSuggest(
myForm,
glow.widgets.AutoSuggest.testDataSource,
{
index: function(item) { // or can be a function that generates keywords to be indexed on
return item.ingredients.concat(item.chef.split(' '));
}
}
);
/*4*/ t.ok(myAutoSuggest.index["=delia"], "Using a function to generate the index should cause the generated keywords to appear as keys in the index.");
myAutoSuggest = new glow.widgets.AutoSuggest(
myForm,
glow.widgets.AutoSuggest.testDataSource,
{
index: ["chef"],
caseSensitive: true
}
);
/*5*/ t.ok(myAutoSuggest.index["=Delia Smith"], "Case sensitive indexing on an array of field names should cause items in those fields to appear as keys in the case sensitive index.");
myAutoSuggest = new glow.widgets.AutoSuggest(
myForm,
glow.widgets.AutoSuggest.testDataSource,
{
index: "ingredients" // or can be multiple field names
}
);
myAutoSuggest.find("pe"); // could be "pears" or "pecans"?
/*6*/ t.equals(myAutoSuggest._found.length, 1, "When a lookFor matches multiple indexes in the same suggestion, the suggestion should only be found once.");
myAutoSuggest.find("");
});
t.test("val", function() {
t.expect(4);
var myForm = glow.dom.get("#example");
myAutoSuggest = new glow.widgets.AutoSuggest(
myForm,
[]
);
/*1*/ t.equals(myAutoSuggest.val(), undefined, "The val should initially be empty.");
myAutoSuggest.val("test value 1");
/*2*/ t.equals(myAutoSuggest.val(), "test value 1", "The set val should be the same as the get value.");
myAutoSuggest.suggest("test value 1xyz");
/*3*/ t.equals(myAutoSuggest.inputElement.val(), "test value 1xyz", "The inputelement val should include suggested text.");
/*4*/ t.equals(myAutoSuggest.val(), "test value 1", "But the val should not include suggested text.");
});
t.test("filter", function() {
t.expect(3);
var myForm = glow.dom.get("#example");
var recipes = [
{name:"Green-bean Chilli", type:"Vegetarian"},
{name:"Green Seaweed Soup", type:"Seafood"},
{name:"Green Oysters on Ice", type:"Seafood"},
{name:"Green Apple Flan", type:"Pastry"}
];
myAutoSuggest = new glow.widgets.AutoSuggest(
myForm,
recipes,
{
filter: function(results) {
return results.slice(0, 2); // limit results to first 2
}
}
);
myAutoSuggest.find("green"); // should find all recipes
/*1*/ t.equals(myAutoSuggest._found.length, 2, "A filter can be applied to limit the results to a given length.");
myAutoSuggest._filter = function(results) {
var filtered = [];
for (var i = results.length; i--;) {
if (results[i].type == "Vegetarian") {
filtered.push(results[i]);
}
}
return filtered;
};
myAutoSuggest.find("green"); // should find all recipes
/*2*/ t.equals(myAutoSuggest._found.length, 1, "A filter can be applied to limit the results to those with a certain property.");
/*3*/ t.equals(myAutoSuggest._found[0].name, "Green-bean Chilli", "A filter can be applied and the expected result is returned.");
myAutoSuggest.find(""); // to hide the results
});
t.test("keyboard navigation", function() {
t.expect(5);
// discard any event handlers that may have been applied to the form in previous tests
window.tempContainer.empty();
window.tempContainer.html('<div><input type="text" id="example"/></div>');
var myForm = glow.dom.get("#example");
var recipes = [
{name:"Green-bean Chilli", type:"Vegetarian"},
{name:"Green Seaweed Soup", type:"Seafood"},
{name:"Green Oysters on Ice", type:"Seafood"},
{name:"Green Apple Flan", type:"Pastry"}
];
myAutoSuggest = new glow.widgets.AutoSuggest(
myForm,
recipes,
{
complete: true
}
);
// simulate a search
myAutoSuggest.val("green");
myAutoSuggest.find(); // should find all recipes
/*1*/ t.ok(myAutoSuggest._found.length, "A new input element can be created and used.");
// simulate an arrow down
glow.events.fire(
myAutoSuggest.inputElement[0],
"keydown",
new glow.events.Event(
{ key: "DOWN" }
)
);
/*2*/ t.equals(myAutoSuggest.inputElement[0].value, "Green Seaweed Soup", "Using arrow down with complete changes the value in the input element.");
// simulate two arrow ups
glow.events.fire(
myAutoSuggest.inputElement[0],
"keydown",
new glow.events.Event(
{ key: "UP" }
)
);
glow.events.fire(
myAutoSuggest.inputElement[0],
"keydown",
new glow.events.Event(
{ key: "UP" }
)
);
/*3*/ t.equals(myAutoSuggest.inputElement[0].value, "green", "Using arrow up restores the original value in the input element.");
// simulate ups and down arrow
glow.events.fire(
myAutoSuggest.inputElement[0],
"keydown",
new glow.events.Event(
{ key: "UP" }
)
);
glow.events.fire(
myAutoSuggest.inputElement[0],
"keydown",
new glow.events.Event(
{ key: "DOWN" }
)
);
/*4*/ t.equals(myAutoSuggest.inputElement[0].value, "green", "Using arrow down restores the original value in the input element.");
// simulate down arrow and then esc
glow.events.fire(
myAutoSuggest.inputElement[0],
"keydown",
new glow.events.Event(
{ key: "DOWN" }
)
);
glow.events.fire(
myAutoSuggest.inputElement[0],
"keydown",
new glow.events.Event(
{ key: "ESC" }
)
);
/*5*/ t.equals(myAutoSuggest.inputElement[0].value, "green", "Using arrow down restores the original value in the input element.");
});
t.test("cleanup", function() {
t.expect(1);
window.tempContainer.empty();
window.tempContainer = null;
t.ok(true, "Cleaned");
});