All pastes #767465 Raw Edit

Gimme event capturing

public javascript v1 · immutable
·published 2007-11-09 19:50 UTC
rendered paste body
	Gimme.ext.captureMouseEvents = function()	{		var elem = this.entities[0];		if (typeof elem.setCapture !== 'undefined')		{			elem.setCapture();		}		else		{			if (g_capturing)			{				this.releaseMouseEvents();			}			g_capturing =			{				elem: elem				,reroute: function(e)				{					e.stopPropagation();										var synthEvent = document.createEvent('MouseEvents');					synthEvent.initMouseEvent(						e.type,							// type						true,							// can bubble?						true,							// cancelable?						window,							// always window here						1,								// mouse click count						e.pageX,						// event's screen x coordinate (copied from the passed in event)						e.pageY,						// event's screen y coordinate (copied from the passed in event)						e.clientX,						// event's client x coordinate (copied from the passed in event)						e.clientY,						// event's client y coordinate (copied from the passed in event)						e.ctrlKey,						// whether or not the ctrl key was pressed during the event (copied from the passed in event)						e.altKey,						// whether or not the alt key was pressed during the event (copied from the passed in event)						e.shiftKey,						// whether or not the shift key was pressed during the event (copied from the passed in event)						e.metaKey,						// whether or not the meta key was pressed during the event (copied from the passed in event)						e.button,						// indicates which button (if any) caused the mouse event (copied from the passed in event)						e.relatedTarget					// relatedTarget (only applicable for 'mouseover' and 'mouseout' events)					);										document.removeEventListener(e.type, g_capturing.reroute, true);					syntheticEvent.originalTarget = e.target;					elem.dispatchEvent(syntheticEvent);					document.addEventListener(e.type, g_capturing.reroute, true);					delete syntheticEvent.originalTarget;								}			}						document.addEventListener('mouseover', g_capturing.reroute, true);			document.addEventListener('mouseout', g_capturing.reroute, true);			document.addEventListener('mousemove', g_capturing.reroute, true);			document.addEventListener('mouseup', g_capturing.reroute, true);			document.addEventListener('mousedown', g_capturing.reroute, true);			document.addEventListener('click', g_capturing.reroute, true);			document.addEventListener('dblclick', g_capturing.reroute, true);		}	};		Gimme.ext.releaseMouseEvents = function()	{		var elem;		if (g_capturing !== null)		{			elem = g_capturing.elem;			if (typeof elem.releaseCapture !== 'undefined')			{				elem.releaseCapture();			}			else			{				document.removeEventListener('mouseover', g_capturing.reroute, true);				document.removeEventListener('mouseout', g_capturing.reroute, true);				document.removeEventListener('mousemove', g_capturing.reroute, true);				document.removeEventListener('mouseup', g_capturing.reroute, true);				document.removeEventListener('mousedown', g_capturing.reroute, true);				document.removeEventListener('click', g_capturing.reroute, true);				document.removeEventListener('dblclick', g_capturing.reroute, true);			}						g_capturing.elem = g_capturing.reroute = g_capturing = null;		}	};