diff options
Diffstat (limited to 'lib/scripts/ajax.js')
-rw-r--r-- | lib/scripts/ajax.js | 127 |
1 files changed, 75 insertions, 52 deletions
diff --git a/lib/scripts/ajax.js b/lib/scripts/ajax.js index 44dcee999..19d4b86a6 100644 --- a/lib/scripts/ajax.js +++ b/lib/scripts/ajax.js @@ -4,67 +4,93 @@ * @license GPL2 (http://www.gnu.org/licenses/gpl.html) * @author Andreas Gohr <andi@splitbrain.org> * @author Adrian Lang <lang@cosmocode.de> + * @author Michal Rezler <m.rezler@centrum.cz> */ -var ajax_quicksearch = { - - inObj: null, - outObj: null, - sackObj: null, - delay: null, - init: function(inID, outID) { +var ajax_quicksearch = { - this.inObj = $(inID); - this.outObj = $(outID); + inObj: null, // jquery object + outObj: null, // jquery object + delay: 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) { + ajax_quicksearch.inObj = jQuery(input); + ajax_quicksearch.outObj = jQuery(output); // 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; + if (ajax_quicksearch.inObj === []) return; + if (ajax_quicksearch.outObj === []) return; // attach eventhandler to search field - this.delay = new Delay(function () { + ajax_quicksearch.delay = new Delay(function () { ajax_quicksearch.clear_results(); - var value = ajax_quicksearch.inObj.value; + var value = ajax_quicksearch.inObj.val(); if(value === ''){ return; } - ajax_quicksearch.sackObj.runAJAX('call=qsearch&q=' + encodeURI(value)); + jQuery.post( + DOKU_BASE + 'lib/exe/ajax.php', + { + call: 'qsearch', + q: encodeURI(value) + }, + ajax_quicksearch.onCompletion, + 'html' + ); }); - addEvent(this.inObj, 'keyup', function () { - ajax_quicksearch.clear_results(); - ajax_quicksearch.delay.start(); - }); + ajax_quicksearch.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'; - }); + ajax_quicksearch.outObj.click( + ajax_quicksearch.outObj.hide + ); + }, + /** + * Empty and hide the output div + */ clear_results: function(){ - ajax_quicksearch.outObj.style.display = 'none'; - ajax_quicksearch.outObj.innerHTML = ''; + ajax_quicksearch.outObj.hide(); + ajax_quicksearch.outObj.text(''); }, - onCompletion: function() { - var data = this.response; // 'this' is sack context + /** + * 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) { if (data === '') { return; } var outObj = ajax_quicksearch.outObj; - outObj.innerHTML = data; - outObj.style.display = 'block'; - outObj.style['white-space'] = 'nowrap'; + outObj.html(data); + outObj.show(); + outObj.css('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++){ + var links = outObj.find('a'); + + for (var i=0; i<links.length; i++) { + var content = links[i].text; + // maximum allowed width: var max = width - links[i].offsetLeft; var isRTL = (document.documentElement.dir == 'rtl'); @@ -72,47 +98,44 @@ var ajax_quicksearch = { if(!isRTL && links[i].offsetWidth < max) continue; if(isRTL && links[i].offsetLeft > 0) continue; - var nsL = links[i].innerText.indexOf('('); - var nsR = links[i].innerText.indexOf(')'); + var nsL = content.indexOf('('); + var nsR = content.indexOf(')'); var eli = 0; var runaway = 0; - while( (nsR - nsL > 3) && + 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); + content = content.substring(0,eli-2) + content.substring(eli); }else{ // cut right - links[i].innerText = links[i].innerText.substring(0,eli+1)+ - links[i].innerText.substring(eli+2); + content = content.substring(0,eli+1) + content.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); + content = content.substring(0,mid)+'…' + content.substring(mid+1); } - eli = links[i].innerText.indexOf('…'); - nsL = links[i].innerText.indexOf('('); - nsR = links[i].innerText.indexOf(')'); + + eli = content.indexOf('…'); + nsL = content.indexOf('('); + nsR = content.indexOf(')'); } } } - }; - -addInitEvent(function(){ - ajax_quicksearch.init('qsearch__in','qsearch__out'); +jQuery(function () { + ajax_quicksearch.init('#qsearch__in','#qsearch__out'); }); |