diff options
author | Michal Rezler <rezlemic@fel.cvut.cz> | 2011-03-23 10:39:45 +0100 |
---|---|---|
committer | Michal Rezler <rezlemic@fel.cvut.cz> | 2011-03-23 10:39:45 +0100 |
commit | 35838d22a57707952f630eaf9f9e9ab4c6c3cfb0 (patch) | |
tree | 3603e2e56314af40a4b7922e14e52c0bc06f6f9d /lib/scripts/ajax.js | |
parent | c4bb7947fcb2d4a5e5f8a15d9e3bbec333e44e13 (diff) | |
parent | ee1214abb2c14cf0f86ff6d9a5b49536c6b01e18 (diff) | |
download | dokuwiki-35838d22a57707952f630eaf9f9e9ab4c6c3cfb0.tar.gz dokuwiki-35838d22a57707952f630eaf9f9e9ab4c6c3cfb0.zip |
jQuery rewrite branch merged into master branch of whole project
Diffstat (limited to 'lib/scripts/ajax.js')
-rw-r--r-- | lib/scripts/ajax.js | 126 |
1 files changed, 98 insertions, 28 deletions
diff --git a/lib/scripts/ajax.js b/lib/scripts/ajax.js index de009d448..44dcee999 100644 --- a/lib/scripts/ajax.js +++ b/lib/scripts/ajax.js @@ -5,44 +5,114 @@ * @author Andreas Gohr <andi@splitbrain.org> * @author Adrian Lang <lang@cosmocode.de> */ -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(); + }); - 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; + // attach eventhandler to output field + addEvent(this.outObj, 'click', function () { + ajax_quicksearch.outObj.style.display = 'none'; + }); + }, + + 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'; - // 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)); - }); + // 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; + var isRTL = (document.documentElement.dir == 'rtl'); - addEvent(inObj, 'keyup', function () {clear_results(); delay.start(); }); + if(!isRTL && links[i].offsetWidth < max) continue; + if(isRTL && links[i].offsetLeft > 0) continue; - // attach eventhandler to output field - addEvent(outObj, 'click', function () {outObj.style.display = 'none'; }); + var nsL = links[i].innerText.indexOf('('); + var nsR = links[i].innerText.indexOf(')'); + var eli = 0; + var runaway = 0; + + 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){ + // 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(')'); + } + } + } + +}; + + +addInitEvent(function(){ + ajax_quicksearch.init('qsearch__in','qsearch__out'); }); + |