Showing Menu on double click

68 views
Skip to first unread message

P Purkayastha

unread,
Feb 15, 2012, 2:31:40 AM2/15/12
to mathja...@googlegroups.com
Hi,

   I am trying to make MathJax show the menu on double click. I am not disabling the right click option. I have written the following code which is loaded via the "extensions" in the config: http://pastebin.com/77sEy2zn

   By enabling a breakpoint on dblclick (via Dragonfly in Opera) I can see that this script is loaded and the function is called. It goes all the way to the statement "return HTMLCSS.ContextMenu.call(this,event,true);" and runs that statement. But I don't see the menu pop up.

   My questions are:
1. Is this the right function to call for showing the menu?
2. Is there something fundamentally wrong in the way I am trying to add this functionality?

  Just FYI: I am very new to javascript (a whole 1 day of "experience" from reading the MathJax code!), and I am also aware that this might clash with the zoom setting in Accessibility.js.

  Thanks and regards.

leathrum

unread,
Feb 15, 2012, 10:32:15 AM2/15/12
to MathJax Users
I want to take a closer look at your code later, but on a quick
cursory read, one thing does kind of stick out to me: why do you have
two "return" keywords effectively side-by-side in your script? I'm a
bit surprised that this even parsed.

Another thing I would recommend is that you push your script onto the
MathJax queue to be sure that it runs after everything else is done.
With the StartupHook call, that probably isn't 100% necessary, but it
is still good form.

I'm not familiar with the Dragonfly/Opera combination, so I'm not sure
how your breakpoint works, but you should try to get your code working
cross-browser. For this reason, I'm also concerned about your code
insisting on the HTML-CSS output jax. It should be possible to write
it more generally (but that does mean catching double-click events for
all browsers).

Once you get this working (and again, I will take a closer look later
to see if I can work out your bug), I would like to see you submit
this to the contributed extensions repository that we maintain on
Github.

P Purkayastha

unread,
Feb 15, 2012, 12:35:28 PM2/15/12
to mathja...@googlegroups.com
On 02/15/2012 11:32 PM, leathrum wrote:
> I want to take a closer look at your code later, but on a quick
> cursory read, one thing does kind of stick out to me: why do you have
> two "return" keywords effectively side-by-side in your script? I'm a
> bit surprised that this even parsed.

Thanks a lot for pointing that out! It is a typo. There should be a ;
after the first "return". Opera happily parsed it without complaining so
I never spotted this even after staring at/modifying the code for a good
2 hours!

> Another thing I would recommend is that you push your script onto the
> MathJax queue to be sure that it runs after everything else is done.
> With the StartupHook call, that probably isn't 100% necessary, but it
> is still good form.

I will have to read up on how to add to the queue.

> I'm not familiar with the Dragonfly/Opera combination, so I'm not sure
> how your breakpoint works, but you should try to get your code working
> cross-browser. For this reason, I'm also concerned about your code
> insisting on the HTML-CSS output jax. It should be possible to write
> it more generally (but that does mean catching double-click events for
> all browsers).

In Dragonfly you need to load the scripts for debugging and then select
DOM -> dblclick. Then a double-click anywhere on the webpage will walk
you through all the function calls step by step.

>
> Once you get this working (and again, I will take a closer look later
> to see if I can work out your bug), I would like to see you submit
> this to the contributed extensions repository that we maintain on
> Github.

Thanks for your comments. I will fix the typo tomorrow when I have
access to the machine (already past midnight here). I suspect that
fixing that typo might make the code "just work."

Davide P. Cervone

unread,
Feb 15, 2012, 3:15:21 PM2/15/12
to mathja...@googlegroups.com
It's not the missing semi-colon that is the problem. Semi-colons are
not mandatory, and often can be left our without problems, and this is
one such situation. If it had been the semi-colon, the extension
would not have compiled and you would have gotten errors about that in
the console log. Further, the file would not execute, so the
loadComplete() would not be called, and MathJax would timeout waiting
for that and give a "failed to load" error message in the lower
left. So it is not a syntax error.

The real problem is the if-then that includes the first return.

if (!HTMLCSS.noContextMenuBug) return

