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 force = ''; 25 26 /** 27 * initialize everything 28 */ 29 pub.init = function() { 30 $msg = jQuery('#plugin__searchindex_msg'); 31 if( ! $msg) return; 32 33 lang = LANG.plugins.searchindex; 34 url = DOKU_BASE + 'lib/plugins/searchindex/ajax.php'; 35 36 $buttons = jQuery('#plugin__searchindex_buttons'); 37 38 // init interface events 39 jQuery('#plugin__searchindex_update').click(pub.update); 40 jQuery('#plugin__searchindex_rebuild').click(pub.rebuild); 41 }; 42 43 /** 44 * Gives textual feedback 45 */ 46 var message = function(text) { 47 if (text.charAt(0) !== '<') { 48 text = '<p>' + text + '</p>' 49 } 50 $msg.html(text); 51 }; 52 53 /** 54 * Starts the indexing of a page. 55 */ 56 var index = function() { 57 if (page) { 58 jQuery.post(url, 'call=indexpage&page=' + encodeURI(page) + '&force=' + force, function(response) { 59 var wait = 250; 60 // next page from queue 61 page = pages.shift(); 62 done++; 63 64 var msg = (response !== 'true') ? lang.notindexed : lang.indexed; 65 status = '<p class="status">' + msg + '</p>'; 66 message('<p>' + lang.indexing + ' ' + done + '/' + count + '</p><p class="name">' + page + '</p>' + status); 67 // next index run 68 window.setTimeout(index, wait); 69 }); 70 } else { 71 finished(); 72 } 73 }; 74 75 var finished = function() { 76 // we're done 77 throbber_off(); 78 message(lang.done); 79 window.setTimeout(function() { 80 message(''); 81 $buttons.show('slow'); 82 }, 3000); 83 }; 84 /** 85 * Cleans the index (ready for complete rebuild) 86 */ 87 var clear = function() { 88 message(lang.clearing); 89 jQuery.post(url, 'call=clearindex', function(response) { 90 if (response !== 'true') { 91 message(response); 92 // retry 93 window.setTimeout(clear,5000); 94 } else { 95 // start indexing 96 force = 'true'; 97 window.setTimeout(index,1000); 98 } 99 }); 100 }; 101 102 pub.rebuild = function() { 103 pub.update(true); 104 }; 105 /** 106 * Starts the index update 107 */ 108 pub.update = function(rebuild) { 109 done = 1; 110 rebuild = rebuild || false; 111 $buttons.hide('slow'); 112 throbber_on(); 113 message(lang.finding); 114 jQuery.post(url, 'call=pagelist', function(response) { 115 if (response !== 'true') { 116 pages = response.split("\n"); 117 count = pages.length; 118 message(lang.pages.replace(/%d/, pages.length)); 119 120 // move the first page from the queue 121 page = pages.shift(); 122 123 // complete index rebuild? 124 if (rebuild === true) { 125 clear(); 126 } else { 127 force = ''; 128 // just start indexing immediately 129 window.setTimeout(index,1000); 130 } 131 } else { 132 finished(); 133 } 134 }); 135 }; 136 137 /** 138 * add a throbber image 139 */ 140 var throbber_on = function() { 141 $msg.addClass('updating'); 142 }; 143 144 /** 145 * Stop the throbber 146 */ 147 var throbber_off = function() { 148 $msg.removeClass('updating'); 149 }; 150 151 // return only public methods/properties 152 return pub; 153})(); 154 155jQuery(function() { 156 plugin_searchindex.init(); 157});