Skip to content
This repository was archived by the owner on Feb 11, 2021. It is now read-only.

Commit 1fc32b7

Browse files
committed
Allow for multiple pointer capturing
Fixes #108
1 parent ef02daa commit 1fc32b7

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

src/dispatcher.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@
9595
*/
9696
var dispatcher = {
9797
pointermap: new scope.PointerMap(),
98-
eventMap: {},
98+
eventMap: Object.create(null),
99+
captureInfo: Object.create(null),
99100
// Scope objects for native events.
100101
// This exists for ease of testing.
101102
eventSources: Object.create(null),
@@ -227,7 +228,7 @@
227228
*/
228229
makeEvent: function(inType, inEvent) {
229230
// relatedTarget must be null if pointer is captured
230-
if (this.captureInfo) {
231+
if (this.captureInfo[inEvent.pointerId]) {
231232
inEvent.relatedTarget = null;
232233
}
233234
var e = new PointerEvent(inType, inEvent);
@@ -274,18 +275,13 @@
274275
getTarget: function(inEvent) {
275276
// if pointer capture is set, route all events for the specified pointerId
276277
// to the capture target
277-
if (this.captureInfo) {
278-
if (this.captureInfo.id === inEvent.pointerId) {
279-
return this.captureInfo.target;
280-
}
281-
}
282-
return inEvent._target;
278+
return this.captureInfo[inEvent.pointerId] || inEvent._target;
283279
},
284280
setCapture: function(inPointerId, inTarget) {
285-
if (this.captureInfo) {
286-
this.releaseCapture(this.captureInfo.id);
281+
if (this.captureInfo[inPointerId]) {
282+
this.releaseCapture(inPointerId);
287283
}
288-
this.captureInfo = {id: inPointerId, target: inTarget};
284+
this.captureInfo[inPointerId] = inTarget;
289285
var e = document.createEvent('Event');
290286
e.initEvent('gotpointercapture', true, false);
291287
e.pointerId = inPointerId;
@@ -296,12 +292,12 @@
296292
this.asyncDispatchEvent(e);
297293
},
298294
releaseCapture: function(inPointerId) {
299-
if (this.captureInfo && this.captureInfo.id === inPointerId) {
295+
var t = this.captureInfo[inPointerId];
296+
if (t) {
300297
var e = document.createEvent('Event');
301298
e.initEvent('lostpointercapture', true, false);
302299
e.pointerId = inPointerId;
303-
var t = this.captureInfo.target;
304-
this.captureInfo = null;
300+
this.captureInfo[inPointerId] = undefined;
305301
document.removeEventListener('pointerup', this.implicitRelease);
306302
document.removeEventListener('pointercancel', this.implicitRelease);
307303
e._target = t;

src/touch.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
(function(scope) {
88
var dispatcher = scope.dispatcher;
9+
var captureInfo = dispatcher.captureInfo;
910
var findTarget = scope.findTarget;
1011
var allShadows = scope.targetFinding.allShadows.bind(scope.targetFinding);
1112
var pointermap = dispatcher.pointermap;
@@ -149,10 +150,8 @@
149150
// Spec specifies that pointerId 1 is reserved for Mouse.
150151
// Touch identifiers can start at 0.
151152
// Add 2 to the touch identifier for compatibility.
152-
var pi = e.pointerId = inTouch.identifier + 2;
153-
var ci = dispatcher.captureInfo;
154-
var t = ci && ci.id === pi && ci.target;
155-
e.target = t || findTarget(e);
153+
var id = e.pointerId = inTouch.identifier + 2;
154+
e.target = captureInfo[id] || findTarget(e);
156155
e.bubbles = true;
157156
e.cancelable = true;
158157
e.detail = this.clickCount;

test/capture.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
*/
66

77
suite('Pointer Capture', function() {
8-
var set = function(el) {
9-
el.setPointerCapture(1);
8+
var set = function(el, id) {
9+
el.setPointerCapture(id || 1);
1010
};
11-
var release = function(el) {
12-
el.releasePointerCapture(1);
11+
var release = function(el, id) {
12+
el.releasePointerCapture(id || 1);
1313
};
1414

1515
test('Element has setPointerCapture and releasePointerCapture', function() {
@@ -90,5 +90,26 @@ suite('Pointer Capture', function() {
9090
fire('up', host);
9191
}
9292
});
93+
94+
test('capture multiple pointers', function(done) {
95+
var pm = PointerEventsPolyfill.dispatcher.pointermap;
96+
var ids = 0;
97+
function wait(e) {
98+
ids += e.pointerId;
99+
if (ids == 3) {
100+
pm.clear();
101+
done();
102+
}
103+
}
104+
host.addEventListener('gotpointercapture', wait);
105+
var e = new PointerEvent('pointerdown', {pointerId: 1});
106+
pm.set(1, e);
107+
host.dispatchEvent(e);
108+
set(host, 1);
109+
e = new PointerEvent('pointerdown', {pointerId: 2});
110+
pm.set(2, e);
111+
host.dispatchEvent(e);
112+
set(host, 2);
113+
});
93114
});
94115
});

0 commit comments

Comments
 (0)