5
<title>Outside Events</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Outside Events</h1>
18
<a href="#toc" class="jump">Jump to Table of Contents</a>
22
<div class="yui3-u-3-4">
24
<div class="content"><div class="intro">
25
<p>The <code>event-outside</code> module adds a <a href="predefined">suite of
26
events</a> based on activity occuring <em>outside</em> the subscribed
27
elements. For example, the "clickoutside" event will fire only if a click
28
occurred on an element <em>other than</em> the Node subscribed or one of
31
<p>The module also adds a <code>Y.Event.defineOutside(...)</code> method to <a
32
href="#defineoutside">create additional outside events</a>.</p>
34
<p>This module was contributed by <a
35
href="https://github.com/brettstimmerman">Brett Stimmerman</a>, inspired
36
by <a href="http://benalman.com/projects/jquery-outside-events-plugin/">Ben
37
Alman's Outside Events jQuery plugin</a>.</p>
40
<h2 id="not-me-those-other-elements">Not me. Those other elements</h2>
42
<p>It's a common UX pattern to close popups or trigger save or cancel actions when users do something in another area of a web page. This family of events makes setting up that behavior easy.</p>
44
<pre class="code prettyprint">node.on('clickoutside', function () {
45
this.hide('fadeOut');
48
survey.on('keyupoutside', heyYoureNotDoneYet);
50
// hide the overlay if the page focus moves somewhere outside the overlay's
51
// content area.
52
overlay.get('boundingBox').on('focusoutside', overlay.hide, overlay);</pre>
55
<h2 id="how-they-work">How they work</h2>
57
<p>When an outside event subscription is made on an element, the actual
58
subscription created is a <code>document</code> level subscription for the corresponding
59
DOM event. When a triggering event occurs on the page and bubbles up to the
60
<code>document</code>, its <code>e.target</code> is compared to the outside event subscriber. If the
61
event originated from an element outside the subscriber, the outside event
62
subscribers are executed.</p>
64
<p>An originating target is considered outside the subscriber if it is not the subscriber itself or any of the subscriber's descendants.</p>
66
<h2 id="predefined"><code>*outside</code></h2>
68
<p>The naming convention for outside events is <code><em><event></em>outside</code>.</p>
70
<p>The module creates the following events by default:</p>
85
<ul class="yui3-g" id="eventlist">
88
<li><code><em>mousedown</em>outside</code></li>
89
<li><code><em>mouseup</em>outside</code></li>
90
<li><code><em>mouseover</em>outside</code></li>
91
<li><code><em>mouseout</em>outside</code></li>
92
<li><code><em>mousemove</em>outside</code></li>
97
<li><code><em>click</em>outside</code></li>
98
<li><code><em>dblclick</em>outside</code></li>
99
<li><code><em>keydown</em>outside</code></li>
100
<li><code><em>keyup</em>outside</code></li>
101
<li><code><em>keypress</em>outside</code></li>
106
<li><code><em>focus</em>outside</code></li>
107
<li><code><em>blur</em>outside</code></li>
108
<li><code><em>change</em>outside</code></li>
109
<li><code><em>select</em>outside</code></li>
110
<li><code><em>submit</em>outside</code></li>
115
<h2 id="defineoutside">Create more outside events</h2>
117
<p>Use the module's <code>Y.Event.defineOutside( triggeringEvent, [alternateName] )</code> method to create more outside
120
<pre class="code prettyprint">// Create a `touchstartoutside` event
121
Y.Event.defineOutside('touchstart');
123
// Create an outside event for another synthetic event and give it
124
// a different name.
125
Y.Event.defineOutside('tripleclick', 'omgletmeout');
127
// would have been tripleclickoutside
128
gooeymess.on('omgletmeout', okYouCanGo);</pre>
131
<h2 id="caveats">Caveats</h2>
133
<p>Outside events require DOM events to bubble to the <code>document</code> so the following caveats apply to their use:</p>
137
Separate subscriptions for the triggering event added to any element
138
below the <code>document</code> will execute before the outside event.
141
If a subcriber from #1 calls <code>e.stopPropagation()</code>, the outside event
145
"outside" is determined by DOM hierarchy, not visual placement of an
146
element, so if a child element of the outside subscriber is placed
147
elsewhere on the page, clicking on that child will not trigger the
151
Some DOM events do not bubble, and some (e.g. <code>submit</code> and <code>reset</code>)
152
bubble only in certain browsers. Unless a workaround synthetic event
153
such as <a href="focus.html"><code>event-focus</code></a> is in place,
154
outside versions of these events won't fire.
161
<div class="yui3-u-1-4">
162
<div class="sidebar">
164
<div id="toc" class="sidebox">
166
<h2 class="no-toc">Table of Contents</h2>
172
<a href="#not-me-those-other-elements">Not me. Those other elements</a>
175
<a href="#how-they-work">How they work</a>
178
<a href="#predefined"><code>*outside</code></a>
181
<a href="#defineoutside">Create more outside events</a>
184
<a href="#caveats">Caveats</a>
192
<div class="sidebox">
194
<h2 class="no-toc">Examples</h2>
198
<ul class="examples">
201
<li data-description="Use the Event Utility to attach simple DOM event handlers.">
202
<a href="basic-example.html">Simple DOM Events</a>
207
<li data-description="Using the synthetic event API to create a DOM event that fires in response to arrow keys being pressed.">
208
<a href="synth-example.html">Creating an Arrow Event for DOM Subscription</a>
213
<li data-description="Supporting cross-device swipe gestures, using the event-move gesture events">
214
<a href="swipe-example.html">Supporting A Swipe Left Gesture</a>
234
<div class="sidebox">
236
<h2 class="no-toc">Examples That Use This Component</h2>
240
<ul class="examples">
249
<li data-description="Shows how to extend the base widget class, to create your own Widgets.">
250
<a href="../widget/widget-extend.html">Extending the Base Widget Class</a>
255
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States.">
256
<a href="../node-focusmanager/node-focusmanager-3.html">Accessible Menu Button</a>
261
<li data-description="Use IO to request data over HTTP.">
262
<a href="../io/get.html">HTTP GET to request data</a>
267
<li data-description="Example Photo Browser application.">
268
<a href="../dd/photo-browser.html">Photo Browser</a>
273
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation.">
274
<a href="../dd/portal-drag.html">Portal Style Example</a>
287
<script src="../assets/vendor/prettify/prettify-min.js"></script>
288
<script>prettyPrint();</script>