JAL-1859 embedded Jmol applet example updated for Jmol 14.2 javascript
[jalview.git] / examples / jmol / Jmol2.js
diff --git a/examples/jmol/Jmol2.js b/examples/jmol/Jmol2.js
new file mode 100644 (file)
index 0000000..6312657
--- /dev/null
@@ -0,0 +1,432 @@
+/* \r
+\r
+Jmol2.js   (JSmol version)\r
+author: Bob Hanson hansonr@stolaf.edu 5/24/2013 12:06:25 PM\r
+\r
+Script replacement for legacy Jmol.js that uses JSmol instead.\r
+Can be used to turn most legacy Jmol.js-based sites to JSmol.\r
+\r
+BH 1/16/2014 10:33:46 PM adding serverURL indication, more notes\r
+BH 1/13/2014 11:14:12 AM incorrect default for missing jmolInitialize() (should be ".")\r
+BH 1/8/2014 5:56:15 AM simplified instructions; removed option for self.Info\r
+BH 11/12/2013 6:34:22 AM adds jmolAppletInline()\r
+BH 9/23/2013 10:07:16 PM adds set of loadInline functions\r
+BH 9/19/2013 7:09:41 AM  fixed jmolSetTarget() to accept "0" as a suffix; use of undefined --> null\r
+\r
+Summary:\r
+\r
+You should not have to change any of your HTML pages.\r
+You are going to replace Jmol.js, wherever that is, with this file.\r
+You are going to replace all your JAR file with the ones in this distribution.\r
+You are going to add about 1000 files in the jsmol/j2s directory to your website. \r
+       Don't worry; only a few will be called. But you won't know which ones.\r
+You will be able to switch from HTML5 to JAVA using ?_USE=SIGNED in the URL\r
+\r
+Procedure:\r
+\r
+1a) If you want to use HTML5, copy all jsmol/j2s/* files into a j2s subdirectory \r
+               in the directory that contains Jmol.js and your old Jmol jar files.\r
+\r
+1b) If you are not using HTML5, change the "use" parameter below from "HTML5" to "JAVA" and save this file.\r
+               Copy all the jsmol/java/* files into the directory containing your current JAR files. This adds\r
+               four JNLP files as well and will replace all your JAR files.\r
+\r
+2) Rename your current Jmol.js file Jmol_old.js in case you want to undo this.\r
+\r
+3) Concatenate Jmol.min.js if you are not using jQuery (or Jmol.min.nojq.js if you are)\r
+        with this file to form a new file (Jmol.min.js first, then Jmol2.js) and replace your\r
+        current Jmol.js with it. Note that if you are using your own version of jQuery, it\r
+        must be version 1.9 or higher, and note that 2.0 or higher will not work with MSIE\r
+        used locally but accessing remote resources. See http://bugs.jquery.com/ticket/14876\r
+        \r
+4) Try your page and see how it goes. You may still have some problems, because not all of the \r
+        methods in the original Jmol.js are included here. Let me know if that's the case.\r
+\r
+If you wish to change the directories your j2s or JAR files and override the default settings\r
+(old JAR file location; j2s directory in the directory of those JAR files) and thus override\r
+your current settings in your HTML files, then you can to that three ways:\r
+\r
+a) You can change the parameters below to override what your pages already use by uncommenting\r
+        one or the other of the jarPath and j2sPath definitions. This will override jmolInitialize\r
+        in ALL your HTML files. \r
+        \r
+b) You can change your jmolInitialization call in an individual HTML file. This sets both \r
+        the JAR path and the j2s path (as [jarPath]/j2s) together.\r
+\r
+c) You can add lines to an individual HTML file along the lines of:\r
+\r
+               Jmol.Info.jarPath = "../../Jmol"\r
+               Jmol.Info.j2sPath = "../../JSmol"\r
+\r
+        or whatever. This will override jmolInitialize in that specific HTML file only. \r
\r
+Note that: \r
+\r
+ -- FireFox works great. You will be able to read binary files from your local machine\r
+ -- Chrome can only read local files if started with the  --allow-file-access-from-files  flag\r
+               and even then the files must be ASCII, not binary.\r
+ -- MSIE and Safari cannot work with local pages\r
+\r
+*/\r
+\r
+Jmol.Info = {      \r
+                       // uncomment one or more of these next lines only if you want to override jmolInitialize()\r
+                       //jarPath: "java", \r
+                       //jarFile: "JmolAppletSigned0.jar", \r
+                       //j2sPath: "j2s", \r
+                       use: "HTML5", // could be JAVA or HTML5\r
+                       // the serverURL path is only used to load binary files in Safari, Chrome, and MSIE\r
+                       serverURL: "http://your.server.here/jsmol.php", // required for binary file loading (Spartan, .gz, .map, etc.)\r
+       disableJ2SLoadMonitor: false,\r
+       disableInitialConsole: true\r
+\r
+}\r
+\r
+////////// private functions /////////////\r
+\r
+var _jmol = {\r
+       appletCount: 0,\r
+       applets: {},\r
+       allowedJmolSize: [25, 2048, 300],   // min, max, default (pixels)\r
+       codebase: ".",\r
+       targetSuffix: 0,\r
+       target: "jmolApplet0",\r
+       buttonCount: 0,\r
+       checkboxCount: 0,\r
+       linkCount: 0,\r
+       cmdCount: 0,\r
+       menuCount: 0,\r
+       radioCount: 0,\r
+       radioGroupCount: 0,\r
+       initialized: false,\r
+       initChecked: false,\r
+       archivePath: "JmolAppletSigned0.jar"\r
+}\r
+\r
+function _jmolApplet(size, inlineModel, script, nameSuffix) {\r
+               nameSuffix == null && (nameSuffix = _jmol.appletCount);\r
+               var id = "jmolApplet" + nameSuffix;\r
+               jmolSetTarget(nameSuffix);\r
+               ++_jmol.appletCount;\r
+               script || (script = "select *");\r
+               inlineModel && (script = 'load DATA "inline"\n' + inlineModel + '\nEND "inline";' + script); \r
+               var Info = {}\r
+               for (var i in Jmol.Info)\r
+                       Info[i] = Jmol.Info[i]\r
+               Info.jarFile || (Info.jarFile = _jmol.archivePath);\r
+               Info.jarPath || (Info.jarPath = _jmol.codebase);\r
+               Info.j2sPath || (Info.j2sPath = Info.jarPath + "/j2s");    \r
+               var sz = _jmolGetAppletSize(size);\r
+               Info.width || (Info.width = sz[0]);\r
+               Info.height || (Info.height = sz[1]);  \r
+               Info.script || (Info.script = script);\r
+               Info.isSigned == null && (Info.isSigned = (Info.jarFile.indexOf("Signed") >= 0));\r
+               for (var i in _jmol.params)\r
+                       if(_jmol.params[i]!="")\r
+                               Info[i] || (Info[i] = _jmol.params[i]);\r
+//  alert(JSON.stringify(Info).replace(/\,/g,"\n\n\n\n"))\r
+               return _jmol.applets[id] = Jmol.getApplet(id, Info)\r
+}\r
+\r
+function _jmolGetJarFilename(fileNameOrFlag) {\r
+       _jmol.archivePath =\r
+               (typeof(fileNameOrFlag) == "string"  ? fileNameOrFlag : (fileNameOrFlag ?  "JmolAppletSigned" : "JmolApplet") + "0.jar");\r
+}\r
+\r
+////////////////////////////////////////////////////////////////\r
+// Legacy Scripting API\r
+////////////////////////////////////////////////////////////////\r
+\r
+function jmolSetParameter(key,value) {\r
+       Jmol.Info[key] = value;\r
+}\r
+\r
+function jmolSetXHTML(id) {\r
+       Jmol.setXHTML(id);\r
+}\r
+\r
+function jmolSetTranslation(TF) {\r
+       // n/a\r
+}\r
+\r
+function jmolInitialize(codebaseDirectory, fileNameOrUseSignedApplet) {\r
+       if (_jmol.initialized)\r
+               return;\r
+       _jmol.initialized = true;\r
+       if(_jmol.jmoljar) {\r
+               var f = _jmol.jmoljar;\r
+               if (f.indexOf("/") >= 0) {\r
+                       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
+                       var ok = prompt("Do you want to use applet " + f + "? ","yes or no")\r
+                       if (ok == "yes") {\r
+                               codebaseDirectory = f.substring(0, f.lastIndexOf("/"));\r
+                               fileNameOrUseSignedApplet = f.substring(f.lastIndexOf("/") + 1);\r
+                       } else {\r
+       _jmolGetJarFilename(fileNameOrUseSignedApplet);\r
+                               alert("The web page URL was ignored. Continuing using " + _jmol.archivePath + ' in directory "' + codebaseDirectory + '"');\r
+                       }\r
+               } else {\r
+                       fileNameOrUseSignedApplet = f;\r
+               }\r
+       }\r
+       _jmol.codebase = codebaseDirectory;\r
+       _jmolGetJarFilename(fileNameOrUseSignedApplet);\r
+}\r
+\r
+function jmolSetDocument(doc) {\r
+       _jmol.currentDocument = doc;\r
+}\r
+\r
+function jmolSetAppletColor(boxbgcolor, boxfgcolor, progresscolor) {\r
+       Jmol.Info.color = boxbgcolor ? boxbgcolor : "black";\r
+}\r
+\r
+function jmolSetAppletWindow(w) {\r
+       _jmol.appletWindow = w;\r
+}\r
+\r
+function jmolApplet(size, script, nameSuffix) {\r
+       return _jmolApplet(size, null, script, nameSuffix);\r
+}\r
+\r
+function jmolAppletInline(size, inlineModel, script, nameSuffix) {\r
+       return _jmolApplet(size, inlineModel, script, nameSuffix);\r
+}\r
+\r
+\r
+\r
+////////////////////////////////////////////////////////////////\r
+// Basic controls\r
+////////////////////////////////////////////////////////////////\r
+\r
+function jmolButton(script, label, id, title) {\r
+       return Jmol.jmolButton(_jmol.target, script, label, id, title);\r
+}\r
+\r
+function jmolCheckbox(scriptWhenChecked, scriptWhenUnchecked, labelHtml, isChecked, id, title) {\r
+       return Jmol.jmolCheckbox(_jmol.target, scriptWhenChecked, scriptWhenUnchecked, labelHtml, isChecked, id, title)\r
+}\r
+\r
+function jmolRadioGroup(arrayOfRadioButtons, separatorHtml, groupName, id, title) {\r
+       return Jmol.jmolRadioGroup(_jmol.target, arrayOfRadioButtons, separatorHtml, groupName, id, title)\r
+}\r
+\r
+\r
+function jmolRadio(script, labelHtml, isChecked, separatorHtml, groupName, id, title) {\r
+       return  Jmol.jmolRadio(_jmol.target, script, labelHtml, isChecked, separatorHtml, groupName, id, title)\r
+}\r
+\r
+function jmolLink(script, label, id, title) {\r
+       return Jmol.jmolLink(_jmol.target, script, label, id, title)\r
+}\r
+\r
+function jmolCommandInput(label, size, id, title) {\r
+       return Jmol.jmolCommandInput(_jmol.target, label, size, id, title);\r
+}\r
+\r
+function jmolMenu(arrayOfMenuItems, size, id, title) {\r
+       return Jmol.jmolMenu(_jmol.target, arrayOfMenuItems, size, id, title);\r
+}\r
+\r
+function jmolHtml(html) {\r
+       return Jmol._documentWrite(html);\r
+}\r
+\r
+function jmolBr() {\r
+       return Jmol._documentWrite("<br />");\r
+}\r
+\r
+////////////////////////////////////////////////////////////////\r
+// advanced scripting functions\r
+////////////////////////////////////////////////////////////////\r
+\r
+function jmolDebugAlert(enableAlerts) {\r
+       // n/a\r
+}\r
+\r
+\r
+function jmolLoadInline(model, targetSuffix) {\r
+       return jmolLoadInlineScript(model, null, targetSuffix, false)\r
+}\r
+\r
+function jmolLoadInlineArray(ModelArray, script, targetSuffix) {\r
+       return jmolLoadInlineScript(ModelArray.join("\n"), script, targetSuffix, false)\r
+}\r
+\r
+function jmolAppendInlineArray(ModelArray, script, targetSuffix) {\r
+       return jmolLoadInlineScript(ModelArray.join("\n"), script, targetSuffix, true)\r
+}\r
+\r
+function jmolAppendInlineScript(model, script, targetSuffix) {\r
+       return jmolLoadInlineScript(model, script, targetSuffix, true)\r
+}\r
+\r
+function jmolLoadInlineScript(model, script, targetSuffix, isAppend) {\r
+       Jmol.script(jmolFindTarget(targetSuffix),\r
+                "load " \r
+                + (isAppend ? "APPEND " : "") \r
+                + "DATA 'mydata'\n" + model.replace(/\"/g,'\\"') + "\nEND 'mydata'\n")\r
+}\r
+\r
+function jmolSetTarget(targetSuffix) {\r
+       targetSuffix == null || (_jmol.targetSuffix = targetSuffix);\r
+       return _jmol.target = "jmolApplet" + _jmol.targetSuffix;\r
+}\r
+\r
+function jmolFindTarget(targetSuffix) {\r
+       return _jmol.applets[jmolSetTarget(targetSuffix)];\r
+}\r
+\r
+function jmolScript(script, targetSuffix) {\r
+       Jmol.script(jmolFindTarget(targetSuffix), script)\r
+}\r
+\r
+function jmolCheckBrowser(action, urlOrMessage, nowOrLater) {\r
+       // unnecessary\r
+}\r
+\r
+////////////////////////////////////////////////////////////////\r
+// Cascading Style Sheet Class support\r
+////////////////////////////////////////////////////////////////\r
+\r
+function jmolSetAppletCssClass(appletCssClass) {\r
+       Jmol.setAppletCss(appletCssClass)\r
+}\r
+\r
+function jmolSetButtonCssClass(s) {\r
+       Jmol.setButtonCss(s)\r
+}\r
+\r
+function jmolSetCheckboxCssClass(s) {\r
+       Jmol.setCheckboxCss(s)\r
+}\r
+\r
+function jmolSetRadioCssClass(s) {\r
+       Jmol.setRadioCss(s)\r
+}\r
+\r
+function jmolSetLinkCssClass(s) {\r
+       Jmol.setLinkCss(s)\r
+}\r
+\r
+function jmolSetMenuCssClass(s) {\r
+       Jmol.setMenuCss(s)\r
+}\r
+\r
+function jmolSetMemoryMb(nMb) {\r
+       // n/a\r
+}\r
+\r
+\r
+function jmolSetCallback(callbackName,funcName) {\r
+//if(!self[funcName])alert(funcName + " is not defined yet in jmolSetCallback")\r
+       Jmol.Info[callbackName] = funcName\r
+       //document.title=("jmolSetCallback " + callbackName + "/" + funcName + " must be included in Info definition")\r
+}\r
+\r
+function jmolSetSyncId(n) {\r
+       alert("jmolSetSyncId " + n + " must be included in Info definition")\r
+}\r
+\r
+function jmolSetLogLevel(n) {\r
+       Jmol.script(_jmol.target, "set loglevel " + n)\r
+}\r
+\r
+function _jmolGetAppletSize(size, units) {\r
+       var width, height;\r
+       if ( (typeof size) == "object" && size != null ) {\r
+               width = size[0]; height = size[1];\r
+       } else {\r
+               width = height = size;\r
+       }\r
+       return [_jmolFixDim(width, units), _jmolFixDim(height, units)];\r
+}\r
+\r
+function _jmolFixDim(x, units) {\r
+       var sx = "" + x;\r
+       return (sx.length == 0 ? (units ? "" : _jmol.allowedJmolSize[2])\r
+       : sx.indexOf("%") == sx.length-1 ? sx\r
+               : (x = parseFloat(x)) <= 1 && x > 0 ? x * 100 + "%"\r
+               : (isNaN(x = Math.floor(x)) ? _jmol.allowedJmolSize[2]\r
+                       : x < _jmol.allowedJmolSize[0] ? _jmol.allowedJmolSize[0]\r
+                               : x > _jmol.allowedJmolSize[1] ? _jmol.allowedJmolSize[1]\r
+                               : x) + (units ? units : ""));\r
+}\r
+\r
+//////////user property/status functions/////////\r
+\r
+function jmolGetStatus(strStatus,targetSuffix){\r
+       return Jmol.getStatus(jmolFindTarget(targetSuffix), strStatus)\r
+}\r
+\r
+function jmolGetPropertyAsArray(sKey,sValue,targetSuffix) {\r
+       return Jmol.getPropertyAsArray(jmolFindTarget(targetSuffix), sKey, sValue)\r
+}\r
+\r
+function jmolGetPropertyAsString(sKey,sValue,targetSuffix) {\r
+       return Jmol.getPropertyAsString(jmolFindTarget(targetSuffix), sKey, sValue)\r
+}\r
+\r
+function jmolGetPropertyAsJSON(sKey,sValue,targetSuffix) {\r
+       return Jmol.getPropertyAsJSON(jmolFindTarget(targetSuffix), sKey, sValue)\r
+}\r
+\r
+function jmolGetPropertyAsJavaObject(sKey,sValue,targetSuffix) {\r
+       return Jmol.getPropertyAsJavaObject(jmolFindTarget(targetSuffix), sKey, sValue)\r
+}\r
+\r
+///////// synchronous scripting ////////\r
+\r
+function jmolScriptWait(script, targetSuffix) {\r
+       return Jmol.scriptWait(jmolFindTarget(targetSuffix), script)\r
+}\r
+\r
+function jmolScriptWaitOutput(script, targetSuffix) {\r
+       return Jmol.scriptWaitOutput(jmolFindTarget(targetSuffix), script)\r
+}\r
+\r
+function jmolEvaluate(molecularMath, targetSuffix) {\r
+       return Jmol.evaluate(jmolFindTarget(targetSuffix), molecularMath)\r
+}\r
+\r
+function jmolScriptEcho(script, targetSuffix) {\r
+       return Jmol.scriptEcho(jmolFindTarget(targetSuffix), script)\r
+}\r
+\r
+\r
+function jmolScriptMessage(script, targetSuffix) {\r
+       return Jmol.scriptMessage(jmolFindTarget(targetSuffix), script)\r
+}\r
+\r
+\r
+function jmolScriptWaitAsArray(script, targetSuffix) {\r
+       return Jmol.scriptWait(jmolFindTarget(targetSuffix), script)\r
+}\r
+\r
+\r
+\r
+////////////   save/restore orientation   /////////////\r
+\r
+function jmolSaveOrientation(id, targetSuffix) {\r
+       return Jmol.saveOrientation(jmolFindTarget(targetSuffix), id)\r
+}\r
+\r
+function jmolRestoreOrientation(id, targetSuffix) {\r
+       return Jmol.restoreOrientation(jmolFindTarget(targetSuffix), id)\r
+}\r
+\r
+function jmolRestoreOrientationDelayed(id, delay, targetSuffix) {\r
+       return Jmol.restoreOrientationDelayed(jmolFindTarget(targetSuffix), id, delay)\r
+}\r
+\r
+function jmolResizeApplet(size, targetSuffix) {\r
+       return Jmol.resizeApplet(jmolFindTarget(targetSuffix), size);\r
+}\r
+\r
+\r
+////////////  add parameter /////////////\r
+\r
+function jmolAppletAddParam(appletCode,name,value){\r
+       alert ("use Info to add a parameter: " + name + "/" + value)\r
+}\r
+\r