scribble-mathjax/extensions/MathEvents.js
2011-08-24 11:24:15 -04:00

17 lines
8.3 KiB
JavaScript

/*
* /MathJax/extensions/MathEvents.js
*
* Copyright (c) 2010 Design Science, Inc.
*
* Part of the MathJax library.
* See http://www.mathjax.org for details.
*
* Licensed under the Apache License, Version 2.0;
* you may not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
(function(d,h,k,g,b){var o="1.1";var i=MathJax.Extension;var c=i.MathEvents={version:o};var j=d.config.menuSettings;var n={hover:500,frame:{x:3.5,y:5,bwidth:1,bcolor:"#A6D",hwidth:"15px",hcolor:"#83A",},button:{x:-4,y:-3,wx:-2,src:k.fileURL(b.imageDir+"/MenuArrow-15.png")},fadeinInc:0.2,fadeoutInc:0.05,fadeDelay:50,fadeoutStart:400,fadeoutDelay:15*1000,styles:{".MathJax_Hover_Frame":{"border-radius":".25em","-webkit-border-radius":".25em","-moz-border-radius":".25em","-khtml-border-radius":".25em","box-shadow":"0px 0px 15px #83A","-webkit-box-shadow":"0px 0px 15px #83A","-moz-box-shadow":"0px 0px 15px #83A","-khtml-box-shadow":"0px 0px 15px #83A",border:"1px solid #A6D ! important",display:"inline-block",position:"absolute"},".MathJax_Hover_Arrow":{position:"absolute",width:"15px",height:"11px",cursor:"pointer"}}};var l=c.Event={LEFTBUTTON:0,RIGHTBUTTON:2,MENUKEY:"altKey",Mousedown:function(p){return l.Handler(p,"Mousedown",this)},Mouseup:function(p){return l.Handler(p,"Mouseup",this)},Mousemove:function(p){return l.Handler(p,"Mousemove",this)},Mouseover:function(p){return l.Handler(p,"Mouseover",this)},Mouseout:function(p){return l.Handler(p,"Mouseout",this)},Click:function(p){return l.Handler(p,"Click",this)},DblClick:function(p){return l.Handler(p,"DblClick",this)},Menu:function(p){return l.Handler(p,"ContextMenu",this)},Handler:function(s,q,r){if(k.loadingMathMenu){return False(s)}var p=b[r.jaxID];if(!s){s=window.event}s.isContextMenu=(q==="ContextMenu");if(p[q]){return p[q](s,r)}if(i.MathZoom){return i.MathZoom.HandleEvent(s,q,r)}},False:function(p){if(!p){p=window.event}if(p){if(p.preventDefault){p.preventDefault()}if(p.stopPropagation){p.stopPropagation()}p.cancelBubble=true;p.returnValue=false}return false},ContextMenu:function(u,t,v){var w=b[t.jaxID],q=w.getJaxFromMath(t);var p=(w.config.showMathMenu!=null?w:d).config.showMathMenu;if(!p||(j.context!=="MathJax"&&!v)){return}if(c.msieEventBug){u=window.event}if(c.safariContextMenuBug){setTimeout("window.getSelection().empty()",0)}if(document.selection){setTimeout("document.selection.empty()",0)}f.ClearHoverTimer();if(q.hover){if(q.hover.remove){clearTimeout(q.hover.remove);delete q.hover.remove}q.hover.nofade=true}var r=MathJax.Menu;if(r){r.jax=q;r.menu.Find("Format").menu.items[1].name=(q.inputJax.id==="MathML"?"Original":q.inputJax.id);return r.menu.Post(u)}else{if(!k.loadingMathMenu){k.loadingMathMenu=true;var s={pageX:u.pageX,pageY:u.pageY,clientX:u.clientX,clientY:u.clientY};g.Queue(k.Require("[MathJax]/extensions/MathMenu.js"),function(){delete k.loadingMathMenu;if(!MathJax.Menu){MathJax.Menu={}}},["ContextMenu",this,s,t,v])}return l.False(u)}},AltContextMenu:function(r,q){var s=b[q.jaxID];var p=(s.config.showMathMenu!=null?s:d).config.showMathMenu;if(p){if(j.context==="MathJax"){if(!c.noContextMenuBug||r.button!==l.RIGHTBUTTON){return}}else{if(!r[l.MENUKEY]||r.button!==l.LEFTBUTTON){return}}return s.ContextMenu(r,q,true)}}};var f=c.Hover={Mouseover:function(r,q){if(j.discoverable){var t=r.fromElement||r.relatedTarget,s=r.toElement||r.target;if(t&&s&&t.isMathJax!=s.isMathJax){var p=this.getJaxFromMath(q);if(p.hover){f.ReHover(p)}else{f.HoverTimer(p,q)}return l.False(r)}}},Mouseout:function(r,q){if(j.discoverable){var t=r.fromElement||r.relatedTarget,s=r.toElement||r.target;if(t&&s&&t.isMathJax!=s.isMathJax){var p=this.getJaxFromMath(q);if(p.hover){f.UnHover(p)}else{f.ClearHoverTimer()}return l.False(r)}}},Mousemove:function(r,q){if(j.discoverable){var p=this.getJaxFromMath(q);if(p.hover){return}if(f.lastX==r.clientX&&f.lastY==r.clientY){return}f.lastX=r.clientX;f.lastY=r.clientY;f.HoverTimer(p,q);return l.False(r)}},HoverTimer:function(p,q){this.ClearHoverTimer();this.hoverTimer=setTimeout(g(["Hover",this,p,q]),n.hover)},ClearHoverTimer:function(){if(this.hoverTimer){clearTimeout(this.hoverTimer);delete this.hoverTimer}},Hover:function(p,t){if(i.MathZoom&&i.MathZoom.Hover({},t)){return}var s=p.outputJax,u=s.getHoverSpan(p,t),x=s.getHoverBBox(p,u,t),v=(s.config.showMathMenu!=null?s:d).config.showMathMenu;var z=n.frame.x,y=n.frame.y,w=n.frame.bwidth;if(c.msieBorderWidthBug){w=0}p.hover={opacity:0,id:p.inputID+"-Hover"};var q=h.Element("span",{id:p.hover.id,isMathJax:true,style:{display:"inline-block",width:0,height:0,position:"relative"}},[["span",{className:"MathJax_Hover_Frame",isMathJax:true,style:{display:"inline-block",position:"absolute",top:this.Px(-x.h-y-w-(x.y||0)),left:this.Px(-z-w+(x.x||0)),width:this.Px(x.w+2*z),height:this.Px(x.h+x.d+2*y),opacity:0,filter:"alpha(opacity=0)"}}]]);var r=h.Element("span",{isMathJax:true,id:p.hover.id+"Menu",style:{display:"inline-block","z-index":1,width:0,height:0,position:"relative"}},[["img",{className:"MathJax_Hover_Arrow",isMathJax:true,math:t,src:n.button.src,onclick:this.HoverMenu,jax:s.id,style:{left:this.Px(x.w+z+w+(x.x||0)+n.button.x),top:this.Px(-x.h-y-w-(x.y||0)-n.button.y),opacity:0,filter:"alpha(opacity=0)"}}]]);if(x.width){q.style.width=r.style.width=x.width;q.style.marginRight=r.style.marginRight="-"+x.width;q.firstChild.style.width=x.width;r.firstChild.style.left="";r.firstChild.style.right=this.Px(n.button.wx)}u.parentNode.insertBefore(q,u);if(v){u.parentNode.insertBefore(r,u)}if(u.style){u.style.position="relative"}this.ReHover(p)},ReHover:function(p){if(p.hover.remove){clearTimeout(p.hover.remove)}p.hover.remove=setTimeout(g(["UnHover",this,p]),n.fadeoutDelay);this.HoverFadeTimer(p,n.fadeinInc)},UnHover:function(p){if(!p.hover.nofade){this.HoverFadeTimer(p,-n.fadeoutInc,n.fadeoutStart)}},HoverFade:function(p){delete p.hover.timer;p.hover.opacity=Math.max(0,Math.min(1,p.hover.opacity+p.hover.inc));p.hover.opacity=Math.floor(1000*p.hover.opacity)/1000;var r=document.getElementById(p.hover.id),q=document.getElementById(p.hover.id+"Menu");r.firstChild.style.opacity=p.hover.opacity;r.firstChild.style.filter="alpha(opacity="+Math.floor(100*p.hover.opacity)+")";if(q){q.firstChild.style.opacity=p.hover.opacity;q.firstChild.style.filter=r.style.filter}if(p.hover.opacity===1){return}if(p.hover.opacity>0){this.HoverFadeTimer(p,p.hover.inc);return}r.parentNode.removeChild(r);if(q){q.parentNode.removeChild(q)}if(p.hover.remove){clearTimeout(p.hover.remove)}delete p.hover},HoverFadeTimer:function(p,r,q){p.hover.inc=r;if(!p.hover.timer){p.hover.timer=setTimeout(g(["HoverFade",this,p]),(q||n.fadeDelay))}},HoverMenu:function(p){if(!p){p=window.event}return b[this.jax].ContextMenu(p,this.math,true)},ClearHover:function(p){if(p.hover.remove){clearTimeout(p.hover.remove)}if(p.hover.timer){clearTimeout(p.hover.timer)}f.ClearHoverTimer();delete p.hover},Px:function(p){if(Math.abs(p)<0.006){return"0px"}return p.toFixed(2).replace(/\.?0+$/,"")+"px"},getImages:function(){var p=new Image();p.src=n.button.src}};var a=c.Touch={last:0,delay:500,start:function(q){var p=new Date().getTime();var r=(p-a.last<a.delay);a.last=p;if(r){a.timeout=setTimeout(a.menu,a.delay,q,this);q.preventDefault()}},end:function(p){if(a.timeout){clearTimeout(a.timeout);delete a.timeout;a.last=0;p.preventDefault();return l.Handler((p.touches[0]||p.touch),"DblClick",this)}},menu:function(q,p){delete a.timeout;a.last=0;return l.Handler((q.touches[0]||q.touch),"ContextMenu",p)}};if(d.Browser.isMobile){var m=n.styles[".MathJax_Hover_Arrow"];m.width="25px";m.height="18px";n.button.x=-6}d.Browser.Select({MSIE:function(p){var q=(document.documentMode||0);c.msieBorderWidthBug=(document.compatMode==="BackCompat");c.msieEventBug=p.isIE9;if(q<9){l.LEFTBUTTON=1}},Safari:function(p){c.safariContextMenuBug=true},Konqueror:function(p){c.noContextMenuBug=true}});c.config=n=d.CombineConfig("MathEvents",n);var e=function(){var p=n.styles[".MathJax_Hover_Frame"];p.border=n.frame.bwidth+"px solid "+n.frame.bcolor+" ! important";p["box-shadow"]=p["-webkit-box-shadow"]=p["-moz-box-shadow"]=p["-khtml-box-shadow"]="0px 0px "+n.frame.hwidth+" "+n.frame.hcolor};g.Queue(d.Register.StartupHook("End Config",{}),[e],["getImages",f],["Styles",k,n.styles],["Post",d.Startup.signal,"MathEvents Ready"],["loadComplete",k,"[MathJax]/extensions/MathEvents.js"])})(MathJax.Hub,MathJax.HTML,MathJax.Ajax,MathJax.Callback,MathJax.OutputJax);