It says that if HTMLCSS.noContextMenuBug is NOT set, then return right
away. That means the call to ContextMenu() only is made if
noContextMenuBug is set. But that is only true for Konqueror (unless
you have set it yourself somewhere). So your

return HTMLCSS.ContextMenu.call(this,event,true);

call is never actually being reached. (It may have LOOKED like it
was, but that might be because of the missing semi-colon. I don't use
Dragonfly, so I don't know its quirks, but it might have highlighted
the wrong source line. Debuggers have been known to do that.)

If you put an alert() before the if-then and one after it, I bet you
will see that the ContextMenu() call is never being reached.

The real question is, what are you trying to accomplish, and why? Why
is using the contextual menu a problem and why do you want to link it
to the equations in another way? Perhaps we can suggest an
alternative approach.

> My questions are:
> 1. Is this the right function to call for showing the menu?

No. There is no API for accessing the menu from javascript, and you
are dealing with the undocumented internals of MathJax. There is no
guarantee that this will stay the same from version to version, and in
fact version 2.0 has a completely revamped event-processing
subsystem. Your changes here will not work with the new version.

> 2. Is there something fundamentally wrong in the way I am trying to
> add this functionality?

Well, I suppose the incorrect use of noContextMenuBug is fundamental.
Removing that should make it work in v1.1 of MathJax, but I suspect it
won't in v2.0.

> Just FYI: I am very new to javascript (a whole 1 day of "experience"
> from reading the MathJax code!), and I am also aware that this might
> clash with the zoom setting in Accessibility.js.

Note that users can use the MathJax menu to set the zoom trigger, so
it is not just the Accessible configuration that might be a problem.
Your changes will actually override the zoom trigger, so that menu
will appear to no longer work.

MathJax is probably not the best place to learn JavaScript, but it
appears that you are able to pick it up pretty well.

Good luck.

Davide

P Purkayastha

unread,
Feb 16, 2012, 12:30:47 AM2/16/12
to mathja...@googlegroups.com
On 02/16/2012 04:15 AM, Davide P. Cervone wrote:
> The real problem is the if-then that includes the first return.
>
> if (!HTMLCSS.noContextMenuBug) return
>
> It says that if HTMLCSS.noContextMenuBug is NOT set, then return right
> away. That means the call to ContextMenu() only is made if
> noContextMenuBug is set. But that is only true for Konqueror (unless you
> have set it yourself somewhere). So your
>
> return HTMLCSS.ContextMenu.call(this,event,true);
>
> call is never actually being reached.

Thanks a lot for your explanation. Indeed, I had misunderstood this
variable. Removing this if statement made the code work right away.

> The real question is, what are you trying to accomplish, and why? Why is
> using the contextual menu a problem and why do you want to link it to
> the equations in another way? Perhaps we can suggest an alternative
> approach.

The main request originated in a Sage mailing list:
https://groups.google.com/d/msg/sage-devel/OvdxfORM46w/XfPhDiTMgogJ

>> My questions are:
>> 1. Is this the right function to call for showing the menu?
>
> No. There is no API for accessing the menu from javascript, and you are
> dealing with the undocumented internals of MathJax. There is no
> guarantee that this will stay the same from version to version, and in
> fact version 2.0 has a completely revamped event-processing subsystem.
> Your changes here will not work with the new version.

Yes. I am aware that this might not work in the future versions of
MathJax. However, the extension is short enough that it can be rewritten
when we move to MathJax-2.0 (MathJax will be distributed with the Sage
web interface so that one can work completely offline).

> MathJax is probably not the best place to learn JavaScript, but it
> appears that you are able to pick it up pretty well.
>
> Good luck.
>
> Davide

Thanks again. The MathJax code seems very well written and well
commented, and so I had minimal trouble in figuring out how to add an
extension.

Davide P. Cervone

unread,
Feb 17, 2012, 8:55:37 AM2/17/12
to mathja...@googlegroups.com
> Thanks a lot for your explanation. Indeed, I had misunderstood this
> variable. Removing this if statement made the code work right away.

Great. Glad it worked out for you.

>> The real question is, what are you trying to accomplish, and why?
>> Why is
>> using the contextual menu a problem and why do you want to link it to
>> the equations in another way? Perhaps we can suggest an alternative
>> approach.
>
> The main request originated in a Sage mailing list:
> https://groups.google.com/d/msg/sage-devel/OvdxfORM46w/XfPhDiTMgogJ

