forked from learnaria/learnaria.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathik_suggest.js
249 lines (188 loc) · 6.82 KB
/
ik_suggest.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
;(function ( $, window, document, undefined ) {
var pluginName = "ik_suggest",
defaults = {
'instructions': "As you start typing the application might suggest similar search terms. Use up and down arrow keys to select a suggested search string.",
'minLength': 2,
'maxResults': 10,
'source': []
};
/**
* @constructs Plugin
* @param {Object} element - Current DOM element from selected collection.
* @param {Object} options - Configuration options.
* @param {string} options.instructions - Custom instructions for screen reader users.
* @param {number} options.minLength - Mininmum string length before sugestions start showing.
* @param {number} options.maxResults - Maximum number of shown suggestions.
*/
function Plugin( element, options ) {
this.element = $(element);
this.options = $.extend( {}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
/** Initializes plugin. */
Plugin.prototype.init = function () {
var $elem, plugin;
plugin = this;
plugin.notify = $('<div/>') // add hidden live region to be used by screen readers
.addClass('ik_readersonly')
.attr({
'role': 'region',
'aria-live': 'polite'
})
;
$elem = plugin.element
.attr({
'autocomplete': 'off'
})
.wrap('<span class="ik_suggest"></span>')
.on('focus', {'plugin': plugin}, plugin.onFocus)
.on('keydown', {'plugin': plugin}, plugin.onKeyDown) // add keydown event
.on('keyup', {'plugin': plugin}, plugin.onKeyUp) // add keyup event
.on('focusout', {'plugin': plugin}, plugin.onFocusOut); // add focusout event
plugin.list = $('<ul/>').addClass('suggestions');
$elem.after(plugin.notify, plugin.list);
};
/**
* Handles focus event on text field.
*
* @param {object} event - Keyboard event.
* @param {object} event.data - Event data.
* @param {object} event.data.plugin - Reference to plugin.
*/
Plugin.prototype.onFocus = function (event) {
var plugin;
plugin = event.data.plugin;
plugin.notify.text(plugin.options.instructions);
};
/**
* Handles kedown event on text field.
*
* @param {object} event - Keyboard event.
* @param {object} event.data - Event data.
* @param {object} event.data.plugin - Reference to plugin.
*/
Plugin.prototype.onKeyDown = function (event) {
var plugin, selected;
plugin = event.data.plugin;
switch (event.keyCode) {
case ik_utils.keys.tab:
case ik_utils.keys.esc:
plugin.list.empty().hide(); // empty list and hide suggestion box
break;
case ik_utils.keys.enter:
selected = plugin.list.find('.selected');
plugin.element.val( selected.text() ); // set text field value to the selected option
plugin.list.empty().hide(); // empty list and hide suggestion box
break;
}
};
/**
* Handles keyup event on text field.
*
* @param {object} event - Keyboard event.
* @param {object} event.data - Event data.
* @param {object} event.data.plugin - Reference to plugin.
*/
Plugin.prototype.onKeyUp = function (event) {
var plugin, $me, suggestions, selected, msg;
plugin = event.data.plugin;
$me = $(event.currentTarget);
switch (event.keyCode) {
case ik_utils.keys.down: // select next suggestion from list
selected = plugin.list.find('.selected');
if(selected.length) {
msg = selected.removeClass('selected').next().addClass('selected').text();
} else {
msg = plugin.list.find('li:first').addClass('selected').text();
}
plugin.notify.text(msg); // add suggestion text to live region to be read by screen reader
break;
case ik_utils.keys.up: // select previous suggestion from list
selected = plugin.list.find('.selected');
if(selected.length) {
msg = selected.removeClass('selected').prev().addClass('selected').text();
}
plugin.notify.text(msg); // add suggestion text to live region to be read by screen reader
break;
default: // get suggestions based on user input
plugin.list.empty();
suggestions = plugin.getSuggestions(plugin.options.source, $me.val());
if (suggestions.length > 1) {
for(var i = 0, l = suggestions.length; i < l; i++) {
$('<li/>').html(suggestions[i])
.on('click', {'plugin': plugin}, plugin.onOptionClick) // add click event handler
.appendTo(plugin.list);
}
plugin.list.show();
} else {
plugin.list.hide();
}
break;
}
};
/**
* Handles fosucout event on text field.
*
* @param {object} event - Focus event.
* @param {object} event.data - Event data.
* @param {object} event.data.plugin - Reference to plugin.
*/
Plugin.prototype.onFocusOut = function (event) {
var plugin = event.data.plugin;
setTimeout(function() { plugin.list.empty().hide(); }, 200);
};
/**
* Handles click event on suggestion box list item.
*
* @param {object} event - Keyboard event.
* @param {object} event.data - Event data.
* @param {object} event.data.plugin - Reference to plugin.
*/
Plugin.prototype.onOptionClick = function (event) {
var plugin, $option;
event.preventDefault();
event.stopPropagation();
plugin = event.data.plugin;
$option = $(event.currentTarget);
plugin.element.val( $option.text() );
plugin.list.empty().hide();
};
/**
* Gets a list of suggestions.
*
* @param {array} arr - Source array.
* @param {string} str - Search string.
*/
Plugin.prototype.getSuggestions = function (arr, str) {
var r, pattern, regex, len, limit;
r = [];
pattern = '(\\b' + str + ')';
regex = new RegExp(pattern, 'gi');
len = this.options.minLength;
limit = this.options.maxResults;
if (str.length >= len) {
for (var i = 0, l = arr.length; i < l ; i++) {
if (r.length > limit ) {
break;
}
if ( regex.test(arr[i]) ) {
r.push(arr[i].replace(regex, '<span>$1</span>'));
}
}
}
if (r.length > 1) { // add instructions to hidden live area
this.notify.text('Suggestions are available for this field. Use up and down arrows to select a suggestion and enter key to use it.');
}
return r;
};
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if ( !$.data(this, pluginName )) {
$.data( this, pluginName,
new Plugin( this, options ));
}
});
}
})( jQuery, window, document );