diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index f0d9cddae5..ca32bb0f28 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -850,11 +850,16 @@ class EditAMoleculeInput(InputTypeBase): """ Can set size of text field. """ - return [Attribute('file'),] + return [Attribute('file'), + Attribute('missing', None)] 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) diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index 5f0e1639b2..b25f429ffa 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -875,7 +875,7 @@ def sympy_check2(): allowed_inputfields = ['textline', 'textbox', 'crystallography', 'chemicalequationinput', 'vsepr_input', - 'drag_and_drop_input'] + 'drag_and_drop_input', 'editamoleculeinput'] def setup_response(self): xml = self.xml diff --git a/common/lib/capa/capa/templates/chemicalequationinput.html b/common/lib/capa/capa/templates/chemicalequationinput.html index becd2a330a..f8d4a9f468 100644 --- a/common/lib/capa/capa/templates/chemicalequationinput.html +++ b/common/lib/capa/capa/templates/chemicalequationinput.html @@ -31,10 +31,11 @@
- +

% if status in ['unsubmitted', 'correct', 'incorrect', 'incomplete']: % endif +2 diff --git a/common/lib/capa/capa/templates/editamolecule.html b/common/lib/capa/capa/templates/editamolecule.html index 75f723f82a..a5ecd36a13 100644 --- a/common/lib/capa/capa/templates/editamolecule.html +++ b/common/lib/capa/capa/templates/editamolecule.html @@ -1,4 +1,5 @@
+
% if status == 'unsubmitted': @@ -11,26 +12,29 @@
% endif - + + + + + + -

- % if status == 'unsubmitted': - unanswered - % elif status == 'correct': - correct - % elif status == 'incorrect': - incorrect - % elif status == 'incomplete': - incomplete - % endif -

+ -
-
- -

+

+ % if status == 'unsubmitted': + unanswered + % elif status == 'correct': + correct + % elif status == 'incorrect': + incorrect + % elif status == 'incomplete': + incomplete + % endif +

-% if status in ['unsubmitted', 'correct', 'incorrect', 'incomplete']: -
+

+ % if status in ['unsubmitted', 'correct', 'incorrect', 'incomplete']: +
% endif
diff --git a/common/static/applets/JME.jar b/common/static/applets/capa/edit-a-molecule.jar similarity index 100% rename from common/static/applets/JME.jar rename to common/static/applets/capa/edit-a-molecule.jar diff --git a/common/static/js/capa/edit-a-molecule.js b/common/static/js/capa/edit-a-molecule.js index 9822c8ee15..2e91f6b91d 100644 --- a/common/static/js/capa/edit-a-molecule.js +++ b/common/static/js/capa/edit-a-molecule.js @@ -1,28 +1,36 @@ -$(document).ready(function(){ - var applet = $("#JME")[0]; - var template = _.template($("#task-template").text()); +(function () { var timeout = 1000; - function waitForApplet() { + function initializeApplet(applet) { + console.log("Initializing " + applet); + waitForApplet(applet); + } + + function waitForApplet(applet) { if (applet.isActive && applet.isActive()) { console.log("Applet is ready."); - loadInitialData(); + requestAppletData(applet); } else if (timeout > 30 * 1000) { console.error("Applet did not load on time."); } else { 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..."); jQuery.ajax({ - url: "dopamine.mol", + url: file, dataType: "text", success: function(data) { console.log("Done."); - setup(data); + loadAppletData(applet, data); }, error: function() { console.error("Cannot load mol data."); @@ -30,44 +38,18 @@ $(document).ready(function(){ }); } - function setup(data) { + function loadAppletData(applet, data) { applet.readMolFile(data); - - setupTasks(); - - $("#update").click(updateInfo); - updateInfo(); + updateAppletInfo(applet); } - 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() { - var info = getInfo(); - $("#properties").html(info.toString()); + function updateAppletInfo(applet) { + var info = getAppletInfo(applet); + console.log(info.toString()); return info; } - function checkTask(task, index) { - var info = updateInfo(); - var value = task.check(info); - $("#tasks li span.result").eq(index).html(value); - } - - function getInfo() { + function getAppletInfo(applet) { var mol = applet.molFile(); var smiles = applet.smiles(); var jme = applet.jmeFile(); @@ -75,13 +57,22 @@ $(document).ready(function(){ return jsmol.API.getInfo(mol, smiles, jme); } - function getTasks() { - var mol = applet.molFile(); - var smiles = applet.smiles(); - var jme = applet.jmeFile(); + console.log('EDIT A MOLECULE'); - 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'); - waitForApplet(); -}); + // 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); }); + +}).call(this);