jetpack.menu.context.page.add api

0 views
Skip to first unread message

ahmedre

unread,
Nov 13, 2009, 2:25:10 AM11/13/09
to mozilla-labs-jetpack
hi,
first off, thanks for jetpack, it's awesome!!

i have a quick question, and while i realize this maybe very simple, i
am asking it because:
1. the demo doesn't taken this into account,
2. the documentation doesn't either,
3. jetpacks published to the jetpack demos don't take this into
account either.

so that having been said, i was wondering:
1. how do you add a context menu *only* if some text is selected. i
know how to do this if an image or a link is selected (using .on()),
or regardless of whether or not something is selected (just using
add), but how can i say "if any text is selected, add a menu" (like
search google or the ubiquity search option)? - the examples for the
menu documentation show a use of jetpack.menu.context.page.replace to
accomplish this by replacing the search menu (which doesn't work for
me btw on firefox 3.5.5 on mac while using jetpack 0.6.1 - no errors
or anything in the console, just the menu is never replaced)...

2. how can i do the above within a text area? i am guessing i need
to find textareas and then add a context menu to them, but how do i
ascertain that the menu only shows up if text is selected?

thanks!
-ahmed

Hernan Rodriguez Colmeiro

unread,
Nov 13, 2009, 8:23:18 AM11/13/09
to mozilla-la...@googlegroups.com
On Fri, Nov 13, 2009 at 04:25, ahmedre <ahm...@gmail.com> wrote:
>
> so that having been said, i was wondering:
> 1.  how do you add a context menu *only* if some text is selected.  i
> know how to do this if an image or a link is selected (using .on()),
> or regardless of whether or not something is selected (just using
> add), but how can i say "if any text is selected, add a menu" (like
> search google or the ubiquity search option)? - the examples for the
> menu documentation show a use of jetpack.menu.context.page.replace to
> accomplish this by replacing the search menu (which doesn't work for
> me btw on firefox 3.5.5 on mac while using jetpack 0.6.1 - no errors
> or anything in the console, just the menu is never replaced)...
>

Maybe beforeShow() can help you? As the wiki says, is "A function
invoked just before the menu is shown".
https://wiki.mozilla.org/Labs/Jetpack/JEP/14#beforeShow

Hernán

Drew Willcoxon

unread,
Nov 13, 2009, 12:48:07 PM11/13/09
to mozilla-la...@googlegroups.com
Yes, Hernán is right. Try this:

jetpack.future.import("menu");
jetpack.future.import("selection");
jetpack.menu.context.page.beforeShow = function (menu, context) {
if (jetpack.selection.text)
menu.set("Selection!");
else
menu.reset();
};

Unfortunately jetpack.selection doesn't work for textareas, only page
content. You can work around that, though:

jetpack.menu.context.page.on("textarea").beforeShow =
function (menu, context) {
var textarea = context.node;
var sel = textarea.value.substring(textarea.selectionStart,
textarea.selectionEnd);
if (sel)
menu.set("Selection!");
else
menu.reset();
};

Drew

ahmedre

unread,
Nov 13, 2009, 1:45:53 PM11/13/09
to mozilla-la...@googlegroups.com
awesome, thanks guys! i'll give this a shot later today.
thanks,
-ahmed
--
http://whatstheplot.com/blog

Aza

unread,
Nov 13, 2009, 10:12:15 PM11/13/09
to mozilla-la...@googlegroups.com
Two thoughts:

I wonder if we should add a pseudo class for having selections. That is, ...on(":selection") which only fires for a selection.


Unfortunately jetpack.selection doesn't work for textareas, only page
content.  You can work around that, though:

Drew: Can you file a bug about this? That should be fixed :)

 

 
-- aza | ɐzɐ --


On Fri, Nov 13, 2009 at 9:48 AM, Drew Willcoxon <a...@mozilla.com> wrote:
Reply all
Reply to author
Forward
0 new messages