File:  [LON-CAPA] / loncom / html / res / adm / pages / reactionresponse / reaction_editor.html
Revision 1.11: download - view: text, annotated - select for diffs
Mon Nov 20 20:33:30 2006 UTC (17 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: version_2_8_0, version_2_7_X, version_2_7_99_1, version_2_7_99_0, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_X, version_2_5_99_1, version_2_5_99_0, version_2_5_2, version_2_5_1, version_2_5_0, version_2_4_X, version_2_4_99_0, version_2_4_2, version_2_4_1, version_2_4_0, version_2_3_X, version_2_3_99_0, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_99_1, version_2_2_99_0, HEAD
- BUG 4808 (patch by Guy Ashkenazi)

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

<!--
 $Id: reaction_editor.html,v 1.11 2006/11/20 20:33:30 albertel 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">
var rightarrow;
var id;
var reaction;
var field
</script>
<!-- parsed by LON-CAPA to get the correct symbol for the arrow -->
<script type="text/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() {
  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';
}

function getCookie(document,name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


</script>
</head>

<body bgcolor="#ffffff" onLoad = "javascript:setup();">
<display>
return '<script type="text/javascript">
          var rightarrow=\''.&xmlparse('<m>$\rightarrow$</m>').'\';
	  var reaction=\''.&EXT('query.reaction').'\';
	  var field=\''.&EXT('query.field').'\';
	  var id=\''.&EXT('query.id').'\';
        </script>';
</display>

<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>