|
| 1 | +/* |
| 2 | + * This file is part of the Symfony package. |
| 3 | + * |
| 4 | + * (c) Fabien Potencier <[email protected]> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * jQuery plugin for an instant searching. |
| 12 | + * |
| 13 | + * @author Oleg Voronkovich <[email protected]> |
| 14 | + */ |
| 15 | +(function($) { |
| 16 | + $.fn.instantSearch = function(config) { |
| 17 | + return this.each(function() { |
| 18 | + initInstantSearch(this, $.extend(true, defaultConfig, config || {})); |
| 19 | + }); |
| 20 | + }; |
| 21 | + |
| 22 | + var defaultConfig = { |
| 23 | + minQueryLength: 2, |
| 24 | + maxPreviewItems: 10, |
| 25 | + previewDelay: 500, |
| 26 | + noItemsFoundMessage: 'No items found' |
| 27 | + }; |
| 28 | + |
| 29 | + function debounce(fn, delay) { |
| 30 | + var timer = null; |
| 31 | + return function () { |
| 32 | + var context = this, args = arguments; |
| 33 | + clearTimeout(timer); |
| 34 | + timer = setTimeout(function () { |
| 35 | + fn.apply(context, args); |
| 36 | + }, delay); |
| 37 | + }; |
| 38 | + } |
| 39 | + |
| 40 | + var initInstantSearch = function(el, config) { |
| 41 | + var $input = $(el); |
| 42 | + var $form = $input.closest('form'); |
| 43 | + var $preview = $('<ul class="search-preview list-group">').appendTo($form); |
| 44 | + |
| 45 | + var setPreviewItems = function(items) { |
| 46 | + $preview.empty(); |
| 47 | + |
| 48 | + $.each(items, function(index, item) { |
| 49 | + if (index > config.maxPreviewItems) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + addItemToPreview(item); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + var addItemToPreview = function(item) { |
| 58 | + var $link = $('<a>').attr('href', item.url).text(item.result); |
| 59 | + var $li = $('<li class="list-group-item">').append($link); |
| 60 | + |
| 61 | + $preview.append($li); |
| 62 | + } |
| 63 | + |
| 64 | + var noItemsFound = function() { |
| 65 | + var $li = $('<li class="list-group-item">').text(config.noItemsFoundMessage); |
| 66 | + |
| 67 | + $preview.empty(); |
| 68 | + $preview.append($li); |
| 69 | + } |
| 70 | + |
| 71 | + var updatePreview = function() { |
| 72 | + var query = $.trim($input.val()).replace(/\s{2,}/g, ' '); |
| 73 | + |
| 74 | + if (query.length < config.minQueryLength) { |
| 75 | + $preview.empty(); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + $.getJSON($form.attr('action') + '?' + $form.serialize(), function(items) { |
| 80 | + if (items.length === 0) { |
| 81 | + noItemsFound(); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + setPreviewItems(items); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + $input.focusout(function(e) { |
| 90 | + $preview.fadeOut(); |
| 91 | + }); |
| 92 | + |
| 93 | + $input.focusin(function(e) { |
| 94 | + $preview.fadeIn(); |
| 95 | + updatePreview(); |
| 96 | + }); |
| 97 | + |
| 98 | + $input.keyup(debounce(updatePreview, config.previewDelay)); |
| 99 | + } |
| 100 | +})(window.jQuery); |
0 commit comments