Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / site / js / JSmoljQueryExt.js
index 06b87e6..edf60fb 100644 (file)
-// JSmoljQueryExt.js\r
-// 9/2/2013 7:43:12 AM BH Opera/Safari fix for binary file reading\r
-// 3/11/2014 6:31:01 AM BH fix for MSIE not working locally\r
-\r
-;(function($) {\r
-\r
-       function createXHR(isMSIE) {\r
-               try {\r
-                       return (isMSIE ? new window.ActiveXObject( "Microsoft.XMLHTTP" ) : new window.XMLHttpRequest());\r
-               } catch( e ) {}\r
-       }\r
-\r
- $.ajaxSettings.xhr = (window.ActiveXObject === undefined ? createXHR :  \r
-       function() {\r
-               return (this.url == document.location || this.url.indexOf("http") == 0 || !this.isLocal) &&  // BH MSIE fix\r
-                       /^(get|post|head|put|delete|options)$/i.test( this.type ) &&\r
-                       createXHR() || createXHR(1);\r
-       });\r
-\r
-\r
-// Bind script tag hack transport\r
-               $.ajaxTransport( "+script", function(s) {\r
-\r
-       // This transport only deals with cross domain requests\r
-       // BH: No! This is not compatible with Chrome\r
-       if ( true || s.crossDomain ) {\r
-\r
-               var script,\r
-                       head = document.head || jQuery("head")[0] || document.documentElement;\r
-\r
-               return {\r
-\r
-                       send: function( _, callback ) {\r
-                               script = document.createElement("script");\r
-                               //script.async = true;\r
-\r
-                               if ( s.scriptCharset ) {\r
-                                       script.charset = s.scriptCharset;\r
-                               }\r
-\r
-                               script.src = s.url;\r
-\r
-                               // Attach handlers for all browsers\r
-                               script.onload = script.onreadystatechange = function( _, isAbort ) {\r
-\r
-                                       if ( isAbort || !script.readyState || /loaded|complete/.test( script.readyState ) ) {\r
-\r
-                                               // Handle memory leak in IE\r
-                                               script.onload = script.onreadystatechange = null;\r
-                                               // Remove the script\r
-                                               if ( script.parentNode ) {\r
-                                                       script.parentNode.removeChild( script );\r
-                                               }\r
-\r
-                                               // Dereference the script\r
-                                               script = null;\r
-\r
-                                               // Callback if not abort\r
-                                               if ( !isAbort ) {\r
-                                                       callback( 200, "success" );\r
-                                               }\r
-                                       }\r
-                               };\r
-\r
-                               // Circumvent IE6 bugs with base elements (#2709 and #4378) by prepending\r
-                               // Use native DOM manipulation to avoid our domManip AJAX trickery\r
-                               head.insertBefore( script, head.firstChild );\r
-                       },\r
-\r
-                       abort: function() {\r
-                               if ( script ) {\r
-                                       script.onload( undefined, true );\r
-                               }\r
-                       }\r
-               };\r
-       }\r
-});\r
\r
-       // incorporates jquery.iecors MSIE asynchronous cross-domain request for MSIE < 10\r
-\r
-       $.extend( $.support, { iecors: !!window.XDomainRequest });\r
-\r
-       if ($.support.iecors) {\r
-               // source: https://github.com/dkastner/jquery.iecors\r
-               // author: Derek Kastner dkastner@gmail.com http://dkastner.github.com    \r
-               $.ajaxTransport(function(s) {\r
-               \r
-                       return {\r
-                               send: function( headers, complete ) {                           \r
-                                       // Note that xdr is not synchronous.\r
-                                       // This is only being used in JSmol for transport of java code packages.\r
-                                       var xdr = new window.XDomainRequest();\r
-                                       xdr.onload = function() {          \r
-                                               var headers = { 'Content-Type': xdr.contentType };\r
-                                               complete(200, 'OK', { text: xdr.responseText }, headers);\r
-                                       };\r
-                                       if ( s.xhrFields ) {\r
-                                               xdr.onerror = s.xhrFields.error;\r
-                                               xdr.ontimeout = s.xhrFields.timeout;\r
-                                       }\r
-                                       xdr.open( s.type, s.url );\r
-                                       xdr.send( ( s.hasContent && s.data ) || null );\r
-                               },\r
-                               abort: function() {        \r
-                                       xdr.abort();\r
-                               }\r
-                       };\r
-               });\r
-\r
-       } else {\r
-\r
-       // adds support for synchronous binary file reading\r
-\r
-               $.ajaxSetup({\r
-                       accepts: { binary: "text/plain; charset=x-user-defined" },\r
-                       responseFields: { binary: "response" }\r
-               })\r
-\r
-\r
-               $.ajaxTransport('binary', function(s) {\r
-               \r
-                       var callback;\r
-                       return {\r
-                               // synchronous or asynchronous binary transfer only\r
-                               send: function( headers, complete ) {        \r
-                                       var xhr = s.xhr();\r
-                                       console.log("xhr.open binary async=" + s.async + " url=" + s.url);\r
-                                       xhr.open( s.type, s.url, s.async );                                     \r
-                                       var isOK = false;\r
-                                       try {\r
-                                               if (xhr.hasOwnProperty("responseType")) {\r
-                                                               xhr.responseType = "arraybuffer";\r
-                                                               isOK = true;\r
-                                               } \r
-                                       } catch(e) {\r
-                                         //\r
-                                       }\r
-                                       try {\r
-                                               if (!isOK && xhr.overrideMimeType) {\r
-                                                       xhr.overrideMimeType('text/plain; charset=x-user-defined');\r
-                                               }\r
-                                       } catch(e) {\r
-                                                       //\r
-                                       }\r
-                                       if ( !s.crossDomain && !headers["X-Requested-With"] ) {\r
-                                               headers["X-Requested-With"] = "XMLHttpRequest";\r
-                                       }\r
-                                       try {\r
-                                               for (var i in headers )\r
-                                                       xhr.setRequestHeader( i, headers[ i ] );\r
-                                       } catch(_) {}\r
-\r
-                                       xhr.send( ( s.hasContent && s.data ) || null );\r
-\r
-                                       // Listener\r
-                                       callback = function( _, isAbort ) {\r
-\r
-                                       var \r
-                                               status = xhr.status,\r
-                                               statusText = "",\r
-                                               responseHeaders = xhr.getAllResponseHeaders(),\r
-                                               responses = {},\r
-                                               xml;\r
-\r
-                                       try {\r
-\r
-                                               // Firefox throws exceptions when accessing properties\r
-                                               // of an xhr when a network error occured\r
-                                               // http://helpful.knobs-dials.com/index.php/Component_returned_failure_code:_0x80040111_(NS_ERROR_NOT_AVAILABLE)\r
-                                               // Was never called and is aborted or complete\r
-                                               if ( callback && ( xhr.readyState === 4 ) ) {\r
-\r
-                                                       // Only called once\r
-                                                       callback = undefined;\r
-\r
-                                                       // When requesting binary data, IE6-9 will throw an exception\r
-                                                       // on any attempt to access responseText (#11426)\r
-                                                       try {\r
-                                                               responses.text = (typeof xhr.responseText === "string" ? xhr.responseText : null);\r
-                                                       } catch( _ ) {\r
-                                                       }\r
-                                                       try {\r
-                                                               responses.binary = xhr.response;\r
-                                                       } catch( _ ) {\r
-                                                       }\r
-\r
-                                                       // Firefox throws an exception when accessing\r
-                                                       // statusText for faulty cross-domain requests\r
-                                                       try {\r
-                                                               statusText = xhr.statusText;\r
-                                                       } catch( _ ) {\r
-                                                               // We normalize with Webkit giving an empty statusText\r
-                                                               statusText = "";\r
-                                                       }\r
-                                                       // Filter status for non standard behaviors\r
-\r
-                                                       // If the request is local and we have data: assume a success\r
-                                                       // (success with no data won't get notified, that's the best we\r
-                                                       // can do given current implementations)\r
-                                                       if ( !status && s.isLocal && !s.crossDomain ) {\r
-                                                               status = (responses.text ? 200 : 404);\r
-                                                       // IE - #1450: sometimes returns 1223 when it should be 204\r
-                                                       } else if ( status === 1223 ) {\r
-                                                               status = 204;\r
-                                                       }\r
-                                                       complete( status, statusText, responses, responseHeaders );\r
-                                               }\r
-                                       } catch( e ) {\r
-                                               alert(e)\r
-                                               complete( -1, e );\r
-                                       }\r
-                                       };\r
-                                       \r
-                                       if ( !s.async ) {\r
-                                               // if we're in sync mode we fire the callback\r
-                                               callback();\r
-                                       } else if ( xhr.readyState === 4 ) {\r
-                                               // (IE6 & IE7) if it's in cache and has been\r
-                                               // retrieved directly we need to fire the callback\r
-                                               setTimeout( callback );\r
-                                       } else {\r
-                                               // Add to the list of active xhr callbacks\r
-                                               xhr.onreadystatechange = callback;\r
-                                       }\r
-                                       \r
-                               },\r
-                               abort: function() {}\r
-                       };\r
-               });\r
-       }\r
-})( jQuery );\r
-        \r
-/*\r
- * jQuery outside events - v1.1 - 3/16/2010\r
- * http://benalman.com/projects/jquery-outside-events-plugin/\r
- * \r
- * Copyright (c) 2010 "Cowboy" Ben Alman\r
- * Dual licensed under the MIT and GPL licenses.\r
- * http://benalman.com/about/license/\r
- * \r
- * Modified by Bob Hanson for JSmol-specific events and to add parameter reference to actual jQuery event.\r
- * Used for closing the pop-up menu.\r
- *   \r
- */\r
-\r
-;(function($,doc,eventList,id){  \r
-       // was 'click dblclick mousemove mousedown mouseup mouseover mouseout change select submit keydown keypress keyup'\r
-       $.map(\r
-               eventList.split(' '),\r
-               function( event_name ) { jq_addOutsideEvent( event_name ); }\r
-       );\r
-       jq_addOutsideEvent( 'focusin',  'focus' + id );\r
-       jq_addOutsideEvent( 'focusout', 'blur' + id );\r
-       function jq_addOutsideEvent( event_name, outside_event_name ) {\r
-               outside_event_name = outside_event_name || event_name + id;\r
-               var elems = $(),\r
-                       event_namespaced = event_name + '.' + outside_event_name + '-special-event';\r
-               $.event.special[ outside_event_name ] = {    \r
-                       setup: function(){\r
-                               elems = elems.add( this );\r
-                               if ( elems.length === 1 ) {\r
-                                       $(doc).bind( event_namespaced, handle_event );\r
-                               }\r
-                       },\r
-                       teardown: function(){\r
-                               self.Jmol && Jmol._setMouseOwner(null);\r
-                               elems = elems.not( this );\r
-                               if ( elems.length === 0 ) {\r
-                                       $(doc).unbind( event_namespaced );\r
-                               }\r
-                       },\r
-                       add: function( handleObj ) {\r
-                               var old_handler = handleObj.handler;\r
-                               handleObj.handler = function( event, elem ) {\r
-                                       event.target = elem;\r
-                                       old_handler.apply( this, arguments );\r
-                               };\r
-                       }\r
-               };\r
-               function handle_event( event ) {\r
-                       $(elems).each(function(){\r
-                               self.Jmol && (outside_event_name.indexOf("mouseup") >= 0 || outside_event_name.indexOf("touchend") >= 0) && Jmol._setMouseOwner(null);\r
-                               var elem = $(this);\r
-                               if ( this !== event.target && !elem.has(event.target).length ) {\r
-                                       //BH: adds event to pass that along to our handler as well.\r
-                                       elem.triggerHandler( outside_event_name, [ event.target, event ] );\r
-                               }\r
-                       });\r
-               };\r
-       };\r
-})(jQuery,document,"click mousemove mouseup touchmove touchend", "outjsmol");\r
+// JSmoljQueryExt.js
+// 9/2/2013 7:43:12 AM BH Opera/Safari fix for binary file reading
+// 3/11/2014 6:31:01 AM BH fix for MSIE not working locally
+
+;(function($) {
+
+       function createXHR(isMSIE) {
+               try {
+                       return (isMSIE ? new window.ActiveXObject( "Microsoft.XMLHTTP" ) : new window.XMLHttpRequest());
+               } catch( e ) {}
+       }
+
+ $.ajaxSettings.xhr = (window.ActiveXObject === undefined ? createXHR :  
+       function() {
+               return (this.url == document.location || this.url.indexOf("http") == 0 || !this.isLocal) &&  // BH MSIE fix
+                       /^(get|post|head|put|delete|options)$/i.test( this.type ) &&
+                       createXHR() || createXHR(1);
+       });
+
+
+// Bind script tag hack transport
+               $.ajaxTransport( "+script", function(s) {
+
+       // This transport only deals with cross domain requests
+       // BH: No! This is not compatible with Chrome
+       if ( true || s.crossDomain ) {
+
+               var script,
+                       head = document.head || jQuery("head")[0] || document.documentElement;
+
+               return {
+
+                       send: function( _, callback ) {
+                               script = document.createElement("script");
+                               //script.async = true;
+
+                               if ( s.scriptCharset ) {
+                                       script.charset = s.scriptCharset;
+                               }
+
+                               script.src = s.url;
+
+                               // Attach handlers for all browsers
+                               script.onload = script.onreadystatechange = function( _, isAbort ) {
+
+                                       if ( isAbort || !script.readyState || /loaded|complete/.test( script.readyState ) ) {
+
+                                               // Handle memory leak in IE
+                                               script.onload = script.onreadystatechange = null;
+                                               // Remove the script
+                                               if ( script.parentNode ) {
+                                                       script.parentNode.removeChild( script );
+                                               }
+
+                                               // Dereference the script
+                                               script = null;
+
+                                               // Callback if not abort
+                                               if ( !isAbort ) {
+                                                       callback( 200, "success" );
+                                               }
+                                       }
+                               };
+
+                               // Circumvent IE6 bugs with base elements (#2709 and #4378) by prepending
+                               // Use native DOM manipulation to avoid our domManip AJAX trickery
+                               head.insertBefore( script, head.firstChild );
+                       },
+
+                       abort: function() {
+                               if ( script ) {
+                                       script.onload( undefined, true );
+                               }
+                       }
+               };
+       }
+});
+       // incorporates jquery.iecors MSIE asynchronous cross-domain request for MSIE < 10
+
+       $.extend( $.support, { iecors: !!window.XDomainRequest });
+
+       if ($.support.iecors) {
+               // source: https://github.com/dkastner/jquery.iecors
+               // author: Derek Kastner dkastner@gmail.com http://dkastner.github.com    
+               $.ajaxTransport(function(s) {
+               
+                       return {
+                               send: function( headers, complete ) {                           
+                                       // Note that xdr is not synchronous.
+                                       // This is only being used in JSmol for transport of java code packages.
+                                       var xdr = new window.XDomainRequest();
+                                       xdr.onload = function() {          
+                                               var headers = { 'Content-Type': xdr.contentType };
+                                               complete(200, 'OK', { text: xdr.responseText }, headers);
+                                       };
+                                       if ( s.xhrFields ) {
+                                               xdr.onerror = s.xhrFields.error;
+                                               xdr.ontimeout = s.xhrFields.timeout;
+                                       }
+                                       xdr.open( s.type, s.url );
+                                       xdr.send( ( s.hasContent && s.data ) || null );
+                               },
+                               abort: function() {        
+                                       xdr.abort();
+                               }
+                       };
+               });
+
+       } else {
+
+       // adds support for synchronous binary file reading
+
+               $.ajaxSetup({
+                       accepts: { binary: "text/plain; charset=x-user-defined" },
+                       responseFields: { binary: "response" }
+               })
+
+
+               $.ajaxTransport('binary', function(s) {
+               
+                       var callback;
+                       return {
+                               // synchronous or asynchronous binary transfer only
+                               send: function( headers, complete ) {        
+                                       var xhr = s.xhr();
+                                       console.log("xhr.open binary async=" + s.async + " url=" + s.url);
+                                       xhr.open( s.type, s.url, s.async );                                     
+                                       var isOK = false;
+                                       try {
+                                               if (xhr.hasOwnProperty("responseType")) {
+                                                               xhr.responseType = "arraybuffer";
+                                                               isOK = true;
+                                               } 
+                                       } catch(e) {
+                                         //
+                                       }
+                                       try {
+                                               if (!isOK && xhr.overrideMimeType) {
+                                                       xhr.overrideMimeType('text/plain; charset=x-user-defined');
+                                               }
+                                       } catch(e) {
+                                                       //
+                                       }
+                                       if ( !s.crossDomain && !headers["X-Requested-With"] ) {
+                                               headers["X-Requested-With"] = "XMLHttpRequest";
+                                       }
+                                       try {
+                                               for (var i in headers )
+                                                       xhr.setRequestHeader( i, headers[ i ] );
+                                       } catch(_) {}
+
+                                       xhr.send( ( s.hasContent && s.data ) || null );
+
+                                       // Listener
+                                       callback = function( _, isAbort ) {
+
+                                       var 
+                                               status = xhr.status,
+                                               statusText = "",
+                                               responseHeaders = xhr.getAllResponseHeaders(),
+                                               responses = {},
+                                               xml;
+
+                                       try {
+
+                                               // Firefox throws exceptions when accessing properties
+                                               // of an xhr when a network error occured
+                                               // http://helpful.knobs-dials.com/index.php/Component_returned_failure_code:_0x80040111_(NS_ERROR_NOT_AVAILABLE)
+                                               // Was never called and is aborted or complete
+                                               if ( callback && ( xhr.readyState === 4 ) ) {
+
+                                                       // Only called once
+                                                       callback = undefined;
+
+                                                       // When requesting binary data, IE6-9 will throw an exception
+                                                       // on any attempt to access responseText (#11426)
+                                                       try {
+                                                               responses.text = (typeof xhr.responseText === "string" ? xhr.responseText : null);
+                                                       } catch( _ ) {
+                                                       }
+                                                       try {
+                                                               responses.binary = xhr.response;
+                                                       } catch( _ ) {
+                                                       }
+
+                                                       // Firefox throws an exception when accessing
+                                                       // statusText for faulty cross-domain requests
+                                                       try {
+                                                               statusText = xhr.statusText;
+                                                       } catch( _ ) {
+                                                               // We normalize with Webkit giving an empty statusText
+                                                               statusText = "";
+                                                       }
+                                                       // Filter status for non standard behaviors
+
+                                                       // If the request is local and we have data: assume a success
+                                                       // (success with no data won't get notified, that's the best we
+                                                       // can do given current implementations)
+                                                       if ( !status && s.isLocal && !s.crossDomain ) {
+                                                               status = (responses.text ? 200 : 404);
+                                                       // IE - #1450: sometimes returns 1223 when it should be 204
+                                                       } else if ( status === 1223 ) {
+                                                               status = 204;
+                                                       }
+                                                       complete( status, statusText, responses, responseHeaders );
+                                               }
+                                       } catch( e ) {
+                                               alert(e)
+                                               complete( -1, e );
+                                       }
+                                       };
+                                       
+                                       if ( !s.async ) {
+                                               // if we're in sync mode we fire the callback
+                                               callback();
+                                       } else if ( xhr.readyState === 4 ) {
+                                               // (IE6 & IE7) if it's in cache and has been
+                                               // retrieved directly we need to fire the callback
+                                               setTimeout( callback );
+                                       } else {
+                                               // Add to the list of active xhr callbacks
+                                               xhr.onreadystatechange = callback;
+                                       }
+                                       
+                               },
+                               abort: function() {}
+                       };
+               });
+       }
+})( jQuery );
+        
+/*
+ * jQuery outside events - v1.1 - 3/16/2010
+ * http://benalman.com/projects/jquery-outside-events-plugin/
+ * 
+ * Copyright (c) 2010 "Cowboy" Ben Alman
+ * Dual licensed under the MIT and GPL licenses.
+ * http://benalman.com/about/license/
+ * 
+ * Modified by Bob Hanson for JSmol-specific events and to add parameter reference to actual jQuery event.
+ * Used for closing the pop-up menu.
+ *   
+ */
+
+;(function($,doc,eventList,id){  
+       // was 'click dblclick mousemove mousedown mouseup mouseover mouseout change select submit keydown keypress keyup'
+       $.map(
+               eventList.split(' '),
+               function( event_name ) { jq_addOutsideEvent( event_name ); }
+       );
+       jq_addOutsideEvent( 'focusin',  'focus' + id );
+       jq_addOutsideEvent( 'focusout', 'blur' + id );
+       function jq_addOutsideEvent( event_name, outside_event_name ) {
+               outside_event_name = outside_event_name || event_name + id;
+               var elems = $(),
+                       event_namespaced = event_name + '.' + outside_event_name + '-special-event';
+               $.event.special[ outside_event_name ] = {    
+                       setup: function(){
+                               elems = elems.add( this );
+                               if ( elems.length === 1 ) {
+                                       $(doc).bind( event_namespaced, handle_event );
+                               }
+                       },
+                       teardown: function(){
+                               self.Jmol && Jmol._setMouseOwner(null);
+                               elems = elems.not( this );
+                               if ( elems.length === 0 ) {
+                                       $(doc).unbind( event_namespaced );
+                               }
+                       },
+                       add: function( handleObj ) {
+                               var old_handler = handleObj.handler;
+                               handleObj.handler = function( event, elem ) {
+                                       event.target = elem;
+                                       old_handler.apply( this, arguments );
+                               };
+                       }
+               };
+               function handle_event( event ) {
+                       $(elems).each(function(){
+                               self.Jmol && (outside_event_name.indexOf("mouseup") >= 0 || outside_event_name.indexOf("touchend") >= 0) && Jmol._setMouseOwner(null);
+                               var elem = $(this);
+                               if ( this !== event.target && !elem.has(event.target).length ) {
+                                       //BH: adds event to pass that along to our handler as well.
+                                       elem.triggerHandler( outside_event_name, [ event.target, event ] );
+                               }
+                       });
+               };
+       };
+})(jQuery,document,"click mousemove mouseup touchmove touchend", "outjsmol");