OK, I've glanced through this briefly, and I should tell you that a
number of the problems cited are resolved in MathJax v2.0. That
includes the STIX font problem in OS X Lion. The "[Math Processing
Error]" message now also has the MathJax contextual menu so that you
can change the renderer again if you hit that. (In the past, you had
to remove the MathJax cookie for the site to reset the renderer.) The
image fonts selection is disabled in the font submenu if image fonts
have been disabled for MathJax. So I think most of the menu problems
that were reported have been taken care of.

> Thanks again. The MathJax code seems very well written and well
> commented, and so I had minimal trouble in figuring out how to add
> an extension.

You are very kind. There is much more that could be done in terms of
comments and documentation, but I'm glad you have been able to make
your way through it.

Davide


leat...@jsu.edu

unread,
Feb 29, 2012, 3:10:19 PM2/29/12
to mathja...@googlegroups.com
I thought it would be a good idea to revisit this thread now that MJ2.0 is out.  Three questions:

1)  Do the changes in the MJ contextual menu for v2.0 render this unnecessary?  (Davide said "I think most of the menu problems that were reported have been taken care of" in v2.0 -- have they?)
2)  Does the code posted earlier in this thread for making the menu appear on double-click still work in v2.0?  (Davide said "There is no guarantee that this will stay the same from version to version, and in fact version 2.0 has a completely revamped event-processing subsystem.  Your changes here will not work with the new version."  Is the code in fact broken in v2.0?  I have not tested it yet.)
3)  Is there still a demand for the menu to appear on a double-click?  (This might appear to repeat question 1, but I think it is a separate issue -- it came up in a specific context, but there may be a broader use case which could still use this double-click capability.)

Overall, what I am trying to determine is if there is any reason to rework, clean up, and document the code to prepare it for the contributed extensions repository.


P Purkayastha

unread,
Feb 29, 2012, 9:38:44 PM2/29/12
to mathja...@googlegroups.com
On 03/01/2012 04:10 AM, leat...@jsu.edu wrote:
> I thought it would be a good idea to revisit this thread now that MJ2.0
> is out. Three questions:
>
> 1) Do the changes in the MJ contextual menu for v2.0 render this
> unnecessary? (Davide said "I think most of the menu problems that were
> reported have been taken care of" in v2.0 -- have they?)

I am not sure. It depends on whether MJ-2 or MJ-1.1 will be included in
the next version of the Sage notebook.

> 2) Does the code posted earlier in this thread for making the menu
> appear on double-click still work in v2.0? (Davide said "There is no
> guarantee that this will stay the same from version to version, and in
> fact version 2.0 has a completely revamped event-processing subsystem.
> Your changes here will not work with the new version." Is the code in
> fact broken in v2.0? I have not tested it yet.)

Yes, I had a look at the v2.0 sources, and the code posted earlier will
not work.

> 3) Is there still a demand for the menu to appear on a double-click?
> (This might appear to repeat question 1, but I think it is a separate
> issue -- it came up in a specific context, but there may be a broader
> use case which could still use this double-click capability.)

The demand stemmed from I believe two reasons:
1. One of the lead developers felt that the right-click in macs wasn't
too reliable.
2. In the current notebook, users already use double-click to get to the
latex source code, so user-interface wise, there won't be much of a
change in the new notebook (once this extension is used).

> Overall, what I am trying to determine is if there is any reason to
> rework, clean up, and document the code to prepare it for the
> contributed extensions repository.

If I modify it for v2.0, then I will post here. For v1.1, I already have
a pull request to the Sage Notebook, with a working version of this
extension (only for HTML-CSS): https://github.com/jasongrout/sagenb/pull/2

Jason Grout

