Fix event.which (#1217)
This commit is contained in:
parent
7e03645a55
commit
ee31297a83
|
@ -179,23 +179,40 @@ jQuery.event = {
|
||||||
},
|
},
|
||||||
|
|
||||||
fix: function(event) {
|
fix: function(event) {
|
||||||
|
// store a copy of the original event object
|
||||||
|
// and clone to set read-only properties
|
||||||
|
var originalEvent = event;
|
||||||
|
event = jQuery.extend({}, originalEvent);
|
||||||
|
|
||||||
|
// add preventDefault and stopPropagation since
|
||||||
|
// they will not work on the clone
|
||||||
|
event.preventDefault = function() {
|
||||||
|
// if preventDefault exists run it on the original event
|
||||||
|
if (originalEvent.preventDefault)
|
||||||
|
return originalEvent.preventDefault();
|
||||||
|
// otherwise set the returnValue property of the original event to false (IE)
|
||||||
|
originalEvent.returnValue = false;
|
||||||
|
};
|
||||||
|
event.stopPropagation = function() {
|
||||||
|
// if stopPropagation exists run it on the original event
|
||||||
|
if (originalEvent.stopPropagation)
|
||||||
|
return originalEvent.stopPropagation();
|
||||||
|
// otherwise set the cancelBubble property of the original event to true (IE)
|
||||||
|
originalEvent.cancelBubble = true;
|
||||||
|
};
|
||||||
|
|
||||||
// Fix target property, if necessary
|
// Fix target property, if necessary
|
||||||
if ( !event.target && event.srcElement )
|
if ( !event.target && event.srcElement )
|
||||||
event.target = event.srcElement;
|
event.target = event.srcElement;
|
||||||
|
|
||||||
|
// check if target is a textnode (safari)
|
||||||
|
if (jQuery.browser.safari && event.target.nodeType == 3)
|
||||||
|
event.target = originalEvent.target.parentNode;
|
||||||
|
|
||||||
// Add relatedTarget, if necessary
|
// Add relatedTarget, if necessary
|
||||||
if ( !event.relatedTarget && event.fromElement )
|
if ( !event.relatedTarget && event.fromElement )
|
||||||
event.relatedTarget = event.fromElement == event.target ? event.toElement : event.fromElement;
|
event.relatedTarget = event.fromElement == event.target ? event.toElement : event.fromElement;
|
||||||
|
|
||||||
// Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs)
|
|
||||||
if ( event.metaKey == null && event.ctrlKey != null )
|
|
||||||
event.metaKey = event.ctrlKey;
|
|
||||||
|
|
||||||
// Add which for click: 1 == left; 2 == middle; 3 == right
|
|
||||||
// Note: button is not normalized, so don't use it
|
|
||||||
if ( event.which == null && event.button != null )
|
|
||||||
event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));
|
|
||||||
|
|
||||||
// Calculate pageX/Y if missing and clientX/Y available
|
// Calculate pageX/Y if missing and clientX/Y available
|
||||||
if ( event.pageX == null && event.clientX != null ) {
|
if ( event.pageX == null && event.clientX != null ) {
|
||||||
var e = document.documentElement || document.body;
|
var e = document.documentElement || document.body;
|
||||||
|
@ -203,44 +220,18 @@ jQuery.event = {
|
||||||
event.pageY = event.clientY + e.scrollTop;
|
event.pageY = event.clientY + e.scrollTop;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add which for keypresses: keyCode
|
// Add which for key events
|
||||||
if ( (event.which == null || event.type == "keypress") && event.keyCode != null )
|
if ( !event.which && (event.charCode || event.keyCode) )
|
||||||
event.which = event.keyCode;
|
event.which = event.charCode || event.keyCode;
|
||||||
|
|
||||||
// If it's a keypress event, add charCode to IE
|
// Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs)
|
||||||
if ( event.charCode == null && event.type == "keypress" )
|
if ( !event.metaKey && event.ctrlKey )
|
||||||
event.charCode = event.keyCode;
|
event.metaKey = event.ctrlKey;
|
||||||
|
|
||||||
// check if target is a textnode (safari)
|
// Add which for click: 1 == left; 2 == middle; 3 == right
|
||||||
if (jQuery.browser.safari && event.target.nodeType == 3) {
|
// Note: button is not normalized, so don't use it
|
||||||
// store a copy of the original event object
|
if ( !event.which && event.button )
|
||||||
// and clone because target is read only
|
event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));
|
||||||
var originalEvent = event;
|
|
||||||
event = jQuery.extend({}, originalEvent);
|
|
||||||
|
|
||||||
// get parentnode from textnode
|
|
||||||
event.target = originalEvent.target.parentNode;
|
|
||||||
|
|
||||||
// add preventDefault and stopPropagation since
|
|
||||||
// they will not work on the clone
|
|
||||||
event.preventDefault = function() {
|
|
||||||
return originalEvent.preventDefault();
|
|
||||||
};
|
|
||||||
event.stopPropagation = function() {
|
|
||||||
return originalEvent.stopPropagation();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// fix preventDefault and stopPropagation
|
|
||||||
if (!event.preventDefault)
|
|
||||||
event.preventDefault = function() {
|
|
||||||
this.returnValue = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!event.stopPropagation)
|
|
||||||
event.stopPropagation = function() {
|
|
||||||
this.cancelBubble = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue