Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emulating event bubble for DOMs inside web components #26

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ build/Release
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules

package-lock.json
58 changes: 58 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!DOCTYPE html>
<html>

<head lang="en">
<meta charset="UTF-8">
<title>Touch Emulator Manual</title>
</head>

<body>

<div>Touch Emulator Manual</div>

<br>
<br>

<div>Manual:</div>
<ul>
<li>
<a href="tests/manual/events.html">events</a>
</li>
<li>
<a href="tests/manual/googlemaps.html">googlemaps</a>
</li>
<li>
<a href="tests/manual/hammer.html">hammer</a>
</li>
<li>
<a href="tests/manual/img.html">img</a>
</li>
<li>
<a href="tests/manual/leaflet.html">leaflet</a>
</li>
<li>
<a href="tests/manual/modernizr.html">modernizr</a>
</li>
</ul>

<br>
<br>
<div>Touch Events:</div>
<ul>
<li>
<a href="tests/web-platform-tests/touch-events/create-touch-touchlist.html">create-touch-touchlist</a>
</li>
<li>
<a href="tests/web-platform-tests/touch-events/multi-touch-interactions-manual.html">multi-touch-interactions-manual</a>

</li>
<li>
<a href="tests/web-platform-tests/touch-events/multi-touch-interfaces-manual.html">multi-touch-interfaces-manual</a>
</li>
<li>
<a href="tests/web-platform-tests/touch-events/single-touch-manual.html">single-touch-manual</a>
</li>
</ul>
</body>

</html>
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test": "tests"
},
"scripts": {
"dev": "vite dev",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand All @@ -25,5 +26,8 @@
"bugs": {
"url": "https://github.com/hammerjs/touchemulator/issues"
},
"homepage": "https://github.com/hammerjs/touchemulator"
"homepage": "https://github.com/hammerjs/touchemulator",
"devDependencies": {
"vite": "^3.2.4"
}
}
35 changes: 27 additions & 8 deletions touch-emulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var isMultiTouch = false;
var multiTouchStartPos;
var eventTarget;
var eventTargetList = [];
var touchElements = {};

// polyfills
Expand Down Expand Up @@ -137,21 +138,39 @@
// The EventTarget on which the touch point started when it was first placed on the surface,
// even if the touch point has since moved outside the interactive area of that element.
// also, when the target doesnt exist anymore, we update it

if (ev.type == 'mousedown' || !eventTarget || (eventTarget && !eventTarget.dispatchEvent)) {
if(ev.composedPath() && ev.composedPath().length > 0){
eventTarget = ev.composedPath()[0]
}else {
var composedPathList = ev.composedPath();
// Emulating event bubble from composedPath()[0] to ev.target for DOMs inside Web Components .
if(composedPathList.length > 0){
for(var i = 0; i < composedPathList.length; i = i+1){
if(composedPathList[i]!==ev.target && (eventTargetList.length === 0 || eventTargetList.indexOf(composedPathList[i]) === -1 )){
eventTargetList.push(composedPathList[i])
} else {
break;
}
}

for(var j = 0; j < eventTargetList.length; j = j+1){
processTriggerForOneElement(ev, touchType, eventTargetList[j])
}
} else {
eventTarget = ev.target;
processTriggerForOneElement(ev, touchType, eventTarget)
}
}

}
}

function processTriggerForOneElement(ev, touchType, eventTarget){
// shiftKey has been lost, so trigger a touchend
if (isMultiTouch && !ev.shiftKey) {
triggerTouch('touchend', ev);
triggerTouch('touchend', ev, eventTarget);
isMultiTouch = false;
}

triggerTouch(touchType, ev);
triggerTouch(touchType, ev, eventTarget);

// we're entering the multi-touch mode!
if (!isMultiTouch && ev.shiftKey) {
Expand All @@ -164,24 +183,24 @@
screenX: ev.screenX,
screenY: ev.screenY
};
triggerTouch('touchstart', ev);
triggerTouch('touchstart', ev, eventTarget);
}

// reset
if (ev.type == 'mouseup') {
multiTouchStartPos = null;
isMultiTouch = false;
eventTarget = null;
eventTargetList = [];
}
}
}

/**
* trigger a touch event
* @param eventName
* @param mouseEv
*/
function triggerTouch(eventName, mouseEv) {
function triggerTouch(eventName, mouseEv, eventTarget) {
var touchEvent = document.createEvent('Event');
touchEvent.initEvent(eventName, true, true);

Expand Down
9 changes: 9 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'vite';

// https://vitejs.dev/config/
export default defineConfig({
server: {
host: '0.0.0.0',
port: 8080,
},
});