Commit f68f023e by Carlos Andrés Rocha Committed by Victor Shnayder

WIP integration of edit-a-molecule

parent c5b86f3e
...@@ -850,11 +850,16 @@ class EditAMoleculeInput(InputTypeBase): ...@@ -850,11 +850,16 @@ class EditAMoleculeInput(InputTypeBase):
""" """
Can set size of text field. Can set size of text field.
""" """
return [Attribute('file'),] return [Attribute('file'),
Attribute('missing', None)]
def _extra_context(self): def _extra_context(self):
""" """
""" """
return {'applet_loader': '/static/js/capa/edit_a_molecule.js',} context = {
'applet_loader': '/static/js/capa/edit-a-molecule.js',
}
return context
registry.register(EditAMoleculeInput) registry.register(EditAMoleculeInput)
...@@ -875,7 +875,7 @@ def sympy_check2(): ...@@ -875,7 +875,7 @@ def sympy_check2():
allowed_inputfields = ['textline', 'textbox', 'crystallography', allowed_inputfields = ['textline', 'textbox', 'crystallography',
'chemicalequationinput', 'vsepr_input', 'chemicalequationinput', 'vsepr_input',
'drag_and_drop_input'] 'drag_and_drop_input', 'editamoleculeinput']
def setup_response(self): def setup_response(self):
xml = self.xml xml = self.xml
......
...@@ -38,3 +38,4 @@ ...@@ -38,3 +38,4 @@
</div> </div>
% endif % endif
</section> </section>
2
<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="${applet_loader}"/> <div class="script_placeholder" data-src="${applet_loader}"/>
% if status == 'unsubmitted': % if status == 'unsubmitted':
...@@ -11,7 +12,14 @@ ...@@ -11,7 +12,14 @@
<div class="incorrect" id="status_${id}"> <div class="incorrect" id="status_${id}">
% endif % endif
<input type="text" name="input_${id}" id="input_${id}" value="${value|h}"/> <!-- <object type="application/x-java-applet" id="applet_${id}" class="applet" height="400" width="500"> -->
<!-- <param name="archive" value="/static/applets/capa/edit-a-molecule.jar" /> -->
<!-- <param name="code" value="JME.class" /> -->
<!-- <param name="file" value="${file}" /> -->
<!-- Applet failed to run. No Java plug-in was found. -->
<!-- </object> -->
<input type="hidden" name="input_${id}" id="input_${id}" value="${value|h}"/>
<p class="status"> <p class="status">
% if status == 'unsubmitted': % if status == 'unsubmitted':
...@@ -25,12 +33,8 @@ ...@@ -25,12 +33,8 @@
% endif % endif
</p> </p>
<div id="input_${id}_preview" class="equation">
</div>
<p id="answer_${id}" class="answer"></p> <p id="answer_${id}" class="answer"></p>
% if status in ['unsubmitted', 'correct', 'incorrect', 'incomplete']:
% if status in ['unsubmitted', 'correct', 'incorrect', 'incomplete']: </div>
</div>
% endif % endif
</section> </section>
$(document).ready(function(){ (function () {
var applet = $("#JME")[0];
var template = _.template($("#task-template").text());
var timeout = 1000; var timeout = 1000;
function waitForApplet() { function initializeApplet(applet) {
console.log("Initializing " + applet);
waitForApplet(applet);
}
function waitForApplet(applet) {
if (applet.isActive && applet.isActive()) { if (applet.isActive && applet.isActive()) {
console.log("Applet is ready."); console.log("Applet is ready.");
loadInitialData(); requestAppletData(applet);
} else if (timeout > 30 * 1000) { } else if (timeout > 30 * 1000) {
console.error("Applet did not load on time."); console.error("Applet did not load on time.");
} else { } else {
console.log("Waiting for applet..."); console.log("Waiting for applet...");
setTimeout(waitForApplet, timeout); setTimeout(function() { waitForApplet(applet); }, timeout);
} }
} }
function loadInitialData() { function requestAppletData(applet) {
var file = $(applet).find('param[name=file]').attr('value');
console.log("Getting file url...");
console.log(file);
console.log("Loading mol data..."); console.log("Loading mol data...");
jQuery.ajax({ jQuery.ajax({
url: "dopamine.mol", url: file,
dataType: "text", dataType: "text",
success: function(data) { success: function(data) {
console.log("Done."); console.log("Done.");
setup(data); loadAppletData(applet, data);
}, },
error: function() { error: function() {
console.error("Cannot load mol data."); console.error("Cannot load mol data.");
...@@ -30,44 +38,18 @@ $(document).ready(function(){ ...@@ -30,44 +38,18 @@ $(document).ready(function(){
}); });
} }
function setup(data) { function loadAppletData(applet, data) {
applet.readMolFile(data); applet.readMolFile(data);
updateAppletInfo(applet);
setupTasks();
$("#update").click(updateInfo);
updateInfo();
}
function setupTasks() {
console.log("Getting initial tasks...");
var tasks = getTasks();
jQuery.each(tasks, function(index, task) {
var value = task.toString();
var fragment = $(template({task:value}));
$("#tasks").append(fragment);
fragment.find("button").click(function() {
checkTask(task, index);
});
});
console.log("Done.");
} }
function updateInfo() { function updateAppletInfo(applet) {
var info = getInfo(); var info = getAppletInfo(applet);
$("#properties").html(info.toString()); console.log(info.toString());
return info; return info;
} }
function checkTask(task, index) { function getAppletInfo(applet) {
var info = updateInfo();
var value = task.check(info);
$("#tasks li span.result").eq(index).html(value);
}
function getInfo() {
var mol = applet.molFile(); var mol = applet.molFile();
var smiles = applet.smiles(); var smiles = applet.smiles();
var jme = applet.jmeFile(); var jme = applet.jmeFile();
...@@ -75,13 +57,22 @@ $(document).ready(function(){ ...@@ -75,13 +57,22 @@ $(document).ready(function(){
return jsmol.API.getInfo(mol, smiles, jme); return jsmol.API.getInfo(mol, smiles, jme);
} }
function getTasks() { console.log('EDIT A MOLECULE');
var mol = applet.molFile();
var smiles = applet.smiles();
var jme = applet.jmeFile();
return jsmol.API.getTasks(mol, smiles, jme); // FIXME: [rocha] This should be called automatically by the GWT
} // script loader, but for some reason it is not.
jsmolcalc.onInjectionDone('jsmolcalc');
// FIXME: [rocha] This is a hack to capture the click on the check
// button and update the hidden field with the applet values
var check = $('.editamoleculeinput').parents('.problem').find('input.check');
check.on('click', function() {console.log("CLICK");});
// TODO: [rocha] add function to update hidden field
// TODO: [rocha] load state from hidden field if available
// initialize applet
var applets = $('.editamoleculeinput object');
applets.each(function(i, el) { initializeApplet(el); });
waitForApplet(); }).call(this);
});
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