jax.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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-2017 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. (function(AJAX, HUB, HTML, PlainSource) {
  25. var EVENT, TOUCH, HOVER; // filled in later
  26. PlainSource.Augment({
  27. settings: HUB.config.menuSettings,
  28. Config: function() {
  29. if (!this.require) this.require = [];
  30. this.SUPER(arguments).Config.call(this);
  31. this.require.push(MathJax.OutputJax.extensionDir + "/MathEvents.js");
  32. },
  33. Startup: function() {
  34. //
  35. // Set up event handling
  36. //
  37. EVENT = MathJax.Extension.MathEvents.Event;
  38. TOUCH = MathJax.Extension.MathEvents.Touch;
  39. HOVER = MathJax.Extension.MathEvents.Hover;
  40. this.ContextMenu = EVENT.ContextMenu;
  41. this.Mousedown = EVENT.AltContextMenu;
  42. this.Mouseover = HOVER.Mouseover;
  43. this.Mouseout = HOVER.Mouseout;
  44. this.Mousemove = HOVER.Mousemove;
  45. return AJAX.Styles(this.config.styles);
  46. },
  47. preTranslate: function(state) {
  48. var scripts = state.jax[this.id],
  49. i, m = scripts.length,
  50. script, prev, span, div, jax;
  51. //
  52. // Loop through the scripts
  53. //
  54. for (i = 0; i < m; i++) {
  55. script = scripts[i];
  56. if (!script.parentNode) continue;
  57. //
  58. // Remove any existing output
  59. //
  60. prev = script.previousSibling;
  61. if (prev && String(prev.className).match(/^MathJax(_PlainSource)?(_Display)?( MathJax_Process(ing|ed))?$/)) {
  62. prev.parentNode.removeChild(prev);
  63. }
  64. //
  65. // Add the span, and a div if in display mode
  66. //
  67. jax = script.MathJax.elementJax;
  68. if (!jax) continue;
  69. jax.PlainSource = {
  70. display: (jax.root.Get("display") === "block")
  71. }
  72. span = div = HTML.Element("span", {
  73. className: "MathJax_PlainSource",
  74. id: jax.inputID + "-Frame",
  75. isMathJax: true,
  76. jaxID: this.id,
  77. oncontextmenu: EVENT.Menu,
  78. onmousedown: EVENT.Mousedown,
  79. onmouseover: EVENT.Mouseover,
  80. onmouseout: EVENT.Mouseout,
  81. onmousemove: EVENT.Mousemove,
  82. onclick: EVENT.Click,
  83. ondblclick: EVENT.DblClick,
  84. // Added for keyboard accessible menu.
  85. onkeydown: EVENT.Keydown,
  86. tabIndex: HUB.getTabOrder(jax)
  87. },[["span"]]);
  88. if (HUB.Browser.noContextMenu) {
  89. span.ontouchstart = TOUCH.start;
  90. span.ontouchend = TOUCH.end;
  91. }
  92. if (jax.PlainSource.display) {
  93. div = HTML.Element("div", {
  94. className: "MathJax_PlainSource_Display"
  95. });
  96. div.appendChild(span);
  97. }
  98. script.parentNode.insertBefore(div, script);
  99. }
  100. },
  101. Translate: function(script, state) {
  102. if (!script.parentNode) return;
  103. //
  104. // Get the data about the math
  105. //
  106. var jax = script.MathJax.elementJax,
  107. math = jax.root,
  108. span = document.getElementById(jax.inputID + "-Frame");
  109. //
  110. // Typeset the math
  111. //
  112. this.initPlainSource(math, span);
  113. var source = jax.originalText;
  114. if (jax.inputJax === "MathML") {
  115. if ((jax.root.data[0].data.length > 0) && (jax.root.data[0].data[0].type === "semantics")) {
  116. var annotations = jax.root.data[0].data[0].data;
  117. for (var a = 0; a < annotations.length; a++){
  118. if (annotations[a].attr.encoding === "application/x-tex"){
  119. source = jax.root.data[0].data[0].data[a].data[0].data[0];
  120. break;
  121. }
  122. if (annotations[a].attr.encoding === "text/x-asciimath") {
  123. source = jax.root.data[0].data[0].data[a].data[0].data[0];
  124. }
  125. }
  126. }
  127. }
  128. jax.PlainSource.source = source;
  129. HTML.addText(span.firstChild,source);
  130. },
  131. postTranslate: function(state) {},
  132. getJaxFromMath: function(math) {
  133. if (math.parentNode.className.match(/MathJax_PlainSource_Display/)) math = math.parentNode;
  134. do {math = math.nextSibling} while (math && math.nodeName.toLowerCase() !== "script");
  135. return HUB.getJaxFor(math);
  136. },
  137. Zoom: function (jax,span,math,Mw,Mh) {
  138. var pad = Math.round(span.parentNode.offsetWidth / 2);
  139. span.style.whiteSpace = "pre";
  140. HTML.addText(span,jax.PlainSource.source);
  141. var mW = math.offsetWidth, mH = math.offsetHeight,
  142. zW = span.offsetWidth, zH = span.offsetHeight;
  143. var Y = -Math.round((zH+mH)/2) - (jax.PlainSource.display ? 0 : pad);
  144. return {mW:mW, mH:mH, zW:zW, zH:zH, Y:Y};
  145. },
  146. initPlainSource: function(math, span) {},
  147. Remove: function(jax) {
  148. var span = document.getElementById(jax.inputID + "-Frame");
  149. if (span) {
  150. if (jax.PlainSource.display) span = span.parentNode;
  151. span.parentNode.removeChild(span);
  152. }
  153. delete jax.PlainSource;
  154. }
  155. });
  156. MathJax.Hub.Register.StartupHook("mml Jax Ready", function() {
  157. MathJax.Hub.Register.StartupHook("onLoad", function() {
  158. setTimeout(MathJax.Callback(["loadComplete", PlainSource, "jax.js"]), 0);
  159. });
  160. });
  161. MathJax.Hub.Register.StartupHook("End Cookie", function() {
  162. if (HUB.config.menuSettings.zoom !== "None") {
  163. AJAX.Require("[MathJax]/extensions/MathZoom.js")
  164. }
  165. });
  166. })(MathJax.Ajax, MathJax.Hub, MathJax.HTML, MathJax.OutputJax.PlainSource);