Annotation of loncom/html/res/adm/pages/reactionresponse/reaction_editor.html, revision 1.13
1.1 albertel 1: <!-- Chemical reaction editor developed by Guy Ashkenazi, guy@fh.huji.ac.il-->
2:
3: <!--
1.13 ! raeburn 4: $Id: reaction_editor.html,v 1.12 2009/04/23 17:27:55 bisitz 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>
1.13 ! raeburn 31: <script type="text/javascript" language="Javascript">
1.1 albertel 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.13 ! raeburn 38: <script type="text/javascript" language="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;
1.11 albertel 147: level = -1;
1.1 albertel 148:
1.9 albertel 149: var hydrate = string.split('.');
150: if (hydrate.length > 1)
151: return html_component(hydrate[0]) + "·" + html_component(hydrate[1]);
152:
1.1 albertel 153: for (;i < string.length;i++)
154: reactant += html_char(string.substring(i,i+1));
155:
156: return reactant;
157: }
158:
159: function html_char(chr) {
1.11 albertel 160: if (level == -1) { // stoichiometric coefficient
161: if (isDigit(chr) || chr == '/')
162: return chr;
163: if (chr == ' ')
164: return "";
165: else
166: level = 0;
167: }
1.1 albertel 168: if (level == 0) { // baseline
169: if (isDigit(chr))
170: return chr.sub();
171: if (chr == '^') {
172: level = 1;
173: return "";
174: }
1.11 albertel 175: if (chr == '+') // baseline or superscript?
1.1 albertel 176: return "?";
177: if (chr == ' ')
178: return "";
1.11 albertel 179: if (chr == '(' || chr == '|') // coefficient
180: level = -1;
1.1 albertel 181: return chr;
182: }
183: if (level == 1) { // superscript
184: if (isDigit(chr))
185: return chr.sup();
1.9 albertel 186: if (chr == '+') {
1.1 albertel 187: level = 0;
188: return chr.sup();
189: }
1.9 albertel 190: if (chr == '-') {
191: level = 0;
192: return "<sup>−</sup>";
193: }
1.1 albertel 194: if (chr == ' ') {
195: level = 0;
196: return "";
197: }
198: level = 0;
199: return chr;
200: }
201: }
202:
203: function isDigit(string) {
204: if (string >= '0' && string <='9')
205: return 1;
206: else
207: return 0;
208: }
209:
210: function openHelpWindow() {
211: window.open("reaction_help.html","","scrollbars=yes,resizable=yes,width=550,height=600")
212: }
213:
214: function submitReaction() {
215: reaction = to_capa(document.form.text.value);
216: if (reaction == "") {
217: alert("Nothing to submit");
218: }
219: else {
1.3 albertel 220: name = field;
1.1 albertel 221: i = 0;
222: while (parent.opener.document.lonhomework.elements[i].name != name)
223: i++;
224: parent.opener.document.lonhomework.elements[i].value = reaction;
1.2 albertel 225: // parent.opener.document.lonhomework.submit();
226: parent.window.close();
1.1 albertel 227: }
228: }
229:
1.3 albertel 230: function setup() {
1.4 albertel 231: document.form.text.value=reaction;
1.12 bisitz 232: parent.viewer.document.writeln('<center><br />'+to_html(document.form.text.value)+'</center>');
1.5 albertel 233: parent.viewer.document.close();
1.6 albertel 234: document.form.submit.value='Insert Answer';
235: parent.document.title='LON-CAPA - Reaction Editor';
1.3 albertel 236: }
237:
1.1 albertel 238: function getCookie(document,name) {
239: var dc = document.cookie;
240: var prefix = name + "=";
241: var begin = dc.indexOf("; " + prefix);
242: if (begin == -1) {
243: begin = dc.indexOf(prefix);
244: if (begin != 0) return null;
245: } else
246: begin += 2;
247: var end = document.cookie.indexOf(";", begin);
248: if (end == -1)
249: end = dc.length;
250: return unescape(dc.substring(begin + prefix.length, end));
251: }
252:
253:
254: </script>
255: </head>
256:
1.3 albertel 257: <body bgcolor="#ffffff" onLoad = "javascript:setup();">
1.10 albertel 258: <display>
1.13 ! raeburn 259: return '<script type="text/javascript" language="Javascript">
! 260: var rightarrow=\''.&xmlparse('<m display="tth">$\rightarrow$</m>').'\';
1.10 albertel 261: var reaction=\''.&EXT('query.reaction').'\';
262: var field=\''.&EXT('query.field').'\';
263: var id=\''.&EXT('query.id').'\';
264: </script>';
265: </display>
266:
1.1 albertel 267: <center>
268: <hr />
269: <form name="form">
1.12 bisitz 270: <input type="text" size="50" name="text" />
271: <input type="button" value="Check" onClick = "parent.viewer.document.writeln('<center><br />'+to_html(document.form.text.value)+'</center>');parent.viewer.document.close()" /><br />
1.1 albertel 272: <br />
273: <input type="button" name="submit" value="Insert reaction for part #?" onClick = "submitReaction();" />
274: <br />
275: <input type="button" value=" Close " onClick = "parent.window.close()" />
276:
277: <input type="button" value=" Help " onClick = "openHelpWindow()" />
278: </form>
279: </center>
280: </body>
281: </html>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>