|
1 YUI.add('event-outside', function (Y, NAME) { |
|
2 |
|
3 /** |
|
4 * Outside events are synthetic DOM events that fire when a corresponding native |
|
5 * or synthetic DOM event occurs outside a bound element. |
|
6 * |
|
7 * The following outside events are pre-defined by this module: |
|
8 * <ul> |
|
9 * <li>blur</li> |
|
10 * <li>change</li> |
|
11 * <li>click</li> |
|
12 * <li>dblclick</li> |
|
13 * <li>focus</li> |
|
14 * <li>keydown</li> |
|
15 * <li>keypress</li> |
|
16 * <li>keyup</li> |
|
17 * <li>mousedown</li> |
|
18 * <li>mousemove</li> |
|
19 * <li>mouseout</li> |
|
20 * <li>mouseover</li> |
|
21 * <li>mouseup</li> |
|
22 * <li>select</li> |
|
23 * <li>submit</li> |
|
24 * </ul> |
|
25 * |
|
26 * Define new outside events with |
|
27 * <code>Y.Event.defineOutside(eventType);</code>. |
|
28 * By default, the created synthetic event name will be the name of the event |
|
29 * with "outside" appended (e.g. "click" becomes "clickoutside"). If you want |
|
30 * a different name for the created Event, pass it as a second argument like so: |
|
31 * <code>Y.Event.defineOutside(eventType, "yonderclick")</code>. |
|
32 * |
|
33 * This module was contributed by Brett Stimmerman, promoted from his |
|
34 * gallery-outside-events module at |
|
35 * http://yuilibrary.com/gallery/show/outside-events |
|
36 * |
|
37 * @module event |
|
38 * @submodule event-outside |
|
39 * @author brettstimmerman |
|
40 * @since 3.4.0 |
|
41 */ |
|
42 |
|
43 // Outside events are pre-defined for each of these native DOM events |
|
44 var nativeEvents = [ |
|
45 'blur', 'change', 'click', 'dblclick', 'focus', 'keydown', 'keypress', |
|
46 'keyup', 'mousedown', 'mousemove', 'mouseout', 'mouseover', 'mouseup', |
|
47 'select', 'submit' |
|
48 ]; |
|
49 |
|
50 /** |
|
51 * Defines a new outside event to correspond with the given DOM event. |
|
52 * |
|
53 * By default, the created synthetic event name will be the name of the event |
|
54 * with "outside" appended (e.g. "click" becomes "clickoutside"). If you want |
|
55 * a different name for the created Event, pass it as a second argument like so: |
|
56 * <code>Y.Event.defineOutside(eventType, "yonderclick")</code>. |
|
57 * |
|
58 * @method defineOutside |
|
59 * @param {String} event DOM event |
|
60 * @param {String} name (optional) custom outside event name |
|
61 * @static |
|
62 * @for Event |
|
63 */ |
|
64 Y.Event.defineOutside = function (event, name) { |
|
65 name = name || (event + 'outside'); |
|
66 |
|
67 var config = { |
|
68 |
|
69 on: function (node, sub, notifier) { |
|
70 sub.handle = Y.one('doc').on(event, function(e) { |
|
71 if (this.isOutside(node, e.target)) { |
|
72 e.currentTarget = node; |
|
73 notifier.fire(e); |
|
74 } |
|
75 }, this); |
|
76 }, |
|
77 |
|
78 detach: function (node, sub, notifier) { |
|
79 sub.handle.detach(); |
|
80 }, |
|
81 |
|
82 delegate: function (node, sub, notifier, filter) { |
|
83 sub.handle = Y.one('doc').delegate(event, function (e) { |
|
84 if (this.isOutside(node, e.target)) { |
|
85 notifier.fire(e); |
|
86 } |
|
87 }, filter, this); |
|
88 }, |
|
89 |
|
90 isOutside: function (node, target) { |
|
91 return target !== node && !target.ancestor(function (p) { |
|
92 return p === node; |
|
93 }); |
|
94 } |
|
95 }; |
|
96 config.detachDelegate = config.detach; |
|
97 |
|
98 Y.Event.define(name, config); |
|
99 }; |
|
100 |
|
101 // Define outside events for some common native DOM events |
|
102 Y.Array.each(nativeEvents, function (event) { |
|
103 Y.Event.defineOutside(event); |
|
104 }); |
|
105 |
|
106 |
|
107 }, '@VERSION@', {"requires": ["event-synthetic"]}); |