Skip to content

Commit

Permalink
style: CRLF linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Gh61 committed Jan 11, 2024
1 parent 8ceaffa commit 4e9e407
Showing 1 changed file with 96 additions and 96 deletions.
192 changes: 96 additions & 96 deletions src/types/prevent-ghostclick.ts
Original file line number Diff line number Diff line change
@@ -1,97 +1,97 @@
/**
* Source: https://hammerjs.github.io/tips/ (Section: "After a tap, also a click is being triggered, I don't want that!")
* Link to: https://gist.github.com/jtangelder/361052976f044200ea17
*
* This is a must-have because of how hammerjs is handling touch vs click.
*
* TZ: Cleaned and Rewritten to TypeScript
*/

export class PreventGhostClick {
private static readonly _threshold = 25;
private static readonly _timeout = 2500;
private static readonly _isEnabled = !!("ontouchstart" in window);

private static _coordinates = new Array<Array<number>>();

// static ctor
static {
if (PreventGhostClick._isEnabled) {
document.addEventListener("click", (ev) => PreventGhostClick.preventGhostClick(ev), true);
}
}

private readonly _el;

/**
* prevent click events after touchstart for the given element
* @param {EventTarget} el
*/
constructor(el: EventTarget) {
this._el = el;
if (PreventGhostClick._isEnabled) {
this._el.addEventListener("touchstart", PreventGhostClick.resetCoordinates, true);
this._el.addEventListener("touchend", PreventGhostClick.registerCoordinates, true);
}
}

/**
* removes listeners for touch events
*/
public destroy(){
this._el.addEventListener("touchstart", PreventGhostClick.resetCoordinates, true);
this._el.addEventListener("touchend", PreventGhostClick.registerCoordinates, true);
}

/**
* prevent clicks if they're in any registered XY region
* @param {MouseEvent} ev
*/
private static preventGhostClick(ev: MouseEvent) {
for (var i = 0; i < PreventGhostClick._coordinates.length; i++) {
var x = PreventGhostClick._coordinates[i][0];
var y = PreventGhostClick._coordinates[i][1];

// within the range, so prevent the click
if (Math.abs(ev.clientX - x) < PreventGhostClick._threshold && Math.abs(ev.clientY - y) < PreventGhostClick._threshold) {
ev.stopImmediatePropagation();
ev.preventDefault();
break;
}
}
}

/**
* reset the coordinates array
*/
private static resetCoordinates() {
PreventGhostClick._coordinates = [];
}

/**
* remove the first coordinates set from the array
*/
private static popCoordinates() {
PreventGhostClick._coordinates.splice(0, 1);
}

/**
* if it is an final touchend, we want to register it's place
* @param {TouchEvent} ev
*/
private static registerCoordinates(ev: Event | TouchEvent) {
// only support TouchEvent
if (!("touches" in ev))
return;

// touchend is triggered on every releasing finger
// changed touches always contain the removed touches on a touchend
// the touches object might contain these also at some browsers (firefox os)
// so touches - changedTouches will be 0 or lower, like -1, on the final touchend
if (ev.touches.length - ev.changedTouches.length <= 0) {
var touch = ev.changedTouches[0];
PreventGhostClick._coordinates.push([touch.clientX, touch.clientY]);
setTimeout(PreventGhostClick.popCoordinates, PreventGhostClick._timeout);
}
}
/**
* Source: https://hammerjs.github.io/tips/ (Section: "After a tap, also a click is being triggered, I don't want that!")
* Link to: https://gist.github.com/jtangelder/361052976f044200ea17
*
* This is a must-have because of how hammerjs is handling touch vs click.
*
* TZ: Cleaned and Rewritten to TypeScript
*/

export class PreventGhostClick {
private static readonly _threshold = 25;
private static readonly _timeout = 2500;
private static readonly _isEnabled = !!("ontouchstart" in window);

Check failure on line 13 in src/types/prevent-ghostclick.ts

View workflow job for this annotation

GitHub Actions / validate

Strings must use singlequote

private static _coordinates = new Array<Array<number>>();

// static ctor
static {
if (PreventGhostClick._isEnabled) {

Check failure on line 19 in src/types/prevent-ghostclick.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected dangling '_' in '_isEnabled'
document.addEventListener("click", (ev) => PreventGhostClick.preventGhostClick(ev), true);

Check failure on line 20 in src/types/prevent-ghostclick.ts

View workflow job for this annotation

GitHub Actions / validate

Strings must use singlequote
}
}

private readonly _el;

/**
* prevent click events after touchstart for the given element
* @param {EventTarget} el
*/
constructor(el: EventTarget) {

Check failure on line 30 in src/types/prevent-ghostclick.ts

View workflow job for this annotation

GitHub Actions / validate

Missing accessibility modifier on method definition constructor
this._el = el;
if (PreventGhostClick._isEnabled) {

Check failure on line 32 in src/types/prevent-ghostclick.ts

View workflow job for this annotation

GitHub Actions / validate

Unexpected dangling '_' in '_isEnabled'
this._el.addEventListener("touchstart", PreventGhostClick.resetCoordinates, true);

Check failure on line 33 in src/types/prevent-ghostclick.ts

View workflow job for this annotation

GitHub Actions / validate

Strings must use singlequote
this._el.addEventListener("touchend", PreventGhostClick.registerCoordinates, true);

Check failure on line 34 in src/types/prevent-ghostclick.ts

View workflow job for this annotation

GitHub Actions / validate

Strings must use singlequote
}
}

/**
* removes listeners for touch events
*/
public destroy(){

Check failure on line 41 in src/types/prevent-ghostclick.ts

View workflow job for this annotation

GitHub Actions / validate

Missing space before opening brace
this._el.addEventListener("touchstart", PreventGhostClick.resetCoordinates, true);

Check failure on line 42 in src/types/prevent-ghostclick.ts

View workflow job for this annotation

GitHub Actions / validate

Strings must use singlequote
this._el.addEventListener("touchend", PreventGhostClick.registerCoordinates, true);

Check failure on line 43 in src/types/prevent-ghostclick.ts

View workflow job for this annotation

GitHub Actions / validate

Strings must use singlequote
}

/**
* prevent clicks if they're in any registered XY region
* @param {MouseEvent} ev
*/
private static preventGhostClick(ev: MouseEvent) {
for (var i = 0; i < PreventGhostClick._coordinates.length; i++) {
var x = PreventGhostClick._coordinates[i][0];
var y = PreventGhostClick._coordinates[i][1];

// within the range, so prevent the click
if (Math.abs(ev.clientX - x) < PreventGhostClick._threshold && Math.abs(ev.clientY - y) < PreventGhostClick._threshold) {
ev.stopImmediatePropagation();
ev.preventDefault();
break;
}
}
}

/**
* reset the coordinates array
*/
private static resetCoordinates() {
PreventGhostClick._coordinates = [];
}

/**
* remove the first coordinates set from the array
*/
private static popCoordinates() {
PreventGhostClick._coordinates.splice(0, 1);
}

/**
* if it is an final touchend, we want to register it's place
* @param {TouchEvent} ev
*/
private static registerCoordinates(ev: Event | TouchEvent) {
// only support TouchEvent
if (!("touches" in ev))
return;

// touchend is triggered on every releasing finger
// changed touches always contain the removed touches on a touchend
// the touches object might contain these also at some browsers (firefox os)
// so touches - changedTouches will be 0 or lower, like -1, on the final touchend
if (ev.touches.length - ev.changedTouches.length <= 0) {
var touch = ev.changedTouches[0];
PreventGhostClick._coordinates.push([touch.clientX, touch.clientY]);
setTimeout(PreventGhostClick.popCoordinates, PreventGhostClick._timeout);
}
}
}

0 comments on commit 4e9e407

Please sign in to comment.