maction.js 6.3 KB

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