unread,
Mar 3, 2012, 5:46:06 AM3/3/12
to mathja...@googlegroups.com
On 2/29/12 8:38 PM, P Purkayastha wrote:
> On 03/01/2012 04:10 AM, leat...@jsu.edu wrote:
>> I thought it would be a good idea to revisit this thread now that MJ2.0
>> is out. Three questions:
>>
>> 1) Do the changes in the MJ contextual menu for v2.0 render this
>> unnecessary? (Davide said "I think most of the menu problems that were
>> reported have been taken care of" in v2.0 -- have they?)
>
> I am not sure. It depends on whether MJ-2 or MJ-1.1 will be included in
> the next version of the Sage notebook.

If Mathjax is included, it will (very hopefully) be 2.0. That said, the
patch hasn't been written or tested yet. We may not even get to upgrade
to mathjax in Sage 5.0---it depends on the timing, testing on the
mathjax branch, etc. The first goal is to get the jsmath version of the
new notebook into Sage 5.0. The next goal is to finish testing and
merging the mathjax branch (and upgrading to 2.0 in the process).

Thanks for your help in this!

Jason

P Purkayastha

unread,
Mar 7, 2012, 8:54:44 PM3/7/12
to mathja...@googlegroups.com

The version for MathJax-2.0 is here: http://goo.gl/cnjeN

Davide P. Cervone

unread,
Mar 8, 2012, 4:30:51 PM3/8/12
to mathja...@googlegroups.com
Overall, what I am trying to determine is if there is any reason to
rework, clean up, and document the code to prepare it for the
contributed extensions repository.

If I modify it for v2.0, then I will post here. For v1.1, I already have
a pull request to the Sage Notebook, with a working version of this
extension (only for HTML-CSS): https://github.com/jasongrout/sagenb/pull/2

The version for MathJax-2.0 is here: http://goo.gl/cnjeN

OK, glad you got a new version working.  Here are a couple of suggestions:

1.  You don't need the MathJax.Callback.Queue, as that creates a private queue that you can use to synchronize actions, but you only provide one.  That means the queue is unneeded and it can just be removed.

2.  You don't really need the extra (function () {...})() closures.  These are used in some MathJax examples to isolate local variables from the window's namespace, but since you are already within a function definition, that is not needed here.

3.  You can look up the Double-Click menu item directly rather than looking up the trigger menu and then indexing into the submenu by hand.

A slimmed-down version would be:

/***************************************************************************
* *
* DoubleClickEvent.js *
* *
* This makes the menu pop up on Double Click with left mouse. To do this, *
* we redefine the DblClick object inside MathEvents to send a fake call as*
* "ContextMenu" to MathEvents.Handler. This is defined only for a browser *
* that is not on a mobile device. *
* *
* Since this interferes with the zoom setting in Accessibility.js, that *
* zoom setting will no longer work. *
* *
***************************************************************************
*/

MathJax.Hub.Register.StartupHook("MathEvents Ready", function () {
  var HUB = MathJax.Hub;
  //
  // Redefine DblClick only if it is not a mobile device. For a mobile
  // device MathJax-2.0 has double tap and hold for ContextMenu.
  //
  if (!HUB.Browser.isMobile) {
      var EVENT = MathJax.Extension.MathEvents.Event;
    EVENT.DblClick = function(event){
      return EVENT.Handler(event,"ContextMenu",this);
    }
      //
    // Disable the DblClick option from the menu. This is taken mostly
    // from MathMenu.js
    //
      var MENU = MathJax.Menu, MENUSETTINGS = HUB.config.menuSettings;
    MENU.menu.Find("Math Settings", "Zoom Trigger","Double-Click").disabled = true;
    if (MENUSETTINGS.zoom === "Double-Click") {MENUSETTINGS.zoom = "None"}
    }

    HUB.Startup.signal.Post("DoubleClickEvent Ready");

});

MathJax.Ajax.loadComplete("[MathJax]/../sage/js/DoubleClickEvent.js");


Hope that helps.

Davide


P Purkayastha

unread,
Mar 8, 2012, 4:37:16 PM3/8/12
to mathja...@googlegroups.com
> = *true*;

>
> if (MENUSETTINGS.zoom === "Double-Click") {MENUSETTINGS.zoom = "None"}
> }
>
> HUB.Startup.signal.Post("DoubleClickEvent Ready");
>
>
> });
>
> MathJax.Ajax.loadComplete("[MathJax]/../sage/js/DoubleClickEvent.js");
>
>
> Hope that helps.
>
> Davide

Thanks a lot for your thorough review! This is very helpful.

Reply all
Reply to author
Forward
0 new messages