jax.js (6286B)
1 /************************************************************* 2 * 3 * MathJax/jax/output/PlainSource/jax.js 4 * 5 * Implements the PlainSource OutputJax that displays whatever 6 * source there was, for assistive technology users who prefer this. 7 * 8 * --------------------------------------------------------------------- 9 * 10 * Copyright (c) 2015 The MathJax Consortium 11 * 12 * Licensed under the Apache License, Version 2.0 (the "License"); 13 * you may not use this file except in compliance with the License. 14 * You may obtain a copy of the License at 15 * 16 * http://www.apache.org/licenses/LICENSE-2.0 17 * 18 * Unless required by applicable law or agreed to in writing, software 19 * distributed under the License is distributed on an "AS IS" BASIS, 20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 * See the License for the specific language governing permissions and 22 * limitations under the License. 23 */ 24 25 26 (function(AJAX, HUB, HTML, PlainSource) { 27 28 var EVENT, TOUCH, HOVER; // filled in later 29 30 PlainSource.Augment({ 31 settings: HUB.config.menuSettings, 32 33 Config: function() { 34 if (!this.require) this.require = []; 35 this.SUPER(arguments).Config.call(this); 36 this.require.push(MathJax.OutputJax.extensionDir + "/MathEvents.js"); 37 }, 38 39 Startup: function() { 40 // 41 // Set up event handling 42 // 43 EVENT = MathJax.Extension.MathEvents.Event; 44 TOUCH = MathJax.Extension.MathEvents.Touch; 45 HOVER = MathJax.Extension.MathEvents.Hover; 46 this.ContextMenu = EVENT.ContextMenu; 47 this.Mousedown = EVENT.AltContextMenu; 48 this.Mouseover = HOVER.Mouseover; 49 this.Mouseout = HOVER.Mouseout; 50 this.Mousemove = HOVER.Mousemove; 51 return AJAX.Styles(this.config.styles); 52 }, 53 54 preTranslate: function(state) { 55 var scripts = state.jax[this.id], 56 i, m = scripts.length, 57 script, prev, span, div, jax; 58 // 59 // Loop through the scripts 60 // 61 for (i = 0; i < m; i++) { 62 script = scripts[i]; 63 if (!script.parentNode) continue; 64 // 65 // Remove any existing output 66 // 67 prev = script.previousSibling; 68 if (prev && String(prev.className).match(/^MathJax_PlainSource(_Display)?( MathJax_Processing)?$/)) { 69 prev.parentNode.removeChild(prev); 70 } 71 // 72 // Add the span, and a div if in display mode, 73 // then set the role and mark it as being processed 74 // 75 jax = script.MathJax.elementJax; 76 if (!jax) continue; 77 jax.PlainSource = { 78 display: (jax.root.Get("display") === "block") 79 } 80 span = div = HTML.Element("span", { 81 className: "MathJax_PlainSource", 82 id: jax.inputID + "-Frame", 83 isMathJax: true, 84 jaxID: this.id, 85 oncontextmenu: EVENT.Menu, 86 onmousedown: EVENT.Mousedown, 87 onmouseover: EVENT.Mouseover, 88 onmouseout: EVENT.Mouseout, 89 onmousemove: EVENT.Mousemove, 90 onclick: EVENT.Click, 91 ondblclick: EVENT.DblClick, 92 // Added for keyboard accessible menu. 93 onkeydown: EVENT.Keydown, 94 tabIndex: HUB.getTabOrder(jax) 95 },[["span"]]); 96 if (HUB.Browser.noContextMenu) { 97 span.ontouchstart = TOUCH.start; 98 span.ontouchend = TOUCH.end; 99 } 100 if (jax.PlainSource.display) { 101 div = HTML.Element("div", { 102 className: "MathJax_PlainSource_Display" 103 }); 104 div.appendChild(span); 105 } 106 script.parentNode.insertBefore(div, script); 107 } 108 }, 109 110 Translate: function(script, state) { 111 if (!script.parentNode) return; 112 113 // 114 // Get the data about the math 115 // 116 var jax = script.MathJax.elementJax, 117 math = jax.root, 118 span = document.getElementById(jax.inputID + "-Frame"); 119 // 120 // Typeset the math 121 // 122 this.initPlainSource(math, span); 123 var source = jax.originalText; 124 if (jax.inputJax === "MathML") { 125 if ((jax.root.data[0].data.length > 0) && (jax.root.data[0].data[0].type === "semantics")) { 126 var annotations = jax.root.data[0].data[0].data; 127 for (var a = 0; a < annotations.length; a++){ 128 if (annotations[a].attr.encoding === "application/x-tex"){ 129 source = jax.root.data[0].data[0].data[a].data[0].data[0]; 130 break; 131 } 132 if (annotations[a].attr.encoding === "text/x-asciimath") { 133 source = jax.root.data[0].data[0].data[a].data[0].data[0]; 134 } 135 } 136 } 137 } 138 jax.PlainSource.source = source; 139 HTML.addText(span.firstChild,source); 140 }, 141 142 postTranslate: function(state) {}, 143 144 getJaxFromMath: function(math) { 145 if (math.parentNode.className === "MathJax_PlainSource_Display") math = math.parentNode; 146 do {math = math.nextSibling} while (math && math.nodeName.toLowerCase() !== "script"); 147 return HUB.getJaxFor(math); 148 }, 149 150 Zoom: function (jax,span,math,Mw,Mh) { 151 var pad = Math.round(span.parentNode.offsetWidth / 2); 152 span.style.whiteSpace = "pre"; 153 HTML.addText(span,jax.PlainSource.source); 154 var mW = math.offsetWidth, mH = math.offsetHeight, 155 zW = span.offsetWidth, zH = span.offsetHeight; 156 var Y = -Math.round((zH+mH)/2) - (jax.PlainSource.display ? 0 : pad); 157 return {mW:mW, mH:mH, zW:zW, zH:zH, Y:Y}; 158 }, 159 160 initPlainSource: function(math, span) {}, 161 162 Remove: function(jax) { 163 var span = document.getElementById(jax.inputID + "-Frame"); 164 if (span) { 165 if (jax.PlainSource.display) span = span.parentNode; 166 span.parentNode.removeChild(span); 167 } 168 delete jax.PlainSource; 169 } 170 171 }); 172 173 MathJax.Hub.Register.StartupHook("mml Jax Ready", function() { 174 MathJax.Hub.Register.StartupHook("onLoad", function() { 175 setTimeout(MathJax.Callback(["loadComplete", PlainSource, "jax.js"]), 0); 176 }); 177 }); 178 179 MathJax.Hub.Register.StartupHook("End Cookie", function() { 180 if (HUB.config.menuSettings.zoom !== "None") { 181 AJAX.Require("[MathJax]/extensions/MathZoom.js") 182 } 183 }); 184 185 })(MathJax.Ajax, MathJax.Hub, MathJax.HTML, MathJax.OutputJax.PlainSource);