Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

fix(gesture): Implement proper event dispatching when using JQuery #1367

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/core/services/gesture/gesture.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ angular.module('material.core')
onCancel: angular.noop,
options: {},

dispatchEvent: dispatchEvent,
dispatchEvent: typeof jQuery !== 'undefined' && angular.element === jQuery ? jQueryDispatchEvent : nativeDispatchEvent,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beware that this will probably break (in certain cases) once angular/angular.js#10761 lands (and I believe it's pretty close to landing).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we would need to check if jQuery object is really jQuery, but it should work even after merge from what I can read.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been merged, btw. The problem is that typeof jQuery !== 'undefined' && angular.element === jQuery might evaluate to false (thus using nativeDispatchEvent although jQuery is used (leading to the wrong behaviour again)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, jQuery may be defined as another name in global namespace, and then properly register using the new ngJq directive.

Maybe we should only check if angular.element has trigger function ? jqLite doesn't have this function.

https://github.com/angular/angular.js/blob/master/src/Angular.js#L1532-L1536

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rschmukler - Can you follow-up on this issue?


start: function(ev, pointer) {
if (this.state.isRunning) return;
Expand Down Expand Up @@ -361,10 +361,11 @@ angular.module('material.core')
bubbles: true,
cancelable: true
};

/*
* NOTE: dispatchEvent is very performance sensitive.
* NOTE: native and jquery event dispatchers are very performance sensitive.
*/
function dispatchEvent(srcEvent, eventType, eventPointer, /*original DOMEvent */ev) {
function nativeDispatchEvent(srcEvent, eventType, eventPointer, /*original DOMEvent */ev) {
eventPointer = eventPointer || pointer;
var eventObj;

Expand All @@ -387,6 +388,17 @@ angular.module('material.core')
eventPointer.target.dispatchEvent(eventObj);
}

// JQuery doesn't use native events for events registration, so using JQuery #trigger() instead of #dispatchEvent().
function jQueryDispatchEvent(srcEvent, eventType, eventPointer, /*original DOMEvent */ev) {
eventPointer = eventPointer || pointer;
var eventObj = new angular.element.Event(eventType)

eventObj.$material = true;
eventObj.pointer = eventPointer;
eventObj.srcEvent = srcEvent;
angular.element(eventPointer.target).trigger(eventObj);
}

return GestureHandler;
});

Expand Down