Annotation of loncom/html/res/adm/pages/reactionresponse/reaction_editor.html, revision 1.2

1.1       albertel    1: <!-- Chemical reaction editor developed by Guy Ashkenazi, guy@fh.huji.ac.il-->
                      2: 
                      3: <!--
1.2     ! albertel    4:  $Id: reaction_editor.html,v 1.1 2003/06/30 20:39:59 albertel Exp $
1.1       albertel    5: 
                      6:  Copyright Michigan State University Board of Trustees
                      7: 
                      8:  This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: 
                     10:  LON-CAPA is free software; you can redistribute it and/or modify
                     11:  it under the terms of the GNU General Public License as published by
                     12:  the Free Software Foundation; either version 2 of the License, or
                     13:  (at your option) any later version.
                     14: 
                     15:  LON-CAPA is distributed in the hope that it will be useful,
                     16:  but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17:  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18:  GNU General Public License for more details.
                     19: 
                     20:  You should have received a copy of the GNU General Public License
                     21:  along with LON-CAPA; if not, write to the Free Software
                     22:  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: 
                     24:  /home/httpd/html/adm/gpl.txt
                     25: 
                     26:  http://www.lon-capa.org/
                     27: -->
                     28: 
                     29: <html>
                     30: <head>
                     31: <script type="text/javascript">
                     32: var rightarrow;
                     33: </script>
                     34: <!-- parsed by LON-CAPA to get the correct symbol for the arrow -->
                     35: <display>
                     36: return '<script type="text/javascript">
                     37:           var rightarrow=\''.&xmlparse('<m>$\rightarrow$</m>').'\';
                     38:         </script>';
                     39: </display>
                     40: <script language="JavaScript">
                     41: 
                     42: var level;
                     43: var reactants;
                     44: var products;
                     45: 
                     46: 
                     47: function parse_reaction(string) {
                     48:   var reaction_array = string.split('->');
                     49:   var i;
                     50:   reactants = new Array(0);
                     51:   products = new Array(0);
                     52: 
                     53:   if (reaction_array.length > 0) 
                     54:     reactants = reaction_array[0].split(' +');
                     55:   if (reaction_array.length > 1) 
                     56:     products = reaction_array[1].split(' +');
                     57: }
                     58: 
                     59: function to_capa(string) {
                     60:   var reaction = "";
                     61:   var i;
                     62: 
                     63:   parse_reaction(string);
                     64:  
                     65:   for (i = 0; i < reactants.length; i++) 
                     66:     reactants[i] = capa_component(reactants[i]);
                     67:   for (i = 0; i < products.length; i++) 
                     68:     products[i] = capa_component(products[i]);
                     69:  
                     70:   reactants.sort();
                     71:   products.sort();
                     72: 
                     73:   for (i = 0; i < reactants.length-1; i++) {
                     74:     reaction += reactants[i];
                     75:     reaction += " + ";
                     76:   }
                     77:   if (i < reactants.length)
                     78:     reaction += reactants[i];
                     79:   if (products.length > 0) {
                     80:     reaction += " -> ";
                     81:     for (i = 0; i < products.length-1; i++) {
                     82:       reaction += products[i];
                     83:       reaction += " + ";
                     84:     }
                     85:     if (i < products.length)
                     86:       reaction += products[i];
                     87:   }
                     88: 
                     89:   return reaction;
                     90: }
                     91: 
                     92: function capa_component(string) {
                     93:   var reactant = "";
                     94:   var i = 0;
                     95:   level = 0;
                     96: 
                     97:   for (;string.substring(i,i+1) == ' ';i++)
                     98:     ;
                     99:   for (;isDigit(string.substring(i,i+1));i++)
                    100:     reactant += string.substring(i,i+1);
                    101:   for (;i < string.length;i++)
                    102:     reactant +=  capa_char(string.substring(i,i+1));
                    103: 
                    104:   return reactant;
                    105: }
                    106: 
                    107: function capa_char(chr) {
                    108:   if (level == 0) { // baseline
                    109:     if (chr == '^') 
                    110:       level = 1;
                    111:     if (chr == ' ')
                    112:       return "";
                    113:     return chr;
                    114:   }
                    115:   if (level == 1) { // superscript
                    116:     if (isDigit(chr))
                    117:       return chr;
                    118:     level = 0;
                    119:     return chr;
                    120:   }
                    121: }
                    122: 
                    123: function to_html(string) {
                    124:   var reaction = "";
                    125:   var i;
                    126: 
                    127:   parse_reaction(string);
                    128:   for (i = 0; i < reactants.length-1; i++) {
                    129:     reaction += html_component(reactants[i]);
                    130:     reaction += " + ";
                    131:   }
                    132:   if (i < reactants.length)
                    133:     reaction += html_component(reactants[i]);
                    134: 
                    135:   if (products.length > 0) {
                    136:     reaction += rightarrow;
                    137:     for (i = 0; i < products.length-1; i++) {
                    138:       reaction += html_component(products[i]);
                    139:       reaction += " + ";
                    140:     }
                    141:     if (i < products.length)
                    142:       reaction += html_component(products[i]);
                    143:   }
                    144: 
                    145:   return reaction;
                    146: }
                    147: 
                    148: function html_component(string) {
                    149:   var reactant = "";
                    150:   var i = 0;
                    151:   level = 0;
                    152: 
                    153:   for (;string.substring(i,i+1) == ' ';i++)
                    154:     ;
                    155:   for (;isDigit(string.substring(i,i+1));i++)
                    156:     reactant += string.substring(i,i+1);
                    157:   for (;i < string.length;i++)
                    158:     reactant +=  html_char(string.substring(i,i+1));
                    159: 
                    160:   return reactant;
                    161: }
                    162: 
                    163: function html_char(chr) {
                    164:   if (level == 0) { // baseline
                    165:     if (isDigit(chr))
                    166:       return chr.sub();
                    167:     if (chr == '^') {
                    168:       level = 1;
                    169:       return "";
                    170:     }
                    171:     if (chr == '+') // baseline or superscript
                    172:       return "?";
                    173:     if (chr == ' ')
                    174:       return "";
                    175:     return chr;
                    176:   }
                    177:   if (level == 1) { // superscript
                    178:     if (isDigit(chr))
                    179:       return chr.sup();
                    180:     if (chr == '+' || chr == '-') {
                    181:       level = 0;
                    182:       return chr.sup();
                    183:     }
                    184:     if (chr == ' ') {
                    185:       level = 0;
                    186:       return "";
                    187:     }
                    188:     level = 0;
                    189:     return chr;
                    190:   }
                    191: }
                    192: 
                    193: function isDigit(string) {
                    194:   if (string >= '0' && string <='9')
                    195:     return 1;
                    196:   else
                    197:     return 0;
                    198: }
                    199: 
                    200: function openHelpWindow() {
                    201:   window.open("reaction_help.html","","scrollbars=yes,resizable=yes,width=550,height=600")
                    202: }
                    203: 
                    204: function submitReaction() {
                    205:   reaction = to_capa(document.form.text.value);
                    206:   if (reaction == "") {
                    207:     alert("Nothing to submit");
                    208:   }
                    209:   else {
                    210:     name = "HWVAL_" + problem;
                    211:     i = 0;
                    212:     while (parent.opener.document.lonhomework.elements[i].name != name) 
                    213:       i++;
                    214:     parent.opener.document.lonhomework.elements[i].value = reaction;
1.2     ! albertel  215: //    parent.opener.document.lonhomework.submit();
        !           216:     parent.window.close();
1.1       albertel  217:   }
                    218: }
                    219: 
                    220: function getCookie(document,name) {
                    221:   var dc = document.cookie;
                    222:   var prefix = name + "=";
                    223:   var begin = dc.indexOf("; " + prefix);
                    224:   if (begin == -1) {
                    225:     begin = dc.indexOf(prefix);
                    226:     if (begin != 0) return null;
                    227:   } else
                    228:     begin += 2;
                    229:   var end = document.cookie.indexOf(";", begin);
                    230:   if (end == -1)
                    231:     end = dc.length;
                    232:   return unescape(dc.substring(begin + prefix.length, end));
                    233: }
                    234: 
                    235: 
                    236: </script>
                    237: </head>
                    238: 
1.2     ! albertel  239: <body bgcolor="#ffffff" onLoad = "problem = getCookie(parent.opener.document,'problem'); document.form.submit.value='Insert reaction for Part #' + problem; parent.document.title='LON-CAPA#'+problem+' - Reaction Editor';">
1.1       albertel  240: <center>
                    241: <hr />
                    242: <form name="form">
                    243: <input type="text" size=50 name="text" />
                    244: <input type="button" value="Check" onClick = "parent.viewer.document.writeln('<center><br>'+to_html(document.form.text.value)+'</center>');parent.viewer.document.close()" /><br />
                    245: <br />
                    246: <input type="button" name="submit" value="Insert reaction for part #?" onClick = "submitReaction();" />
                    247: <br />
                    248: <input type="button" value="  Close  " onClick = "parent.window.close()" />
                    249: &nbsp;&nbsp;
                    250: <input type="button" value="  Help  " onClick = "openHelpWindow()" />
                    251: </form>
                    252: </center>
                    253: </body>
                    254: </html>

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