|
@@ -0,0 +1,435 @@
|
|
1
|
+/* =============================================================
|
|
2
|
+ * bootstrap3-typeahead.js v3.0.3
|
|
3
|
+ * https://github.com/bassjobsen/Bootstrap-3-Typeahead
|
|
4
|
+ * =============================================================
|
|
5
|
+ * Original written by @mdo and @fat
|
|
6
|
+ * =============================================================
|
|
7
|
+ * Copyright 2014 Bass Jobsen @bassjobsen
|
|
8
|
+ *
|
|
9
|
+ * Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
+ * you may not use this file except in compliance with the License.
|
|
11
|
+ * You may obtain a copy of the License at
|
|
12
|
+ *
|
|
13
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+ *
|
|
15
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
16
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
+ * See the License for the specific language governing permissions and
|
|
19
|
+ * limitations under the License.
|
|
20
|
+ * ============================================================ */
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+(function(root, factory) {
|
|
24
|
+
|
|
25
|
+ "use strict";
|
|
26
|
+
|
|
27
|
+ // CommonJS module is defined
|
|
28
|
+ if (typeof module !== 'undefined' && module.exports) {
|
|
29
|
+ module.exports = factory(require('jquery')(root));
|
|
30
|
+ }
|
|
31
|
+ // AMD module is defined
|
|
32
|
+ else if (typeof define === "function" && define.amd) {
|
|
33
|
+ define("bootstrap3-typeahead", ["jquery"], function($) {
|
|
34
|
+ return factory($);
|
|
35
|
+ });
|
|
36
|
+ } else {
|
|
37
|
+ factory(root.jQuery);
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+}(this, function($) {
|
|
41
|
+
|
|
42
|
+ "use strict";
|
|
43
|
+ // jshint laxcomma: true
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+ /* TYPEAHEAD PUBLIC CLASS DEFINITION
|
|
47
|
+ * ================================= */
|
|
48
|
+
|
|
49
|
+ var Typeahead = function (element, options) {
|
|
50
|
+ this.$element = $(element);
|
|
51
|
+ this.options = $.extend({}, $.fn.typeahead.defaults, options);
|
|
52
|
+ this.matcher = this.options.matcher || this.matcher;
|
|
53
|
+ this.sorter = this.options.sorter || this.sorter;
|
|
54
|
+ this.select = this.options.select || this.select;
|
|
55
|
+ this.autoSelect = typeof this.options.autoSelect == 'boolean' ? this.options.autoSelect : true;
|
|
56
|
+ this.highlighter = this.options.highlighter || this.highlighter;
|
|
57
|
+ this.render = this.options.render || this.render;
|
|
58
|
+ this.updater = this.options.updater || this.updater;
|
|
59
|
+ this.source = this.options.source;
|
|
60
|
+ this.delay = typeof this.options.delay == 'number' ? this.options.delay : 250;
|
|
61
|
+ this.$menu = $(this.options.menu);
|
|
62
|
+ this.shown = false;
|
|
63
|
+ this.listen();
|
|
64
|
+ this.showHintOnFocus = typeof this.options.showHintOnFocus == 'boolean' ? this.options.showHintOnFocus : false;
|
|
65
|
+ };
|
|
66
|
+
|
|
67
|
+ Typeahead.prototype = {
|
|
68
|
+
|
|
69
|
+ constructor: Typeahead
|
|
70
|
+
|
|
71
|
+ , select: function () {
|
|
72
|
+ var val = this.$menu.find('.active').data('value');
|
|
73
|
+ if(this.autoSelect || val) {
|
|
74
|
+ this.$element
|
|
75
|
+ .val(this.updater(val))
|
|
76
|
+ .change();
|
|
77
|
+ }
|
|
78
|
+ return this.hide();
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ , updater: function (item) {
|
|
82
|
+ return item;
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ , setSource: function (source) {
|
|
86
|
+ this.source = source;
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ , show: function () {
|
|
90
|
+ var pos = $.extend({}, this.$element.position(), {
|
|
91
|
+ height: this.$element[0].offsetHeight
|
|
92
|
+ }), scrollHeight;
|
|
93
|
+
|
|
94
|
+ scrollHeight = typeof this.options.scrollHeight == 'function' ?
|
|
95
|
+ this.options.scrollHeight.call() :
|
|
96
|
+ this.options.scrollHeight;
|
|
97
|
+
|
|
98
|
+ this.$menu
|
|
99
|
+ .insertAfter(this.$element)
|
|
100
|
+ .css({
|
|
101
|
+ top: pos.top + pos.height + scrollHeight
|
|
102
|
+ , left: pos.left
|
|
103
|
+ })
|
|
104
|
+ .show();
|
|
105
|
+
|
|
106
|
+ this.shown = true;
|
|
107
|
+ return this;
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ , hide: function () {
|
|
111
|
+ this.$menu.hide();
|
|
112
|
+ this.shown = false;
|
|
113
|
+ return this;
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ , lookup: function (query) {
|
|
117
|
+ var items;
|
|
118
|
+ if (typeof(query) != 'undefined' && query !== null) {
|
|
119
|
+ this.query = query;
|
|
120
|
+ } else {
|
|
121
|
+ this.query = this.$element.val() || '';
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ if ((this.query.length < this.options.minLength) && !this.showHintOnFocus) {
|
|
125
|
+ return this.shown ? this.hide() : this;
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ var worker = $.proxy(function() {
|
|
129
|
+ items = $.isFunction(this.source) ? this.source(this.query, $.proxy(this.process, this)) : this.source;
|
|
130
|
+ if (items) {
|
|
131
|
+ this.process(items);
|
|
132
|
+ }
|
|
133
|
+ }, this)
|
|
134
|
+
|
|
135
|
+ clearTimeout(this.lookupWorker)
|
|
136
|
+ this.lookupWorker = setTimeout(worker, this.delay)
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ , process: function (items) {
|
|
140
|
+ var that = this;
|
|
141
|
+
|
|
142
|
+ items = $.grep(items, function (item) {
|
|
143
|
+ return that.matcher(item);
|
|
144
|
+ });
|
|
145
|
+
|
|
146
|
+ items = this.sorter(items);
|
|
147
|
+
|
|
148
|
+ if (!items.length) {
|
|
149
|
+ return this.shown ? this.hide() : this;
|
|
150
|
+ }
|
|
151
|
+
|
|
152
|
+ if (this.options.items == 'all') {
|
|
153
|
+ return this.render(items).show();
|
|
154
|
+ } else {
|
|
155
|
+ return this.render(items.slice(0, this.options.items)).show();
|
|
156
|
+ }
|
|
157
|
+ }
|
|
158
|
+
|
|
159
|
+ , matcher: function (item) {
|
|
160
|
+ return ~item.toLowerCase().indexOf(this.query.toLowerCase());
|
|
161
|
+ }
|
|
162
|
+
|
|
163
|
+ , sorter: function (items) {
|
|
164
|
+ var beginswith = []
|
|
165
|
+ , caseSensitive = []
|
|
166
|
+ , caseInsensitive = []
|
|
167
|
+ , item;
|
|
168
|
+
|
|
169
|
+ while ((item = items.shift())) {
|
|
170
|
+ if (!item.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(item);
|
|
171
|
+ else if (~item.indexOf(this.query)) caseSensitive.push(item);
|
|
172
|
+ else caseInsensitive.push(item);
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ return beginswith.concat(caseSensitive, caseInsensitive);
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ , highlighter: function (item) {
|
|
179
|
+ var html = $('<div></div>');
|
|
180
|
+ var query = this.query;
|
|
181
|
+ var i = item.indexOf(query);
|
|
182
|
+ var len, leftPart, middlePart, rightPart, strong;
|
|
183
|
+ len = query.length;
|
|
184
|
+ if(len == 0){
|
|
185
|
+ return html.text(item).html();
|
|
186
|
+ }
|
|
187
|
+ while (i > -1) {
|
|
188
|
+ leftPart = item.substr(0, i);
|
|
189
|
+ middlePart = item.substr(i, len);
|
|
190
|
+ rightPart = item.substr(i + len);
|
|
191
|
+ strong = $('<strong></strong>').text(middlePart);
|
|
192
|
+ html
|
|
193
|
+ .append(document.createTextNode(leftPart))
|
|
194
|
+ .append(strong);
|
|
195
|
+ item = rightPart;
|
|
196
|
+ i = item.indexOf(query);
|
|
197
|
+ }
|
|
198
|
+ return html.append(document.createTextNode(item)).html();
|
|
199
|
+ }
|
|
200
|
+
|
|
201
|
+ , render: function (items) {
|
|
202
|
+ var that = this;
|
|
203
|
+
|
|
204
|
+ items = $(items).map(function (i, item) {
|
|
205
|
+ i = $(that.options.item).data('value', item);
|
|
206
|
+ i.find('a').html(that.highlighter(item));
|
|
207
|
+ return i[0];
|
|
208
|
+ });
|
|
209
|
+
|
|
210
|
+ if (this.autoSelect) {
|
|
211
|
+ items.first().addClass('active');
|
|
212
|
+ }
|
|
213
|
+ this.$menu.html(items);
|
|
214
|
+ return this;
|
|
215
|
+ }
|
|
216
|
+
|
|
217
|
+ , next: function (event) {
|
|
218
|
+ var active = this.$menu.find('.active').removeClass('active')
|
|
219
|
+ , next = active.next();
|
|
220
|
+
|
|
221
|
+ if (!next.length) {
|
|
222
|
+ next = $(this.$menu.find('li')[0]);
|
|
223
|
+ }
|
|
224
|
+
|
|
225
|
+ next.addClass('active');
|
|
226
|
+ }
|
|
227
|
+
|
|
228
|
+ , prev: function (event) {
|
|
229
|
+ var active = this.$menu.find('.active').removeClass('active')
|
|
230
|
+ , prev = active.prev();
|
|
231
|
+
|
|
232
|
+ if (!prev.length) {
|
|
233
|
+ prev = this.$menu.find('li').last();
|
|
234
|
+ }
|
|
235
|
+
|
|
236
|
+ prev.addClass('active');
|
|
237
|
+ }
|
|
238
|
+
|
|
239
|
+ , listen: function () {
|
|
240
|
+ this.$element
|
|
241
|
+ .on('focus', $.proxy(this.focus, this))
|
|
242
|
+ .on('blur', $.proxy(this.blur, this))
|
|
243
|
+ .on('keypress', $.proxy(this.keypress, this))
|
|
244
|
+ .on('keyup', $.proxy(this.keyup, this));
|
|
245
|
+
|
|
246
|
+ if (this.eventSupported('keydown')) {
|
|
247
|
+ this.$element.on('keydown', $.proxy(this.keydown, this));
|
|
248
|
+ }
|
|
249
|
+
|
|
250
|
+ this.$menu
|
|
251
|
+ .on('click', $.proxy(this.click, this))
|
|
252
|
+ .on('mouseenter', 'li', $.proxy(this.mouseenter, this))
|
|
253
|
+ .on('mouseleave', 'li', $.proxy(this.mouseleave, this));
|
|
254
|
+ }
|
|
255
|
+ , destroy : function () {
|
|
256
|
+ this.$element.data('typeahead',null);
|
|
257
|
+ this.$element
|
|
258
|
+ .off('focus')
|
|
259
|
+ .off('blur')
|
|
260
|
+ .off('keypress')
|
|
261
|
+ .off('keyup');
|
|
262
|
+
|
|
263
|
+ if (this.eventSupported('keydown')) {
|
|
264
|
+ this.$element.off('keydown');
|
|
265
|
+ }
|
|
266
|
+
|
|
267
|
+ this.$menu.remove();
|
|
268
|
+ }
|
|
269
|
+ , eventSupported: function(eventName) {
|
|
270
|
+ var isSupported = eventName in this.$element;
|
|
271
|
+ if (!isSupported) {
|
|
272
|
+ this.$element.setAttribute(eventName, 'return;');
|
|
273
|
+ isSupported = typeof this.$element[eventName] === 'function';
|
|
274
|
+ }
|
|
275
|
+ return isSupported;
|
|
276
|
+ }
|
|
277
|
+
|
|
278
|
+ , move: function (e) {
|
|
279
|
+ if (!this.shown) return;
|
|
280
|
+
|
|
281
|
+ switch(e.keyCode) {
|
|
282
|
+ case 9: // tab
|
|
283
|
+ case 13: // enter
|
|
284
|
+ case 27: // escape
|
|
285
|
+ e.preventDefault();
|
|
286
|
+ break;
|
|
287
|
+
|
|
288
|
+ case 38: // up arrow
|
|
289
|
+ // with the shiftKey (this is actually the left parenthesis)
|
|
290
|
+ if (e.shiftKey) return;
|
|
291
|
+ e.preventDefault();
|
|
292
|
+ this.prev();
|
|
293
|
+ break;
|
|
294
|
+
|
|
295
|
+ case 40: // down arrow
|
|
296
|
+ // with the shiftKey (this is actually the right parenthesis)
|
|
297
|
+ if (e.shiftKey) return;
|
|
298
|
+ e.preventDefault();
|
|
299
|
+ this.next();
|
|
300
|
+ break;
|
|
301
|
+ }
|
|
302
|
+
|
|
303
|
+ e.stopPropagation();
|
|
304
|
+ }
|
|
305
|
+
|
|
306
|
+ , keydown: function (e) {
|
|
307
|
+ this.suppressKeyPressRepeat = ~$.inArray(e.keyCode, [40,38,9,13,27]);
|
|
308
|
+ if (!this.shown && e.keyCode == 40) {
|
|
309
|
+ this.lookup("");
|
|
310
|
+ } else {
|
|
311
|
+ this.move(e);
|
|
312
|
+ }
|
|
313
|
+ }
|
|
314
|
+
|
|
315
|
+ , keypress: function (e) {
|
|
316
|
+ if (this.suppressKeyPressRepeat) return;
|
|
317
|
+ this.move(e);
|
|
318
|
+ }
|
|
319
|
+
|
|
320
|
+ , keyup: function (e) {
|
|
321
|
+ switch(e.keyCode) {
|
|
322
|
+ case 40: // down arrow
|
|
323
|
+ case 38: // up arrow
|
|
324
|
+ case 16: // shift
|
|
325
|
+ case 17: // ctrl
|
|
326
|
+ case 18: // alt
|
|
327
|
+ break;
|
|
328
|
+
|
|
329
|
+ case 9: // tab
|
|
330
|
+ case 13: // enter
|
|
331
|
+ if (!this.shown) return;
|
|
332
|
+ this.select();
|
|
333
|
+ break;
|
|
334
|
+
|
|
335
|
+ case 27: // escape
|
|
336
|
+ if (!this.shown) return;
|
|
337
|
+ this.hide();
|
|
338
|
+ break;
|
|
339
|
+ default:
|
|
340
|
+ this.lookup();
|
|
341
|
+ }
|
|
342
|
+
|
|
343
|
+ e.stopPropagation();
|
|
344
|
+ e.preventDefault();
|
|
345
|
+ }
|
|
346
|
+
|
|
347
|
+ , focus: function (e) {
|
|
348
|
+ if (!this.focused) {
|
|
349
|
+ this.focused = true;
|
|
350
|
+ if (this.options.minLength === 0 && !this.$element.val() || this.options.showHintOnFocus) {
|
|
351
|
+ this.lookup();
|
|
352
|
+ }
|
|
353
|
+ }
|
|
354
|
+ }
|
|
355
|
+
|
|
356
|
+ , blur: function (e) {
|
|
357
|
+ this.focused = false;
|
|
358
|
+ if (!this.mousedover && this.shown) this.hide();
|
|
359
|
+ }
|
|
360
|
+
|
|
361
|
+ , click: function (e) {
|
|
362
|
+ e.stopPropagation();
|
|
363
|
+ e.preventDefault();
|
|
364
|
+ this.select();
|
|
365
|
+ this.$element.focus();
|
|
366
|
+ }
|
|
367
|
+
|
|
368
|
+ , mouseenter: function (e) {
|
|
369
|
+ this.mousedover = true;
|
|
370
|
+ this.$menu.find('.active').removeClass('active');
|
|
371
|
+ $(e.currentTarget).addClass('active');
|
|
372
|
+ }
|
|
373
|
+
|
|
374
|
+ , mouseleave: function (e) {
|
|
375
|
+ this.mousedover = false;
|
|
376
|
+ if (!this.focused && this.shown) this.hide();
|
|
377
|
+ }
|
|
378
|
+
|
|
379
|
+ };
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+ /* TYPEAHEAD PLUGIN DEFINITION
|
|
383
|
+ * =========================== */
|
|
384
|
+
|
|
385
|
+ var old = $.fn.typeahead;
|
|
386
|
+
|
|
387
|
+ $.fn.typeahead = function (option) {
|
|
388
|
+ var arg = arguments;
|
|
389
|
+ return this.each(function () {
|
|
390
|
+ var $this = $(this)
|
|
391
|
+ , data = $this.data('typeahead')
|
|
392
|
+ , options = typeof option == 'object' && option;
|
|
393
|
+ if (!data) $this.data('typeahead', (data = new Typeahead(this, options)));
|
|
394
|
+ if (typeof option == 'string') {
|
|
395
|
+ if (arg.length > 1) {
|
|
396
|
+ data[option].apply(data, Array.prototype.slice.call(arg ,1));
|
|
397
|
+ } else {
|
|
398
|
+ data[option]();
|
|
399
|
+ }
|
|
400
|
+ }
|
|
401
|
+ });
|
|
402
|
+ };
|
|
403
|
+
|
|
404
|
+ $.fn.typeahead.defaults = {
|
|
405
|
+ source: []
|
|
406
|
+ , items: 8
|
|
407
|
+ , menu: '<ul class="typeahead dropdown-menu"></ul>'
|
|
408
|
+ , item: '<li><a href="#"></a></li>'
|
|
409
|
+ , minLength: 1
|
|
410
|
+ , scrollHeight: 0
|
|
411
|
+ , autoSelect: true
|
|
412
|
+ };
|
|
413
|
+
|
|
414
|
+ $.fn.typeahead.Constructor = Typeahead;
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+ /* TYPEAHEAD NO CONFLICT
|
|
418
|
+ * =================== */
|
|
419
|
+
|
|
420
|
+ $.fn.typeahead.noConflict = function () {
|
|
421
|
+ $.fn.typeahead = old;
|
|
422
|
+ return this;
|
|
423
|
+ };
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+ /* TYPEAHEAD DATA-API
|
|
427
|
+ * ================== */
|
|
428
|
+
|
|
429
|
+ $(document).on('focus.typeahead.data-api', '[data-provide="typeahead"]', function (e) {
|
|
430
|
+ var $this = $(this);
|
|
431
|
+ if ($this.data('typeahead')) return;
|
|
432
|
+ $this.typeahead($this.data());
|
|
433
|
+ });
|
|
434
|
+
|
|
435
|
+}));
|