File:  [LON-CAPA] / loncom / html / res / adm / pages / reactionresponse / reaction_editor.html
Revision 1.18: download - view: text, annotated - select for diffs
Fri Dec 24 15:28:10 2021 UTC (2 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_5_msu, version_2_11_5, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, HEAD
- Reverse rev. 1.17

<!-- Chemical reaction editor developed by Guy Ashkenazi, guy@fh.huji.ac.il-->

<!--
 $Id: reaction_editor.html,v 1.18 2021/12/24 15:28:10 raeburn Exp $

 Copyright Michigan State University Board of Trustees

 This file is part of the LearningOnline Network with CAPA (LON-CAPA).

 LON-CAPA is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 LON-CAPA is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with LON-CAPA; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 /home/httpd/html/adm/gpl.txt

 http://www.lon-capa.org/
-->

<html>
<head>
<script type="text/javascript" language="Javascript">
var rightarrow = '&rarr;';
var id;
var reaction;
var field
</script>
<!-- parsed by LON-CAPA to get the correct symbol for the arrow -->
<script type="text/javascript" language="Javascript">

var level;
var reactants;
var products;


function parse_reaction(string) {
  var reaction_array = string.split('->');
  var i;
  reactants = new Array(0);
  products = new Array(0);

  if (reaction_array.length > 0) 
    reactants = reaction_array[0].split(' +');
  if (reaction_array.length > 1) 
    products = reaction_array[1].split(' +');
}

function to_capa(string) {
  var reaction = "";
  var i;

  parse_reaction(string);
 
  for (i = 0; i < reactants.length; i++) 
    reactants[i] = capa_component(reactants[i]);
  for (i = 0; i < products.length; i++) 
    products[i] = capa_component(products[i]);
 
//   reactants.sort();
//   products.sort();

  for (i = 0; i < reactants.length-1; i++) {
    reaction += reactants[i];
    reaction += " + ";
  }
  if (i < reactants.length)
    reaction += reactants[i];
  if (products.length > 0) {
    reaction += " -> ";
    for (i = 0; i < products.length-1; i++) {
      reaction += products[i];
      reaction += " + ";
    }
    if (i < products.length)
      reaction += products[i];
  }

  return reaction;
}

function capa_component(string) {
  var reactant = "";
  var i = 0;
  level = 0;

  for (;string.substring(i,i+1) == ' ';i++)
    ;
  for (;isDigit(string.substring(i,i+1));i++)
    reactant += string.substring(i,i+1);
  for (;i < string.length;i++)
    reactant +=  capa_char(string.substring(i,i+1));

  return reactant;
}

function capa_char(chr) {
  if (level == 0) { // baseline
    if (chr == '^') 
      level = 1;
    if (chr == ' ')
      return "";
    return chr;
  }
  if (level == 1) { // superscript
    if (isDigit(chr))
      return chr;
    level = 0;
    return chr;
  }
}

function to_html(string) {
  var reaction = "";
  var i;

  parse_reaction(string);
  for (i = 0; i < reactants.length-1; i++) {
    reaction += html_component(reactants[i]);
    reaction += " + ";
  }
  reaction += html_component(reactants[i]);

  if (products.length > 0) {
    reaction += " "+rightarrow+" ";
    for (i = 0; i < products.length-1; i++) {
      reaction += html_component(products[i]);
      reaction += " + ";
    }
    reaction += html_component(products[i]);
  }

  return reaction;
}

function html_component(string) {
  var reactant = "";
  var i = 0;
  level = -1;

  var hydrate = string.split('.');
  if (hydrate.length > 1) 
    return html_component(hydrate[0]) + "&middot;" + html_component(hydrate[1]);
      
  for (;i < string.length;i++)
    reactant +=  html_char(string.substring(i,i+1));

  return reactant;
}

function html_char(chr) {
  if (level == -1) { // stoichiometric coefficient
    if (isDigit(chr) || chr == '/')
      return chr;
    if (chr == ' ')
      return "";
    else
      level = 0;
  }
  if (level == 0) { // baseline
    if (isDigit(chr))
      return chr.sub();
    if (chr == '^') {
      level = 1;
      return "";
    }
    if (chr == '+') // baseline or superscript?
      return "?";
    if (chr == ' ')
      return "";
    if (chr == '(' || chr == '|') // coefficient
      level = -1;
    return chr;
  }
  if (level == 1) { // superscript
    if (isDigit(chr))
      return chr.sup();
    if (chr == '+') {
      level = 0;
      return chr.sup();
    }
    if (chr == '-') {
      level = 0;
      return "<sup>&minus;</sup>";
    } 
    if (chr == ' ') {
      level = 0;
      return "";
    }
    level = 0;
    return chr;
  }
}

function isDigit(string) {
  if (string >= '0' && string <='9')
    return 1;
  else
    return 0;
}

function openHelpWindow() {
  window.open("reaction_help.html","","scrollbars=yes,resizable=yes,width=550,height=600")
}

function submitReaction() {
  reaction = to_capa(document.form.text.value);
  if (reaction == "") {
    alert("Nothing to submit");
  }
  else {
    name = field;
    i = 0;
    while (parent.opener.document.lonhomework.elements[i].name != name) 
      i++;
    parent.opener.document.lonhomework.elements[i].value = reaction;
//    parent.opener.document.lonhomework.submit();
    parent.window.close();
  }
}

function setup() {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
      var pair = vars[i].split("=");
      if (pair[0] == 'reaction' ) {
          reaction = decodeURIComponent(pair[1]);
      }
      if (pair[0] == 'field') {
          field = decodeURIComponent(pair[1]);
      }
      if (pair[0] == 'id') {
          id = decodeURIComponent(pair[1]);
      }
  }
  document.form.text.value=reaction;
  parent.viewer.document.writeln('<center><br />'+to_html(document.form.text.value)+'</center>');
  parent.viewer.document.close();
  document.form.submit.value='Insert Answer';
  parent.document.title='LON-CAPA - Reaction Editor';
}

</script>
</head>

<body bgcolor="#ffffff" onLoad = "javascript:setup();">
<center>
<hr />
<form name="form">
<input type="text" size="50" name="text" />
<input type="button" value="Check" onclick = "parent.viewer.document.writeln('<center><br />'+to_html(document.form.text.value)+'</center>');parent.viewer.document.close()" /><br />
<br />
<input type="button" name="submit" value="Insert reaction for part #?" onclick = "submitReaction();" />
<br />
<input type="button" value="  Close  " onclick = "parent.window.close()" />
&nbsp;&nbsp;
<input type="button" value="  Help  " onclick = "openHelpWindow()" />
</form>
</center>
</body>
</html>

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>