Skip to content

Commit 74068a0

Browse files
committed
Event: change event hooks API to jQuery.event.addProp
1 parent ed7aa50 commit 74068a0

File tree

1 file changed

+105
-92
lines changed

1 file changed

+105
-92
lines changed

src/event.js

Lines changed: 105 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -93,36 +93,6 @@ function on( elem, types, selector, data, fn, one ) {
9393
} );
9494
}
9595

96-
function addEventGetter( unused, name ) {
97-
Object.defineProperty( jQuery.Event.prototype, name, {
98-
enumerable: true,
99-
configurable: true,
100-
101-
get: function() {
102-
var value, hook;
103-
104-
if ( this.originalEvent ) {
105-
if ( hook = jQuery.event.propHooks[ name ] ) {
106-
value = hook( this.originalEvent );
107-
} else {
108-
value = this.originalEvent[ name ];
109-
}
110-
}
111-
112-
return value;
113-
},
114-
115-
set: function( value ) {
116-
Object.defineProperty( this, name, {
117-
enumerable: true,
118-
configurable: true,
119-
writable: true,
120-
value: value
121-
} );
122-
}
123-
} );
124-
}
125-
12696
/*
12797
* Helper functions for managing events -- not part of the public interface.
12898
* Props to Dean Edwards' addEvent library for many of the ideas.
@@ -428,75 +398,40 @@ jQuery.event = {
428398
return handlerQueue;
429399
},
430400

431-
// Includes all common event props including KeyEvent and MouseEvent specific props
432-
props: ( "altKey bubbles cancelable ctrlKey detail eventPhase " +
433-
"metaKey shiftKey view which char charCode key keyCode " +
434-
"button buttons clientX clientY offsetX offsetY pageX pageY screenX screenY toElement"
435-
).split( " " ),
436-
437-
propHooks: {
438-
which: function( event ) {
439-
var button = event.button;
440-
441-
// Add which for key events
442-
if ( event.which == null && rkeyEvent.test( event.type ) ) {
443-
return event.charCode != null ? event.charCode : event.keyCode;
444-
}
445-
446-
// Add which for click: 1 === left; 2 === middle; 3 === right
447-
if ( !event.which && button !== undefined && rmouseEvent.test( event.type ) ) {
448-
return ( button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ) );
449-
}
450-
451-
return event.which;
452-
},
453-
454-
pageX: function( event ) {
455-
var eventDoc, doc, body;
456-
457-
// Calculate pageX if missing and clientX available
458-
if ( event.pageX == null && event.clientX != null ) {
459-
eventDoc = event.target.ownerDocument || document;
460-
doc = eventDoc.documentElement;
461-
body = eventDoc.body;
462-
463-
return event.clientX +
464-
( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) -
465-
( doc && doc.clientLeft || body && body.clientLeft || 0 );
466-
}
467-
468-
return event.pageX;
469-
},
401+
addProp: function( name, hook ) {
402+
var getter = jQuery.isFunction( hook ) ?
403+
function hookGetter() {
404+
if ( this.originalEvent ) {
405+
return hook( this.originalEvent );
406+
}
407+
} :
408+
function propGetter() {
409+
if ( this.originalEvent ) {
410+
return this.originalEvent[ name ];
411+
}
412+
};
470413

471-
pageY: function( event ) {
472-
var eventDoc, doc, body;
414+
Object.defineProperty( jQuery.Event.prototype, name, {
415+
enumerable: true,
416+
configurable: true,
473417

474-
// Calculate pageY if missing and clientY available
475-
if ( event.pageY == null && event.clientY != null ) {
476-
eventDoc = event.target.ownerDocument || document;
477-
doc = eventDoc.documentElement;
478-
body = eventDoc.body;
418+
get: getter,
479419

480-
return event.clientY +
481-
( doc && doc.scrollTop || body && body.scrollTop || 0 ) -
482-
( doc && doc.clientTop || body && body.clientTop || 0 );
420+
set: function( value ) {
421+
Object.defineProperty( this, name, {
422+
enumerable: true,
423+
configurable: true,
424+
writable: true,
425+
value: value
426+
} );
483427
}
484-
485-
return event.pageY;
486-
}
428+
} );
487429
},
488430

489431
fix: function( originalEvent ) {
490-
if ( originalEvent[ jQuery.expando ] ) {
491-
return originalEvent;
492-
}
493-
494-
// Setup any prop hooks added since the last fix
495-
if ( this.props.length ) {
496-
jQuery.each( this.props.splice( 0 ), addEventGetter );
497-
}
498-
499-
return new jQuery.Event( originalEvent );
432+
return originalEvent[ jQuery.expando ] ?
433+
originalEvent :
434+
new jQuery.Event( originalEvent );
500435
},
501436

502437
special: {
@@ -650,6 +585,84 @@ jQuery.Event.prototype = {
650585
}
651586
};
652587

588+
// Includes all common event props including KeyEvent and MouseEvent specific props
589+
jQuery.each( {
590+
altKey: true,
591+
bubbles: true,
592+
cancelable: true,
593+
ctrlKey: true,
594+
detail: true,
595+
eventPhase: true,
596+
metaKey: true,
597+
shiftKey: true,
598+
view: true,
599+
"char": true,
600+
charCode: true,
601+
key: true,
602+
keyCode: true,
603+
button: true,
604+
buttons: true,
605+
clientX: true,
606+
clientY: true,
607+
offsetX: true,
608+
offsetY: true,
609+
screenX: true,
610+
screenY: true,
611+
toElement: true,
612+
613+
which: function( event ) {
614+
var button = event.button;
615+
616+
// Add which for key events
617+
if ( event.which == null && rkeyEvent.test( event.type ) ) {
618+
return event.charCode != null ? event.charCode : event.keyCode;
619+
}
620+
621+
// Add which for click: 1 === left; 2 === middle; 3 === right
622+
if ( !event.which && button !== undefined && rmouseEvent.test( event.type ) ) {
623+
return ( button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ) );
624+
}
625+
626+
return event.which;
627+
},
628+
629+
pageX: function( event ) {
630+
var eventDoc, doc, body;
631+
632+
// Calculate pageX if missing and clientX available
633+
if ( event.pageX == null && event.clientX != null ) {
634+
eventDoc = event.target.ownerDocument || document;
635+
doc = eventDoc.documentElement;
636+
body = eventDoc.body;
637+
638+
return event.clientX +
639+
( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) -
640+
( doc && doc.clientLeft || body && body.clientLeft || 0 );
641+
}
642+
643+
return event.pageX;
644+
},
645+
646+
pageY: function( event ) {
647+
var eventDoc, doc, body;
648+
649+
// Calculate pageY if missing and clientY available
650+
if ( event.pageY == null && event.clientY != null ) {
651+
eventDoc = event.target.ownerDocument || document;
652+
doc = eventDoc.documentElement;
653+
body = eventDoc.body;
654+
655+
return event.clientY +
656+
( doc && doc.scrollTop || body && body.scrollTop || 0 ) -
657+
( doc && doc.clientTop || body && body.clientTop || 0 );
658+
}
659+
660+
return event.pageY;
661+
}
662+
}, function( prop, hook ) {
663+
jQuery.event.addProp( prop, hook );
664+
} );
665+
653666
// Create mouseenter/leave events using mouseover/out and event-time checks
654667
// so that event delegation works in jQuery.
655668
// Do the same for pointerenter/pointerleave and pointerover/pointerout

0 commit comments

Comments
 (0)