Labs/Jetpack/Reboot/JEP/112: Difference between revisions

Jump to navigation Jump to search
→‎Specifying Contexts: Rearranged, clarified
(→‎Specifying Contexts: Forgot about array contexts)
(→‎Specifying Contexts: Rearranged, clarified)
Line 93: Line 93:


Items should be added to the context menu, as its name implies, only when some particular context arises.  Context can be related to page content or the page itself, but it should never be external to the page.  (See [[#Non-Use Cases]] for examples.)
Items should be added to the context menu, as its name implies, only when some particular context arises.  Context can be related to page content or the page itself, but it should never be external to the page.  (See [[#Non-Use Cases]] for examples.)
Instead of requiring consumers to manually add and remove items when particular contexts arise, this proposal introduces the notion that items are bound to one or more contexts, much as event listeners are bound to events.  This binding occurs through an item's <tt>context</tt> property.  When the context menu is invoked, all of the items bound to the context giving rise to the menu are added to the menu.  If no items are bound to the context, no items are added to the menu.


Contexts may be specified with any of the following types:
Contexts may be specified with any of the following types:
Line 102: Line 104:
; function: An arbitrary predicate.  The context arises when the function returns true.  The function is passed an object describing the current context.  See [[#Context_Descriptions]].
; function: An arbitrary predicate.  The context arises when the function returns true.  The function is passed an object describing the current context.  See [[#Context_Descriptions]].


; array: An array of any of the other types.
; array: An array of any of the other types.  The context arises when any context in the array does.
 
Instead of requiring consumers to manually add and remove items when particular contexts arise, this proposal introduces the notion that items are bound to one or more contexts, much as event listeners are bound to eventsThis binding occurs through an item's <tt>context</tt> property.  When the context menu is invoked, all of the items bound to the context giving rise to the menu are added to the menu.  If no items are bound to the context, no items are added to the menu.


The <tt>context</tt> property is a collection, similar to event listener collections common throughout Jetpack's various APIs.  A single context may be bound by assigning one of the above types to the <tt>context</tt> property either on construction or after:
An item's <tt>context</tt> property is a collection, similar to event listener collections common throughout Jetpack's various APIs.  A single context may be bound by assigning one of the above types to the <tt>context</tt> property either on construction or after:


<pre class="brush:js;toolbar:false;">
<pre class="brush:js;toolbar:false;">
var item = new contextMenu.Item({ context: "img" });
var item = contextMenu.Item({ context: "img" });
item.context = "img";
item.context = "img";
</pre>
</pre>
Line 116: Line 116:


<pre class="brush:js;toolbar:false;">
<pre class="brush:js;toolbar:false;">
var item = new contextMenu.Item({ context: ["img", "a[href]"] });
var item = contextMenu.Item({ context: ["img", "a[href]"] });
item.context = ["img", "a[href]"];
item.context = ["img", "a[href]"];
</pre>
</pre>
Confirmed users
764

edits

Navigation menu