xref: /plugin/searchindex/script.js (revision 73c6efcd6d8a4f75f0c193ebfdfe99e616e7347e)
1/**
2 * Javascript for searchindex manager plugin
3 *
4 * @author Andreas Gohr <andi@splitbrain.org>
5 * @author Symon Bent <hendrybadao@gmail.com>
6 *     Complete rewrite using jQuery and revealing module pattern
7 *     Separate update and rebuild options
8 */
9
10var plugin_searchindex = (function() {
11
12    // public methods/properties
13    var pub = {};
14
15    // private vars
16    var pages = null,
17        page =  null,
18        url =  null,
19        done =  1,
20        count = 0,
21        $msg = null,
22        $buttons = null,
23        lang = null;
24
25    /**
26     * initialize everything
27     */
28    pub.init = function() {
29        $msg = jQuery('#plugin__searchindex_msg');
30        if( ! $msg) return;
31
32        lang = LANG.plugins.searchindex;
33        url = DOKU_BASE + 'lib/plugins/searchindex/ajax.php';
34
35        $buttons = jQuery('#plugin__searchindex_buttons');
36
37        // init interface events
38        jQuery('#plugin__searchindex_update').click(pub.update);
39        jQuery('#plugin__searchindex_rebuild').click(pub.rebuild);
40    };
41
42    /**
43     * Gives textual feedback
44     */
45    var message = function(text) {
46        if (text.charAt(0) !== '<') {
47            text = '<p>' + text + '</p>'
48        }
49        $msg.html(text);
50    };
51
52    /**
53     * Starts the indexing of a page.
54     */
55    var index = function() {
56        if (page) {
57            jQuery.post(url, 'call=indexpage&page=' + encodeURI(page), function(response) {
58                var wait = 250;
59                var status = lang.indexed;
60                if (response !== 1) {
61                    // either up-to-date or error: skipped
62                    status = '<p class="status">' + lang.notindexed + '</p>';
63                }
64                // next page from queue
65                page = pages.shift();
66                done++;
67
68                message('<p>' + lang.indexing + ' ' + done + '/' + count + '</p><p class="name">' + page + '</p>' + status);
69                // next index run
70                window.setTimeout(index, wait);
71            });
72        } else {
73            finished();
74        }
75    };
76
77    var finished = function() {
78        // we're done
79        throbber_off();
80        message(lang.done);
81        window.setTimeout(function() {
82            message('');
83            $buttons.show('slow');
84        }, 3000);
85    };
86    /**
87     * Cleans the index (ready for complete rebuild)
88     */
89    var clear = function() {
90        message(lang.clearing);
91        jQuery.post(url, 'call=clearindex', function(response) {
92            if (response !== 1) {
93                message(response);
94                // retry
95                window.setTimeout(clear,5000);
96            }
97        });
98    };
99
100    pub.rebuild = function() {
101        pub.update(true);
102    };
103    /**
104     * Starts the index update
105     */
106    pub.update = function(rebuild) {
107        rebuild = rebuild || false;
108        $buttons.hide('slow');
109        throbber_on();
110        message(lang.finding);
111        jQuery.post(url, 'call=pagelist', function(response) {
112            if (response != 1) {
113                pages = response.split("\n");
114                count = pages.length;
115                message(lang.pages.replace(/%d/, pages.length));
116
117                // move the first page from the queue
118                page = pages.shift();
119
120                // complete index rebuild?
121                if (rebuild === true) clear();
122
123                // start indexing
124                window.setTimeout(index,1000);
125            } else {
126                finished();
127            }
128        });
129    };
130
131    /**
132     * add a throbber image
133     */
134    var throbber_on = function() {
135        $msg.addClass('updating');
136    };
137
138    /**
139     * Stop the throbber
140     */
141    var throbber_off = function() {
142        $msg.removeClass('updating');
143    };
144
145    // return only public methods/properties
146    return pub;
147})();
148
149jQuery(function() {
150    plugin_searchindex.init();
151});