From 5ba8d196ffe346ef6fbd8e6ffc11f0c849537ed3 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 14 Jan 2011 12:20:19 +0100 Subject: shorten quicksearch namespaces in JavaScript This patch moves the shortening of namespaces in the quicksearch results to JavaScript. This makes it independend from used template and will always try to fill the width of the result pane correctly. Things missing: * Make it work with RTL-languages * Check Browser compatibility (only tested in Chrome so far) --- lib/scripts/ajax.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'lib/scripts/ajax.js') diff --git a/lib/scripts/ajax.js b/lib/scripts/ajax.js index de009d448..761329f0f 100644 --- a/lib/scripts/ajax.js +++ b/lib/scripts/ajax.js @@ -31,6 +31,47 @@ addInitEvent(function () { outObj.innerHTML = data; outObj.style.display = 'block'; + outObj.style['white-space'] = 'nowrap'; + + // shorten namespaces if too long + var width = outObj.clientWidth; + var links = outObj.getElementsByTagName('a'); + for(var i=0; i 3) && links[i].offsetWidth > max ){ + if(runaway++ > 500) return; // just in case something went wrong + + if(eli){ + // elipsis already inserted + if( (eli - nsL) > (nsR - eli) ){ + // cut left + links[i].innerText = links[i].innerText.substring(0,eli-2)+ + links[i].innerText.substring(eli); + }else{ + // cut right + links[i].innerText = links[i].innerText.substring(0,eli+1)+ + links[i].innerText.substring(eli+2); + } + }else{ + // replace middle with ellipsis + var mid = Math.floor( nsL + ((nsR-nsL)/2) ); + links[i].innerText = links[i].innerText.substring(0,mid)+'…'+ + links[i].innerText.substring(mid+1); + } + eli = links[i].innerText.indexOf('…'); + nsL = links[i].innerText.indexOf('('); + nsR = links[i].innerText.indexOf(')'); + } + } + }; // attach eventhandler to search field -- cgit v1.2.3 From 301971b3769a2d1a440cf58fd84f2c100a1348e3 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 14 Jan 2011 13:59:23 +0100 Subject: correctly(?) shorten namespaces for RTL langunages in quicksearch --- lib/scripts/ajax.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'lib/scripts/ajax.js') diff --git a/lib/scripts/ajax.js b/lib/scripts/ajax.js index 761329f0f..d752edb38 100644 --- a/lib/scripts/ajax.js +++ b/lib/scripts/ajax.js @@ -38,15 +38,23 @@ addInitEvent(function () { var links = outObj.getElementsByTagName('a'); for(var i=0; i 0) continue; var nsL = links[i].innerText.indexOf('('); var nsR = links[i].innerText.indexOf(')'); var eli = 0; var runaway = 0; - while( (nsR - nsL > 3) && links[i].offsetWidth > max ){ + while( (nsR - nsL > 3) && + ( + (!isRTL && links[i].offsetWidth > max) || + (isRTL && links[i].offsetLeft < 0) + ) + ){ if(runaway++ > 500) return; // just in case something went wrong if(eli){ -- cgit v1.2.3 From a8254dfa8d8e02d4ac011fe915f3dcb4fdf7a361 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sat, 15 Jan 2011 10:29:42 +0100 Subject: made ajax quicksearch its own object This makes it possible for plugin and template authors to overwrite or extend the quicksearch JavaScript logic. --- lib/scripts/ajax.js | 79 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 29 deletions(-) (limited to 'lib/scripts/ajax.js') diff --git a/lib/scripts/ajax.js b/lib/scripts/ajax.js index d752edb38..44dcee999 100644 --- a/lib/scripts/ajax.js +++ b/lib/scripts/ajax.js @@ -5,30 +5,58 @@ * @author Andreas Gohr * @author Adrian Lang */ -addInitEvent(function () { +var ajax_quicksearch = { - var inID = 'qsearch__in'; - var outID = 'qsearch__out'; + inObj: null, + outObj: null, + sackObj: null, + delay: null, - var inObj = document.getElementById(inID); - var outObj = document.getElementById(outID); + init: function(inID, outID) { - // objects found? - if (inObj === null){ return; } - if (outObj === null){ return; } + this.inObj = $(inID); + this.outObj = $(outID); - function clear_results(){ - outObj.style.display = 'none'; - outObj.innerHTML = ''; - } + // objects found? + if (this.inObj === null) return; + if (this.outObj === null) return; + + // prepare AJAX + this.sackObj = new sack(DOKU_BASE + 'lib/exe/ajax.php'); + this.sackObj.AjaxFailedAlert = ''; + this.sackObj.encodeURIString = false; + this.sackObj.onCompletion = ajax_quicksearch.onCompletion; + + // attach eventhandler to search field + this.delay = new Delay(function () { + ajax_quicksearch.clear_results(); + var value = ajax_quicksearch.inObj.value; + if(value === ''){ return; } + ajax_quicksearch.sackObj.runAJAX('call=qsearch&q=' + encodeURI(value)); + }); + + addEvent(this.inObj, 'keyup', function () { + ajax_quicksearch.clear_results(); + ajax_quicksearch.delay.start(); + }); + + // attach eventhandler to output field + addEvent(this.outObj, 'click', function () { + ajax_quicksearch.outObj.style.display = 'none'; + }); + }, - var sack_obj = new sack(DOKU_BASE + 'lib/exe/ajax.php'); - sack_obj.AjaxFailedAlert = ''; - sack_obj.encodeURIString = false; - sack_obj.onCompletion = function () { - var data = sack_obj.response; + clear_results: function(){ + ajax_quicksearch.outObj.style.display = 'none'; + ajax_quicksearch.outObj.innerHTML = ''; + }, + + onCompletion: function() { + var data = this.response; // 'this' is sack context if (data === '') { return; } + var outObj = ajax_quicksearch.outObj; + outObj.innerHTML = data; outObj.style.display = 'block'; outObj.style['white-space'] = 'nowrap'; @@ -79,19 +107,12 @@ addInitEvent(function () { nsR = links[i].innerText.indexOf(')'); } } + } - }; - - // attach eventhandler to search field - var delay = new Delay(function () { - clear_results(); - var value = inObj.value; - if(value === ''){ return; } - sack_obj.runAJAX('call=qsearch&q=' + encodeURI(value)); - }); +}; - addEvent(inObj, 'keyup', function () {clear_results(); delay.start(); }); - // attach eventhandler to output field - addEvent(outObj, 'click', function () {outObj.style.display = 'none'; }); +addInitEvent(function(){ + ajax_quicksearch.init('qsearch__in','qsearch__out'); }); + -- cgit v1.2.3