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 !== 'true') { 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 !== 'true') { 93 message(response); 94 // retry 95 window.setTimeout(clear,5000); 96 } else { 97 // start indexing 98 window.setTimeout(index,1000); 99 } 100 }); 101 }; 102 103 pub.rebuild = function() { 104 pub.update(true); 105 }; 106 /** 107 * Starts the index update 108 */ 109 pub.update = function(rebuild) { 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 // just start indexing 128 window.setTimeout(index,1000); 129 } 130 } else { 131 finished(); 132 } 133 }); 134 }; 135 136 /** 137 * add a throbber image 138 */ 139 var throbber_on = function() { 140 $msg.addClass('updating'); 141 }; 142 143 /** 144 * Stop the throbber 145 */ 146 var throbber_off = function() { 147 $msg.removeClass('updating'); 148 }; 149 150 // return only public methods/properties 151 return pub; 152})(); 153 154jQuery(function() { 155 plugin_searchindex.init(); 156});