Skip to content

Commit

Permalink
also render viewers' molfiles via hidden input fields
Browse files Browse the repository at this point in the history
  • Loading branch information
flange-ipb committed Mar 22, 2021
1 parent 0064f76 commit 4bf54af
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
}

private void encodeViewer(FacesContext context, ResponseWriter writer, MolPluginCore plugin) throws IOException {
String divId = plugin.getClientId() + "_MarvinJSViewer";
String clientId = plugin.getClientId();
String hiddenInputId = clientId + "_Input";
String divId = clientId + "_MarvinJSViewer";

encodeViewerHTML(writer, plugin, divId);
encodeViewerJS(context, writer, plugin, divId);
encodeViewerHTML(writer, plugin, divId, hiddenInputId);
encodeViewerJS(context, writer, plugin, divId, hiddenInputId);
}

/**
Expand All @@ -97,14 +99,23 @@ private void encodeViewer(FacesContext context, ResponseWriter writer, MolPlugin
*
* @param writer
* @param plugin
* @param divId DOM id of the embedded <div> element
* @param divId DOM id of the embedded <div> element
* @param hiddenInputId DOM id of the embedded hidden <input> element
*/
private void encodeViewerHTML(ResponseWriter writer, MolPluginCore plugin, String divId) throws IOException {
private void encodeViewerHTML(ResponseWriter writer, MolPluginCore plugin, String divId, String hiddenInputId)
throws IOException {
// inner <div> is used for the plugin's rendering (aka the Javascript target)
writer.startElement("div", plugin);
writer.writeAttribute("id", divId, null);
writer.writeAttribute("style", generateDivStyle(plugin), null);
writer.endElement("div");

// hidden <input> without "name" attribute (prevents submission)
writer.startElement("input", plugin);
writer.writeAttribute("type", "hidden", null);
writer.writeAttribute("id", hiddenInputId, null);
writer.writeAttribute("value", plugin.getValue(), "value");
writer.endElement("input");
}

/**
Expand All @@ -113,10 +124,11 @@ private void encodeViewerHTML(ResponseWriter writer, MolPluginCore plugin, Strin
* @param context
* @param writer
* @param plugin
* @param divId DOM id of the &lt;div&gt; element
* @param divId DOM id of the &lt;div&gt; element
* @param hiddenInputId DOM id of the embedded hidden &lt;input&gt; element
*/
private void encodeViewerJS(FacesContext context, ResponseWriter writer, MolPluginCore plugin, String divId)
throws IOException {
private void encodeViewerJS(FacesContext context, ResponseWriter writer, MolPluginCore plugin, String divId,
String hiddenInputId) throws IOException {
String escapedMolecule = escape((String) plugin.getValue());

writer.startElement("script", plugin);
Expand Down Expand Up @@ -145,8 +157,10 @@ private void encodeViewerJS(FacesContext context, ResponseWriter writer, MolPlug
* embedded in a Promise.
*/
fmt.format("%s.status().then(() => {", loaderJSVar);
fmt.format("return molecularfaces.MarvinJSViewer.newViewer(\"%s\", \"%s\", \"%s\", %d, %d);", divId, escapedMolecule,
installPath, plugin.getHeight(), plugin.getWidth());
fmt.format(
"return molecularfaces.MarvinJSViewer.newViewer(\"%s\", "
+ "document.getElementById(\"%s\").getAttribute(\"value\"), \"%s\", %d, %d);",
divId, hiddenInputId, installPath, plugin.getHeight(), plugin.getWidth());

fmt.close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
}

private void encodeViewer(FacesContext context, ResponseWriter writer, MolPluginCore plugin) throws IOException {
String divId = plugin.getClientId() + "_MolPaintJSViewer";
String clientId = plugin.getClientId();
String hiddenInputId = clientId + "_Input";
String divId = clientId + "_MolPaintJSViewer";

encodeViewerHTML(writer, plugin, divId);
encodeViewerJS(context, writer, plugin, divId);
encodeViewerHTML(writer, plugin, divId, hiddenInputId);
encodeViewerJS(context, writer, plugin, divId, hiddenInputId);
}

/**
Expand All @@ -97,14 +99,23 @@ private void encodeViewer(FacesContext context, ResponseWriter writer, MolPlugin
*
* @param writer
* @param plugin
* @param divId DOM id of the embedded &lt;div&gt; element
* @param divId DOM id of the embedded &lt;div&gt; element
* @param hiddenInputId DOM id of the embedded hidden &lt;input&gt; element
*/
private void encodeViewerHTML(ResponseWriter writer, MolPluginCore plugin, String divId) throws IOException {
private void encodeViewerHTML(ResponseWriter writer, MolPluginCore plugin, String divId, String hiddenInputId)
throws IOException {
// inner <div> is used for the plugin's rendering (aka the Javascript target)
writer.startElement("div", plugin);
writer.writeAttribute("id", divId, null);
writer.writeAttribute("style", generateDivStyle(plugin), null);
writer.endElement("div");

// hidden <input> without "name" attribute (prevents submission)
writer.startElement("input", plugin);
writer.writeAttribute("type", "hidden", null);
writer.writeAttribute("id", hiddenInputId, null);
writer.writeAttribute("value", plugin.getValue(), "value");
writer.endElement("input");
}

/**
Expand All @@ -113,10 +124,11 @@ private void encodeViewerHTML(ResponseWriter writer, MolPluginCore plugin, Strin
* @param context
* @param writer
* @param plugin
* @param divId DOM id of the &lt;div&gt; element
* @param divId DOM id of the &lt;div&gt; element
* @param hiddenInputId DOM id of the embedded hidden &lt;input&gt; element
*/
private void encodeViewerJS(FacesContext context, ResponseWriter writer, MolPluginCore plugin, String divId)
throws IOException {
private void encodeViewerJS(FacesContext context, ResponseWriter writer, MolPluginCore plugin, String divId,
String hiddenInputId) throws IOException {
String escapedMolecule = escape((String) plugin.getValue());

writer.startElement("script", plugin);
Expand All @@ -140,8 +152,10 @@ private void encodeViewerJS(FacesContext context, ResponseWriter writer, MolPlug
* embedded in a Promise.
*/
fmt.format("%s.status().then(() => {", loaderJSVar);
fmt.format("return molecularfaces.MolPaintJSViewer.newViewer(\"%s\", \"%s\", %d, %d);", divId, escapedMolecule,
plugin.getHeight(), plugin.getWidth());
fmt.format(
"return molecularfaces.MolPaintJSViewer.newViewer(\"%s\", "
+ "document.getElementById(\"%s\").getAttribute(\"value\"), %d, %d);",
divId, hiddenInputId, plugin.getHeight(), plugin.getWidth());

fmt.close();

Expand Down Expand Up @@ -232,7 +246,7 @@ private void encodeEditorJS(FacesContext context, ResponseWriter writer, MolPlug
*/
fmt.format(
".then((editor) => editor.addChangeListener("
+ "(mol) => { document.getElementById(\"%s\").setAttribute(\"value\", mol); }));",
+ "(mol) => { document.getElementById(\"%s\").setAttribute(\"value\", mol); }));",
hiddenInputId);

fmt.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
}

private void encodeViewer(ResponseWriter writer, MolPluginCore plugin) throws IOException {
String divId = plugin.getClientId() + "_OpenChemLibJSViewer";
String clientId = plugin.getClientId();
String hiddenInputId = clientId + "_Input";
String divId = clientId + "_OpenChemLibJSViewer";

encodeViewerHTML(writer, plugin, divId);
encodeViewerJS(writer, plugin, divId);
encodeViewerHTML(writer, plugin, divId, hiddenInputId);
encodeViewerJS(writer, plugin, divId, hiddenInputId);
}

/**
Expand All @@ -97,24 +99,35 @@ private void encodeViewer(ResponseWriter writer, MolPluginCore plugin) throws IO
*
* @param writer
* @param plugin
* @param divId DOM id of the embedded &lt;div&gt; element
* @param divId DOM id of the embedded &lt;div&gt; element
* @param hiddenInputId DOM id of the embedded hidden &lt;input&gt; element
*/
private void encodeViewerHTML(ResponseWriter writer, MolPluginCore plugin, String divId) throws IOException {
private void encodeViewerHTML(ResponseWriter writer, MolPluginCore plugin, String divId, String hiddenInputId)
throws IOException {
// inner <div> is used for the plugin's rendering (aka the Javascript target)
writer.startElement("div", plugin);
writer.writeAttribute("id", divId, null);
writer.writeAttribute("style", generateDivStyle(plugin), null);
writer.endElement("div");

// hidden <input> without "name" attribute (prevents submission)
writer.startElement("input", plugin);
writer.writeAttribute("type", "hidden", null);
writer.writeAttribute("id", hiddenInputId, null);
writer.writeAttribute("value", plugin.getValue(), "value");
writer.endElement("input");
}

/**
* Encodes the Javascript part of the plugin viewer into the writer.
*
* @param writer
* @param plugin
* @param divId DOM id of the &lt;div&gt; element
* @param divId DOM id of the &lt;div&gt; element
* @param hiddenInputId DOM id of the embedded hidden &lt;input&gt; element
*/
private void encodeViewerJS(ResponseWriter writer, MolPluginCore plugin, String divId) throws IOException {
private void encodeViewerJS(ResponseWriter writer, MolPluginCore plugin, String divId, String hiddenInputId)
throws IOException {
String escapedMolecule = escape((String) plugin.getValue());

writer.startElement("script", plugin);
Expand All @@ -138,8 +151,10 @@ private void encodeViewerJS(ResponseWriter writer, MolPluginCore plugin, String
* embedded in a Promise.
*/
fmt.format("%s.status().then(() => {", loaderJSVar);
fmt.format("return molecularfaces.OpenChemLibJSViewer.newViewer(\"%s\", \"%s\", %d, %d);", divId,
escapedMolecule, plugin.getHeight(), plugin.getWidth());
fmt.format(
"return molecularfaces.OpenChemLibJSViewer.newViewer(\"%s\", "
+ "document.getElementById(\"%s\").getAttribute(\"value\"), %d, %d);",
divId, hiddenInputId, plugin.getHeight(), plugin.getWidth());

fmt.close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public void testInvalidMolfile() {
// invalid for both relaxed and strict
assertEquals(1, relaxedConstraintViolations.size());
assertEquals(1, strictConstraintViolations.size());
assertEquals("invalid MolFile", relaxedConstraintViolations.iterator().next().getMessage());
assertEquals("invalid MolFile", strictConstraintViolations.iterator().next().getMessage());
assertEquals("invalid MDL Molfile V2000", relaxedConstraintViolations.iterator().next().getMessage());
assertEquals("invalid MDL Molfile V2000", strictConstraintViolations.iterator().next().getMessage());
}

@Test
Expand Down

0 comments on commit 4bf54af

Please sign in to comment.