Script and makefile adjustments for updating extlib
[larjonas-mediagoblin.git] / extlib / leaflet / src / dom / DomEvent.js
blobbcabebc259bf4439ae1e3acf205d8e54c25c9257
1 /*
2 * L.DomEvent contains functions for working with DOM events.
3 */
5 L.DomEvent = {
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);
10 function handler(e) {
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);
28 } else {
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;
41 handler = obj[key];
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);
51 } else {
52 obj.removeEventListener(type, handler, false);
54 } else if ('detachEvent' in obj) {
55 obj.detachEvent("on" + type, handler);
57 obj[key] = null;
60 _checkMouse: function(el, e) {
61 var related = e.relatedTarget;
63 if (!related) return true;
65 try {
66 while (related && (related != el)) {
67 related = related.parentNode;
69 } catch(err) { return false; }
71 return (related != el);
74 _getEvent: function()/*->Event*/ {
75 var e = window.event;
76 if (!e) {
77 var caller = arguments.callee.caller;
78 while (caller) {
79 e = caller['arguments'][0];
80 if (e && Event == e.constructor) { break; }
81 caller = caller.caller;
84 return e;
87 stopPropagation: function(/*Event*/ e) {
88 if (e.stopPropagation) {
89 e.stopPropagation();
90 } else {
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) {
103 e.preventDefault();
104 } else {
105 e.returnValue = false;
109 stop: function(e) {
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);
121 return (container ?
122 pos.subtract(L.DomUtil.getCumulativeOffset(container)) : pos);
125 getWheelDelta: function(e) {
126 var delta = 0;
127 if (e.wheelDelta) { delta = e.wheelDelta/120; }
128 if (e.detail) { delta = -e.detail/3; }
129 return delta;