1 | /**
|
---|
2 | * AJAX functions for the pagename quicksearch
|
---|
3 | *
|
---|
4 | * @license GPL2 (http://www.gnu.org/licenses/gpl.html)
|
---|
5 | * @author Andreas Gohr <[email protected]>
|
---|
6 | * @author Adrian Lang <[email protected]>
|
---|
7 | */
|
---|
8 | var ajax_quicksearch = {
|
---|
9 |
|
---|
10 | inObj: null,
|
---|
11 | outObj: null,
|
---|
12 | sackObj: null,
|
---|
13 | delay: null,
|
---|
14 |
|
---|
15 | init: function(inID, outID) {
|
---|
16 |
|
---|
17 | this.inObj = $(inID);
|
---|
18 | this.outObj = $(outID);
|
---|
19 |
|
---|
20 | // objects found?
|
---|
21 | if (this.inObj === null) return;
|
---|
22 | if (this.outObj === null) return;
|
---|
23 |
|
---|
24 | // prepare AJAX
|
---|
25 | this.sackObj = new sack(DOKU_BASE + 'lib/exe/ajax.php');
|
---|
26 | this.sackObj.AjaxFailedAlert = '';
|
---|
27 | this.sackObj.encodeURIString = false;
|
---|
28 | this.sackObj.onCompletion = ajax_quicksearch.onCompletion;
|
---|
29 |
|
---|
30 | // attach eventhandler to search field
|
---|
31 | this.delay = new Delay(function () {
|
---|
32 | ajax_quicksearch.clear_results();
|
---|
33 | var value = ajax_quicksearch.inObj.value;
|
---|
34 | if(value === ''){ return; }
|
---|
35 | ajax_quicksearch.sackObj.runAJAX('call=qsearch&q=' + encodeURI(value));
|
---|
36 | });
|
---|
37 |
|
---|
38 | addEvent(this.inObj, 'keyup', function () {
|
---|
39 | ajax_quicksearch.clear_results();
|
---|
40 | ajax_quicksearch.delay.start();
|
---|
41 | });
|
---|
42 |
|
---|
43 | // attach eventhandler to output field
|
---|
44 | addEvent(this.outObj, 'click', function () {
|
---|
45 | ajax_quicksearch.outObj.style.display = 'none';
|
---|
46 | });
|
---|
47 | },
|
---|
48 |
|
---|
49 | clear_results: function(){
|
---|
50 | ajax_quicksearch.outObj.style.display = 'none';
|
---|
51 | ajax_quicksearch.outObj.innerHTML = '';
|
---|
52 | },
|
---|
53 |
|
---|
54 | onCompletion: function() {
|
---|
55 | var data = this.response; // 'this' is sack context
|
---|
56 | if (data === '') { return; }
|
---|
57 |
|
---|
58 | var outObj = ajax_quicksearch.outObj;
|
---|
59 |
|
---|
60 | outObj.innerHTML = data;
|
---|
61 | outObj.style.display = 'block';
|
---|
62 | outObj.style['white-space'] = 'nowrap';
|
---|
63 |
|
---|
64 | // shorten namespaces if too long
|
---|
65 | var width = outObj.clientWidth;
|
---|
66 | var links = outObj.getElementsByTagName('a');
|
---|
67 | for(var i=0; i<links.length; i++){
|
---|
68 | // maximum allowed width:
|
---|
69 | var max = width - links[i].offsetLeft;
|
---|
70 | var isRTL = (document.documentElement.dir == 'rtl');
|
---|
71 |
|
---|
72 | if(!isRTL && links[i].offsetWidth < max) continue;
|
---|
73 | if(isRTL && links[i].offsetLeft > 0) continue;
|
---|
74 |
|
---|
75 | var nsL = links[i].innerText.indexOf('(');
|
---|
76 | var nsR = links[i].innerText.indexOf(')');
|
---|
77 | var eli = 0;
|
---|
78 | var runaway = 0;
|
---|
79 |
|
---|
80 | while( (nsR - nsL > 3) &&
|
---|
81 | (
|
---|
82 | (!isRTL && links[i].offsetWidth > max) ||
|
---|
83 | (isRTL && links[i].offsetLeft < 0)
|
---|
84 | )
|
---|
85 | ){
|
---|
86 | if(runaway++ > 500) return; // just in case something went wrong
|
---|
87 |
|
---|
88 | if(eli){
|
---|
89 | // elipsis already inserted
|
---|
90 | if( (eli - nsL) > (nsR - eli) ){
|
---|
91 | // cut left
|
---|
92 | links[i].innerText = links[i].innerText.substring(0,eli-2)+
|
---|
93 | links[i].innerText.substring(eli);
|
---|
94 | }else{
|
---|
95 | // cut right
|
---|
96 | links[i].innerText = links[i].innerText.substring(0,eli+1)+
|
---|
97 | links[i].innerText.substring(eli+2);
|
---|
98 | }
|
---|
99 | }else{
|
---|
100 | // replace middle with ellipsis
|
---|
101 | var mid = Math.floor( nsL + ((nsR-nsL)/2) );
|
---|
102 | links[i].innerText = links[i].innerText.substring(0,mid)+'âŠ'+
|
---|
103 | links[i].innerText.substring(mid+1);
|
---|
104 | }
|
---|
105 | eli = links[i].innerText.indexOf('âŠ');
|
---|
106 | nsL = links[i].innerText.indexOf('(');
|
---|
107 | nsR = links[i].innerText.indexOf(')');
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | };
|
---|
113 |
|
---|
114 |
|
---|
115 | addInitEvent(function(){
|
---|
116 | ajax_quicksearch.init('qsearch__in','qsearch__out');
|
---|
117 | });
|
---|
118 |
|
---|