[sagenb] 91/157: Rewrite jmol_lib

felix salfelder felix-guest at moszumanska.debian.org
Mon Dec 22 16:51:55 UTC 2014


This is an automated email from the git hooks/post-receive script.

felix-guest pushed a commit to branch master
in repository sagenb.

commit ed4be502270b1cd9852c8718887dee81504113a2
Author: Volker Braun <vbraun.name at gmail.com>
Date:   Sun Oct 5 02:12:02 2014 +0100

    Rewrite jmol_lib
---
 sagenb/data/sage/html/notebook/base.html |   2 -
 sagenb/data/sage/js/jmol_lib.js          | 247 ++++++++++++++-----------------
 sagenb/data/sage/js/notebook_lib.js      |  21 ++-
 3 files changed, 124 insertions(+), 146 deletions(-)

diff --git a/sagenb/data/sage/html/notebook/base.html b/sagenb/data/sage/html/notebook/base.html
index 2da83d3..64b5dc8 100644
--- a/sagenb/data/sage/html/notebook/base.html
+++ b/sagenb/data/sage/html/notebook/base.html
@@ -34,7 +34,6 @@ INPUT:
 
 <!-- Jmol - embedded 3D graphics **needs to be before jmol_lib.js** -->
 <script type="text/javascript" src="/jsmol/JSmol.min.nojq.js"></script>
-<!-- <script type="text/javascript" src="/jsmol/js/Jmol2.js"></script> -->
 
 {% if not worksheet.is_published() or worksheet.notebook().conf()['pub_interact'] %}
 <script type="text/javascript" src="/javascript/sage/js/notebook_lib.js"></script>
@@ -143,7 +142,6 @@ INPUT:
                onchange="pretty_print_check(this.checked);"
                class="worksheet" value="pretty_print" {{ "checked" if worksheet.pretty_print() else "" }} /> Typeset
         <input id="3D_check", type="checkbox" title="{{ gettext('Load 3-D Live. Not recommended for worksheets with > 2 3-D Plots.') }}"
-               onchange="live_3D_check(this.checked);"
                class="worksheet" value="live_3D" {{ "checked" if worksheet.live_3D() else "" }} /> {{ gettext('Load 3-D Live') }}
         {% endif %}
     {% endif %} 
