JAL-1859 embedded Jmol applet example updated for Jmol 14.2 javascript
[jalview.git] / examples / jmol / Jmol2.js
1 /* \r
2 \r
3 Jmol2.js   (JSmol version)\r
4 author: Bob Hanson hansonr@stolaf.edu 5/24/2013 12:06:25 PM\r
5 \r
6 Script replacement for legacy Jmol.js that uses JSmol instead.\r
7 Can be used to turn most legacy Jmol.js-based sites to JSmol.\r
8 \r
9 BH 1/16/2014 10:33:46 PM adding serverURL indication, more notes\r
10 BH 1/13/2014 11:14:12 AM incorrect default for missing jmolInitialize() (should be ".")\r
11 BH 1/8/2014 5:56:15 AM simplified instructions; removed option for self.Info\r
12 BH 11/12/2013 6:34:22 AM adds jmolAppletInline()\r
13 BH 9/23/2013 10:07:16 PM adds set of loadInline functions\r
14 BH 9/19/2013 7:09:41 AM  fixed jmolSetTarget() to accept "0" as a suffix; use of undefined --> null\r
15 \r
16 Summary:\r
17 \r
18 You should not have to change any of your HTML pages.\r
19 You are going to replace Jmol.js, wherever that is, with this file.\r
20 You are going to replace all your JAR file with the ones in this distribution.\r
21 You are going to add about 1000 files in the jsmol/j2s directory to your website. \r
22         Don't worry; only a few will be called. But you won't know which ones.\r
23 You will be able to switch from HTML5 to JAVA using ?_USE=SIGNED in the URL\r
24 \r
25 Procedure:\r
26 \r
27 1a) If you want to use HTML5, copy all jsmol/j2s/* files into a j2s subdirectory \r
28                 in the directory that contains Jmol.js and your old Jmol jar files.\r
29 \r
30 1b) If you are not using HTML5, change the "use" parameter below from "HTML5" to "JAVA" and save this file.\r
31                 Copy all the jsmol/java/* files into the directory containing your current JAR files. This adds\r
32                 four JNLP files as well and will replace all your JAR files.\r
33 \r
34 2) Rename your current Jmol.js file Jmol_old.js in case you want to undo this.\r
35 \r
36 3) Concatenate Jmol.min.js if you are not using jQuery (or Jmol.min.nojq.js if you are)\r
37          with this file to form a new file (Jmol.min.js first, then Jmol2.js) and replace your\r
38          current Jmol.js with it. Note that if you are using your own version of jQuery, it\r
39          must be version 1.9 or higher, and note that 2.0 or higher will not work with MSIE\r
40          used locally but accessing remote resources. See http://bugs.jquery.com/ticket/14876\r
41          \r
42 4) Try your page and see how it goes. You may still have some problems, because not all of the \r
43          methods in the original Jmol.js are included here. Let me know if that's the case.\r
44 \r
45 If you wish to change the directories your j2s or JAR files and override the default settings\r
46 (old JAR file location; j2s directory in the directory of those JAR files) and thus override\r
47 your current settings in your HTML files, then you can to that three ways:\r
48 \r
49 a) You can change the parameters below to override what your pages already use by uncommenting\r
50          one or the other of the jarPath and j2sPath definitions. This will override jmolInitialize\r
51          in ALL your HTML files. \r
52          \r
53 b) You can change your jmolInitialization call in an individual HTML file. This sets both \r
54          the JAR path and the j2s path (as [jarPath]/j2s) together.\r
55 \r
56 c) You can add lines to an individual HTML file along the lines of:\r
57 \r
58                 Jmol.Info.jarPath = "../../Jmol"\r
59                 Jmol.Info.j2sPath = "../../JSmol"\r
60 \r
61          or whatever. This will override jmolInitialize in that specific HTML file only. \r
62  \r
63 Note that: \r
64 \r
65  -- FireFox works great. You will be able to read binary files from your local machine\r
66  -- Chrome can only read local files if started with the  --allow-file-access-from-files  flag\r
67                 and even then the files must be ASCII, not binary.\r
68  -- MSIE and Safari cannot work with local pages\r
69 \r
70 */\r
71 \r
72 Jmol.Info = {      \r
73                         // uncomment one or more of these next lines only if you want to override jmolInitialize()\r
74                         //jarPath: "java", \r
75                         //jarFile: "JmolAppletSigned0.jar", \r
76                         //j2sPath: "j2s", \r
77                         use: "HTML5", // could be JAVA or HTML5\r
78                         // the serverURL path is only used to load binary files in Safari, Chrome, and MSIE\r
79                         serverURL: "http://your.server.here/jsmol.php", // required for binary file loading (Spartan, .gz, .map, etc.)\r
80         disableJ2SLoadMonitor: false,\r
81         disableInitialConsole: true\r
82 \r
83 }\r
84 \r
85 ////////// private functions /////////////\r
86 \r
87 var _jmol = {\r
88         appletCount: 0,\r
89         applets: {},\r
90         allowedJmolSize: [25, 2048, 300],   // min, max, default (pixels)\r
91         codebase: ".",\r
92         targetSuffix: 0,\r
93         target: "jmolApplet0",\r
94         buttonCount: 0,\r
95         checkboxCount: 0,\r
96         linkCount: 0,\r
97         cmdCount: 0,\r
98         menuCount: 0,\r
99         radioCount: 0,\r
100         radioGroupCount: 0,\r
101         initialized: false,\r
102         initChecked: false,\r
103         archivePath: "JmolAppletSigned0.jar"\r
104 }\r
105 \r
106 function _jmolApplet(size, inlineModel, script, nameSuffix) {\r
107                 nameSuffix == null && (nameSuffix = _jmol.appletCount);\r
108                 var id = "jmolApplet" + nameSuffix;\r
109                 jmolSetTarget(nameSuffix);\r
110                 ++_jmol.appletCount;\r
111                 script || (script = "select *");\r
112                 inlineModel && (script = 'load DATA "inline"\n' + inlineModel + '\nEND "inline";' + script); \r
113                 var Info = {}\r
114                 for (var i in Jmol.Info)\r
115                         Info[i] = Jmol.Info[i]\r
116                 Info.jarFile || (Info.jarFile = _jmol.archivePath);\r
117                 Info.jarPath || (Info.jarPath = _jmol.codebase);\r
118                 Info.j2sPath || (Info.j2sPath = Info.jarPath + "/j2s");    \r
119                 var sz = _jmolGetAppletSize(size);\r
120                 Info.width || (Info.width = sz[0]);\r
121                 Info.height || (Info.height = sz[1]);  \r
122                 Info.script || (Info.script = script);\r
123                 Info.isSigned == null && (Info.isSigned = (Info.jarFile.indexOf("Signed") >= 0));\r
124                 for (var i in _jmol.params)\r
125                         if(_jmol.params[i]!="")\r
126                                 Info[i] || (Info[i] = _jmol.params[i]);\r
127 //  alert(JSON.stringify(Info).replace(/\,/g,"\n\n\n\n"))\r
128                 return _jmol.applets[id] = Jmol.getApplet(id, Info)\r
129 }\r
130 \r
131 function _jmolGetJarFilename(fileNameOrFlag) {\r
132         _jmol.archivePath =\r
133                 (typeof(fileNameOrFlag) == "string"  ? fileNameOrFlag : (fileNameOrFlag ?  "JmolAppletSigned" : "JmolApplet") + "0.jar");\r
134 }\r
135 \r
136 ////////////////////////////////////////////////////////////////\r
137 // Legacy Scripting API\r
138 ////////////////////////////////////////////////////////////////\r
139 \r
140 function jmolSetParameter(key,value) {\r
141         Jmol.Info[key] = value;\r
142 }\r
143 \r
144 function jmolSetXHTML(id) {\r
145         Jmol.setXHTML(id);\r
146 }\r
147 \r
148 function jmolSetTranslation(TF) {\r
149         // n/a\r
150 }\r
151 \r
152 function jmolInitialize(codebaseDirectory, fileNameOrUseSignedApplet) {\r
153         if (_jmol.initialized)\r
154                 return;\r
155         _jmol.initialized = true;\r
156         if(_jmol.jmoljar) {\r
157                 var f = _jmol.jmoljar;\r
158                 if (f.indexOf("/") >= 0) {\r
159                         alert ("This web page URL is requesting that the applet used be " + f + ". This is a possible security risk, particularly if the applet is signed, because signed applets can read and write files on your local machine or network.")\r
160                         var ok = prompt("Do you want to use applet " + f + "? ","yes or no")\r
161                         if (ok == "yes") {\r
162                                 codebaseDirectory = f.substring(0, f.lastIndexOf("/"));\r
163                                 fileNameOrUseSignedApplet = f.substring(f.lastIndexOf("/") + 1);\r
164                         } else {\r
165         _jmolGetJarFilename(fileNameOrUseSignedApplet);\r
166                                 alert("The web page URL was ignored. Continuing using " + _jmol.archivePath + ' in directory "' + codebaseDirectory + '"');\r
167                         }\r
168                 } else {\r
169                         fileNameOrUseSignedApplet = f;\r
170                 }\r
171         }\r
172         _jmol.codebase = codebaseDirectory;\r
173         _jmolGetJarFilename(fileNameOrUseSignedApplet);\r
174 }\r
175 \r
176 function jmolSetDocument(doc) {\r
177         _jmol.currentDocument = doc;\r
178 }\r
179 \r
180 function jmolSetAppletColor(boxbgcolor, boxfgcolor, progresscolor) {\r
181         Jmol.Info.color = boxbgcolor ? boxbgcolor : "black";\r
182 }\r
183 \r
184 function jmolSetAppletWindow(w) {\r
185         _jmol.appletWindow = w;\r
186 }\r
187 \r
188 function jmolApplet(size, script, nameSuffix) {\r
189         return _jmolApplet(size, null, script, nameSuffix);\r
190 }\r
191 \r
192 function jmolAppletInline(size, inlineModel, script, nameSuffix) {\r
193         return _jmolApplet(size, inlineModel, script, nameSuffix);\r
194 }\r
195 \r
196 \r
197 \r
198 ////////////////////////////////////////////////////////////////\r
199 // Basic controls\r
200 ////////////////////////////////////////////////////////////////\r
201 \r
202 function jmolButton(script, label, id, title) {\r
203         return Jmol.jmolButton(_jmol.target, script, label, id, title);\r
204 }\r
205 \r
206 function jmolCheckbox(scriptWhenChecked, scriptWhenUnchecked, labelHtml, isChecked, id, title) {\r
207         return Jmol.jmolCheckbox(_jmol.target, scriptWhenChecked, scriptWhenUnchecked, labelHtml, isChecked, id, title)\r
208 }\r
209 \r
210 function jmolRadioGroup(arrayOfRadioButtons, separatorHtml, groupName, id, title) {\r
211         return Jmol.jmolRadioGroup(_jmol.target, arrayOfRadioButtons, separatorHtml, groupName, id, title)\r
212 }\r
213 \r
214 \r
215 function jmolRadio(script, labelHtml, isChecked, separatorHtml, groupName, id, title) {\r
216         return  Jmol.jmolRadio(_jmol.target, script, labelHtml, isChecked, separatorHtml, groupName, id, title)\r
217 }\r
218 \r
219 function jmolLink(script, label, id, title) {\r
220         return Jmol.jmolLink(_jmol.target, script, label, id, title)\r
221 }\r
222 \r
223 function jmolCommandInput(label, size, id, title) {\r
224         return Jmol.jmolCommandInput(_jmol.target, label, size, id, title);\r
225 }\r
226 \r
227 function jmolMenu(arrayOfMenuItems, size, id, title) {\r
228         return Jmol.jmolMenu(_jmol.target, arrayOfMenuItems, size, id, title);\r
229 }\r
230 \r
231 function jmolHtml(html) {\r
232         return Jmol._documentWrite(html);\r
233 }\r
234 \r
235 function jmolBr() {\r
236         return Jmol._documentWrite("<br />");\r
237 }\r
238 \r
239 ////////////////////////////////////////////////////////////////\r
240 // advanced scripting functions\r
241 ////////////////////////////////////////////////////////////////\r
242 \r
243 function jmolDebugAlert(enableAlerts) {\r
244         // n/a\r
245 }\r
246 \r
247 \r
248 function jmolLoadInline(model, targetSuffix) {\r
249         return jmolLoadInlineScript(model, null, targetSuffix, false)\r
250 }\r
251 \r
252 function jmolLoadInlineArray(ModelArray, script, targetSuffix) {\r
253         return jmolLoadInlineScript(ModelArray.join("\n"), script, targetSuffix, false)\r
254 }\r
255 \r
256 function jmolAppendInlineArray(ModelArray, script, targetSuffix) {\r
257         return jmolLoadInlineScript(ModelArray.join("\n"), script, targetSuffix, true)\r
258 }\r
259 \r
260 function jmolAppendInlineScript(model, script, targetSuffix) {\r
261         return jmolLoadInlineScript(model, script, targetSuffix, true)\r
262 }\r
263 \r
264 function jmolLoadInlineScript(model, script, targetSuffix, isAppend) {\r
265         Jmol.script(jmolFindTarget(targetSuffix),\r
266                  "load " \r
267                  + (isAppend ? "APPEND " : "") \r
268                  + "DATA 'mydata'\n" + model.replace(/\"/g,'\\"') + "\nEND 'mydata'\n")\r
269 }\r
270 \r
271 function jmolSetTarget(targetSuffix) {\r
272         targetSuffix == null || (_jmol.targetSuffix = targetSuffix);\r
273         return _jmol.target = "jmolApplet" + _jmol.targetSuffix;\r
274 }\r
275 \r
276 function jmolFindTarget(targetSuffix) {\r
277         return _jmol.applets[jmolSetTarget(targetSuffix)];\r
278 }\r
279 \r
280 function jmolScript(script, targetSuffix) {\r
281         Jmol.script(jmolFindTarget(targetSuffix), script)\r
282 }\r
283 \r
284 function jmolCheckBrowser(action, urlOrMessage, nowOrLater) {\r
285         // unnecessary\r
286 }\r
287 \r
288 ////////////////////////////////////////////////////////////////\r
289 // Cascading Style Sheet Class support\r
290 ////////////////////////////////////////////////////////////////\r
291 \r
292 function jmolSetAppletCssClass(appletCssClass) {\r
293         Jmol.setAppletCss(appletCssClass)\r
294 }\r
295 \r
296 function jmolSetButtonCssClass(s) {\r
297         Jmol.setButtonCss(s)\r
298 }\r
299 \r
300 function jmolSetCheckboxCssClass(s) {\r
301         Jmol.setCheckboxCss(s)\r
302 }\r
303 \r
304 function jmolSetRadioCssClass(s) {\r
305         Jmol.setRadioCss(s)\r
306 }\r
307 \r
308 function jmolSetLinkCssClass(s) {\r
309         Jmol.setLinkCss(s)\r
310 }\r
311 \r
312 function jmolSetMenuCssClass(s) {\r
313         Jmol.setMenuCss(s)\r
314 }\r
315 \r
316 function jmolSetMemoryMb(nMb) {\r
317         // n/a\r
318 }\r
319 \r
320 \r
321 function jmolSetCallback(callbackName,funcName) {\r
322 //if(!self[funcName])alert(funcName + " is not defined yet in jmolSetCallback")\r
323         Jmol.Info[callbackName] = funcName\r
324         //document.title=("jmolSetCallback " + callbackName + "/" + funcName + " must be included in Info definition")\r
325 }\r
326 \r
327 function jmolSetSyncId(n) {\r
328         alert("jmolSetSyncId " + n + " must be included in Info definition")\r
329 }\r
330 \r
331 function jmolSetLogLevel(n) {\r
332         Jmol.script(_jmol.target, "set loglevel " + n)\r
333 }\r
334 \r
335 function _jmolGetAppletSize(size, units) {\r
336         var width, height;\r
337         if ( (typeof size) == "object" && size != null ) {\r
338                 width = size[0]; height = size[1];\r
339         } else {\r
340                 width = height = size;\r
341         }\r
342         return [_jmolFixDim(width, units), _jmolFixDim(height, units)];\r
343 }\r
344 \r
345 function _jmolFixDim(x, units) {\r
346         var sx = "" + x;\r
347         return (sx.length == 0 ? (units ? "" : _jmol.allowedJmolSize[2])\r
348         : sx.indexOf("%") == sx.length-1 ? sx\r
349                 : (x = parseFloat(x)) <= 1 && x > 0 ? x * 100 + "%"\r
350                 : (isNaN(x = Math.floor(x)) ? _jmol.allowedJmolSize[2]\r
351                         : x < _jmol.allowedJmolSize[0] ? _jmol.allowedJmolSize[0]\r
352                                 : x > _jmol.allowedJmolSize[1] ? _jmol.allowedJmolSize[1]\r
353                                 : x) + (units ? units : ""));\r
354 }\r
355 \r
356 //////////user property/status functions/////////\r
357 \r
358 function jmolGetStatus(strStatus,targetSuffix){\r
359         return Jmol.getStatus(jmolFindTarget(targetSuffix), strStatus)\r
360 }\r
361 \r
362 function jmolGetPropertyAsArray(sKey,sValue,targetSuffix) {\r
363         return Jmol.getPropertyAsArray(jmolFindTarget(targetSuffix), sKey, sValue)\r
364 }\r
365 \r
366 function jmolGetPropertyAsString(sKey,sValue,targetSuffix) {\r
367         return Jmol.getPropertyAsString(jmolFindTarget(targetSuffix), sKey, sValue)\r
368 }\r
369 \r
370 function jmolGetPropertyAsJSON(sKey,sValue,targetSuffix) {\r
371         return Jmol.getPropertyAsJSON(jmolFindTarget(targetSuffix), sKey, sValue)\r
372 }\r
373 \r
374 function jmolGetPropertyAsJavaObject(sKey,sValue,targetSuffix) {\r
375         return Jmol.getPropertyAsJavaObject(jmolFindTarget(targetSuffix), sKey, sValue)\r
376 }\r
377 \r
378 ///////// synchronous scripting ////////\r
379 \r
380 function jmolScriptWait(script, targetSuffix) {\r
381         return Jmol.scriptWait(jmolFindTarget(targetSuffix), script)\r
382 }\r
383 \r
384 function jmolScriptWaitOutput(script, targetSuffix) {\r
385         return Jmol.scriptWaitOutput(jmolFindTarget(targetSuffix), script)\r
386 }\r
387 \r
388 function jmolEvaluate(molecularMath, targetSuffix) {\r
389         return Jmol.evaluate(jmolFindTarget(targetSuffix), molecularMath)\r
390 }\r
391 \r
392 function jmolScriptEcho(script, targetSuffix) {\r
393         return Jmol.scriptEcho(jmolFindTarget(targetSuffix), script)\r
394 }\r
395 \r
396 \r
397 function jmolScriptMessage(script, targetSuffix) {\r
398         return Jmol.scriptMessage(jmolFindTarget(targetSuffix), script)\r
399 }\r
400 \r
401 \r
402 function jmolScriptWaitAsArray(script, targetSuffix) {\r
403         return Jmol.scriptWait(jmolFindTarget(targetSuffix), script)\r
404 }\r
405 \r
406 \r
407 \r
408 ////////////   save/restore orientation   /////////////\r
409 \r
410 function jmolSaveOrientation(id, targetSuffix) {\r
411         return Jmol.saveOrientation(jmolFindTarget(targetSuffix), id)\r
412 }\r
413 \r
414 function jmolRestoreOrientation(id, targetSuffix) {\r
415         return Jmol.restoreOrientation(jmolFindTarget(targetSuffix), id)\r
416 }\r
417 \r
418 function jmolRestoreOrientationDelayed(id, delay, targetSuffix) {\r
419         return Jmol.restoreOrientationDelayed(jmolFindTarget(targetSuffix), id, delay)\r
420 }\r
421 \r
422 function jmolResizeApplet(size, targetSuffix) {\r
423         return Jmol.resizeApplet(jmolFindTarget(targetSuffix), size);\r
424 }\r
425 \r
426 \r
427 ////////////  add parameter /////////////\r
428 \r
429 function jmolAppletAddParam(appletCode,name,value){\r
430         alert ("use Info to add a parameter: " + name + "/" + value)\r
431 }\r
432 \r