diff options
author | Unrud <unrud@openaliasbox.org> | 2017-03-10 23:02:35 +0100 |
---|---|---|
committer | Unrud <unrud@openaliasbox.org> | 2017-03-11 00:42:55 +0100 |
commit | e8e32f7a1194baabd6e158805f40396c667e6c56 (patch) | |
tree | 03d0cc2d188fcc148e726f0a6b24e8821a9e8635 /radicale_web/web/infcloud/lib/jquery.quicksearch.js | |
parent | Init (diff) | |
download | radicaleinfcloud-e8e32f7a1194baabd6e158805f40396c667e6c56.tar.gz radicaleinfcloud-e8e32f7a1194baabd6e158805f40396c667e6c56.tar.bz2 radicaleinfcloud-e8e32f7a1194baabd6e158805f40396c667e6c56.zip |
Add InfCloud
Diffstat (limited to 'radicale_web/web/infcloud/lib/jquery.quicksearch.js')
-rw-r--r-- | radicale_web/web/infcloud/lib/jquery.quicksearch.js | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/radicale_web/web/infcloud/lib/jquery.quicksearch.js b/radicale_web/web/infcloud/lib/jquery.quicksearch.js new file mode 100644 index 0000000..fe2f0a5 --- /dev/null +++ b/radicale_web/web/infcloud/lib/jquery.quicksearch.js @@ -0,0 +1,205 @@ +(function($, window, document, undefined) { + $.fn.quicksearch = function (target, opt) { + + var timeout, cache, rowcache, jq_results, val = '', e = this, options = $.extend({ + delay: 100, + selector: null, + stripeRows: null, + loader: null, + noResults: '', + matchedResultsCount: 0, + bind: 'keyup', + onBefore: function () { + return; + }, + onAfter: function () { + return; + }, + show: function () { + this.style.display = ""; + }, + hide: function () { + this.style.display = "none"; + }, + prepareQuery: function (val) { + return val.toLowerCase().split(' '); + }, + testQuery: function (query, txt, _row) { + for (var i = 0; i < query.length; i += 1) { + if (txt.indexOf(query[i]) === -1) { + return false; + } + } + return true; + } + }, opt); + + this.go = function () { + + var i = 0, + numMatchedRows = 0, + noresults = true, + query = options.prepareQuery(val), + val_empty = (val.replace(' ', '').length === 0); + + for (var i = 0, len = rowcache.length; i < len; i++) { + if (val_empty || options.testQuery(query, cache[i], rowcache[i])) { + options.show.apply(rowcache[i]); + noresults = false; + numMatchedRows++; + } else { + options.hide.apply(rowcache[i]); + } + } + + if (noresults) { + this.results(false); + } else { + this.results(true); + this.stripe(); + } + + this.matchedResultsCount = numMatchedRows; + this.loader(false); + options.onAfter(); + + return this; + }; + + /* + * External API so that users can perform search programatically. + * */ + this.search = function (submittedVal) { + val = submittedVal; + e.trigger(); + }; + + /* + * External API to get the number of matched results as seen in + * https://github.com/ruiz107/quicksearch/commit/f78dc440b42d95ce9caed1d087174dd4359982d6 + * */ + this.currentMatchedResults = function() { + return this.matchedResultsCount; + }; + + this.stripe = function () { + + if (typeof options.stripeRows === "object" && options.stripeRows !== null) + { + var joined = options.stripeRows.join(' '); + var stripeRows_length = options.stripeRows.length; + + jq_results.not(':hidden').each(function (i) { + $(this).removeClass(joined).addClass(options.stripeRows[i % stripeRows_length]); + }); + } + + return this; + }; + + this.strip = function (input) { + return $.trim(input.toLowerCase()); + }; + + this.strip_html = function (input) { + var output = input.replace(new RegExp('<[^<]+\>', 'g'), ""); + output = $.trim(output.toLowerCase()); + return output; + }; + + this.results = function (bool) { + if (typeof options.noResults === "string" && options.noResults !== "") { + if (bool) { + $(options.noResults).hide(); + } else { + $(options.noResults).show(); + } + } + return this; + }; + + this.loader = function (bool) { + if (typeof options.loader === "string" && options.loader !== "") { + (bool) ? $(options.loader).show() : $(options.loader).hide(); + } + return this; + }; + + this.cache = function () { + var type = $.type(target); + + // Target is a string - selector for HTML elements + if(type==='string') + jq_results = $(target); + // Target is an array of objects + else if(type==='array') { + jq_results=target.slice(); + } + // Target is an object containing ... + else if(type==='object') { + jq_results = []; + $.each(target, function(key, val){ + // ... arrays of objects + if(val instanceof Array) + $.merge(jq_results,val); + // ... other objects + else + jq_results.push(val); + }); + } + + // if (typeof options.noResults==="string" && options.noResults!=="") { + // jq_results = jq_results.not(options.noResults); + // } + + // var t = (typeof options.selector === "string") ? jq_results.find(options.selector) : $(target).not(options.noResults); + + cache = $.map(jq_results, function (item) { + return (type==='string') ? e.strip_html(item.innerHTML) : (typeof item.searchvalue==='string') ? e.strip(item.searchvalue) : ''; + }); + + rowcache = $.map(jq_results, function (item) { + return item; + }); + + /* + * Modified fix for sync-ing "val". + * Original fix https://github.com/michaellwest/quicksearch/commit/4ace4008d079298a01f97f885ba8fa956a9703d1 + * */ + val = val || this.val() || ""; + + return this.go(); + }; + + this.trigger = function () { + this.loader(true); + options.onBefore(); + + window.clearTimeout(timeout); + timeout = window.setTimeout(function () { + e.go(); + }, options.delay); + + return this; + }; + + this.cache(); + this.results(true); + this.stripe(); + this.loader(false); + + return this.each(function () { + + /* + * Changed from .bind to .on. + * */ + $(this).on(options.bind, function () { + + val = $(this).val(); + e.trigger(); + }); + }); + + }; + +}(jQuery, this, document)); |