Jmol JavaScript Object

From Jmol
Revision as of 23:07, 12 August 2012 by Hansonrstolaf (talk | contribs) (Initialization parameters)
Jump to navigation Jump to search

The Jmol JavaScript Object ("Jmol-JSO") is a set of JavaScript functions and utilities refactored and reorganized into a single JavaScript object. It supersedes the former Jmol.js JavaScript library, allowing a cleaner, more efficient way to interact with the Jmol applet on a web page, and abstracting the visualization of a molecular model so that the applet can be seamlessly replaced by an image or a third-party non-Java visualization resource (using WebGL and/or HTML5) depending on client's platform resources.

Using Jmol-JSO, a web page developer can target Java-challenged (iPad, iPhone) and Applet-challenged (Android) platforms but still implement Jmol on traditional platforms. Compromises will have to be made, because no other resource has the scripting power of Jmol. However, the wide variety of options available allows a subset of Jmol functionality on all such devices, and there should be no need to ever see a message "Sorry, you don't have Java installed...."

Jmol-JSO also allows easy access to public databases such as the RCSB PDB database, the National Cancer Institute CACTVS server, and PubChem, even when the Jmol Java applet is not available because of a user's platform.

In addition, Jmol-JSO allows easy interaction with the JSpecView applet (for spectroscopy) and the JME applet (for 2D drawing input) on a web page. Thus, 3D models can be tied to IR, NMR, UV/VIS, GC, and GC/MS spectra (using the JCAMP-DX Version 6 standard), 3D models can be embedded in JCAMP-DX files (see Jmol-JSpecView-specs.pdf), and 3D models can be introduce by a page visitor using simple chemical drawing.

Jmol-JSO is fully compatible with jQuery, creating the single global Jmol object. The library has been W3C and XHTML validated.

Installation

JmolJSO requires a small suite of JavaScript libraries that replace Jmol.js. However, the Java files necessary are the same:

  • File icon.gifJmolApplet.jar or File icon.gifJmolAppletSigned.jar or File icon.gifJmolApplet0*.jar or File icon.gifJmolAppletSigned0*.jar
(That is, a set of files, where the * stands for several suffixes. You may need to read about signed vs. unsigned applet if you are unfamiliar with the Jmol jar files.)

Note that Java scurity policy imposes limitations on the folder location of files.

The basic suite of JavaScript libraries include these four:

  • File icon.gifJmolCore.js (required)
  • File icon.gifJmolApplet.js (required)
  • File icon.gifJmolControls.js (optional -- required if any buttons or links or other input methods will be used)
  • File icon.gifJmolApi.js (required)

However, AJAX access (by the unsigned applet or non-Java resources) to cross-domain resources such as public databases also requires jQuery.min.js and jQuery.iecors.js (loaded prior to JmolCore.js), and use of ChemDoodle, GLmol, JSpecView, or JME requires additional libraries.

Initialization

Note that JavaScript libraries not starting with "Jmol" may not be included in the Jmol distribution. Files can be found at chemapps.stolaf.edu/jmol and associated subdirectories. These include:

Basic

The web page at the very least should have the following in the head section (referencing files in appropriate directories if not the directory of the web page, as shown here):

<script type="text/javascript" src="JmolCore.js"></script>
<script type="text/javascript" src="JmolApplet.js"></script>
<script type="text/javascript" src="JmolControls.js"></script>
<script type="text/javascript" src="JmolApi.js"></script>

JmolControls.js may be omitted if no buttons, links, menus, or other user-interactive features are implemented on the page. All functions discussed below are found in JmolApi.js. The other files contain code that creates the various Jmol object prototypes and methods that are called from JmolApi.js.

Unsigned Jmol applet requiring access to public databases and all non-Java options

Web pages requiring the unsigned applet to access public databases, as well as all non-Jmol options, additionally require jQuery so that they can perform cross-browser or host-server AJAX calls:

