From a617e2a50ff93e6a0fc9f79808b9af6b4f672ce8 Mon Sep 17 00:00:00 2001 From: Cory Hake Date: Tue, 11 Oct 2016 17:00:37 -0500 Subject: [PATCH 1/2] Include support for a max_results property. To improve performance when dealing with large data sets, implement a maximum results flag. This will load and highlight only the first N results, with an option to click and load all remaining results. --- jquery.hideseek.js | 550 +++++++++++++++++++++++++-------------------- 1 file changed, 308 insertions(+), 242 deletions(-) diff --git a/jquery.hideseek.js b/jquery.hideseek.js index 4f7d05d..38d0b0e 100644 --- a/jquery.hideseek.js +++ b/jquery.hideseek.js @@ -1,242 +1,308 @@ -/** - * 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'); + 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 < t.childNodes.length; ++h) h += e(t.childNodes[h], i); + return n + } + return this.length && t && t.length ? this.each(function() { e(this, t.toUpperCase()) }) : this +}, jQuery.fn.removeHighlight = function() { + return this.find("mark.highlight").each(function() { with(this.parentNode.firstChild.nodeName, this.parentNode) replaceChild(this.firstChild, this), normalize() }).end() +}; + +// Ignore accents +String.prototype.removeAccents = function(enabled) { + if (enabled) return this + .replace(/[áàãâä]/gi, "a") + .replace(/[éè¨ê]/gi, "e") + .replace(/[íìïî]/gi, "i") + .replace(/[óòöôõ]/gi, "o") + .replace(/[úùüû]/gi, "u") + .replace(/[ç]/gi, "c") + .replace(/[ñ]/gi, "n"); + return this; +} From a6dfc8b559c1b66712068c9c0118b1207ef2f90b Mon Sep 17 00:00:00 2001 From: Cory Hake Date: Wed, 12 Oct 2016 10:14:59 -0500 Subject: [PATCH 2/2] Match original formatting --- jquery.hideseek.js | 389 +++++++++++++++++++++++---------------------- 1 file changed, 200 insertions(+), 189 deletions(-) diff --git a/jquery.hideseek.js b/jquery.hideseek.js index 38d0b0e..f613bea 100644 --- a/jquery.hideseek.js +++ b/jquery.hideseek.js @@ -11,298 +11,309 @@ * */ -(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 - }; + /* 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, + max_results: 10 + }; - var options = $.extend(defaults, options); + 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)'); + 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'); - }); - }; + $this.closest('div').find('.description-list').find('#max-results').click(function() { + $this.trigger('updateListNoMax'); + }); + }; - return this.each(function() { + return this.each(function() { - var $this = $(this); + var $this = $(this); - $this.opts = []; + $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]; - }); + $.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; + if ($this.opts.headers) + $this.opts.ignore += $this.opts.ignore ? ', ' + $this.opts.headers : $this.opts.headers; - var $list = $($this.opts.list); + var $list = $($this.opts.list); - if ($list.find('#max-results').length == 0) addMaxResults($this); + if ($list.find('#max-results').length == 0) addMaxResults($this); - if ($this.opts.navigation) $this.attr('autocomplete', 'off'); + if ($this.opts.navigation) $this.attr('autocomplete', 'off'); - if ($this.opts.hidden_mode) $list.children().hide(); + if ($this.opts.hidden_mode) $list.children().hide(); - $this.on('keyup', function(e) { - $this.trigger('updateList'); - }); + $this.on('keyup', function(e) { + $this.trigger('updateList'); + }); - $this.on('updateList', function(e) { - updateList(e, true); - }); + $this.on('updateList', function(e) { + updateList(e, true); + }); - $this.on("updateListNoMax", function(e) { - updateList(e, false); - }); + $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)) { + 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; + 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() { + if ( max ) { + $list.children($this.opts.ignore.trim() ? ":not(" + $this.opts.ignore + ")" : '').removeClass('selected').each(function() { - $(this).hide(); + $(this).hide(); - }); - } + }); + } - $list.children($this.opts.ignore.trim() ? ":not(" + $this.opts.ignore + ")" : '').removeClass('selected').each(function() { + $list.children($this.opts.ignore.trim() ? ":not(" + $this.opts.ignore + ")" : '').removeClass('selected').each(function() { - if (max) { + if (max) { - if (($this.opts.max_results != 0) && (matches >= $this.opts.max_results)) { + if (($this.opts.max_results != 0) && (matches >= $this.opts.max_results)) { - maxReached = true; + maxReached = true; - return false; + return false; - } + } - } + } - var data = ( - $this.opts.attribute != 'text' ? ($(this).attr($this.opts.attribute) || '') : $(this).text() + 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); + var treaty = data.removeAccents($this.opts.ignore_accents).indexOf(q) == -1 || q === ($this.opts.hidden_mode ? '' : false); - if (treaty) { + if (treaty) { - $(this).hide(); + $(this).hide(); - $this.trigger('_after_each'); + $this.trigger('_after_each'); - } else { + } else { - if ($this.opts.highlight) { + if ($this.opts.highlight) { - $(this).removeHighlight().highlight(q).show(); + $(this).removeHighlight().highlight(q).show(); - $('#max-results').removeHighlight(); + $('#max-results').removeHighlight(); - } else { + } else { - $(this).show(); + $(this).show(); - } + } - $this.trigger('_after_each'); + $this.trigger('_after_each'); - if (max) matches += 1; + if (max) matches += 1; + + } - } + }); - }); + if (max) { - if (max) { + if (maxReached) { - if (maxReached) { + $list.find('#max-results').show(); - $list.find('#max-results').show(); + } else { - } else { + $list.find('#max-results').hide(); - $list.find('#max-results').hide(); + } - } + } else { - } else { + $list.find('#max-results').hide(); - $list.find('#max-results').hide(); + } - } + // No results message + if ($this.opts.nodata) { - // No results message - if ($this.opts.nodata) { + $list.find('.no-results').remove(); - $list.find('.no-results').remove(); + if (!$list.children(':not([style*="display: none"])').length) { - 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); - $list - .children() - .first() - .clone() - .removeHighlight() - .addClass('no-results') - .show() - .prependTo($this.opts.list) - .text($this.opts.nodata); + $this.trigger('_after_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(); + } + }); + } - // 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'); - $this.trigger('_after'); + }; - }; + // Navigation + function current(element) { + return element.children('.selected:visible'); + }; - // Navigation - function current(element) { - return element.children('.selected:visible'); - }; + function prev(element) { + return current(element).prevAll(":visible:first"); + }; - function prev(element) { - return current(element).prevAll(":visible:first"); - }; + function next(element) { + return current(element).nextAll(":visible:first"); + }; - function next(element) { - return current(element).nextAll(":visible:first"); - }; + if ($this.opts.navigation) { - if ($this.opts.navigation) { + if (e.keyCode == 38) { - if (e.keyCode == 38) { + if (current($list).length) { - if (current($list).length) { + prev($list).addClass('selected'); - prev($list).addClass('selected'); + current($list).last().removeClass('selected'); - current($list).last().removeClass('selected'); + } else { - } else { + $list.children(':visible').last().addClass('selected'); - $list.children(':visible').last().addClass('selected'); + }; - }; + } else if (e.keyCode == 40) { - } else if (e.keyCode == 40) { + if (current($list).length) { - if (current($list).length) { + next($list).addClass('selected'); - next($list).addClass('selected'); + current($list).first().removeClass('selected'); - current($list).first().removeClass('selected'); + } else { - } else { + $list.children(':visible').first().addClass('selected'); - $list.children(':visible').first().addClass('selected'); + }; - }; + } else if (e.keyCode == 13) { - } else if (e.keyCode == 13) { + if (current($list).find('a').length) { - if (current($list).find('a').length) { + document.location = current($list).find('a').attr('href'); - document.location = current($list).find('a').attr('href'); + } else { - } else { + $this.val(current($list).text()); - $this.val(current($list).text()); + }; - }; + }; - }; + }; + }; - }; - }; + }); - }); + }; - }; + $(document).ready(function () { $('[data-toggle="hideseek"]').hideseek(); }); - $(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 < t.childNodes.length; ++h) h += e(t.childNodes[h], i); - return n - } - return this.length && t && t.length ? this.each(function() { e(this, t.toUpperCase()) }) : this -}, jQuery.fn.removeHighlight = function() { - return this.find("mark.highlight").each(function() { with(this.parentNode.firstChild.nodeName, this.parentNode) replaceChild(this.firstChild, this), normalize() }).end() -}; +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