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

1.1       albertel    1: <!-- Chemical reaction editor developed by Guy Ashkenazi, guy@fh.huji.ac.il-->
                      2: 
                      3: <!--
1.10    ! albertel    4:  $Id: reaction_editor.html,v 1.9 2005/03/29 17:57:52 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;
1.3       albertel   33: var id;
1.4       albertel   34: var reaction;
1.3       albertel   35: var field
1.1       albertel   36: </script>
                     37: <!-- parsed by LON-CAPA to get the correct symbol for the arrow -->
1.10    ! albertel   38: <script type="text/javascript">
1.1       albertel   39: 
                     40: var level;
                     41: var reactants;
                     42: var products;
                     43: 
                     44: 
                     45: function parse_reaction(string) {
                     46:   var reaction_array = string.split('->');
                     47:   var i;
                     48:   reactants = new Array(0);
                     49:   products = new Array(0);
                     50: 
                     51:   if (reaction_array.length > 0) 
                     52:     reactants = reaction_array[0].split(' +');
                     53:   if (reaction_array.length > 1) 
                     54:     products = reaction_array[1].split(' +');
                     55: }
                     56: 
                     57: function to_capa(string) {
                     58:   var reaction = "";
                     59:   var i;
                     60: 
                     61:   parse_reaction(string);
                     62:  
                     63:   for (i = 0; i < reactants.length; i++) 
                     64:     reactants[i] = capa_component(reactants[i]);
                     65:   for (i = 0; i < products.length; i++) 
                     66:     products[i] = capa_component(products[i]);
                     67:  
1.7       albertel   68: //   reactants.sort();
                     69: //   products.sort();
1.1       albertel   70: 
                     71:   for (i = 0; i < reactants.length-1; i++) {
                     72:     reaction += reactants[i];
                     73:     reaction += " + ";
                     74:   }
                     75:   if (i < reactants.length)
                     76:     reaction += reactants[i];
                     77:   if (products.length > 0) {
                     78:     reaction += " -> ";
                     79:     for (i = 0; i < products.length-1; i++) {
                     80:       reaction += products[i];
                     81:       reaction += " + ";
                     82:     }
                     83:     if (i < products.length)
                     84:       reaction += products[i];
                     85:   }
                     86: 
                     87:   return reaction;
                     88: }
                     89: 
                     90: function capa_component(string) {
                     91:   var reactant = "";
                     92:   var i = 0;
                     93:   level = 0;
                     94: 
                     95:   for (;string.substring(i,i+1) == ' ';i++)
                     96:     ;
                     97:   for (;isDigit(string.substring(i,i+1));i++)
                     98:     reactant += string.substring(i,i+1);
                     99:   for (;i < string.length;i++)
                    100:     reactant +=  capa_char(string.substring(i,i+1));
                    101: 
                    102:   return reactant;
                    103: }
                    104: 
                    105: function capa_char(chr) {
                    106:   if (level == 0) { // baseline
                    107:     if (chr == '^') 
                    108:       level = 1;
                    109:     if (chr == ' ')
                    110:       return "";
                    111:     return chr;
                    112:   }
                    113:   if (level == 1) { // superscript
                    114:     if (isDigit(chr))
                    115:       return chr;
                    116:     level = 0;
                    117:     return chr;
                    118:   }
                    119: }
                    120: 
                    121: function to_html(string) {
                    122:   var reaction = "";
                    123:   var i;
                    124: 
                    125:   parse_reaction(string);
                    126:   for (i = 0; i < reactants.length-1; i++) {
                    127:     reaction += html_component(reactants[i]);
                    128:     reaction += " + ";
                    129:   }
1.9       albertel  130:   reaction += html_component(reactants[i]);
1.1       albertel  131: 
                    132:   if (products.length > 0) {
1.8       albertel  133:     reaction += " "+rightarrow+" ";
1.1       albertel  134:     for (i = 0; i < products.length-1; i++) {
                    135:       reaction += html_component(products[i]);
                    136:       reaction += " + ";
                    137:     }
1.9       albertel  138:     reaction += html_component(products[i]);
1.1       albertel  139:   }
                    140: 
                    141:   return reaction;
                    142: }
                    143: 
                    144: function html_component(string) {
                    145:   var reactant = "";
                    146:   var i = 0;
                    147:   level = 0;
                    148: 
1.9       albertel  149:   var hydrate = string.split('.');
                    150:   if (hydrate.length > 1) 
                    151:     return html_component(hydrate[0]) + "&middot;" + html_component(hydrate[1]);
                    152:       
1.1       albertel  153:   for (;string.substring(i,i+1) == ' ';i++)
                    154:     ;
1.9       albertel  155:   for (;isDigit(string.substring(i,i+1)) || string.substring(i,i+1) == '/'; i++) // stoichiometric coefficient
1.1       albertel  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();
1.9       albertel  180:     if (chr == '+') {
1.1       albertel  181:       level = 0;
                    182:       return chr.sup();
                    183:     }
1.9       albertel  184:     if (chr == '-') {
                    185:       level = 0;
                    186:       return "<sup>&minus;</sup>";
                    187:     } 
1.1       albertel  188:     if (chr == ' ') {
                    189:       level = 0;
                    190:       return "";
                    191:     }
                    192:     level = 0;
                    193:     return chr;
                    194:   }
                    195: }
                    196: 
                    197: function isDigit(string) {
                    198:   if (string >= '0' && string <='9')
                    199:     return 1;
                    200:   else
                    201:     return 0;
                    202: }
                    203: 
                    204: function openHelpWindow() {
                    205:   window.open("reaction_help.html","","scrollbars=yes,resizable=yes,width=550,height=600")
                    206: }
                    207: 
                    208: function submitReaction() {
                    209:   reaction = to_capa(document.form.text.value);
                    210:   if (reaction == "") {
                    211:     alert("Nothing to submit");
                    212:   }
                    213:   else {
1.3       albertel  214:     name = field;
1.1       albertel  215:     i = 0;
                    216:     while (parent.opener.document.lonhomework.elements[i].name != name) 
                    217:       i++;
                    218:     parent.opener.document.lonhomework.elements[i].value = reaction;
1.2       albertel  219: //    parent.opener.document.lonhomework.submit();
                    220:     parent.window.close();
1.1       albertel  221:   }
                    222: }
                    223: 
