2 * L.DomEvent contains functions for working with DOM events.
6 /* inpired by John Resig, Dean Edwards and YUI addEvent implementations */
7 addListener: function(/*HTMLElement*/ obj
, /*String*/ type
, /*Function*/ fn
, /*Object*/ context
) {
8 var id
= L
.Util
.stamp(fn
);
11 return fn
.call(context
|| obj
, e
|| L
.DomEvent
._getEvent());
14 if (L
.Browser
.touch
&& (type
== 'dblclick') && this.addDoubleTapListener
) {
15 this.addDoubleTapListener(obj
, handler
, id
);
16 } else if ('addEventListener' in obj
) {
17 if (type
== 'mousewheel') {
18 obj
.addEventListener('DOMMouseScroll', handler
, false);
19 obj
.addEventListener(type
, handler
, false);
20 } else if ((type
== 'mouseenter') || (type
== 'mouseleave')) {
21 var originalHandler
= handler
,
22 newType
= (type
== 'mouseenter' ? 'mouseover' : 'mouseout');
23 handler = function(e
) {
24 if (!L
.DomEvent
._checkMouse(obj
, e
)) return;
25 return originalHandler(e
);
27 obj
.addEventListener(newType
, handler
, false);
29 obj
.addEventListener(type
, handler
, false);
31 } else if ('attachEvent' in obj
) {
32 obj
.attachEvent("on" + type
, handler
);
35 obj
['_leaflet_' + type
+ id
] = handler
;
38 removeListener: function(/*HTMLElement*/ obj
, /*String*/ type
, /*Function*/ fn
) {
39 var id
= L
.Util
.stamp(fn
),
40 key
= '_leaflet_' + type
+ id
;
43 if (L
.Browser
.mobileWebkit
&& (type
== 'dblclick') && this.removeDoubleTapListener
) {
44 this.removeDoubleTapListener(obj
, id
);
45 } else if ('removeEventListener' in obj
) {
46 if (type
== 'mousewheel') {
47 obj
.removeEventListener('DOMMouseScroll', handler
, false);
48 obj
.removeEventListener(type
, handler
, false);
49 } else if ((type
== 'mouseenter') || (type
== 'mouseleave')) {
50 obj
.removeEventListener((type
== 'mouseenter' ? 'mouseover' : 'mouseout'), handler
, false);
52 obj
.removeEventListener(type
, handler
, false);
54 } else if ('detachEvent' in obj
) {
55 obj
.detachEvent("on" + type
, handler
);
60 _checkMouse: function(el
, e
) {
61 var related
= e
.relatedTarget
;
63 if (!related
) return true;
66 while (related
&& (related
!= el
)) {
67 related
= related
.parentNode
;
69 } catch(err
) { return false; }
71 return (related
!= el
);
74 _getEvent: function()/*->Event*/ {
77 var caller
= arguments
.callee
.caller
;
79 e
= caller
['arguments'][0];
80 if (e
&& Event
== e
.constructor) { break; }
81 caller
= caller
.caller
;
87 stopPropagation: function(/*Event*/ e
) {
88 if (e
.stopPropagation
) {
91 e
.cancelBubble
= true;
95 disableClickPropagation: function(/*HTMLElement*/ el
) {
96 L
.DomEvent
.addListener(el
, 'mousedown', L
.DomEvent
.stopPropagation
);
97 L
.DomEvent
.addListener(el
, 'click', L
.DomEvent
.stopPropagation
);
98 L
.DomEvent
.addListener(el
, 'dblclick', L
.DomEvent
.stopPropagation
);
101 preventDefault: function(/*Event*/ e
) {
102 if (e
.preventDefault
) {
105 e
.returnValue
= false;
110 L
.DomEvent
.preventDefault(e
);
111 L
.DomEvent
.stopPropagation(e
);
114 getMousePosition: function(e
, container
) {
115 var x
= e
.pageX
? e
.pageX
: e
.clientX
+
116 document
.body
.scrollLeft
+ document
.documentElement
.scrollLeft
,
117 y
= e
.pageY
? e
.pageY
: e
.clientY
+
118 document
.body
.scrollTop
+ document
.documentElement
.scrollTop
,
119 pos
= new L
.Point(x
, y
);
122 pos
.subtract(L
.DomUtil
.getCumulativeOffset(container
)) : pos
);
125 getWheelDelta: function(e
) {
127 if (e
.wheelDelta
) { delta
= e
.wheelDelta
/120; }
128 if (e
.detail
) { delta
= -e
.detail
/3; }