1/**
2 * Javascript for the dokukiwix plugin
3 *
4 * @author Andreas Gohr <andi@splitbrain.org>
5 * @author Yann Hamon <yann.hamon@gmail.com>
6 */
7
8/**
9 * Class to hold some values
10 */
11function plugin_dokukiwix_class(){
12    this.pages = null;
13    this.page = null;
14    this.sack = null;
15    this.done = 1;
16    this.count = 0;
17    this.play = 0;
18    this.timeoutid = 0;
19}
20var pl_dokukiwix = new plugin_dokukiwix_class();
21pl_dokukiwix.sack = new sack(DOKU_BASE + 'lib/plugins/dokukiwix/ajax.php');
22pl_dokukiwix.sack.AjaxFailedAlert = '';
23pl_dokukiwix.sack.encodeURIString = false;
24
25/**
26 * Display the loading gif
27 */
28function plugin_dokukiwix_showThrobber(on){
29    obj = document.getElementById('pl_dokukiwix_throbber');
30    if(on){
31        obj.style.visibility='visible';
32        obj.style.display='inline';
33    }else{
34        obj.style.visibility='hidden';
35        obj.style.display='none';
36    }
37}
38
39/**
40 * Display the loading gif
41 */
42function plugin_dokukiwix_showStopButton(on){
43    obj = document.getElementById('pl_dokukiwix_stop');
44    if(on){
45        obj.style.visibility='visible';
46    }else{
47        obj.style.visibility='hidden';
48    }
49}
50
51
52/**
53 * Gives textual feedback
54 */
55function plugin_dokukiwix_status(text){
56    obj = document.getElementById('pl_dokukiwix_out');
57    obj.innerHTML = text;
58}
59
60function plugin_dokukiwix_reinit() {
61    window.clearTimeout(pl_dokukiwix.timeoutid);
62    plugin_dokukiwix_showThrobber(false);
63    pl_dokukiwix.pages = null;
64    pl_dokukiwix.page = null;
65    pl_dokukiwix.done = 1;
66    pl_dokukiwix.count = 0;
67    pl_dokukiwix.play = 0;
68    pl_dokukiwix.timeoutid = 0;
69    plugin_dokukiwix_showStopButton(false);
70    plugin_dokukiwix_showThrobber(false);
71    obj = document.getElementById('pl_dokukiwix_toggle_startpause');
72    obj.src="lib/plugins/dokukiwix/images/play.png";
73}
74
75/**
76 * Callback. Gets the list of all pages
77 */
78function plugin_dokukiwix_cb_pages(){
79    data = this.response;
80    pl_dokukiwix.pages = data.split("\n");
81    pl_dokukiwix.count = pl_dokukiwix.pages.length;
82    plugin_dokukiwix_status(pl_dokukiwix.pages.length+" pages found");
83    plugin_dokukiwix_log('Found '+pl_dokukiwix.pages.length+' pages.');
84    pl_dokukiwix.page = pl_dokukiwix.pages.shift();
85    pl_dokukiwix.timeoutid = window.setTimeout("plugin_dokukiwix_index()",1000);
86}
87
88function plugin_dokukiwix_pause(){
89    pl_dokukiwix.play = 0;
90    obj = document.getElementById('pl_dokukiwix_toggle_startpause');
91    obj.src="lib/plugins/dokukiwix/images/play.png";
92    plugin_dokukiwix_showThrobber(false);
93    window.clearTimeout(pl_dokukiwix.timeoutid);
94    plugin_dokukiwix_log('Generation paused.');
95}
96
97function plugin_dokukiwix_start(){
98    pl_dokukiwix.play = 1;
99    obj = document.getElementById('pl_dokukiwix_toggle_startpause');
100    obj.src="lib/plugins/dokukiwix/images/pause.png";
101    plugin_dokukiwix_showThrobber(true);
102
103    if (pl_dokukiwix.done > 1) {
104      pl_dokukiwix.timeoutid = window.setTimeout("plugin_dokukiwix_index()",1000);
105      plugin_dokukiwix_log('Generation resumed.');
106    }
107    else
108      plugin_dokukiwix_go(); // First time call
109}
110
111function plugin_dokukiwix_toggle_startpause(){
112    if (pl_dokukiwix.play == 0)
113        plugin_dokukiwix_start();
114    else
115        plugin_dokukiwix_pause();
116}
117
118/**
119 * Stop function. Reinitializes all the variables and deletes the lock file.
120 */
121function plugin_dokukiwix_stop(){
122    plugin_dokukiwix_pause();
123    if(confirm("Warning: You won't be able to resume if you stop now. Are you sure you want to stop?")) {
124        pl_dokukiwix.sack.onCompletion = ';';
125        pl_dokukiwix.sack.URLString = '';
126        pl_dokukiwix.sack.runAJAX('call=removeLock&page='+encodeURI(pl_dokukiwix.page));
127        plugin_dokukiwix_reinit();
128        plugin_dokukiwix_status('Genereration stopped.');
129        plugin_dokukiwix_log('Generation stopped.');
130    }
131    else
132     plugin_dokukiwix_start();
133}
134
135/**
136 * Callback. Gets the info if building of a page was successful
137 *
138 * Calls the next index run.
139 */
140function plugin_dokukiwix_cb_index(){
141    ok = this.response;
142
143    if (pl_dokukiwix.play == 1) {
144        if(ok == 1){
145            plugin_dokukiwix_log('Generated: '+pl_dokukiwix.page);
146            pl_dokukiwix.page = pl_dokukiwix.pages.shift();
147            pl_dokukiwix.done++;
148            // get next one
149            pl_dokukiwix.timeoutid  = window.setTimeout("plugin_dokukiwix_index()",500);
150        }else{
151            plugin_dokukiwix_status(ok);
152            // get next one
153            pl_dokukiwix.timeoutid = window.setTimeout("plugin_dokukiwix_index()",2000);
154        }
155    }
156}
157
158/**
159 * Starts the indexing of a page.
160 */
161function plugin_dokukiwix_index(){
162    if(pl_dokukiwix.page){
163        plugin_dokukiwix_status('Generating '+pl_dokukiwix.page+' ('+pl_dokukiwix.done+'/'+pl_dokukiwix.count+')');
164        pl_dokukiwix.sack.onCompletion = plugin_dokukiwix_cb_index;
165        pl_dokukiwix.sack.URLString = '';
166        pl_dokukiwix.sack.runAJAX('call=buildOfflinePage&page='+encodeURI(pl_dokukiwix.page));
167    }else{
168        plugin_dokukiwix_status('finished');
169        plugin_dokukiwix_showThrobber(false);
170        pl_dokukiwix.sack.onCompletion = ';';
171        pl_dokukiwix.sack.URLString = '';
172        pl_dokukiwix.sack.runAJAX('call=removeLock&page='+encodeURI(pl_dokukiwix.page));
173        plugin_dokukiwix_log('Task finished.');
174    }
175}
176
177function plugin_dokukiwix_find_pages(){
178    plugin_dokukiwix_showThrobber(true);
179    plugin_dokukiwix_showStopButton(true);
180
181    plugin_dokukiwix_status('Finding all pages...');
182    pl_dokukiwix.sack.onCompletion = plugin_dokukiwix_cb_pages;
183    pl_dokukiwix.sack.URLString = '';
184    pl_dokukiwix.sack.runAJAX('call=pagelist');
185    plugin_dokukiwix_log('Finding pages...');
186}
187
188/**
189 * Starts the whole index rebuild process
190 */
191function plugin_dokukiwix_startup(){
192     data = this.response;
193
194    if (data == 1) {
195      if (!confirm("Warning: Dokukiwix is locked. This may mean that another instance is already running. Proceed anyway? (this will stop the other instance if any)")) {
196        plugin_dokukiwix_reinit();
197        plugin_dokukiwix_status('Genereration canceled.');
198        return;
199      }
200    }
201
202    plugin_dokukiwix_status('Initialising...');
203    pl_dokukiwix.sack.onCompletion = plugin_dokukiwix_find_pages;
204    pl_dokukiwix.sack.URLString = '';
205    pl_dokukiwix.sack.runAJAX('call=dokukiwix_start');
206    plugin_dokukiwix_log('Plugin initialised.');
207}
208
209
210/**
211 * Creates the lock
212 */
213function plugin_dokukiwix_go(){
214    plugin_dokukiwix_status('Creating lock...');
215    pl_dokukiwix.sack.onCompletion = plugin_dokukiwix_startup;
216    pl_dokukiwix.sack.URLString = '';
217    pl_dokukiwix.sack.runAJAX('call=createLock');
218    plugin_dokukiwix_log('Created Lock');
219}
220
221/**
222 * Log every event
223 */
224function plugin_dokukiwix_log(logstring) {
225  var currentDateTime = new Date();
226  var currentHours, currentMinutes, currentSeconds;
227
228  if (currentDateTime.getHours()<10) currentHours = "0"+currentDateTime.getHours() ; else currentHours = currentDateTime.getHours();
229  if (currentDateTime.getMinutes()<10) currentMinutes = "0"+currentDateTime.getMinutes() ; else currentMinutes = currentDateTime.getMinutes();
230  if (currentDateTime.getSeconds()<10) currentSeconds = "0"+currentDateTime.getSeconds() ; else currentSeconds = currentDateTime.getSeconds();
231
232  document.getElementById("pl_dokukiwix_log").value += currentHours+":"+currentMinutes+":"+currentSeconds+" "+logstring+"\n";
233  document.getElementById("pl_dokukiwix_log").scrollTop = document.getElementById("pl_dokukiwix_log").scrollHeight;
234}
235
236//Setup VIM: ex: et ts=4 enc=utf-8 :
237