Using PointerEvents on browsers that support it

This commit is contained in:
Francisco Hodge 2019-03-07 07:32:07 -05:00
parent 47ddf0b1e8
commit 5ab58f2528
2 changed files with 66 additions and 13 deletions

View File

@ -717,6 +717,29 @@ class SimpleKeyboard {
if (typeof this.options.beforeFirstRender === "function") if (typeof this.options.beforeFirstRender === "function")
this.options.beforeFirstRender(); this.options.beforeFirstRender();
/**
* Notify about PointerEvents usage
*/
if (
this.utilities.pointerEventsSupported() &&
!this.options.useTouchEvents
) {
if (this.options.debug) {
console.log("Using PointerEvents as it is supported by this browser");
}
}
/**
* Notify about touch events usage
*/
if (this.options.useTouchEvents) {
if (this.options.debug) {
console.log(
"useTouchEvents has been enabled. Only touch events will be used."
);
}
}
} }
/** /**
@ -868,6 +891,28 @@ class SimpleKeyboard {
buttonThemeClass ? " " + buttonThemeClass : "" buttonThemeClass ? " " + buttonThemeClass : ""
}`; }`;
/**
* Handle button click event
*/
/* istanbul ignore next */
if (this.utilities.pointerEventsSupported() && !useTouchEvents) {
/**
* PointerEvents support
*/
buttonDOM.onpointerdown = e => {
if (this.options.preventMouseDownDefault) e.preventDefault();
this.handleButtonClicked(button);
this.handleButtonMouseDown(button, e);
};
buttonDOM.onpointerup = e => {
if (this.options.preventMouseDownDefault) e.preventDefault();
this.handleButtonMouseUp();
};
buttonDOM.onpointercancel = e => this.handleButtonMouseUp();
} else {
/**
* Fallback for browsers not supporting PointerEvents
*/
if (useTouchEvents) { if (useTouchEvents) {
buttonDOM.ontouchstart = e => { buttonDOM.ontouchstart = e => {
this.handleButtonClicked(button); this.handleButtonClicked(button);
@ -885,6 +930,7 @@ class SimpleKeyboard {
this.handleButtonMouseDown(button, e); this.handleButtonMouseDown(button, e);
}; };
} }
}
/** /**
* Adding identifier * Adding identifier

View File

@ -372,6 +372,13 @@ class Utilities {
return "ontouchstart" in window || navigator.maxTouchPoints; return "ontouchstart" in window || navigator.maxTouchPoints;
} }
/**
* Determines whether pointer events are supported
*/
pointerEventsSupported() {
return window.PointerEvent;
}
/** /**
* Bind all methods in a given class * Bind all methods in a given class
*/ */