1.3       albertel  224: function setup() {
1.4       albertel  225:   document.form.text.value=reaction;
1.5       albertel  226:   parent.viewer.document.writeln('<center><br>'+to_html(document.form.text.value)+'</center>');
                    227:   parent.viewer.document.close();
1.6       albertel  228:   document.form.submit.value='Insert Answer';
                    229:   parent.document.title='LON-CAPA - Reaction Editor';
1.3       albertel  230: }
                    231: 
1.1       albertel  232: function getCookie(document,name) {
                    233:   var dc = document.cookie;
                    234:   var prefix = name + "=";
                    235:   var begin = dc.indexOf("; " + prefix);
                    236:   if (begin == -1) {
                    237:     begin = dc.indexOf(prefix);
                    238:     if (begin != 0) return null;
                    239:   } else
                    240:     begin += 2;
                    241:   var end = document.cookie.indexOf(";", begin);
                    242:   if (end == -1)
                    243:     end = dc.length;
                    244:   return unescape(dc.substring(begin + prefix.length, end));
                    245: }
                    246: 
                    247: 
                    248: </script>
                    249: </head>
                    250: 
1.3       albertel  251: <body bgcolor="#ffffff" onLoad = "javascript:setup();">
1.10    ! albertel  252: <display>
        !           253: return '<script type="text/javascript">
        !           254:           var rightarrow=\''.&xmlparse('<m>$\rightarrow$</m>').'\';
        !           255: 	  var reaction=\''.&EXT('query.reaction').'\';
        !           256: 	  var field=\''.&EXT('query.field').'\';
        !           257: 	  var id=\''.&EXT('query.id').'\';
        !           258:         </script>';
        !           259: </display>
        !           260: 
1.1       albertel  261: <center>
                    262: <hr />
                    263: <form name="form">
                    264: <input type="text" size=50 name="text" />
                    265: <input type="button" value="Check" onClick = "parent.viewer.document.writeln('<center><br>'+to_html(document.form.text.value)+'</center>');parent.viewer.document.close()" /><br />
                    266: <br />
                    267: <input type="button" name="submit" value="Insert reaction for part #?" onClick = "submitReaction();" />
                    268: <br />
                    269: <input type="button" value="  Close  " onClick = "parent.window.close()" />
                    270: &nbsp;&nbsp;
                    271: <input type="button" value="  Help  " onClick = "openHelpWindow()" />
                    272: </form>
                    273: </center>
                    274: </body>
                    275: </html>

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