From c949174a2e8c324e3e463a9d10e9e6dc07b0ba9e Mon Sep 17 00:00:00 2001 From: Adrian Lang Date: Wed, 22 Jun 2011 21:05:17 +0200 Subject: Fix and refactor ajax.js * Move file to qsearch.js * Rename object to dw_qsearch * Remove unnecessary usage of Delay * Use $ prefix for jQuery objects * Fix result list hiding on click * Fix namespace shorting --- lib/scripts/qsearch.js | 144 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 lib/scripts/qsearch.js (limited to 'lib/scripts/qsearch.js') diff --git a/lib/scripts/qsearch.js b/lib/scripts/qsearch.js new file mode 100644 index 000000000..97eaa7ef0 --- /dev/null +++ b/lib/scripts/qsearch.js @@ -0,0 +1,144 @@ +/*jslint sloppy: true, plusplus: true, continue: true */ +/*global jQuery, DOKU_BASE, window, document, substr_replace */ + +/** + * AJAX functions for the pagename quicksearch + * + * @license GPL2 (http://www.gnu.org/licenses/gpl.html) + * @author Andreas Gohr + * @author Adrian Lang + * @author Michal Rezler + */ + +var dw_qsearch = { + + $inObj: null, + $outObj: null, + + /** + * initialize the quick search + * + * Attaches the event handlers + * + * @param input element (JQuery selector/DOM obj) + * @param output element (JQuery selector/DOM obj) + */ + init: function (input, output) { + var do_qsearch; + + dw_qsearch.$inObj = jQuery(input); + dw_qsearch.$outObj = jQuery(output); + + // objects found? + if (dw_qsearch.$inObj.length === 0 || + dw_qsearch.$outObj.length === 0) { + return; + } + + // attach eventhandler to search field + do_qsearch = function () { + dw_qsearch.clear_results(); + var value = dw_qsearch.$inObj.val(); + if (value === '') { + return; + } + jQuery.post( + DOKU_BASE + 'lib/exe/ajax.php', + { + call: 'qsearch', + q: encodeURI(value) + }, + dw_qsearch.onCompletion, + 'html' + ); + }; + + dw_qsearch.$inObj.keyup( + function() { + dw_qsearch.clear_results(); + window.setTimeout(do_qsearch, 500); + } + ); + + // attach eventhandler to output field + dw_qsearch.$outObj.click(dw_qsearch.clear_results); + }, + + /** + * Empty and hide the output div + */ + clear_results: function(){ + dw_qsearch.$outObj.hide(); + dw_qsearch.$outObj.text(''); + }, + + /** + * Callback. Reformat and display the results. + * + * Namespaces are shortened here to keep the results from overflowing + * or wrapping + * + * @param data The result HTML + */ + onCompletion: function(data) { + var max, $links, too_big; + + if (data === '') { return; } + + dw_qsearch.$outObj + .html(data) + .show() + .css('white-space', 'nowrap'); + + // shorten namespaces if too long + max = dw_qsearch.$outObj[0].clientWidth; + $links = dw_qsearch.$outObj.find('a'); + too_big = (document.documentElement.dir === 'rtl') + ? function (l) { return l.offsetLeft < 0; } + : function (l) { return l.offsetWidth + l.offsetLeft > max; }; + + $links.each(function () { + var start, length, replace, nsL, nsR, eli, runaway; + + if (!too_big(this)) { + return; + } + + nsL = this.innerText.indexOf('('); + nsR = this.innerText.indexOf(')'); + eli = 0; + runaway = 0; + + while((nsR - nsL > 3) && too_big(this) && runaway++ < 500) { + if(eli !== 0){ + // elipsis already inserted + if( (eli - nsL) > (nsR - eli) ){ + // cut left + start = eli - 2; + length = 2; + }else{ + // cut right + start = eli + 1; + length = 1; + } + replace = ''; + }else{ + // replace middle with ellipsis + start = Math.floor( nsL + ((nsR-nsL)/2) ); + length = 1; + replace = '…'; + } + this.innerText = substr_replace(this.innerText, + replace, start, length); + + eli = this.innerText.indexOf('…'); + nsL = this.innerText.indexOf('('); + nsR = this.innerText.indexOf(')'); + } + }); + } +}; + +jQuery(function () { + dw_qsearch.init('#qsearch__in','#qsearch__out'); +}); -- cgit v1.2.3 From 110749cc8c83fb3b492208c80ed1bc351abd1752 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Thu, 7 Jul 2011 21:45:44 +0200 Subject: fixed delay in qsearch The ajax quicksearch sent a request on every keystroke instead of waiting for the user to stop typing. --- lib/scripts/qsearch.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'lib/scripts/qsearch.js') diff --git a/lib/scripts/qsearch.js b/lib/scripts/qsearch.js index 97eaa7ef0..f83b7a5a1 100644 --- a/lib/scripts/qsearch.js +++ b/lib/scripts/qsearch.js @@ -14,6 +14,7 @@ var dw_qsearch = { $inObj: null, $outObj: null, + timer: null, /** * initialize the quick search @@ -55,8 +56,12 @@ var dw_qsearch = { dw_qsearch.$inObj.keyup( function() { + if(dw_qsearch.timer){ + window.clearTimeout(dw_qsearch.timer); + dw_qsearch.timer = null; + } dw_qsearch.clear_results(); - window.setTimeout(do_qsearch, 500); + dw_qsearch.timer = window.setTimeout(do_qsearch, 500); } ); -- cgit v1.2.3 From ba6c070edd92ca0fc8a6ee85d51769d64a19ee7c Mon Sep 17 00:00:00 2001 From: Adrian Lang Date: Sun, 4 Sep 2011 13:52:43 +0200 Subject: tmp --- lib/scripts/qsearch.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'lib/scripts/qsearch.js') diff --git a/lib/scripts/qsearch.js b/lib/scripts/qsearch.js index f83b7a5a1..c7128b9e3 100644 --- a/lib/scripts/qsearch.js +++ b/lib/scripts/qsearch.js @@ -1,6 +1,3 @@ -/*jslint sloppy: true, plusplus: true, continue: true */ -/*global jQuery, DOKU_BASE, window, document, substr_replace */ - /** * AJAX functions for the pagename quicksearch * -- cgit v1.2.3 From 1ffc211ddb46bfabe649bbacd1e36bc8e035afa3 Mon Sep 17 00:00:00 2001 From: Adrian Lang Date: Sun, 4 Sep 2011 15:32:41 +0200 Subject: Revert tmp commits This reverts commit ba6c070edd92ca0fc8a6ee85d51769d64a19ee7c. This reverts commit 923510088dda99cb2790b15308593e47369d4f01. --- lib/scripts/qsearch.js | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lib/scripts/qsearch.js') diff --git a/lib/scripts/qsearch.js b/lib/scripts/qsearch.js index c7128b9e3..f83b7a5a1 100644 --- a/lib/scripts/qsearch.js +++ b/lib/scripts/qsearch.js @@ -1,3 +1,6 @@ +/*jslint sloppy: true, plusplus: true, continue: true */ +/*global jQuery, DOKU_BASE, window, document, substr_replace */ + /** * AJAX functions for the pagename quicksearch * -- cgit v1.2.3 From 5e7a292691951a0fa0a18f06c8b9bcfb509a032d Mon Sep 17 00:00:00 2001 From: Adrian Lang Date: Fri, 9 Sep 2011 22:26:16 +0200 Subject: Various JavaScript improvements, JSLint, jQuery --- lib/scripts/qsearch.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'lib/scripts/qsearch.js') diff --git a/lib/scripts/qsearch.js b/lib/scripts/qsearch.js index f83b7a5a1..c7128b9e3 100644 --- a/lib/scripts/qsearch.js +++ b/lib/scripts/qsearch.js @@ -1,6 +1,3 @@ -/*jslint sloppy: true, plusplus: true, continue: true */ -/*global jQuery, DOKU_BASE, window, document, substr_replace */ - /** * AJAX functions for the pagename quicksearch * -- cgit v1.2.3