xref: /plugin/searchindex/script.js (revision 2a0afb61a73a397b1d1cccbae4f77e3b6bc98f04)
1/**
2 * Javascript for searchindex manager plugin
3 *
4 * @author Andreas Gohr <andi@splitbrain.org>
5 */
6
7var pl_si = {
8
9    // hold some values
10    pages: null,
11    page:  null,
12    sack:  null,
13    done:  1,
14    count: 0,
15    output: null,
16    lang: null,
17
18    /**
19     * initialize everything
20     */
21    init: function(){
22        pl_si.sack = new sack(DOKU_BASE + 'lib/plugins/searchindex/ajax.php');
23        pl_si.sack.AjaxFailedAlert = '';
24        pl_si.sack.encodeURIString = false;
25        pl_si.output = $('plugin__searchindex');
26        pl_si.lang = LANG.plugins.searchindex;
27
28        // init interface
29        pl_si.status('<button id="plugin__searchindex_btn" class="button">'+pl_si.lang.rebuild+'</button>');
30        addEvent($('plugin__searchindex_btn'),'click',pl_si.go);
31    },
32
33    /**
34     * Gives textual feedback
35     */
36    status: function(text){
37        pl_si.output.innerHTML = text;
38    },
39
40    /**
41     * Callback.
42     * Executed when the index was cleared.
43     * Starts the indexing
44     */
45    cb_clear: function(){
46        var ok = this.response;
47        if(ok == 1){
48            // start indexing
49            window.setTimeout(pl_si.index,1000);
50        }else{
51            pl_si.status(ok);
52            // retry
53            window.setTimeout(pl_si.clear,5000);
54        }
55    },
56
57    /**
58     * Callback.
59     * Executed when the list of pages came back.
60     * Starts the index clearing
61     */
62    cb_pages: function(){
63        var data = this.response;
64        pl_si.pages = data.split("\n");
65        pl_si.count = pl_si.pages.length;
66        pl_si.status(pl_si.lang.pages.replace(/%d/,pl_si.pages.length));
67
68        // move the first page from the queue
69        pl_si.page = pl_si.pages.shift();
70
71        // start index cleaning
72        window.setTimeout(pl_si.clear,1000);
73    },
74
75    /**
76     * Callback.
77     * Returned after indexing one page
78     * Calls the next index run.
79     */
80    cb_index: function(){
81        var ok = this.response;
82        var wait = 500;
83        if(ok == 1){
84            // next page from queue
85            pl_si.page = pl_si.pages.shift();
86            pl_si.done++;
87        }else{
88            // something went wrong, show message
89            pl_si.status(ok);
90            wait = 5000;
91        }
92        // next index run
93        window.setTimeout(pl_si.index,500);
94    },
95
96    /**
97     * Starts the indexing of a page.
98     */
99    index: function(){
100        if(pl_si.page){
101            pl_si.status(pl_si.lang.indexing+' <b>'+pl_si.page+'</b> ('+pl_si.done+'/'+pl_si.count+')');
102            pl_si.sack.onCompletion = pl_si.cb_index;
103            pl_si.sack.URLString = '';
104            pl_si.sack.runAJAX('call=indexpage&page='+encodeURI(pl_si.page));
105        }else{
106            // we're done
107            pl_si.throbber_off();
108            pl_si.status(pl_si.lang.done);
109        }
110    },
111
112    /**
113     * Cleans the index
114     */
115    clear: function(){
116        pl_si.status(pl_si.lang.clearing);
117        pl_si.sack.onCompletion = pl_si.cb_clear;
118        pl_si.sack.URLString = '';
119        pl_si.sack.runAJAX('call=clearindex');
120    },
121
122    /**
123     * Starts the whole index rebuild process
124     */
125    go: function(){
126        pl_si.throbber_on();
127        pl_si.status(pl_si.lang.finding);
128        pl_si.sack.onCompletion = pl_si.cb_pages;
129        pl_si.sack.URLString = '';
130        pl_si.sack.runAJAX('call=pagelist');
131    },
132
133    /**
134     * add a throbber image
135     */
136    throbber_on: function(){
137        pl_si.output.style['background-image'] = "url('"+DOKU_BASE+'lib/images/throbber.gif'+"')";
138        pl_si.output.style['background-repeat'] = 'no-repeat';
139    },
140
141    /**
142     * Stop the throbber
143     */
144    throbber_off: function(){
145        pl_si.output.style['background-image'] = 'none';
146    }
147};
148
149addInitEvent(function(){
150    pl_si.init();
151});
152
153
154//Setup VIM: ex: et ts=4 enc=utf-8 :
155