diff --git a/jquery.hideseek.js b/jquery.hideseek.js index 4f7d05d..f613bea 100644 --- a/jquery.hideseek.js +++ b/jquery.hideseek.js @@ -1,242 +1,319 @@ -/** - * HideSeek jQuery plugin - * - * @copyright Copyright 2015, Dimitris Krestos - * @license Apache License, Version 2.0 (http://www.opensource.org/licenses/apache2.0.php) - * @link http://vdw.staytuned.gr - * @version v0.7.1 - * - * Dependencies are include in minified versions at the bottom: - * 1. Highlight v4 by Johann Burkard - * - */ - - /* Sample html structure - - - - - or - - -
- item 1 - ... - item 2 -
- - or any similar structure... - - */ - -;(function($, window, undefined) { - "use strict"; - - $.fn.hideseek = function(options) { - - var defaults = { - list: '.hideseek-data', - nodata: '', - attribute: 'text', - highlight: false, - ignore: '', - headers: '', - navigation: false, - ignore_accents: false, - hidden_mode: false, - min_chars: 1 - }; - - var options = $.extend(defaults, options); - - return this.each(function() { - - var $this = $(this); - - $this.opts = []; - - $.map( ['list', 'nodata', 'attribute', 'highlight', 'ignore', 'headers', 'navigation', 'ignore_accents', 'hidden_mode', 'min_chars'], function( val, i ) { - $this.opts[val] = $this.data(val) || options[val]; - } ); - - if ($this.opts.headers) - $this.opts.ignore += $this.opts.ignore ? ', ' + $this.opts.headers : $this.opts.headers; - - var $list = $($this.opts.list); - - if ($this.opts.navigation) $this.attr('autocomplete', 'off'); - - if ($this.opts.hidden_mode) $list.children().hide(); - - $this.keyup(function(e) { - - if (e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13 && ( e.keyCode != 8 ? $this.val().length >= $this.opts.min_chars : true ) ) { - - var q = $this.val().toLowerCase(); - - $list.children($this.opts.ignore.trim() ? ":not(" + $this.opts.ignore + ")" : '').removeClass('selected').each(function() { - - var data = ( - $this.opts.attribute != 'text' - ? ($(this).attr($this.opts.attribute) || '') - : $(this).text() - ).toLowerCase(); - - var treaty = data.removeAccents($this.opts.ignore_accents).indexOf(q) == -1 || q === ($this.opts.hidden_mode ? '' : false) - - if (treaty) { - - $(this).hide(); - - $this.trigger('_after_each'); - - } else { - - $this.opts.highlight ? $(this).removeHighlight().highlight(q).show() : $(this).show(); - - $this.trigger('_after_each'); - - } - - }); - - // No results message - if ($this.opts.nodata) { - - $list.find('.no-results').remove(); - - if (!$list.children(':not([style*="display: none"])').length) { - - $list - .children() - .first() - .clone() - .removeHighlight() - .addClass('no-results') - .show() - .prependTo($this.opts.list) - .text($this.opts.nodata); - - $this.trigger('_after_nodata'); - - } - - } - - // hide headers with no results - if ($this.opts.headers) { - $($this.opts.headers, $list).each(function() { - if (!$(this).nextUntil($this.opts.headers).not('[style*="display: none"],' + $this.opts.ignore).length) { - $(this).hide(); - } else { - $(this).show(); - } - }); - } - - $this.trigger('_after'); - - }; - - // Navigation - function current(element) { - return element.children('.selected:visible'); - }; - - function prev(element) { - return current(element).prevAll(":visible:first"); - }; - - function next(element) { - return current(element).nextAll(":visible:first"); - }; - - if ($this.opts.navigation) { - - if (e.keyCode == 38) { - - if (current($list).length) { - - prev($list).addClass('selected'); - - current($list).last().removeClass('selected'); - - } else { - - $list.children(':visible').last().addClass('selected'); - - }; - - } else if (e.keyCode == 40) { - - if (current($list).length) { - - next($list).addClass('selected'); - - current($list).first().removeClass('selected'); - - } else { - - $list.children(':visible').first().addClass('selected'); - - }; - - } else if (e.keyCode == 13) { - - if (current($list).find('a').length) { - - document.location = current($list).find('a').attr('href'); - - } else { - - $this.val(current($list).text()); - - }; - - }; - - }; - - }); - - }); - - }; - - $(document).ready(function () { $('[data-toggle="hideseek"]').hideseek(); }); - -})(jQuery); - -/* - -highlight v4 - -Highlights arbitrary terms. - - - -MIT license. - -Johann Burkard - - - -*/ -jQuery.fn.highlight=function(t){function e(t,i){var n=0;if(3==t.nodeType){var a=t.data.removeAccents(true).toUpperCase().indexOf(i);if(a>=0){var s=document.createElement("mark");s.className="highlight";var r=t.splitText(a);r.splitText(i.length);var o=r.cloneNode(!0);s.appendChild(o),r.parentNode.replaceChild(s,r),n=1}}else if(1==t.nodeType&&t.childNodes&&!/(script|style)/i.test(t.tagName))for(var h=0;h + + + or + + +
+ item 1 + ... + item 2 +
+ + or any similar structure... + + */ + +;(function($, window, undefined) { + "use strict"; + + $.fn.hideseek = function(options) { + + var defaults = { + list: '.hideseek-data', + nodata: '', + attribute: 'text', + highlight: false, + ignore: '', + headers: '', + navigation: false, + ignore_accents: false, + hidden_mode: false, + min_chars: 1, + max_results: 10 + }; + + var options = $.extend(defaults, options); + + const addMaxResults = function addMax($this) { + $($this.opts.list).append('
'); + const $maxRes = $('div#max-results'); + $maxRes.hide() + .append('Max results limit (' + $this.opts.max_results + ') reached') + .append('Click to display all results for this search term. (This may take a while)'); + + $this.closest('div').find('.description-list').find('#max-results').click(function() { + $this.trigger('updateListNoMax'); + }); + }; + + return this.each(function() { + + var $this = $(this); + + $this.opts = []; + + $.map( ['list', 'nodata', 'attribute', 'highlight', 'ignore', 'headers', 'navigation', 'ignore_accents', 'hidden_mode', 'min_chars', 'max_results'], function(val, i) { + $this.opts[val] = $this.data(val) || options[val]; + } ); + + if ($this.opts.headers) + $this.opts.ignore += $this.opts.ignore ? ', ' + $this.opts.headers : $this.opts.headers; + + var $list = $($this.opts.list); + + if ($list.find('#max-results').length == 0) addMaxResults($this); + + if ($this.opts.navigation) $this.attr('autocomplete', 'off'); + + if ($this.opts.hidden_mode) $list.children().hide(); + + $this.on('keyup', function(e) { + $this.trigger('updateList'); + }); + + $this.on('updateList', function(e) { + updateList(e, true); + }); + + $this.on("updateListNoMax", function(e) { + updateList(e, false); + }); + + const updateList = function listUpdate(event, max) { + if (e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13 && ( e.keyCode != 8 ? $this.val().length >= $this.opts.min_chars : true ) ) { + + var q = $this.val().toLowerCase(); + var matches = 0; + var maxReached = false; + + if ( max ) { + $list.children($this.opts.ignore.trim() ? ":not(" + $this.opts.ignore + ")" : '').removeClass('selected').each(function() { + + $(this).hide(); + + }); + } + + $list.children($this.opts.ignore.trim() ? ":not(" + $this.opts.ignore + ")" : '').removeClass('selected').each(function() { + + if (max) { + + if (($this.opts.max_results != 0) && (matches >= $this.opts.max_results)) { + + maxReached = true; + + return false; + + } + + } + + var data = ( + $this.opts.attribute != 'text' + ? ($(this).attr($this.opts.attribute) || '') + : $(this).text() + ).toLowerCase(); + + var treaty = data.removeAccents($this.opts.ignore_accents).indexOf(q) == -1 || q === ($this.opts.hidden_mode ? '' : false); + + if (treaty) { + + $(this).hide(); + + $this.trigger('_after_each'); + + } else { + + if ($this.opts.highlight) { + + $(this).removeHighlight().highlight(q).show(); + + $('#max-results').removeHighlight(); + + } else { + + $(this).show(); + + } + + $this.trigger('_after_each'); + + if (max) matches += 1; + + } + + }); + + if (max) { + + if (maxReached) { + + $list.find('#max-results').show(); + + } else { + + $list.find('#max-results').hide(); + + } + + } else { + + $list.find('#max-results').hide(); + + } + + // No results message + if ($this.opts.nodata) { + + $list.find('.no-results').remove(); + + if (!$list.children(':not([style*="display: none"])').length) { + + $list + .children() + .first() + .clone() + .removeHighlight() + .addClass('no-results') + .show() + .prependTo($this.opts.list) + .text($this.opts.nodata); + + $this.trigger('_after_nodata'); + + } + + } + + // hide headers with no results + if ($this.opts.headers) { + $($this.opts.headers, $list).each(function() { + if (!$(this).nextUntil($this.opts.headers).not('[style*="display: none"],' + $this.opts.ignore).length) { + $(this).hide(); + } else { + $(this).show(); + } + }); + } + + $this.trigger('_after'); + + }; + + // Navigation + function current(element) { + return element.children('.selected:visible'); + }; + + function prev(element) { + return current(element).prevAll(":visible:first"); + }; + + function next(element) { + return current(element).nextAll(":visible:first"); + }; + + if ($this.opts.navigation) { + + if (e.keyCode == 38) { + + if (current($list).length) { + + prev($list).addClass('selected'); + + current($list).last().removeClass('selected'); + + } else { + + $list.children(':visible').last().addClass('selected'); + + }; + + } else if (e.keyCode == 40) { + + if (current($list).length) { + + next($list).addClass('selected'); + + current($list).first().removeClass('selected'); + + } else { + + $list.children(':visible').first().addClass('selected'); + + }; + + } else if (e.keyCode == 13) { + + if (current($list).find('a').length) { + + document.location = current($list).find('a').attr('href'); + + } else { + + $this.val(current($list).text()); + + }; + + }; + + }; + }; + + }); + + }; + + $(document).ready(function () { $('[data-toggle="hideseek"]').hideseek(); }); + +})(jQuery); + +/* + +highlight v4 + +Highlights arbitrary terms. + + + +MIT license. + +Johann Burkard + + + +*/ +jQuery.fn.highlight=function(t){function e(t,i){var n=0;if(3==t.nodeType){var a=t.data.removeAccents(true).toUpperCase().indexOf(i);if(a>=0){var s=document.createElement("mark");s.className="highlight";var r=t.splitText(a);r.splitText(i.length);var o=r.cloneNode(!0);s.appendChild(o),r.parentNode.replaceChild(s,r),n=1}}else if(1==t.nodeType&&t.childNodes&&!/(script|style)/i.test(t.tagName))for(var h=0;h