xref: /plugin/searchindex/script.js (revision 7befb32abe3d4f5cb157e5c9b5c7ded3ad839a78)
1/**
2 * Javascript for searchindex manager plugin
3 *
4 * @author Andreas Gohr <andi@splitbrain.org>
5 */
6
7var plugin_searchindex = {
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        plugin_searchindex.output = $('plugin__searchindex');
23        if(!plugin_searchindex.output) return;
24
25        plugin_searchindex.sack = new sack(DOKU_BASE + 'lib/plugins/searchindex/ajax.php');
26        plugin_searchindex.sack.AjaxFailedAlert = '';
27        plugin_searchindex.sack.encodeURIString = false;
28        plugin_searchindex.lang = LANG.plugins.searchindex;
29
30        // init interface
31        plugin_searchindex.status('<button id="plugin__searchindex_btn" class="button">'+plugin_searchindex.lang.rebuild+'</button>');
32        addEvent($('plugin__searchindex_btn'),'click',plugin_searchindex.go);
33    },
34
35    /**
36     * Gives textual feedback
37     */
38    status: function(text){
39        plugin_searchindex.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(plugin_searchindex.index,1000);
52        }else{
53            plugin_searchindex.status(ok);
54            // retry
55            window.setTimeout(plugin_searchindex.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        plugin_searchindex.pages = data.split("\n");
67        plugin_searchindex.count = plugin_searchindex.pages.length;
68        plugin_searchindex.status(plugin_searchindex.lang.pages.replace(/%d/,plugin_searchindex.pages.length));
69
70        // move the first page from the queue
71        plugin_searchindex.page = plugin_searchindex.pages.shift();
72
73        // start index cleaning
74        window.setTimeout(plugin_searchindex.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            plugin_searchindex.page = plugin_searchindex.pages.shift();
88            plugin_searchindex.done++;
89        }else{
90            // something went wrong, show message
91            plugin_searchindex.status(ok);
92            wait = 5000;
93        }
94        // next index run
95        window.setTimeout(plugin_searchindex.index,500);
96    },
97
98    /**
99     * Starts the indexing of a page.
100     */
101    index: function(){
102        if(plugin_searchindex.page){
103            plugin_searchindex.status(plugin_searchindex.lang.indexing+' <b>'+plugin_searchindex.page+'</b> ('+plugin_searchindex.done+'/'+plugin_searchindex.count+')');
104            plugin_searchindex.sack.onCompletion = plugin_searchindex.cb_index;
105            plugin_searchindex.sack.URLString = '';
106            plugin_searchindex.sack.runAJAX('call=indexpage&page='+encodeURI(plugin_searchindex.page));
107        }else{
108            // we're done
109            plugin_searchindex.throbber_off();
110            plugin_searchindex.status(plugin_searchindex.lang.done);
111        }
112    },
113
114    /**
115     * Cleans the index
116     */
117    clear: function(){
118        plugin_searchindex.status(plugin_searchindex.lang.clearing);
119        plugin_searchindex.sack.onCompletion = plugin_searchindex.cb_clear;
120        plugin_searchindex.sack.URLString = '';
121        plugin_searchindex.sack.runAJAX('call=clearindex');
122    },
123
124    /**
125     * Starts the whole index rebuild process
126     */
127    go: function(){
128        plugin_searchindex.throbber_on();
129        plugin_searchindex.status(plugin_searchindex.lang.finding);
130        plugin_searchindex.sack.onCompletion = plugin_searchindex.cb_pages;
131        plugin_searchindex.sack.URLString = '';
132        plugin_searchindex.sack.runAJAX('call=pagelist');
133    },
134
135    /**
136     * add a throbber image
137     */
138    throbber_on: function(){
139        plugin_searchindex.output.style['background-image'] = "url('"+DOKU_BASE+'lib/images/throbber.gif'+"')";
140        plugin_searchindex.output.style['background-repeat'] = 'no-repeat';
141    },
142
143    /**
144     * Stop the throbber
145     */
146    throbber_off: function(){
147        plugin_searchindex.output.style['background-image'] = 'none';
148    }
149};
150
151addInitEvent(function(){
152    plugin_searchindex.init();
153});
154
155
156//Setup VIM: ex: et ts=4 enc=utf-8 :
157