This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Autocomplete.js
486 lines (396 loc) · 15.5 KB
/
Autocomplete.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
(function (window, $, ch) {
'use strict';
/**
* Autocomplete Component shows a list of suggestions for a HTMLInputElement.
* @memberof ch
* @constructor
* @augments ch.Component
* @requires ch.Popover
* @param {(jQuerySelector | ZeptoSelector)} $el A jQuery or Zepto Selector to create an instance of ch.Autocomplete.
* @param {Object} [options] Options to customize an instance.
* @param {String} [options.loadingClass] Default: "ch-autocomplete-loading".
* @param {String} [options.highlightedClass] Default: "ch-autocomplete-highlighted".
* @param {String} [options.itemClass] Default: "ch-autocomplete-item".
* @param {String} [options.addClass] CSS class names that will be added to the container on the component initialization. Default: "ch-box-lite ch-autocomplete".
* @param {Number} [options.keystrokesTime] Default: 150.
* @param {Boolean} [options.html] Default: false.
* @param {String} [options.side] The side option where the target element will be positioned. You must use: "left", "right", "top", "bottom" or "center". Default: "bottom".
* @param {String} [options.align] The align options where the target element will be positioned. You must use: "left", "right", "top", "bottom" or "center". Default: "left".
* @param {Number} [options.offsetX] The offsetX option specifies a distance to displace the target horitontally.
* @param {Number} [options.offsetY] The offsetY option specifies a distance to displace the target vertically.
* @param {String} [options.positioned] The positioned option specifies the type of positioning used. You must use: "absolute" or "fixed". Default: "absolute".
* @returns {autocomplete}
* @example
* // Create a new AutoComplete with configuration.
* var autocomplete = $el.autocomplete();
*/
function Autocomplete($el, options) {
/**
* Reference to context of an instance.
* @type {Object}
* @private
*/
var that = this;
this._init($el, options);
/**
* Event emitted when the component is ready to use.
* @event ch.Autocomplete#ready
* @example
* // Subscribe to "ready" event.
* autocomplete.on('ready',function () {
* // Some code here!
* });
*/
window.setTimeout(function () { that.emit('ready'); }, 50);
return this;
}
// Inheritance
var parent = ch.util.inherits(Autocomplete, ch.Component);
/**
* The name of the component.
* @type {String}
*/
Autocomplete.prototype.name = 'autocomplete';
/**
* Returns a reference to the constructor function.
* @memberof! ch.Autocomplete.prototype
* @function
*/
Autocomplete.prototype.constructor = Autocomplete;
/**
* Configuration by default.
* @type {Object}
* @private
*/
Autocomplete.prototype._defaults = {
'loadingClass': 'ch-autocomplete-loading',
'highlightedClass': 'ch-autocomplete-highlighted',
'itemClass': 'ch-autocomplete-item',
'addClass': 'ch-box-lite ch-autocomplete',
'side': 'bottom',
'align': 'left',
'html': false,
'_hiddenby': 'none',
'keystrokesTime': 150,
'_itemTemplate': '<li class="{{itemClass}}"{{suggestedData}}>{{term}}<i class="ch-icon-arrow-up" data-js="ch-autocomplete-complete-query"></i></li>'
};
/**
* Initialize a new instance of Autocomplete and merge custom options with defaults options.
* @memberof! ch.Autocomplete.prototype
* @function
* @private
* @returns {autocomplete}
*/
Autocomplete.prototype._init = function ($el, options) {
/**
* Reference to context of an instance.
* @type {Object}
* @private
*/
var that = this,
POINTERDOWN = 'mousedown' + '.' + this.name,
MOUSEENTER = 'mouseover' + '.' + this.name,
// there is no mouseenter to highlight the item, so it happens when the user do mousedown
highlightEvent = (ch.support.touch) ? POINTERDOWN : MOUSEENTER;
// Call to its parent init method
parent._init.call(this, $el, options);
// creates the basic item template for this instance
this._options._itemTemplate = this._options._itemTemplate.replace('{{itemClass}}', this._options.itemClass);
/**
* The autocomplete suggestion list.
* @type {(jQuerySelector | ZeptoSelector)}
* @private
*/
this._$suggestionsList = $('<ul class="ch-autocomplete-list"></ul>');
// The component who shows and manage the suggestions.
this._popover = $.popover({
'reference': this._$el,
'content': this._$suggestionsList,
'side': this._options.side,
'align': this._options.align,
'addClass': this._options.addClass,
'hiddenby': this._options._hiddenby,
'width': (this._el.getBoundingClientRect().width - 22) + 'px',
'fx': this._options.fx
});
/**
* The autocomplete container.
* @type {(jQuerySelector | ZeptoSelector)}
* @example
* // Gets the autocomplete container.
* autocomplete.$container;
*/
this.$container = this._popover.$container.attr('aria-hidden', 'true')
.on(highlightEvent, function (event) {
that._highlightSuggestion($(event.target));
})
.on(POINTERDOWN, function (event) {
// completes the value, it is a shortcut to avoid write the complete word
if (event.target.nodeName === 'I' && !that._options.html) {
ch.util.prevent(event);
that._el.value = that._suggestions[that._highlighted];
that.emit('type', that._el.value);
return;
}
if ((event.target.nodeName === 'LI' && event.target.className.indexOf(that._options.itemClass) !== -1) || (event.target.parentElement.nodeName === 'LI' && event.target.parentElement.className.indexOf(that._options.itemClass) !== -1)) {
that._selectSuggestion();
}
});
/**
* The autocomplete trigger.
* @type {(jQuerySelector | ZeptoSelector)}
*/
this.$trigger = this._$el
.attr({
'aria-autocomplete': 'list',
'aria-haspopup': 'true',
'aria-owns': this.$container[0].id,
'autocomplete': 'off'
})
.on('focus.' + this.name, function () {
that._turnOn();
})
.on('blur.' + this.name, function () {
that._turnOff();
});
// The number of the selected item or null when no selected item is.
this._highlighted = null;
// Collection of suggestions to be shown.
this._suggestions = [];
// Used to show when the user cancel the suggestions
this._originalQuery = this._currentQuery = this._el.value;
this.on('disable', function () {
if (that.isShown()) {
that.hide();
that._el.blur();
}
});
if (this._configureShortcuts !== undefined) {
this._configureShortcuts();
}
return this;
};
/**
*
* @memberof! ch.Autocomplete.prototype
* @function
* @private
* @returns {autocomplete}
*/
Autocomplete.prototype._turnOn = function () {
if (!this._enabled) {
return this;
}
var that = this;
this._originalQuery = this._el.value;
this.$trigger.on(ch.onkeyinput, function () {
// .trim()
that._currentQuery = that._el.value.replace(/^\s+|\s+$/g, '');
if (that._currentQuery === '') {
return that.hide();
}
// when the user writes
window.clearTimeout(that._stopTyping);
that._stopTyping = window.setTimeout(function () {
that.$trigger.addClass(that._options.loadingClass);
that.emit('type', that._currentQuery);
}, that._options.keystrokesTime);
});
return this;
};
/**
*
* @memberof! ch.Autocomplete.prototype
* @function
* @private
* @returns {autocomplete}
*/
Autocomplete.prototype._turnOff = function () {
if (!this._enabled) {
return this;
}
this.hide();
this.$trigger.off(ch.onkeyinput);
return this;
};
/**
* It sets to the HTMLInputElement the selected query and it emits a 'select' event.
* @memberof! ch.Autocomplete.prototype
* @function
* @private
* @returns {autocomplete}
*/
Autocomplete.prototype._selectSuggestion = function () {
window.clearTimeout(this._stopTyping);
if (this._highlighted === null) {
return this;
}
if (!this._options.html) {
this._el.value = this._suggestions[this._highlighted];
}
this._el.blur();
/**
* Event emitted when a suggestion is selected.
* @event ch.Autocomplete#select
* @example
* // Subscribe to "select" event.
* autocomplete.on('select', function () {
* // Some code here!
* });
*/
this.emit('select');
return this;
};
/**
* Selects the items
* @memberof! ch.Autocomplete.prototype
* @function
* @private
* @returns {autocomplete}
*/
Autocomplete.prototype._highlightSuggestion = function ($target) {
var $suggestion = $target.attr('aria-posinset') ? $target : $target.parents('li[aria-posinset]');
// TODO: Documentation - Number or null
this._highlighted = ($suggestion[0] !== undefined) ? (parseInt($suggestion.attr('aria-posinset'), 10) - 1) : null;
this._toogleHighlighted();
return this;
};
/**
* It highlights the item adding the "ch-autocomplete-highlighted" class name or the class name that you configured as "highlightedClass" option.
* @memberof! ch.Autocomplete.prototype
* @function
* @private
* @returns {autocomplete}
*/
Autocomplete.prototype._toogleHighlighted = function () {
var id = '#' + this.$container[0].id,
// null is when is not a selected item but,
// increments 1 _highlighted because aria-posinset starts in 1 instead 0 as the collection that stores the data
current = (this._highlighted === null) ? null : (this._highlighted + 1);
// background the highlighted item
$(id + ' [aria-posinset].' + this._options.highlightedClass).removeClass(this._options.highlightedClass);
// highlight the selected item
$(id + ' [aria-posinset="' + current + '"]').addClass(this._options.highlightedClass);
return this;
};
/**
* Add suggestions to be shown.
* @memberof! ch.Autocomplete.prototype
* @function
* @returns {autocomplete}
*/
Autocomplete.prototype.suggest = function (suggestions) {
/**
* Reference to context of an instance.
* @type {Object}
* @private
*/
var that = this,
items = [],
matchedRegExp = new RegExp('(' + this._currentQuery.replace(/([.*+?^=!:${}()|[\]\/\\])/g, "\\$1") + ')', 'ig'),
totalItems = 0,
$items,
itemTemplate = this._options._itemTemplate,
suggestedItem,
term,
suggestionsLength = suggestions.length,
el;
// hide the loading feedback
this.$trigger.removeClass(that._options.loadingClass);
// hides the suggestions list
if (suggestionsLength === 0) {
this._popover.hide();
return this;
}
// shows the suggestions list when the is closed and the element is withs focus
if (!this._popover.isShown() && window.document.activeElement === this._el) {
this._popover.show();
}
// remove the class from the extra added items
$('.' + this._options.highlightedClass, this.$container).removeClass(this._options.highlightedClass);
// add each suggested item to the suggestion list
for (suggestedItem = 0; suggestedItem < suggestionsLength; suggestedItem += 1) {
// get the term to be replaced
term = suggestions[suggestedItem];
// for the html configured component doesn't highlight the term matched it must be done by the user
if (!that._options.html) {
term = term.replace(matchedRegExp, '<strong>$1</strong>');
itemTemplate = this._options._itemTemplate.replace('{{suggestedData}}', ' data-suggested="' + suggestions[suggestedItem] + '"');
}
items.push(itemTemplate.replace('{{term}}', term));
}
this._$suggestionsList[0].innerHTML = items.join('');
$items = $('.' + this._options.itemClass, this.$container);
// with this we set the aria-setsize value that counts the total
totalItems = $items.length;
// Reset suggestions collection.
this._suggestions.length = 0;
for (suggestedItem = 0; suggestedItem < totalItems; suggestedItem += 1) {
el = $items[suggestedItem];
// add the data to the suggestions collection
that._suggestions.push(el.getAttribute('data-suggested'));
el.setAttribute('aria-posinset', that._suggestions.length);
el.setAttribute('aria-setsize', totalItems);
}
this._highlighted = null;
this._suggestionsQuantity = this._suggestions.length;
return this;
};
/**
* Hides component's container.
* @memberof! ch.Autocomplete.prototype
* @function
* @returns {autocomplete}
* @example
* // Hides an autocomplete.
* autocomplete.hide();
*/
Autocomplete.prototype.hide = function () {
if (!this._enabled) {
return this;
}
this._popover.hide();
/**
* Event emitted when the Autocomplete container is hidden.
* @event ch.Autocomplete#hide
* @example
* // Subscribe to "hide" event.
* autocomplete.on('hide', function () {
* // Some code here!
* });
*/
this.emit('hide');
return this;
};
/**
* Returns a Boolean if the component's core behavior is shown. That means it will return 'true' if the component is on and it will return false otherwise.
* @memberof! ch.Autocomplete.prototype
* @function
* @returns {Boolean}
* @example
* // Execute a function if the component is shown.
* if (autocomplete.isShown()) {
* fn();
* }
*/
Autocomplete.prototype.isShown = function () {
return this._popover.isShown();
};
/**
* Destroys an Autocomplete instance.
* @memberof! ch.Autocomplete.prototype
* @function
* @example
* // Destroying an instance of Autocomplete.
* autocomplete.destroy();
*/
Autocomplete.prototype.destroy = function () {
this.$trigger
.off('.' + this.name)
.removeAttr('autocomplete aria-autocomplete aria-haspopup aria-owns');
this._popover.destroy();
parent.destroy.call(this);
return;
};
ch.factory(Autocomplete);
}(this, this.ch.$, this.ch));