| 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-2020 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_Process(ing|ed))?$/)) { |
| 69 |
prev.parentNode.removeChild(prev); |
| 70 |
} |
| 71 |
// |
| 72 |
// Add the span, and a div if in display mode |
| 73 |
// |
| 74 |
jax = script.MathJax.elementJax; |
| 75 |
if (!jax) continue; |
| 76 |
jax.PlainSource = { |
| 77 |
display: (jax.root.Get("display") === "block") |
| 78 |
} |
| 79 |
span = div = HTML.Element("span", { |
| 80 |
className: "MathJax_PlainSource", |
| 81 |
id: jax.inputID + "-Frame", |
| 82 |
isMathJax: true, |
| 83 |
jaxID: this.id, |
| 84 |
oncontextmenu: EVENT.Menu, |
| 85 |
onmousedown: EVENT.Mousedown, |
| 86 |
onmouseover: EVENT.Mouseover, |
| 87 |
onmouseout: EVENT.Mouseout, |
| 88 |
onmousemove: EVENT.Mousemove, |
| 89 |
onclick: EVENT.Click, |
| 90 |
ondblclick: EVENT.DblClick, |
| 91 |
// Added for keyboard accessible menu. |
| 92 |
onkeydown: EVENT.Keydown, |
| 93 |
tabIndex: HUB.getTabOrder(jax) |
| 94 |
},[["span"]]); |
| 95 |
if (HUB.Browser.noContextMenu) { |
| 96 |
span.ontouchstart = TOUCH.start; |
| 97 |
span.ontouchend = TOUCH.end; |
| 98 |
} |
| 99 |
if (jax.PlainSource.display) { |
| 100 |
div = HTML.Element("div", { |
| 101 |
className: "MathJax_PlainSource_Display" |
| 102 |
}); |
| 103 |
div.appendChild(span); |
| 104 |
} |
| 105 |
script.parentNode.insertBefore(div, script); |
| 106 |
} |
| 107 |
}, |
| 108 |
|
| 109 |
Translate: function(script, state) { |
| 110 |
if (!script.parentNode) return; |
| 111 |
|
| 112 |
// |
| 113 |
// Get the data about the math |
| 114 |
// |
| 115 |
var jax = script.MathJax.elementJax, |
| 116 |
math = jax.root, |
| 117 |
span = document.getElementById(jax.inputID + "-Frame"); |
| 118 |
// |
| 119 |
// Typeset the math |
| 120 |
// |
| 121 |
this.initPlainSource(math, span); |
| 122 |
var source = jax.originalText; |
| 123 |
if (jax.inputJax === "MathML") { |
| 124 |
if ((jax.root.data[0].data.length > 0) && (jax.root.data[0].data[0].type === "semantics")) { |
| 125 |
var annotations = jax.root.data[0].data[0].data; |
| 126 |
for (var a = 0; a < annotations.length; a++){ |
| 127 |
if (annotations[a].attr.encoding === "application/x-tex"){ |
| 128 |
source = jax.root.data[0].data[0].data[a].data[0].data[0]; |
| 129 |
break; |
| 130 |
} |
| 131 |
if (annotations[a].attr.encoding === "text/x-asciimath") { |
| 132 |
source = jax.root.data[0].data[0].data[a].data[0].data[0]; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
jax.PlainSource.source = source; |
| 138 |
HTML.addText(span.firstChild,source); |
| 139 |
}, |
| 140 |
|
| 141 |
postTranslate: function(state) {}, |
| 142 |
|
| 143 |
getJaxFromMath: function(math) { |
| 144 |
if (math.parentNode.className.match(/MathJax_PlainSource_Display/)) math = math.parentNode; |
| 145 |
do {math = math.nextSibling} while (math && math.nodeName.toLowerCase() !== "script"); |
| 146 |
return HUB.getJaxFor(math); |
| 147 |
}, |
| 148 |
|
| 149 |
Zoom: function (jax,span,math,Mw,Mh) { |
| 150 |
var pad = Math.round(span.parentNode.offsetWidth / 2); |
| 151 |
span.style.whiteSpace = "pre"; |
| 152 |
HTML.addText(span,jax.PlainSource.source); |
| 153 |
var mW = math.offsetWidth, mH = math.offsetHeight, |
| 154 |
zW = span.offsetWidth, zH = span.offsetHeight; |
| 155 |
var Y = -Math.round((zH+mH)/2) - (jax.PlainSource.display ? 0 : pad); |
| 156 |
return {mW:mW, mH:mH, zW:zW, zH:zH, Y:Y}; |
| 157 |
}, |
| 158 |
|
| 159 |
initPlainSource: function(math, span) {}, |
| 160 |
|
| 161 |
Remove: function(jax) { |
| 162 |
var span = document.getElementById(jax.inputID + "-Frame"); |
| 163 |
if (span) { |
| 164 |
if (jax.PlainSource.display) span = span.parentNode; |
| 165 |
span.parentNode.removeChild(span); |
| 166 |
} |
| 167 |
delete jax.PlainSource; |
| 168 |
} |
| 169 |
|
| 170 |
}); |
| 171 |
|
| 172 |
MathJax.Hub.Register.StartupHook("mml Jax Ready", function() { |
| 173 |
MathJax.Hub.Register.StartupHook("onLoad", function() { |
| 174 |
setTimeout(MathJax.Callback(["loadComplete", PlainSource, "jax.js"]), 0); |
| 175 |
}); |
| 176 |
}); |
| 177 |
|
| 178 |
MathJax.Hub.Register.StartupHook("End Cookie", function() { |
| 179 |
if (HUB.config.menuSettings.zoom !== "None") { |
| 180 |
AJAX.Require("[MathJax]/extensions/MathZoom.js") |
| 181 |
} |
| 182 |
}); |
| 183 |
|
| 184 |
})(MathJax.Ajax, MathJax.Hub, MathJax.HTML, MathJax.OutputJax.PlainSource); |