Debian Codegraph @

unpacked/extensions/MathEvents.js

raw
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/extensions/MathEvents.js
7 *
8 * Implements the event handlers needed by the output jax to perform
9 * menu, hover, and other events.
10 *
11 * ---------------------------------------------------------------------
12 *
13 * Copyright (c) 2011-2020 The MathJax Consortium
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License");
16 * you may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 */
27
28 (function (HUB,HTML,AJAX,CALLBACK,LOCALE,OUTPUT,INPUT) {
29 var VERSION = "2.7.9";
30
31 var EXTENSION = MathJax.Extension;
32 var ME = EXTENSION.MathEvents = {version: VERSION};
33
34 var SETTINGS = HUB.config.menuSettings;
35
36 var CONFIG = {
37 hover: 500, // time required to be considered a hover
38 frame: {
39 x: 3.5, y: 5, // frame padding and
40 bwidth: 1, // frame border width (in pixels)
41 bcolor: "#A6D", // frame border color
42 hwidth: "15px", // haze width
43 hcolor: "#83A" // haze color
44 },
45 button: {
46 x: -6, y: -3, // menu button offsets
47 wx: -2 // button offset for full-width equations
48 },
49 fadeinInc: .2, // increment for fade-in
50 fadeoutInc: .05, // increment for fade-out
51 fadeDelay: 50, // delay between fade-in or fade-out steps
52 fadeoutStart: 400, // delay before fade-out after mouseout
53 fadeoutDelay: 15*1000, // delay before automatic fade-out
54
55 styles: {
56 ".MathJax_Hover_Frame": {
57 "border-radius": ".25em", // Opera 10.5 and IE9
58 "-webkit-border-radius": ".25em", // Safari and Chrome
59 "-moz-border-radius": ".25em", // Firefox
60 "-khtml-border-radius": ".25em", // Konqueror
61
62 "box-shadow": "0px 0px 15px #83A", // Opera 10.5 and IE9
63 "-webkit-box-shadow": "0px 0px 15px #83A", // Safari and Chrome
64 "-moz-box-shadow": "0px 0px 15px #83A", // Forefox
65 "-khtml-box-shadow": "0px 0px 15px #83A", // Konqueror
66
67 border: "1px solid #A6D ! important",
68 display: "inline-block", position:"absolute"
69 },
70
71 ".MathJax_Menu_Button .MathJax_Hover_Arrow": {
72 position:"absolute",
73 cursor:"pointer",
74 display:"inline-block",
75 border:"2px solid #AAA",
76 "border-radius":"4px",
77 "-webkit-border-radius": "4px", // Safari and Chrome
78 "-moz-border-radius": "4px", // Firefox
79 "-khtml-border-radius": "4px", // Konqueror
80 "font-family":"'Courier New',Courier",
81 "font-size":"9px",
82 color:"#F0F0F0"
83 },
84 ".MathJax_Menu_Button .MathJax_Hover_Arrow span": {
85 display:"block",
86 "background-color":"#AAA",
87 border:"1px solid",
88 "border-radius":"3px",
89 "line-height":0,
90 padding:"4px"
91 },
92 ".MathJax_Hover_Arrow:hover": {
93 color:"white!important",
94 border:"2px solid #CCC!important"
95 },
96 ".MathJax_Hover_Arrow:hover span": {
97 "background-color":"#CCC!important"
98 }
99 }
100 };
101
102
103 //
104 // Common event-handling code
105 //
106 var EVENT = ME.Event = {
107
108 LEFTBUTTON: 0, // the event.button value for left button
109 RIGHTBUTTON: 2, // the event.button value for right button
110 MENUKEY: "altKey", // the event value for alternate context menu
111
112 /*************************************************************/
113 /*
114 * Enum element for key codes.
115 */
116 KEY: {
117 RETURN: 13,
118 ESCAPE: 27,
119 SPACE: 32,
120 LEFT: 37,
121 UP: 38,
122 RIGHT: 39,
123 DOWN: 40,
124 LESS: 188,
125 GREATER: 190,
126 DASH: 189
127 },
128
129 Mousedown: function (event) {return EVENT.Handler(event,"Mousedown",this)},
130 Mouseup: function (event) {return EVENT.Handler(event,"Mouseup",this)},
131 Mousemove: function (event) {return EVENT.Handler(event,"Mousemove",this)},
132 Mouseover: function (event) {return EVENT.Handler(event,"Mouseover",this)},
133 Mouseout: function (event) {return EVENT.Handler(event,"Mouseout",this)},
134 Click: function (event) {return EVENT.Handler(event,"Click",this)},
135 DblClick: function (event) {return EVENT.Handler(event,"DblClick",this)},
136 Menu: function (event) {return EVENT.Handler(event,"ContextMenu",this)},
137
138 //
139 // Call the output jax's event handler or the zoom handler
140 //
141 Handler: function (event,type,math) {
142 if (AJAX.loadingMathMenu) {return EVENT.False(event)}
143 var jax = OUTPUT[math.jaxID];
144 if (!event) {event = window.event}
145 event.isContextMenu = (type === "ContextMenu");
146 if (jax[type]) {return jax[type](event,math)}
147 if (EXTENSION.MathZoom) {return EXTENSION.MathZoom.HandleEvent(event,type,math)}
148 },
149
150 //
151 // Try to cancel the event in every way we can
152 //
153 False: function (event) {
154 if (!event) {event = window.event}
155 if (event) {
156 if (event.preventDefault) {event.preventDefault()} else {event.returnValue = false}
157 if (event.stopPropagation) {event.stopPropagation()}
158 event.cancelBubble = true;
159 }
160 return false;
161 },
162
163 //
164 // Keydown event handler. Should only fire on Space key.
165 //
166 Keydown: function (event, math) {
167 if (!event) event = window.event;
168 if (event.keyCode === EVENT.KEY.SPACE) {
169 EVENT.ContextMenu(event, this);
170 };
171 },
172
173 //
174 // Load the contextual menu code, if needed, and post the menu
175 //
176 ContextMenu: function (event,math,force) {
177 //
178 // Check if we are showing menus
179 //
180 var JAX = OUTPUT[math.jaxID], jax = JAX.getJaxFromMath(math);
181 var show = (JAX.config.showMathMenu != null ? JAX : HUB).config.showMathMenu;
182 if (!show || (SETTINGS.context !== "MathJax" && !force)) return;
183
184 //
185 // Remove selections, remove hover fades
186 //
187 if (ME.msieEventBug) {event = window.event || event}
188 EVENT.ClearSelection(); HOVER.ClearHoverTimer();
189 if (jax.hover) {
190 if (jax.hover.remove) {clearTimeout(jax.hover.remove); delete jax.hover.remove}
191 jax.hover.nofade = true;
192 }
193
194 //
195 // If the menu code is loaded,
196 // Check if localization needs loading;
197 // If not, post the menu, and return.
198 // Otherwise wait for the localization to load
199 // Otherwse load the menu code.
200 // Try again after the file is loaded.
201 //
202 var MENU = MathJax.Menu; var load, fn;
203 if (MENU) {
204 if (MENU.loadingDomain) {return EVENT.False(event)}
205 load = LOCALE.loadDomain("MathMenu");
206 if (!load) {
207 MENU.jax = jax;
208 var source = MENU.menu.Find("Show Math As").submenu;
209 source.items[0].name = jax.sourceMenuTitle;
210 source.items[0].format = (jax.sourceMenuFormat||"MathML");
211 source.items[1].name = INPUT[jax.inputJax].sourceMenuTitle;
212 source.items[5].disabled = !INPUT[jax.inputJax].annotationEncoding;
213
214 //
215 // Try and find each known annotation format and enable the menu
216 // items accordingly.
217 //
218 var annotations = source.items[2]; annotations.disabled = true;
219 var annotationItems = annotations.submenu.items;
220 annotationList = MathJax.Hub.Config.semanticsAnnotations;
221 for (var i = 0, m = annotationItems.length; i < m; i++) {
222 var name = annotationItems[i].name[1]
223 if (jax.root && jax.root.getAnnotation(name) !== null) {
224 annotations.disabled = false;
225 annotationItems[i].hidden = false;
226 } else {
227 annotationItems[i].hidden = true;
228 }
229 }
230
231 var MathPlayer = MENU.menu.Find("Math Settings","MathPlayer");
232 MathPlayer.hidden = !(jax.outputJax === "NativeMML" && HUB.Browser.hasMathPlayer);
233 return MENU.menu.Post(event);
234 }
235 MENU.loadingDomain = true;
236 fn = function () {delete MENU.loadingDomain};
237 } else {
238 if (AJAX.loadingMathMenu) {return EVENT.False(event)}
239 AJAX.loadingMathMenu = true;
240 load = AJAX.Require("[MathJax]/extensions/MathMenu.js");
241 fn = function () {
242 delete AJAX.loadingMathMenu;
243 if (!MathJax.Menu) {MathJax.Menu = {}}
244 }
245 }
246 var ev = {
247 pageX:event.pageX, pageY:event.pageY,
248 clientX:event.clientX, clientY:event.clientY
249 };
250 CALLBACK.Queue(
251 load, fn, // load the file and delete the marker when done
252 ["ContextMenu",EVENT,ev,math,force] // call this function again
253 );
254 return EVENT.False(event);
255 },
256
257 //
258 // Mousedown handler for alternate means of accessing menu
259 //
260 AltContextMenu: function (event,math) {
261 var JAX = OUTPUT[math.jaxID];
262 var show = (JAX.config.showMathMenu != null ? JAX : HUB).config.showMathMenu;
263 if (show) {
264 show = (JAX.config.showMathMenuMSIE != null ? JAX : HUB).config.showMathMenuMSIE;
265 if (SETTINGS.context === "MathJax" && !SETTINGS.mpContext && show) {
266 if (!ME.noContextMenuBug || event.button !== EVENT.RIGHTBUTTON) return;
267 } else {
268 if (!event[EVENT.MENUKEY] || event.button !== EVENT.LEFTBUTTON) return;
269 }
270 return JAX.ContextMenu(event,math,true);
271 }
272 },
273
274 ClearSelection: function () {
275 if (ME.safariContextMenuBug) {setTimeout("window.getSelection().empty()",0)}
276 if (document.selection) {setTimeout("document.selection.empty()",0)}
277 },
278
279 getBBox: function (span) {
280 span.appendChild(ME.topImg);
281 var h = ME.topImg.offsetTop, d = span.offsetHeight-h, w = span.offsetWidth;
282 span.removeChild(ME.topImg);
283 return {w:w, h:h, d:d};
284 }
285
286 };
287
288 //
289 // Handle hover "discoverability"
290 //
291 var HOVER = ME.Hover = {
292
293 //
294 // Check if we are moving from a non-MathJax element to a MathJax one
295 // and either start fading in again (if it is fading out) or start the
296 // timer for the hover
297 //
298 Mouseover: function (event,math) {
299 if (SETTINGS.discoverable || SETTINGS.zoom === "Hover") {
300 var from = event.fromElement || event.relatedTarget,
301 to = event.toElement || event.target;
302 if (from && to && (HUB.isMathJaxNode(from) !== HUB.isMathJaxNode(to) ||
303 HUB.getJaxFor(from) !== HUB.getJaxFor(to))) {
304 var jax = this.getJaxFromMath(math);
305 if (jax.hover) {HOVER.ReHover(jax)} else {HOVER.HoverTimer(jax,math)}
306 return EVENT.False(event);
307 }
308 }
309 },
310 //
311 // Check if we are moving from a MathJax element to a non-MathJax one
312 // and either start fading out, or clear the timer if we haven't
313 // hovered yet
314 //
315 Mouseout: function (event,math) {
316 if (SETTINGS.discoverable || SETTINGS.zoom === "Hover") {
317 var from = event.fromElement || event.relatedTarget,
318 to = event.toElement || event.target;
319 if (from && to && (HUB.isMathJaxNode(from) !== HUB.isMathJaxNode(to) ||
320 HUB.getJaxFor(from) !== HUB.getJaxFor(to))) {
321 var jax = this.getJaxFromMath(math);
322 if (jax.hover) {HOVER.UnHover(jax)} else {HOVER.ClearHoverTimer()}
323 return EVENT.False(event);
324 }
325 }
326 },
327 //
328 // Restart hover timer if the mouse moves
329 //
330 Mousemove: function (event,math) {
331 if (SETTINGS.discoverable || SETTINGS.zoom === "Hover") {
332 var jax = this.getJaxFromMath(math); if (jax.hover) return;
333 if (HOVER.lastX == event.clientX && HOVER.lastY == event.clientY) return;
334 HOVER.lastX = event.clientX; HOVER.lastY = event.clientY;
335 HOVER.HoverTimer(jax,math);
336 return EVENT.False(event);
337 }
338 },
339
340 //
341 // Clear the old timer and start a new one
342 //
343 HoverTimer: function (jax,math) {
344 this.ClearHoverTimer();
345 this.hoverTimer = setTimeout(CALLBACK(["Hover",this,jax,math]),CONFIG.hover);
346 },
347 ClearHoverTimer: function () {
348 if (this.hoverTimer) {clearTimeout(this.hoverTimer); delete this.hoverTimer}
349 },
350
351 //
352 // Handle putting up the hover frame
353 //
354 Hover: function (jax,math) {
355 //
356 // Check if Zoom handles the hover event
357 //
358 if (EXTENSION.MathZoom && EXTENSION.MathZoom.Hover({},math)) return;
359 //
360 // Get the hover data
361 //
362 var JAX = OUTPUT[jax.outputJax],
363 span = JAX.getHoverSpan(jax,math),
364 bbox = JAX.getHoverBBox(jax,span,math),
365 show = (JAX.config.showMathMenu != null ? JAX : HUB).config.showMathMenu;
366 var dx = CONFIG.frame.x, dy = CONFIG.frame.y, dd = CONFIG.frame.bwidth; // frame size
367 if (ME.msieBorderWidthBug) {dd = 0}
368 jax.hover = {opacity:0, id:jax.inputID+"-Hover"};
369 //
370 // The frame and menu button
371 //
372 var frame = HTML.Element("span",{
373 id:jax.hover.id, isMathJax: true,
374 style:{display:"inline-block", width:0, height:0, position:"relative"}
375 },[["span",{
376 className:"MathJax_Hover_Frame", isMathJax: true,
377 style:{
378 display:"inline-block", position:"absolute",
379 top:this.Px(-bbox.h-dy-dd-(bbox.y||0)), left:this.Px(-dx-dd+(bbox.x||0)),
380 width:this.Px(bbox.w+2*dx), height:this.Px(bbox.h+bbox.d+2*dy),
381 opacity:0, filter:"alpha(opacity=0)"
382 }}
383 ]]
384 );
385 var button = HTML.Element("span",{
386 isMathJax: true, id:jax.hover.id+"Menu", className:"MathJax_Menu_Button",
387 style:{display:"inline-block", "z-index": 1, width:0, height:0, position:"relative"}
388 },[["span",{
389 className: "MathJax_Hover_Arrow", isMathJax: true, math: math,
390 onclick: this.HoverMenu, jax:JAX.id,
391 style: {
392 left:this.Px(bbox.w+dx+dd+(bbox.x||0)+CONFIG.button.x),
393 top:this.Px(-bbox.h-dy-dd-(bbox.y||0)-CONFIG.button.y),
394 opacity:0, filter:"alpha(opacity=0)"
395 }
396 },[["span",{isMathJax:true},"\u25BC"]]]]
397 );
398 if (bbox.width) {
399 frame.style.width = button.style.width = bbox.width;
400 frame.style.marginRight = button.style.marginRight = "-"+bbox.width;
401 frame.firstChild.style.width = bbox.width;
402 button.firstChild.style.left = "";
403 button.firstChild.style.right = this.Px(CONFIG.button.wx);
404 }
405 //
406 // Add the frame and button
407 //
408 span.parentNode.insertBefore(frame,span);
409 if (show) {span.parentNode.insertBefore(button,span)}
410 if (span.style) {span.style.position = "relative"} // so math is on top of hover frame
411 //
412 // Start the hover fade-in
413 //
414 this.ReHover(jax);
415 },
416 //
417 // Restart the hover fade in and fade-out timers
418 //
419 ReHover: function (jax) {
420 if (jax.hover.remove) {clearTimeout(jax.hover.remove)}
421 jax.hover.remove = setTimeout(CALLBACK(["UnHover",this,jax]),CONFIG.fadeoutDelay);
422 this.HoverFadeTimer(jax,CONFIG.fadeinInc);
423 },
424 //
425 // Start the fade-out
426 //
427 UnHover: function (jax) {
428 if (!jax.hover.nofade) {this.HoverFadeTimer(jax,-CONFIG.fadeoutInc,CONFIG.fadeoutStart)}
429 },
430 //
431 // Handle the fade-in and fade-out
432 //
433 HoverFade: function (jax) {
434 delete jax.hover.timer;
435 jax.hover.opacity = Math.max(0,Math.min(1,jax.hover.opacity + jax.hover.inc));
436 jax.hover.opacity = Math.floor(1000*jax.hover.opacity)/1000;
437 var frame = document.getElementById(jax.hover.id),
438 button = document.getElementById(jax.hover.id+"Menu");
439 frame.firstChild.style.opacity = jax.hover.opacity;
440 frame.firstChild.style.filter = "alpha(opacity="+Math.floor(100*jax.hover.opacity)+")";
441 if (button) {
442 button.firstChild.style.opacity = jax.hover.opacity;
443 button.firstChild.style.filter = frame.style.filter;
444 }
445 if (jax.hover.opacity === 1) {return}
446 if (jax.hover.opacity > 0) {this.HoverFadeTimer(jax,jax.hover.inc); return}
447 frame.parentNode.removeChild(frame);
448 if (button) {button.parentNode.removeChild(button)}
449 if (jax.hover.remove) {clearTimeout(jax.hover.remove)}
450 delete jax.hover;
451 },
452 //
453 // Set the fade to in or out (via inc) and start the timer, if needed
454 //
455 HoverFadeTimer: function (jax,inc,delay) {
456 jax.hover.inc = inc;
457 if (!jax.hover.timer) {
458 jax.hover.timer = setTimeout(CALLBACK(["HoverFade",this,jax]),(delay||CONFIG.fadeDelay));
459 }
460 },
461
462 //
463 // Handle a click on the menu button
464 //
465 HoverMenu: function (event) {
466 if (!event) {event = window.event}
467 return OUTPUT[this.jax].ContextMenu(event,this.math,true);
468 },
469
470 //
471 // Clear all hover timers
472 //
473 ClearHover: function (jax) {
474 if (jax.hover.remove) {clearTimeout(jax.hover.remove)}
475 if (jax.hover.timer) {clearTimeout(jax.hover.timer)}
476 HOVER.ClearHoverTimer();
477 delete jax.hover;
478 },
479
480 //
481 // Make a measurement in pixels
482 //
483 Px: function (m) {
484 if (Math.abs(m) < .006) {return "0px"}
485 return m.toFixed(2).replace(/\.?0+$/,"") + "px";
486 },
487
488 //
489 // Preload images so they show up with the menu
490 //
491 getImages: function () {
492 if (SETTINGS.discoverable) {
493 var menu = new Image();
494 menu.src = CONFIG.button.src;
495 }
496 }
497
498 };
499
500 //
501 // Handle touch events.
502 //
503 // Use double-tap-and-hold as a replacement for context menu event.
504 // Use double-tap as a replacement for double click.
505 //
506 var TOUCH = ME.Touch = {
507
508 last: 0, // time of last tap event
509 delay: 500, // delay time for double-click
510
511 //
512 // Check if this is a double-tap, and if so, start the timer
513 // for the double-tap and hold (to trigger the contextual menu)
514 //
515 start: function (event) {
516 var now = new Date().getTime();
517 var dblTap = (now - TOUCH.last < TOUCH.delay && TOUCH.up);
518 TOUCH.last = now; TOUCH.up = false;
519 if (dblTap) {
520 TOUCH.timeout = setTimeout(TOUCH.menu,TOUCH.delay,event,this);
521 event.preventDefault();
522 }
523 },
524
525 //
526 // Check if there is a timeout pending, i.e., we have a
527 // double-tap and were waiting to see if it is held long
528 // enough for the menu. Since we got the end before the
529 // timeout, it is a double-click, not a double-tap-and-hold.
530 // Prevent the default action and issue a double click.
531 //
532 end: function (event) {
533 var now = new Date().getTime();
534 TOUCH.up = (now - TOUCH.last < TOUCH.delay);
535 if (TOUCH.timeout) {
536 clearTimeout(TOUCH.timeout);
537 delete TOUCH.timeout; TOUCH.last = 0; TOUCH.up = false;
538 event.preventDefault();
539 return EVENT.Handler((event.touches[0]||event.touch),"DblClick",this);
540 }
541 },
542
543 //
544 // If the timeout passes without an end event, we issue
545 // the contextual menu event.
546 //
547 menu: function (event,math) {
548 delete TOUCH.timeout; TOUCH.last = 0; TOUCH.up = false;
549 return EVENT.Handler((event.touches[0]||event.touch),"ContextMenu",math);
550 }
551
552 };
553
554 /*
555 * //
556 * // Mobile screens are small, so use larger version of arrow
557 * //
558 * if (HUB.Browser.isMobile) {
559 * var arrow = CONFIG.styles[".MathJax_Hover_Arrow"];
560 * arrow.width = "25px"; arrow.height = "18px";
561 * CONFIG.button.x = -6;
562 * }
563 */
564
565 //
566 // Set up browser-specific values
567 //
568 HUB.Browser.Select({
569 MSIE: function (browser) {
570 var mode = (document.documentMode || 0);
571 var isIE8 = browser.versionAtLeast("8.0");
572 ME.msieBorderWidthBug = (document.compatMode === "BackCompat"); // borders are inside offsetWidth/Height
573 ME.msieEventBug = browser.isIE9; // must get event from window even though event is passed
574 ME.msieAlignBug = (!isIE8 || mode < 8); // inline-block spans don't rest on baseline
575 if (mode < 9) {EVENT.LEFTBUTTON = 1} // IE < 9 has wrong event.button values
576 },
577 Safari: function (browser) {
578 ME.safariContextMenuBug = true; // selection can be started by contextmenu event
579 },
580 Opera: function (browser) {
581 ME.operaPositionBug = true; // position is wrong unless border is used
582 },
583 Konqueror: function (browser) {
584 ME.noContextMenuBug = true; // doesn't produce contextmenu event
585 }
586 });
587
588 //
589 // Used in measuring zoom and hover positions
590 //
591 ME.topImg = (ME.msieAlignBug ?
592 HTML.Element("img",{style:{width:0,height:0,position:"relative"},src:"about:blank"}) :
593 HTML.Element("span",{style:{width:0,height:0,display:"inline-block"}})
594 );
595 if (ME.operaPositionBug) {ME.topImg.style.border="1px solid"}
596
597 //
598 // Get configuration from user
599 //
600 ME.config = CONFIG = HUB.CombineConfig("MathEvents",CONFIG);
601 var SETFRAME = function () {
602 var haze = CONFIG.styles[".MathJax_Hover_Frame"];
603 haze.border = CONFIG.frame.bwidth+"px solid "+CONFIG.frame.bcolor+" ! important";
604 haze["box-shadow"] = haze["-webkit-box-shadow"] =
605 haze["-moz-box-shadow"] = haze["-khtml-box-shadow"] =
606 "0px 0px "+CONFIG.frame.hwidth+" "+CONFIG.frame.hcolor;
607 };
608
609 //
610 // Queue the events needed for startup
611 //
612 CALLBACK.Queue(
613 HUB.Register.StartupHook("End Config",{}), // wait until config is complete
614 [SETFRAME],
615 ["getImages",HOVER],
616 ["Styles",AJAX,CONFIG.styles],
617 ["Post",HUB.Startup.signal,"MathEvents Ready"],
618 ["loadComplete",AJAX,"[MathJax]/extensions/MathEvents.js"]
619 );
620
621 })(MathJax.Hub,MathJax.HTML,MathJax.Ajax,MathJax.Callback,
622 MathJax.Localization,MathJax.OutputJax,MathJax.InputJax);