summaryrefslogtreecommitdiff
path: root/lib/scripts/ajax.js
blob: de009d4487d686e903537f4d840d0b6dcb718880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
 * AJAX functions for the pagename quicksearch
 *
 * @license  GPL2 (http://www.gnu.org/licenses/gpl.html)
 * @author   Andreas Gohr <andi@splitbrain.org>
 * @author   Adrian Lang <lang@cosmocode.de>
 */
addInitEvent(function () {

    var inID  = 'qsearch__in';
    var outID = 'qsearch__out';

    var inObj  = document.getElementById(inID);
    var outObj = document.getElementById(outID);

    // objects found?
    if (inObj === null){ return; }
    if (outObj === null){ return; }

    function clear_results(){
        outObj.style.display = 'none';
        outObj.innerHTML = '';
    }

    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;
        if (data === '') { return; }

        outObj.innerHTML = data;
        outObj.style.display = 'block';
    };

    // 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));
    });

    addEvent(inObj, 'keyup', function () {clear_results(); delay.start(); });

    // attach eventhandler to output field
    addEvent(outObj, 'click', function () {outObj.style.display = 'none'; });
});