<script type="text/javascript" src="jQuery.min.js"></script>
<script type="text/javascript" src="jQuery.iecors.js"></script>
<script type="text/javascript" src="JmolCore.js"></script>
<script type="text/javascript" src="JmolApplet.js"></script>
<script type="text/javascript" src="JmolControls.js"></script>
<script type="text/javascript" src="JmolApi.js"></script>

ChemDoodle option

Web pages utilizing ChemDoodle (either HTML5/WebGL or HTML5/non-WebGL versions) should have the following in their head section:

<script type="text/javascript" src="jQuery.min.js"></script>
<script type="text/javascript" src="jQuery.iecors.js"></script>
<script type="text/javascript" src="JmolCore.js"></script>
<script type="text/javascript" src="JmolApplet.js"></script>
<script type="text/javascript" src="JmolControls.js"></script>
<script type="text/javascript" src="JmolApi.js"></script>
<script type="text/javascript" src="gl-matrix-min.js"></script>
<script type="text/javascript" src="mousewheel.js"></script>
<script type="text/javascript" src="ChemDoodleWeb.js"></script>
<script type="text/javascript" src="JmolCD.js"></script>

GLmol option

<script type="text/javascript" src="jQuery.min.js"></script>
<script type="text/javascript" src="jQuery.iecors.js"></script>
<script type="text/javascript" src="JmolCore.js"></script>
<script type="text/javascript" src="JmolApplet.js"></script>
<script type="text/javascript" src="JmolControls.js"></script>
<script type="text/javascript" src="JmolApi.js"></script>
<script type="text/javascript" src="Three49custom.js"></script>
<script type="text/javascript" src="GLmol.js"></script>
<script type="text/javascript" src="JmolGLmol.js"></script>

Initialization parameters

All of the initialization parameters common to Jmol.js can be implemented in Jmol-JSO. However, the method of indicating variables is different. The essential call to create an applet or other non-Java viewer is simply:

jmol = Jmol.getApplet("jmol", Info)

where Info is an associative array (a set of key/value pairs) indicating all the desired characteristics of the applet. For a full, up-to-date description of all of the Info options, see JmolApi.js. It is recommended that any page developer interested in utilizing Jmol extensively take a look at this file. The Info keys and their default values are given and below:

<code>
		var Info = {
			addSelectionOptions: false,
			color: "#FFFFFF",
			debug: false,
			defaultModel: "",
			height: 300,
			isSigned: false,
			jarFile: "JmolApplet0.jar",
			jarPath: ".",
			readyFunction: null,
			script: null,
			serverURL: "http://chemapps.stolaf.edu/jmol/jmolcd.php",
			src: null,
			useNoApplet: false,
			useJmolOnly: true,
			useWebGlIfAvailable: true,
			useImageOnly: false,
			width: 300
		};	 
</code>
  • Info.addSelectionOptions Set this value to true if you want the user to see a menu with public database options.
  • Info.color The background color of the applet.
  • Info.debug Set this value to true if you are having problems getting your page to show the applet or buttons.
  • Info.defaultModel A model from a public database to obtain and display when the applet is loaded.
  • Info.height The width of the applet in pixels or expressed as percent of its container as a string in quotes: "100%"
  • Info.isSigned Make sure to change this to true if the file indicated for jarFile is the signed applet.
  • Info.jarFile The file used, usually either JmolApplet0.jar (for faster incremental loading) or JmolApplet.jar (a single 2.5+Mb download).
  • Info.jarPath The path to the jar file. Note that if the unsigned applet is used and the page is tested locally, then all model files must in or under the directory indicated by this value.
  • Info.readyFunction The name of a JavaScript function to call when the applet has been created and is ready to receive commands (and also called when the applet is destroyed when the page is closed).
  • Info.script A script to run when the applet has loaded. Note that indicating a script here is not necessary; with Jmol-JSO, unlike with Jmol.js, you can start making script calls to the applet as soon as the applet wrapper object has been created in JavaScript; there is no need to wait for Java applet initialization to have completed.
  • Info.width The width of the applet in pixels or expressed as percent of its container as a string in quotes: "100%"

API