Bit nicer source flow.
[redakcja.git] / src / redakcja / static / js / book_text / jquery.eventdelegation.js
1 /*
2  * jQuery Event Delegation Plugin - jquery.eventdelegation.js
3  * Fast flexible event handling
4  *
5  * January 2008 - Randy Morey (http://dev.distilldesign.com/)
6  */
7
8 (function ($) {
9         /* setup list of allowed events for event delegation
10          * only events that bubble are appropriate
11          */
12         var allowed = {};
13         $.each([
14                 'click',
15                 'dblclick',
16                 'mousedown',
17                 'mouseup',
18                 'mousemove',
19                 'mouseover',
20                 'mouseout',
21                 'keydown',
22                 'keypress',
23                 'keyup'
24                 ], function(i, eventName) {
25                         allowed[eventName] = true;
26         });
27
28         $.fn.extend({
29                 delegate: function (event, selector, f) {
30                         return $(this).each(function () {
31                                 if (allowed[event])
32                                         $(this).bind(event, function (e) {
33                                                 var el = $(e.target),
34                                                         result = false;
35
36                                                 while (!$(el).is('body')) {
37                                                         if ($(el).is(selector)) {
38                                                                 result = f.apply($(el)[0], [e]);
39                                                                 if (result === false)
40                                                                         e.preventDefault();
41                                                                 return;
42                                                         }
43
44                                                         el = $(el).parent();
45                                                 }
46                                         });
47                         });
48                 },
49                 undelegate: function (event) {
50                         return $(this).each(function () {
51                                 $(this).unbind(event);
52                         });
53                 }
54         });
55 })(jQuery);