From: Paul P. <pau...@ac...> - 2006-10-26 13:23:24
|
I have been working on a javascript function in order to cancel the last actions performed by the user (an undo function). I think it might interest some of the Jmol users : - the new history command gives the succession of all the commands that have been sent to the applet. - the consecutive sets of commands sent are separated by a | character. - if 2 commands are separated by a semi-colon and are sent to the applet (eg: "select all; cpk on;") they appear in the history as a single command (eg: "previous command; | select all; cpk on; | next command..."). To make an undo function I choose to split the history based on the | character, and to concat the different pieces except the last one. The new script preceded by a command which reset the interface is then sent to the applet and it works great. The problem begins when you want to perform a second undo command : as all the previous commands are sent as one set, they are no longer separated by a | in the history and they appear as a single command. I found a workaround (there might be some others) by inserting a #|; between the commands from the history in order to keep a separation between these consecutives commands. In my opinion, the easyest way would be that the | character is understood by Jmol as a separator for the history, then one would only have to send a substring of the history in order to perform an undo, and it could be used recursively. I don't know if it could be easily implemented but I think it should help a lot. By the way, if some are interested by the javascript I used, here it is : function undo() { answer = jmolScriptWaitAsArray('show history; history on;'); script = answer[0][0][3]; pieces = script.split(' #|;'); fin = pieces[pieces.length-1].split('|'); debut = pieces.slice(0,-1); script = debut.concat(fin); while (script[script.length-1]==' ') { script.length--; } spt=''; for (i=0;i<script.length-1;i++) { if(script[i]!='') spt += script[i]+' #|;' ; } if (script[0].indexOf(initscript)==-1) { spt = initscript + ' #|;' + spt; } //alert(spt); executer(spt); } Paul |