/* gui-js/filefox.js (c) 2008-20011 whygee@f-cpu.org This file is distributed under terms of the Affero GPL v3 license or later, see http://yasep.org created 2009-05-29 from various previous prototypes version 2009-05-31 version 2009-07-07 : moved to JSgui version 2011-10-18 : moved to gui-js, cleanup, etc. version 2011-10-28 : changed into an object to be available from eval() version 2011-11-07 : addd ygwm hooks, timeout version 2012-04-23 : splitting DOM/backend, integrating load? Based on an article published in GNU/Linux Magazine France and written by myself. Many enhancements have been included. For the backup of JS data to the local disk, 2 methods are implemented : - "save as open window" (simple but not very practical) - loopback with PHP (better but more complex) Note that the loopback may be deactivated on yasep.org but you can add one on your local machine. It's easy under Linux, just install the Apache and PHP packages. It's even easier on Windows with EasyPHP ! Load, save and configuration are managed by separate windows/keys. It's not yet possible to save/load from multiple different keys. * "settings" : the configuration window is just an interface to some of the internal variables (they are now decoupled from the GUI). * "save" : a dedicated window that gets a string and saves it to a file. gui-js/savefile.js contains */ //BORK!!!!!!!!!!!!!!!!!!!! blackList("filefox_form"); FF={ // Various DOM objects for the input/output of informations, // beware of race conditions msg_id:null, loopback_address_id:null, save_with_loopback_id:null, callback_user:null, // can be replaced by something more suitable depending on the interface message: function(col, text) { FF.msg_id.innerHTML = ''+text+""; }, retry_delay:30000, // don't probe again within 30s (reduce server load) retry_flag:null, retry: function(){ FF.retry_flag=null; FF.message("black",""); }, /////////////////////////////////////////////////////////////////// // Check the loopback server /////////////////////////////////////////////////////////////////// // address of the loopback server : target_loopback:null, semaphore:0, form:null, old_action:"", save_with_loopback:true, getSemaphore: function() { FF.semaphore++; }, releaseSemaphore: function() { if (FF.semaphore<=0) { Al8N("FFor"); return; // Justin Case } FF.semaphore--; if (FF.semaphore==0){ if (typeof FF.callback_user=="function") { FF.callback_user(); FF.callback_user=null; // cleanup } } }, // Asynchronous detection : probe_server: function(up, down) { FF.message("black", I8N("Pts")); FF.getSemaphore(); try{ var XHR = new XMLHttpRequest(); XHR.open("POST",FF.target_loopback, true); XHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); XHR.onreadystatechange=function() { if (XHR.readyState==4) { try{ if ((XHR.status == 200) // HTTP OK && (XHR.responseText == "signaturemagique")) { FF.message("green", "Loopback: OK"); if (FF.retry_flag) clearTimeout(FF.retry_flag); FF.retry_flag=setTimeout(FF.retry,FF.retry_delay); up(); } else down(); } catch(err) { down(); } } }; XHR.send("probe=yakekin"); } catch(err) { down(); } }, /////////////////////////////////////////////////////////////////// // Export to file / Save to disk /////////////////////////////////////////////////////////////////// // Save the contents to a pop-up window, its contents must be cut-pasted // by the user to the desired file. saveWin:null, // container of the created pop-up window save_to_screen: function(data) { // Characters with accents (ASCII code > 126) // should also be replaced, but YASEP doesn't use them (AFAIK) // Close the window if it is already open if (FF.saveWin) FF.saveWin.close(); // create the new window FF.saveWin = window.open("", "", "menubar=yes,width=300,height=300"); // commit the data to the new window FF.saveWin.document.write("
"+data.replace(/"); FF.saveWin.document.close(); }, // Callback if success : send data server_is_up: function() { FF.old_action=FF.form.action; // backup FF.form.action=FF.target_loopback; FF.form.submit(); setTimeout(function(){ FF.form.action=FF.old_action; FF.message("black","OK"); FF.releaseSemaphore(); },200); // restore }, // Callback if failure server_is_down: function() { FF.callback_is_down(); // no server ? then use the archaic method Al8n("nls"); FF.save_to_screen(FF.form.filecontents.value); }, callback_is_down: function() { FF.message("red", FF.target_loopback+I8N("iur")); FF.callback_user=null; // cleanup FF.disable_loopback(); FF.releaseSemaphore(); }, // This is the entry point for "save" the form : save_to_disk: function(){ if (FF.save_with_loopback==true) { if (FF.semaphore!=0) FF.message("orange", I8N("Foip")); else { if (!FF.retry_flag) // Last probe was long ago, recheck FF.probe_server(FF.server_is_up, FF.server_is_down); else { FF.getSemaphore(); FF.server_is_up(); } } } else FF.save_to_screen(FF.form.filecontents.value); }, /* // These functions generate HTML code for the forms // (unused ?) make_form: function(filename, columns, lines) { if (!lines) lines="7"; if (!columns) columns="50"; var m='\n' +' ' +''; return m; }, make_ctl: function() { var m='
Use PHP loopback'
+'
'
+'Loopback address : '
+''
+'