Commit d4afa02b by Carlos Andrés Rocha

Modify editamolecule to load GWT scripts dynamically

parent 31b8f0ce
<section id="editamoleculeinput_${id}" class="editamoleculeinput"> <section id="editamoleculeinput_${id}" class="editamoleculeinput">
<div class="script_placeholder" data-src="/static/js/capa/jsmolcalc/jsmolcalc.nocache.js"/>
<div class="script_placeholder" data-src="/static/js/capa/jsme/jsme_export.nocache.js"/>
<div class="script_placeholder" data-src="${applet_loader}"/> <div class="script_placeholder" data-src="${applet_loader}"/>
% if status == 'unsubmitted': % if status == 'unsubmitted':
......
...@@ -2,23 +2,53 @@ ...@@ -2,23 +2,53 @@
var timeout = 100; var timeout = 100;
// Simple "lock" to prevent applets from being initialized more than once // Simple "lock" to prevent applets from being initialized more than once
if (typeof(_editamolecule_lock) == 'undefined' || _editamolecule_lock == false) { if (typeof(_editamolecule_loaded) == 'undefined') {
_editamolecule_lock = true; _editamolecule_loaded = true;
loadGWTScripts();
waitForGWT(); waitForGWT();
} else { } else {
return; return;
} }
// FIXME: [rocha] jsme and jsmolcalc are not initialized automatically by function loadScript(src) {
// the GWT script loader. To fix this, wait for the scripts to load, initialize var script = document.createElement('script');
// them manually and wait until they are ready script.setAttribute('type', 'text/javascript');
script.setAttribute('src', src);
$('head')[0].appendChild(script);
}
function loadGWTScripts() {
// The names of the script are split to prevent them from
// being rewritten by LMS. GWT uses the filename of the script
// to find the URL path in which the script lives. If the name
// of the file is changed, GWT won't load correctly
var jsmolcalc_src = '/sta' + 'tic/js/capa/jsmolcalc/jsmolcalc.nocache.js';
var jsme_src = '/sta' + 'tic/js/capa/jsme/jsme_export.nocache.js';
// Make sure we don't request the scripts twice
if (typeof (_jsmolcalc) == 'undefined') {
_jsmolcalc = true;
loadScript(jsmolcalc_src);
}
if (typeof (_jsme) == 'undefined') {
_jsme = true;
loadScript(jsme_src);
}
}
function waitForGWT() { function waitForGWT() {
if (typeof(jsmolcalc) != "undefined" && jsmolcalc) // jsme and jsmolcalc are not initialized automatically by the GWT
// script loader. To fix this, wait for the scripts to load,
// initialize them manually and wait until they are ready
if (typeof(jsmolcalc) != 'undefined' && jsmolcalc)
{ {
jsmolcalc.onInjectionDone('jsmolcalc'); jsmolcalc.onInjectionDone('jsmolcalc');
} }
if (typeof(jsme_export) != "undefined" && jsme_export) if (typeof(jsme_export) != 'undefined' && jsme_export)
{ {
// dummy function called by jsme_export // dummy function called by jsme_export
window.jsmeOnLoad = function() {}; window.jsmeOnLoad = function() {};
...@@ -29,7 +59,7 @@ ...@@ -29,7 +59,7 @@
if (typeof(jsmol) != 'undefined' && typeof(JavaScriptApplet) != 'undefined') { if (typeof(jsmol) != 'undefined' && typeof(JavaScriptApplet) != 'undefined') {
// ready, initialize applets // ready, initialize applets
initializeApplets(); initializeApplets();
_editamolecule_lock = false; // release lock, for reloading _editamolecule_loaded = false;
} else { } else {
setTimeout(waitForGWT, timeout); setTimeout(waitForGWT, timeout);
} }
...@@ -59,7 +89,6 @@ ...@@ -59,7 +89,6 @@
var reset_button = parent.find('button.reset'); var reset_button = parent.find('button.reset');
// Add div for error messages // Add div for error messages
$('<br/> <br/> <div class="errormsgs" style="padding: 5px 5px 5px 5px;\ $('<br/> <br/> <div class="errormsgs" style="padding: 5px 5px 5px 5px;\
visibility:hidden; background-color:#FA6666; height:60px;\ visibility:hidden; background-color:#FA6666; height:60px;\
width:400px;"> </div>').appendTo(parent); width:400px;"> </div>').appendTo(parent);
...@@ -93,16 +122,14 @@ ...@@ -93,16 +122,14 @@
function requestAppletData(element, applet, input_field) { function requestAppletData(element, applet, input_field) {
var molFile = $(element).data('molfile-src'); var molFile = $(element).data('molfile-src');
jQuery.ajax({ jQuery.ajax({
url: molFile, url: molFile,
dataType: "text", dataType: "text",
success: function(data) { success: function(data) {
//console.log("Done.");
loadAppletData(applet, data, input_field); loadAppletData(applet, data, input_field);
}, },
error: function() { error: function() {
console.error("Cannot load mol data."); console.error("Cannot load mol data from: " + molFile);
} }
}); });
} }
...@@ -118,12 +145,9 @@ ...@@ -118,12 +145,9 @@
var jme = applet.jmeFile(); var jme = applet.jmeFile();
var info = formatInfo(jsmol.API.getInfo(mol, smiles, jme).toString(), var info = formatInfo(jsmol.API.getInfo(mol, smiles, jme).toString(),
input_field, element); input_field, element);
var value = { mol: mol, info: info }; var value = { mol: mol, info: info };
//console.log("Molecule info:");
//console.log(info);
input_field.val(JSON.stringify(value)); input_field.val(JSON.stringify(value));
return value; return value;
...@@ -131,10 +155,8 @@ ...@@ -131,10 +155,8 @@
function formatInfo(info, input_field, element) { function formatInfo(info, input_field, element) {
var results = []; var results = [];
//console.log("element parent is"+$(element).parent())
var errordiv = $(element).parent().find('.errormsgs')[0]; var errordiv = $(element).parent().find('.errormsgs')[0];
//console.log(errordiv);
if (!errordiv) { if (!errordiv) {
// This is a bit hackish, but works. // This is a bit hackish, but works.
// There are situations where formatInfo is called but no div yet exists // There are situations where formatInfo is called but no div yet exists
...@@ -143,7 +165,7 @@ ...@@ -143,7 +165,7 @@
//console.log("There is no errordiv loaded yet. trying again soon"); //console.log("There is no errordiv loaded yet. trying again soon");
return [] return []
} }
if (info.search("It is not possible") == -1) { if (info.search("It is not possible") == -1) {
errordiv.innerHTML = ''; errordiv.innerHTML = '';
errordiv.style.visibility = 'hidden'; errordiv.style.visibility = 'hidden';
...@@ -153,12 +175,10 @@ ...@@ -153,12 +175,10 @@
}); });
} }
else { else {
//console.log("err");
// remove Brian's html tags // remove Brian's html tags
var tags = /<((\/)?\w{1,7})>/g; var tags = /<((\/)?\w{1,7})>/g;
var errmsg = info.replace(tags, ' '); var errmsg = info.replace(tags, ' ');
//console.log(errmsg);
errordiv.innerHTML = errmsg; errordiv.innerHTML = errmsg;
errordiv.style.visibility = 'visible'; errordiv.style.visibility = 'visible';
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment