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