diff options
author | Andreas Gohr <andi@splitbrain.org> | 2011-01-14 12:20:19 +0100 |
---|---|---|
committer | Andreas Gohr <andi@splitbrain.org> | 2011-01-14 12:20:19 +0100 |
commit | 5ba8d196ffe346ef6fbd8e6ffc11f0c849537ed3 (patch) | |
tree | ebd31e5968176e326c39adcf8a51caa24ac367dc | |
parent | ef7acde6835f3eda384a189e4d6f2f86a3ad9d3c (diff) | |
download | rpg-5ba8d196ffe346ef6fbd8e6ffc11f0c849537ed3.tar.gz rpg-5ba8d196ffe346ef6fbd8e6ffc11f0c849537ed3.tar.bz2 |
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)
-rw-r--r-- | lib/scripts/ajax.js | 41 |
1 files changed, 41 insertions, 0 deletions
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<links.length; i++){ + // maximum allowed width: + var max = width - links[i].offsetLeft; //FIXME use offsetRight for RTL! + if(links[i].offsetWidth < max) 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 ){ + 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 |