From 3dd7bd5b4bd41b0ed20ecb7bfe175e71fce40dd8 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Tue, 16 Oct 2007 03:19:38 +0000 Subject: [PATCH] prototype: Cross-browser Event#isLeftClick with the addition of is(Middle|Right)Click. Closes #7520. --- CHANGELOG | 2 + src/event.js | 82 ++++++++----- test/functional/event.html | 241 +++++++++++++++++++++++++++++++++++++ 3 files changed, 294 insertions(+), 31 deletions(-) create mode 100644 test/functional/event.html diff --git a/CHANGELOG b/CHANGELOG index d248bcb..dffc0a6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Cross-browser Event#isLeftClick with the addition of is(Middle|Right)Click. Closes #7520. [Christophe Porteneuve, Richard Quadling, Mislav Marohnić] + * Changed Selector to use the non-XPath approach for Safari 3 because of bugs in its version of document.evaluate. [Andrew Dupont] * Changed the Selector regex that tests whether the selector can be expressed in XPath; added :checked, since XPath can't detect all scenarios in which an is checked. Fixes #9776. [StelardActek, kangax, Andrew Dupont] diff --git a/src/event.js b/src/event.js index c8a1dcf..30b4e8a 100644 --- a/src/event.js +++ b/src/event.js @@ -29,39 +29,59 @@ Object.extend(Event, { } }); -Event.Methods = { - element: function(event) { - var node = event.target; - return Element.extend(node.nodeType == Node.TEXT_NODE ? node.parentNode : node); - }, - - findElement: function(event, expression) { - var element = Event.element(event); - return element.match(expression) ? element : element.up(expression); - }, - - isLeftClick: function(event) { - return (((event.which) && (event.which == 1)) || - ((event.button) && (event.button == 1))); - }, - - pointer: function(event) { - return { - x: event.pageX || (event.clientX + - (document.documentElement.scrollLeft || document.body.scrollLeft)), - y: event.pageY || (event.clientY + - (document.documentElement.scrollTop || document.body.scrollTop)) +Event.Methods = (function() { + // mouse button detection is terribly inconsistent + if (Prototype.Browser.IE) { + var isButton = function(event, code) { + return event.button == ({ 0:1, 1:4, 2:2 })[code]; + }; + } else if (Prototype.Browser.WebKit) { + var isButton = function(event, code) { + switch (code) { + case 0: return event.which == 1 && !event.metaKey; + case 1: return event.which == 1 && event.metaKey; + default: return false; + } + }; + } else { + var isButton = function(event, code) { + return event.which ? (event.which === code + 1) : (event.button === code); }; - }, - - pointerX: function(event) { return Event.pointer(event).x }, - pointerY: function(event) { return Event.pointer(event).y }, - - stop: function(event) { - event.preventDefault(); - event.stopPropagation(); } -}; + + return { + isLeftClick: function(event) { return isButton(event, 0) }, + isMiddleClick: function(event) { return isButton(event, 1) }, + isRightClick: function(event) { return isButton(event, 2) }, + + element: function(event) { + var node = event.target; + return Element.extend(node.nodeType == Node.TEXT_NODE ? node.parentNode : node); + }, + + findElement: function(event, expression) { + var element = Event.element(event); + return element.match(expression) ? element : element.up(expression); + }, + + pointer: function(event) { + return { + x: event.pageX || (event.clientX + + (document.documentElement.scrollLeft || document.body.scrollLeft)), + y: event.pageY || (event.clientY + + (document.documentElement.scrollTop || document.body.scrollTop)) + }; + }, + + pointerX: function(event) { return Event.pointer(event).x }, + pointerY: function(event) { return Event.pointer(event).y }, + + stop: function(event) { + event.preventDefault(); + event.stopPropagation(); + } + }; +})(); Event.extend = (function() { var methods = Object.keys(Event.Methods).inject({ }, function(m, name) { diff --git a/test/functional/event.html b/test/functional/event.html new file mode 100644 index 0000000..d0dfc37 --- /dev/null +++ b/test/functional/event.html @@ -0,0 +1,241 @@ + + + + Prototype functional test file + + + + + + + + +

Prototype functional tests for the Event module

+ +
log empty
+ +

A basic event test - click here

+

click to stop observing the first test

+ + + +

Scope test - scope of the handler should be this element

+ + + +

Event object test - should be present as a first argument

+ + + +

Hijack link test (preventDefault)

+ + + +
+ + +

Mouse click: + left middle right

+ + + +

Context menu event (tries to prevent default)

+ + + +

Event.element() test

+ + + +

Event.currentTarget test

+ + + +

Event.findElement() test

+ + + +

Stop propagation test (bubbling)

+ + + +
+

Keyup test - focus on the textarea and type

+ +
+ + + +

bindAsEventListener() test

+ + + +

Object.inspect(event) test

+ + + +

Add unload events

+ + + +