maction.js (6444B)
1 /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 4 /************************************************************* 5 * 6 * MathJax/jax/output/CommonHTML/autoload/maction.js 7 * 8 * Implements the CommonHTML output for <maction> elements. 9 * 10 * --------------------------------------------------------------------- 11 * 12 * Copyright (c) 2015 The MathJax Consortium 13 * 14 * Licensed under the Apache License, Version 2.0 (the "License"); 15 * you may not use this file except in compliance with the License. 16 * You may obtain a copy of the License at 17 * 18 * http://www.apache.org/licenses/LICENSE-2.0 19 * 20 * Unless required by applicable law or agreed to in writing, software 21 * distributed under the License is distributed on an "AS IS" BASIS, 22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23 * See the License for the specific language governing permissions and 24 * limitations under the License. 25 */ 26 27 MathJax.Hub.Register.StartupHook("CommonHTML Jax Ready",function () { 28 var VERSION = "2.6.0"; 29 var MML = MathJax.ElementJax.mml, 30 CHTML = MathJax.OutputJax.CommonHTML; 31 32 var currentTip, hover, clear; 33 34 // 35 // Add configuration for tooltips 36 // 37 var CONFIG = CHTML.config.tooltip = MathJax.Hub.Insert({ 38 delayPost: 600, delayClear: 600, 39 offsetX: 10, offsetY: 5 40 },CHTML.config.tooltip||{}); 41 42 43 MML.maction.Augment({ 44 CHTMLtooltip: CHTML.addElement(document.body,"div",{id:"MathJax_CHTML_Tooltip"}), 45 46 toCommonHTML: function (node) { 47 var selected = this.Get("selection"); 48 node = this.CHTMLcreateNode(node); 49 this.CHTML = CHTML.BBOX.empty(); 50 this.CHTMLhandleStyle(node); 51 this.CHTMLhandleScale(node); 52 this.CHTMLaddChild(node,selected-1,{}); 53 this.CHTML.clean(); 54 this.CHTMLhandleSpace(node); 55 this.CHTMLhandleBBox(node); 56 this.CHTMLhandleColor(node); 57 58 var type = this.Get("actiontype"); 59 if (this.CHTMLaction[type] && this.CHTMLaction.hasOwnProperty(type)) 60 this.CHTMLaction[type].call(this,node,selected); 61 62 return node; 63 }, 64 CHTMLcoreNode: function (node) {return this.CHTMLchildNode(node,0)}, 65 66 // 67 // Implementations for the various actions 68 // 69 CHTMLaction: { 70 toggle: function (node,selection) { 71 this.selection = selection; 72 node.onclick = MathJax.Callback(["CHTMLclick",this,CHTML.jax]); 73 node.style.cursor = "pointer"; 74 }, 75 76 statusline: function (node,selection) { 77 node.onmouseover = MathJax.Callback(["CHTMLsetStatus",this]); 78 node.onmouseout = MathJax.Callback(["CHTMLclearStatus",this]); 79 node.onmouseover.autoReset = node.onmouseout.autoReset = true; 80 }, 81 82 tooltip: function(node,selection) { 83 if (this.data[1] && this.data[1].isToken) { 84 node.title = node.alt = this.data[1].data.join(""); 85 } else { 86 node.onmouseover = MathJax.Callback(["CHTMLtooltipOver",this,CHTML.jax]); 87 node.onmouseout = MathJax.Callback(["CHTMLtooltipOut",this,CHTML.jax]); 88 node.onmouseover.autoReset = node.onmouseout.autoReset = true; 89 } 90 } 91 }, 92 93 // 94 // Handle a click on the maction element 95 // (remove the original rendering and rerender) 96 // 97 CHTMLclick: function (jax,event) { 98 this.selection++; 99 if (this.selection > this.data.length) this.selection = 1; 100 var hover = !!jax.hover; 101 jax.Update(); 102 if (hover) { 103 var span = document.getElementById(jax.inputID+"-Span"); 104 MathJax.Extension.MathEvents.Hover.Hover(jax,span); 105 } 106 return MathJax.Extension.MathEvents.Event.False(event); 107 }, 108 109 // 110 // Set/Clear the window status message 111 // 112 CHTMLsetStatus: function (event) { 113 // FIXME: Do something better with non-token elements 114 this.messageID = MathJax.Message.Set 115 ((this.data[1] && this.data[1].isToken) ? 116 this.data[1].data.join("") : this.data[1].toString()); 117 }, 118 CHTMLclearStatus: function (event) { 119 if (this.messageID) MathJax.Message.Clear(this.messageID,0); 120 delete this.messageID; 121 }, 122 123 // 124 // Handle tooltips 125 // 126 CHTMLtooltipOver: function (jax,event) { 127 if (!event) event = window.event; 128 if (clear) {clearTimeout(clear); clear = null} 129 if (hover) clearTimeout(hover); 130 var x = event.pageX; var y = event.pageY; 131 if (x == null) { 132 x = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; 133 y = event.clientY + document.body.scrollTop + document.documentElement.scrollTop; 134 } 135 var callback = MathJax.Callback(["CHTMLtooltipPost",this,jax,x+CONFIG.offsetX,y+CONFIG.offsetY]) 136 hover = setTimeout(callback,CONFIG.delayPost); 137 }, 138 CHTMLtooltipOut: function (jax,event) { 139 if (hover) {clearTimeout(hover); hover = null} 140 if (clear) clearTimeout(clear); 141 var callback = MathJax.Callback(["CHTMLtooltipClear",this,80]); 142 clear = setTimeout(callback,CONFIG.delayClear); 143 }, 144 CHTMLtooltipPost: function (jax,x,y) { 145 hover = null; if (clear) {clearTimeout(clear); clear = null} 146 var tip = this.CHTMLtooltip; 147 tip.style.display = "block"; tip.style.opacity = ""; 148 // tip.style.filter = CHTML.config.styles["#MathJax_CHTML_Tooltip"].filter; 149 if (this === currentTip) return; 150 tip.style.left = x+"px"; tip.style.top = y+"px"; 151 tip.innerHTML = '<span class="mjx-chtml"><span class="mjx-math"></span></span>'; 152 CHTML.getMetrics(jax); 153 try {this.data[1].toCommonHTML(tip.firstChild.firstChild)} catch(err) { 154 if (!err.restart) throw err; 155 tip.style.display = "none"; 156 MathJax.Callback.After(["CHTMLtooltipPost",this,jax,x,y],err.restart); 157 return; 158 } 159 currentTip = this; 160 }, 161 CHTMLtooltipClear: function (n) { 162 var tip = this.CHTMLtooltip; 163 if (n <= 0) { 164 tip.style.display = "none"; 165 tip.style.opacity = tip.style.filter = ""; 166 clear = null; 167 } else { 168 tip.style.opacity = n/100; 169 tip.style.filter = "alpha(opacity="+n+")"; 170 clear = setTimeout(MathJax.Callback(["CHTMLtooltipClear",this,n-20]),50); 171 } 172 } 173 }); 174 175 MathJax.Hub.Startup.signal.Post("CommonHTML maction Ready"); 176 MathJax.Ajax.loadComplete(CHTML.autoloadDir+"/maction.js"); 177 }); 178