From b29919f7882f5825a2b980e97f208e0bb3aeffa0 Mon Sep 17 00:00:00 2001 From: Michal Rezler Date: Thu, 24 Mar 2011 21:49:25 +0100 Subject: ajax.js is jQueryfied --- lib/scripts/ajax.js | 124 ++++++++++++++++++++++++++++------------------------ 1 file changed, 68 insertions(+), 56 deletions(-) (limited to 'lib/scripts/ajax.js') diff --git a/lib/scripts/ajax.js b/lib/scripts/ajax.js index 44dcee999..ac634fa2c 100644 --- a/lib/scripts/ajax.js +++ b/lib/scripts/ajax.js @@ -4,67 +4,81 @@ * @license GPL2 (http://www.gnu.org/licenses/gpl.html) * @author Andreas Gohr * @author Adrian Lang + * @author Michal Rezler */ -var ajax_quicksearch = { - - inObj: null, - outObj: null, - sackObj: null, - delay: null, - - init: function(inID, outID) { - - this.inObj = $(inID); - this.outObj = $(outID); + +(function ($) { + var init, clear_results, onCompletion; + + var ajax_quicksearch = { + inObj: null, + outObj: null, + sackObj: null, + delay: null, + }; + + init = function(inID, outID) { + + ajax_quicksearch.inObj = $(inID); + ajax_quicksearch.outObj = $(outID); // objects found? - if (this.inObj === null) return; - if (this.outObj === null) return; + if (ajax_quicksearch.inObj === null) return; + if (ajax_quicksearch.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; + ajax_quicksearch.sackObj = new sack(DOKU_BASE + 'lib/exe/ajax.php'); + ajax_quicksearch.sackObj.AjaxFailedAlert = ''; + ajax_quicksearch.sackObj.encodeURIString = false; + ajax_quicksearch.sackObj.onCompletion = ajax_quicksearch.onCompletion; // 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; 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 input field + $(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( + function() { + ajax_quicksearch.outObj.hide(); + } + ); + + }; - clear_results: function(){ - ajax_quicksearch.outObj.style.display = 'none'; - ajax_quicksearch.outObj.innerHTML = ''; - }, + clear_results = function(){ + ajax_quicksearch.outObj.hide(); + ajax_quicksearch.outObj.text(''); + }; - onCompletion: function() { + 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'; + outObj.text(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 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(')'); } } - } - -}; - + }; + + $(function () { + init('qsearch__in','qsearch__out'); + }); -addInitEvent(function(){ - ajax_quicksearch.init('qsearch__in','qsearch__out'); -}); +}(jQuery)); -- cgit v1.2.3 From 3044fa04f4879fe194de48009c13d119ac7782a5 Mon Sep 17 00:00:00 2001 From: Michal Rezler Date: Sat, 26 Mar 2011 13:24:46 +0100 Subject: using POST instead of GET in searching by ajax --- lib/scripts/ajax.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'lib/scripts/ajax.js') diff --git a/lib/scripts/ajax.js b/lib/scripts/ajax.js index ac634fa2c..fe93502cb 100644 --- a/lib/scripts/ajax.js +++ b/lib/scripts/ajax.js @@ -4,16 +4,15 @@ * @license GPL2 (http://www.gnu.org/licenses/gpl.html) * @author Andreas Gohr * @author Adrian Lang - * @author Michal Rezler + * @author Michal Rezler */ -(function ($) { + (function ($) { var init, clear_results, onCompletion; var ajax_quicksearch = { inObj: null, outObj: null, - sackObj: null, delay: null, }; @@ -26,21 +25,24 @@ if (ajax_quicksearch.inObj === null) return; if (ajax_quicksearch.outObj === null) return; - // prepare AJAX - ajax_quicksearch.sackObj = new sack(DOKU_BASE + 'lib/exe/ajax.php'); - ajax_quicksearch.sackObj.AjaxFailedAlert = ''; - ajax_quicksearch.sackObj.encodeURIString = false; - ajax_quicksearch.sackObj.onCompletion = ajax_quicksearch.onCompletion; - // attach eventhandler to search field ajax_quicksearch.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)); + $.post( + DOKU_BASE + 'lib/exe/ajax.php', + { + call: 'qsearch', + q: encodeURI(value) + }, + function (data) { + onCompletion(data); + }, + 'html' + ); }); - // attach eventhandler to input field $(ajax_quicksearch.inObj).keyup( function() { ajax_quicksearch.clear_results(); @@ -62,8 +64,7 @@ ajax_quicksearch.outObj.text(''); }; - onCompletion = function() { - var data = this.response; // 'this' is sack context + onCompletion = function(data) { if (data === '') { return; } var outObj = ajax_quicksearch.outObj; @@ -126,5 +127,4 @@ init('qsearch__in','qsearch__out'); }); -}(jQuery)); - +}(jQuery)); \ No newline at end of file -- cgit v1.2.3 From e91ea5c1942bfc6533e12375b8140ce774c7d8ca Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 3 Jun 2011 13:34:44 +0200 Subject: Fixed quick search This was broken by the JQuery port. This patch makes the page search work again and also removes the anonymous wrapper function around ajax_quicksearch again. --- lib/scripts/ajax.js | 87 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 38 deletions(-) (limited to 'lib/scripts/ajax.js') diff --git a/lib/scripts/ajax.js b/lib/scripts/ajax.js index aa083978c..19d4b86a6 100644 --- a/lib/scripts/ajax.js +++ b/lib/scripts/ajax.js @@ -7,43 +7,45 @@ * @author Michal Rezler */ - (function ($) { - var init, clear_results, onCompletion; - - var ajax_quicksearch = { - inObj: null, - outObj: null, - delay: null, - }; - - init = function(inID, outID) { - - ajax_quicksearch.inObj = $(inID); - ajax_quicksearch.outObj = $(outID); +var ajax_quicksearch = { + + 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 (ajax_quicksearch.inObj === null) return; - if (ajax_quicksearch.outObj === null) return; + if (ajax_quicksearch.inObj === []) return; + if (ajax_quicksearch.outObj === []) return; // attach eventhandler to search field 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; } - $.post( + jQuery.post( DOKU_BASE + 'lib/exe/ajax.php', { call: 'qsearch', q: encodeURI(value) }, - function (data) { - onCompletion(data); - }, + ajax_quicksearch.onCompletion, 'html' ); }); - $(ajax_quicksearch.inObj).keyup( + ajax_quicksearch.inObj.keyup( function() { ajax_quicksearch.clear_results(); ajax_quicksearch.delay.start(); @@ -51,34 +53,43 @@ ); // attach eventhandler to output field - $(ajax_quicksearch.outObj).click( - function() { - ajax_quicksearch.outObj.hide(); - } + ajax_quicksearch.outObj.click( + ajax_quicksearch.outObj.hide ); - }; + }, - clear_results = function(){ + /** + * Empty and hide the output div + */ + clear_results: function(){ ajax_quicksearch.outObj.hide(); ajax_quicksearch.outObj.text(''); - }; - - onCompletion = function(data) { + }, + + /** + * 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.text(data); + outObj.html(data); outObj.show(); outObj.css('white-space', 'nowrap'); // shorten namespaces if too long var width = outObj.clientWidth; - var links = $('ajax_quicksearch outObj a'); + var links = outObj.find('a'); for (var i=0; i 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/ajax.js | 141 ---------------------------------------------------- 1 file changed, 141 deletions(-) delete mode 100644 lib/scripts/ajax.js (limited to 'lib/scripts/ajax.js') diff --git a/lib/scripts/ajax.js b/lib/scripts/ajax.js deleted file mode 100644 index 19d4b86a6..000000000 --- a/lib/scripts/ajax.js +++ /dev/null @@ -1,141 +0,0 @@ -/** - * 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 ajax_quicksearch = { - - 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 (ajax_quicksearch.inObj === []) return; - if (ajax_quicksearch.outObj === []) return; - - // attach eventhandler to search field - ajax_quicksearch.delay = new Delay(function () { - ajax_quicksearch.clear_results(); - var value = ajax_quicksearch.inObj.val(); - if(value === ''){ return; } - jQuery.post( - DOKU_BASE + 'lib/exe/ajax.php', - { - call: 'qsearch', - q: encodeURI(value) - }, - ajax_quicksearch.onCompletion, - 'html' - ); - }); - - ajax_quicksearch.inObj.keyup( - function() { - ajax_quicksearch.clear_results(); - ajax_quicksearch.delay.start(); - } - ); - - // attach eventhandler to output field - ajax_quicksearch.outObj.click( - ajax_quicksearch.outObj.hide - ); - - }, - - /** - * Empty and hide the output div - */ - clear_results: function(){ - ajax_quicksearch.outObj.hide(); - ajax_quicksearch.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) { - if (data === '') { return; } - - var outObj = ajax_quicksearch.outObj; - - outObj.html(data); - outObj.show(); - outObj.css('white-space', 'nowrap'); - - // shorten namespaces if too long - var width = outObj.clientWidth; - var links = outObj.find('a'); - - for (var i=0; i 0) continue; - - var nsL = content.indexOf('('); - var nsR = content.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 - content = content.substring(0,eli-2) + content.substring(eli); - }else{ - // cut right - content = content.substring(0,eli+1) + content.substring(eli+2); - } - }else{ - // replace middle with ellipsis - var mid = Math.floor( nsL + ((nsR-nsL)/2) ); - content = content.substring(0,mid)+'…' + content.substring(mid+1); - } - - eli = content.indexOf('…'); - nsL = content.indexOf('('); - nsR = content.indexOf(')'); - } - } - } -}; - -jQuery(function () { - ajax_quicksearch.init('#qsearch__in','#qsearch__out'); -}); - -- cgit v1.2.3