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