diff --git a/sagenb/data/sage/js/jmol_lib.js b/sagenb/data/sage/js/jmol_lib.js
index bd87c7b..89179c8 100644
--- a/sagenb/data/sage/js/jmol_lib.js
+++ b/sagenb/data/sage/js/jmol_lib.js
@@ -1,111 +1,125 @@
-/*This is a very vanilla script to get Jmol/JSmol working in Sage again.  I
-  have removed all the controls and the automatic sleeping that avoided
-  memory overload of web browsers.
-  Jonathan Gutow <gutow at uwosh.edu> February 2014 
-*/
-
-// Turn off the JSmolCore.js: synchronous binary file transfer is
-// requested but not available" warning
-Jmol._alertNoBinary = false
-
-jmol_isReady = function(jmolApplet) {
-    console.log('Jmol is ready');
-    Jmol.script(jmolApplet, "set platformSpeed 6;");
-    //alert("Applet: "+jmolApplet+" has launched.");
+
+
+SageJmolManager = function() {
+    this._prepare_jmol();
+    this._count = 0;
+    this._applets = new Object();  
+    this._limit = 1;     // allow at most this many active jmols.
+    this._watcher = setInterval(this.activator.bind(this), 500);
+}
+
+SageJmolManager.prototype._prepare_jmol = function() {
+    // Turn off the JSmolCore.js: synchronous binary file transfer is
+    // requested but not available" warning
+    Jmol._alertNoBinary = false;
+}
+
+
+SageJmolManager.prototype.default_info = function() {
+    // Before adding anything here make sure it is not overwritten in
+    // add_applet()
+    return {
+        width: "100%",
+        height: "100%",
+        // debug=true will pop up alert boxes
+        debug: false,
+        color: "white",
+        addSelectionOptions: false,
+        use: "HTML5 WebGL Java",
+        // Tooltip when the mouse is over the static image
+        coverTitle: 
+            'Click on 3-D image to make it live. ' + 
+            'Right-click on live image for a control menu.',
+        deferApplet: true,                  // wait to load applet until click
+        deferUncover: true,                 // wait to uncover applet until script completed
+        //The paths below assume your server is set up with standard JSmol directory.  If not
+        //they will need modification for the page to work.
+        jarPath: "/jsmol/java", //path to applet .jar files on server.
+        j2sPath: "/jsmol/j2s",//path to javascript version.
+        makeLiveImg:"/jsmol/j2s/img/play_make_live.jpg",  //path to activate 3-D image.
+        jarFile: "JmolAppletSigned0.jar",
+        isSigned: true,
+        //disableJ2SLoadMonitor: true,
+        disableInitialConsole: true,
+        script: "",
+        z: 5,
+        zIndexBase: 5,
+        menuFile: "/jsmol/appletweb/SageMenu.mnu", //special sagemenu
+        platformSpeed: 6,
+    };
 }
 
-var jmolInfo = { //default values
-    width: "100%",
-    height: "100%",
-    // debug=true will pop up alert boxes
-    debug: false,
-    color: "white",
-    addSelectionOptions: false,
-    use: "HTML5 WebGL Java",
-    // Tooltip when the mouse is over the static image
-    coverTitle: 'Click on 3-D image to make it live.  Right-click on live image for a control menu.',
-    deferApplet: true,                  // wait to load applet until click
-    deferUncover: true,                 // wait to uncover applet until script completed
-    //The paths below assume your server is set up with standard JSmol directory.  If not
-    //they will need modification for the page to work.
-    jarPath: "/jsmol/java", //path to applet .jar files on server.
-    j2sPath: "/jsmol/j2s",//path to javascript version.
-    makeLiveImg:"/jsmol/j2s/img/play_make_live.jpg",  //path to activate 3-D image.
-    jarFile: "JmolAppletSigned0.jar",
-    isSigned: true,
-    //disableJ2SLoadMonitor: true,
-    disableInitialConsole: true,
-    readyFunction: jmol_isReady,
-    script: "",
-    z: 5,
-    zIndexBase: 5,
-    menuFile: "/jsmol/appletweb/SageMenu.mnu", //special sagemenu
-    platformSpeed: 6,
+SageJmolManager.prototype.ready_callback = function (name, applet) {
+    console.log('Jmol applet has launched ' + name);
+    this._applets[name] = applet;
+    this.enforce_limit();
 }
 
-var jmol_count = 0;
-
-jmolStatus = {//most of these not used in the lightweight version, kept to make widgets easy to add back.
-    maxLiveAllowed: 4,
-    numLive: 0,
-    loadSigned: false, //when set to true will load signed applets.
-    signed: new Array(), //false for unsigned applets, true for signed applets.
-    jmolArray: new Array(),//-2 loading failed, -1 deleted, 0 awake, 1 sleeping, 2 loading, 3 waiting to load.
-    urls: new Array(),
-    defaultdirectory: new Array(),
-    widths: new Array(),
-    heights: new Array(),
-    controlStrs: new Array(),
-    captionStrs: new Array(),
-    pictureStrs: new Array(),
-    stateScripts: new Array(),
-    cntrls: new Array(),
-    attempts: new Array(),
-    jmolInfo: new Object(),
+
+// Make sure that there are not too many active applets
+SageJmolManager.prototype.enforce_limit = function() {
+    var applet_names = Object.keys(this._applets);
+    // alphabetical sort equals order of construction because of the counter
+    applet_names.sort();
+    for (i = 0; i < applet_names.length - this._limit; i++) {
+        var name = applet_names[i];
+        var applet = this._applets[name];
+        Jmol.coverApplet(applet, true);
+    }
 }
 
-//Some default constants
-//applet sizes
-var miniature = 100;
-var small = 250;
-var medium = 500;
-var large = 800;
-
-//Check whether to load 3-D live
-//live_3D_state = $('#3D_check').prop('checked');
-//start the watch function
-jmolWatcher = setInterval('jmolActivator();',500);
-
-// makes a new applet and puts it into the dom
-function make_jmol_applet(size, image, script, server_url, cell_num, functionnames) {
-    var appletID = jmol_count;
-    jmol_count = jmol_count + 1;
-    jmolStatus.jmolArray[appletID] = 3; //queued to load.
-    if (size == 500)
-        size = medium; // set to medium size otherwise we will keep other sizes.
-    Jmol.setDocument(false); // manually insert Jmol.getAppletHtml later
-    jmolStatus.jmolInfo[appletID] = jQuery.extend({}, jmolInfo); // shallow copy default values
-    jmolStatus.jmolInfo[appletID].coverImage = image; //this should be the image url
-    jmolStatus.jmolInfo[appletID].script = "script "+script; //this should be the script name
-    jmolStatus.jmolInfo[appletID].serverURL = server_url;
+
+SageJmolManager.prototype.add_applet = 
+    function (size, image, script, server_url, cell_num) 
+{
+    // The id of the container div holding the applet html, use this
+    // to query the dom later to see if the applet is stil there.
+    var applet_name = 'jmolApplet' + this._count;
+    var info = this.default_info();
+    info.coverImage = image;
+    info.script = 'script ' + script;
+    info.serverURL = server_url;
+    info.readyFunction = this.ready_callback.bind(this, applet_name);
+    if (size != 500)
+        // 500 is the hardcoded (and ill-chosen) default, ignore it
+        info.width = info.height = size;
+
     // jmolStatus.jmolInfo[appletID].deferApplet = jQuery('#3D_check').prop('checked');
-    jmolDivStr = "jmol"+appletID;
-    jmolStatus.widths[appletID] = size;
-    jmolStatus.heights[appletID]= size;
-    // appending to cell_ID
-    var cell_ID = 'sage_jmol_' + cell_num;
-    // $('#'+cell_ID).append('<span></span>');
-    $('#'+cell_ID).append('<div id="'+jmolDivStr+'" style="height:'+size+'px; width:'+size+'px;" >JSmol here</div>');
-    // launching JSmol/Jmol applet
-    var applet_html = Jmol.getAppletHtml("jmolApplet"+appletID, jmolStatus.jmolInfo[appletID]);
-    $('#'+jmolDivStr).html(applet_html);
-    // we will still set all the data for this applet so that other asynchronously created applets do not grab its ID.
-    jmolStatus.signed[appletID] = jmolStatus.loadSigned;
-    return jmolDivStr;//for historical compatibility
+
+    // append container to dom
+    jQuery('#sage_jmol_' + cell_num).append(
+        '<div id="'+applet_name+'" style="height:'+size+'px; width:'+size+'px;" >JSmol here</div>'
+    );
+
+   // launching JSmol/Jmol applet
+    Jmol.setDocument(false); // manually insert Jmol.getAppletHtml
+    var applet_html = Jmol.getAppletHtml(applet_name, info);
+    jQuery('#' + applet_name).html(applet_html);
+
+    // Finished
+    this._count += 1;
+}
+
+// Callback for Action -> Delete all Output
+SageJmolManager.prototype.delete_all_callback = function() {
+    console.log('jmol: delete_all');
+    this.delete_callback();
+}
+
+// Callback for deleting single cell (may not contain jmol)
+SageJmolManager.prototype.delete_callback = function() {
+    console.log('jmol: delete_check');
+    var applet_names = Object.keys(this._applets);
+    for (i = 0; i < applet_names.length; i++) {
+        var name = applet_names[i];
+        console.log('checking ' + name);
+        if (jQuery('#' + name).length == 0)
+            delete this._applets[name];
+    }
 }
 
-function jmolActivator(){
-    if (document.getElementById("loadJmol")){
+SageJmolManager.prototype.activator = function () {
+    if (document.getElementById("loadJmol")) {
         var parentdiv = jQuery("#loadJmol").parent();
         // This div contains the ID number
         var cell_num = parentdiv.children("#loadJmol").html();
@@ -114,41 +128,10 @@ function jmolActivator(){
         var img = parentdiv.children("#sage_jmol_img_"+cell_num).html();
         var script = parentdiv.children("#sage_jmol_script_"+cell_num).html();
         var server_url = parentdiv.children("#sage_jmol_server_url_"+cell_num).html();
-        make_jmol_applet(size, img, script, server_url, cell_num);
+        sage_jmol.add_applet(size, img, script, server_url, cell_num);
         parentdiv.children("#sage_jmol_status_"+cell_num).html("Activated");
     }
 }
 
-function live_3D_check(s) {
-    /*
-    Send a message back to the server either turn live_3D on of off.
-    INPUT:
-        s -- boolean; whether the live 3D box is checked.
-    */
-    var live_3D_state = s;
-    //alert ('live_3D_state:'+live_3D_state);
-    async_request(worksheet_command('live_3D/' + s));
-}
-
-//The following two delete functions do not do anything in this truncated jmol_lib.
-//They are for compatibility with the notebook_lib.js
-function jmol_delete_all_output() {
-    //called by the delete_all_output function of the notebook to get jmol parameters cleaned up.
-    jmol_count=0;
-    jmolStatus.numLive=0;
-    jmolStatus.jmolArray=new Array();
-}
 
-function jmol_delete_check() {
-    //called when cells are evaluated to see if any jmols have been deleted.  If so update their status.
-    liveCount = jmolStatus.jmolArray.length;
-    for ( k = 0; k< liveCount; k++) {
-        testId= 'Jmol_Table_Jmol'+k; //looking for the whole table Jmol is in, since if the table is there it is sleeping.
-        if (!get_element(testId)) { //we need to set this as deleted and maybe free up the ID?
-            jmolStatus.jmolArray[k] = -1;
-            //for the time being old IDs will not be reused.  Shouldn't be real big problem as completely resets
-            //each time a page is opened.
-        }
-    }
-    //jmol_numLiveUpdate();
-}
+sage_jmol = new SageJmolManager();
diff --git a/sagenb/data/sage/js/notebook_lib.js b/sagenb/data/sage/js/notebook_lib.js
index e28d205..a212b20 100644
--- a/sagenb/data/sage/js/notebook_lib.js
+++ b/sagenb/data/sage/js/notebook_lib.js
@@ -2148,7 +2148,7 @@ function evaluate_text_cell_input(id, value, settings) {
         input: value
     });
     //update jmol applet list
-    jmol_delete_check();
+    sage_jmol.delete_callback();
 }
 
 
@@ -2318,7 +2318,7 @@ function cell_delete(id) {
         id: id
     });
     //update jmol applet list
-    jmol_delete_check();
+    sage_jmol.delete_callback();
 }
 
 
@@ -2367,9 +2367,8 @@ function cell_delete_callback(status, response) {
     if (in_slide_mode) {
         current_cell = -1;
         slide_mode();
-    //update jmol applet list
-    jmol_delete_check();
-
+        //update jmol applet list
+        sage_jmol.delete_callback();
     }
 }
 
@@ -2393,7 +2392,7 @@ function cell_delete_output(id) {
                       id: id
                   });
     //update jmol applet list
-    jmol_delete_check();
+    sage_jmol.delete_callback();
 
 }
 
@@ -2424,7 +2423,7 @@ function cell_delete_output_callback(status, response) {
     // Set the cell to not evaluated.
     cell_set_not_evaluated(X.id);
     //update list of jmol applets
-    jmol_delete_check();
+    sage_jmol.delete_callback();
 }
 
 
@@ -3069,8 +3068,7 @@ function evaluate_cell(id, newcell) {
         input: cell_input.value
     });
     //update jmol applet list
-    jmol_delete_check();
-
+    sage_jmol.delete_callback();
 }
 
 
@@ -3231,8 +3229,7 @@ function evaluate_cell_callback(status, response) {
 
     start_update_check();
     //update jmol applet list
-    jmol_delete_check();
-
+    sage_jmol.delete_callback();
 }
 
 
@@ -4512,7 +4509,7 @@ function delete_all_output() {
     // of the user interface.
     async_request(worksheet_command('delete_all_output'));
     //update jmol applet info
-    jmol_delete_all_output();
+    sage_jmol.delete_all_callback();
 }
 
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/sagenb.git



More information about the debian-science-commits mailing list