forked from cowboy/jquery-outside-events
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.ba-outside-events.js
71 lines (54 loc) · 2.23 KB
/
jquery.ba-outside-events.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*!
* jQuery live outside events - v0.1 - 14/05/2011
*
* Remake of a slick project by Ben Alman:
* http://benalman.com/projects/jquery-outside-events-plugin/
*
* This project is inferior in every way to the work of Ben Alman
* but it allows for attaching live events.
*
*/
(function(jQuery, document) {
var bindings = {};
var live_handler = {};
jQuery.fn.outside = function(event_name, attach_method, handler) {
if (attach_method == 'live' || attach_method == 'bind') {
if (live_handler[event_name] == null) {
live_handler[event_name] = liveOutsideHandlerFactory();
$(document).bind(event_name, live_handler[event_name]);
}
bindings[event_name] = bindings[event_name] || [];
bindings[event_name].push({
targetSelector: attach_method == 'live' ? this.selector : null,
targetElements: attach_method == 'bind' ? this : null,
without: [],
handler: handler
});
}
if (attach_method == 'die' || attach_method == 'unbind') {
var _that = this;
$.each(bindings[event_name], function(idx, elem) {
elem.without.push(attach_method == 'die' ? _that.selector : _that);
});
}
function liveOutsideHandlerFactory() {
return function(event) {
var event_name = event.type;
if (bindings[event_name]) {
$.each(bindings[event_name], function(idx, binding) {
var handler = binding.handler;
var target_elements = $(binding.targetSelector || binding.targetElements);
$.each(binding.without, function(idx, elem) {
target_elements = target_elements.not($(elem));
});
target_elements.each(function(idx, elem) {
if (elem != event.target && !$(elem).has(event.target).length) {
handler.apply(elem, [event]);
}
});
});
}
}
}
};
})(jQuery, document);