Skip to content

Commit

Permalink
Merge pull request #241 from MasatoMakino/refactor-toggle-event-listener
Browse files Browse the repository at this point in the history
refactor : toggleEventListener
  • Loading branch information
MasatoMakino authored Mar 14, 2024
2 parents e1d9af0 + 9199e2c commit b598c66
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 37 deletions.
25 changes: 12 additions & 13 deletions src/SliderView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
EventEmitter,
} from "pixi.js";
import {
ScrollBarViewUtil,
SliderEventContext,
SliderEventTypes,
SliderViewOption,
Expand Down Expand Up @@ -111,11 +112,11 @@ export class SliderView extends Container {
const localPos = this.toLocal(e.global);
this.dragStartPos.set(localPos.x - target.x, localPos.y - target.y);

if (this.buttonRootContainer instanceof HTMLCanvasElement) {
this.buttonRootContainer.addEventListener("pointermove", this.moveSlider);
} else {
this.buttonRootContainer.on("pointermove", this.moveSlider);
}
SliderViewUtil.addEventListenerToTarget(
this.buttonRootContainer,
"pointermove",
this.moveSlider,
);

this._slideButton.on("pointerup", this.moveSliderFinish);
this._slideButton.on("pointerupoutside", this.moveSliderFinish);
Expand Down Expand Up @@ -185,14 +186,12 @@ export class SliderView extends Container {
*/
private moveSliderFinish = () => {
this.isDragging = false;
if (this.buttonRootContainer instanceof HTMLCanvasElement) {
this.buttonRootContainer.removeEventListener(
"pointermove",
this.moveSlider,
);
} else {
this.buttonRootContainer.off("pointermove", this.moveSlider);
}

SliderViewUtil.removeEventListenerFromTarget(
this.buttonRootContainer,
"pointermove",
this.moveSlider,
);

this._slideButton.off("pointerup", this.moveSliderFinish);
this._slideButton.off("pointerupoutside", this.moveSliderFinish);
Expand Down
42 changes: 42 additions & 0 deletions src/SliderViewUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
Rectangle,
Container,
Bounds,
EventEmitter,
ContainerEvents,
} from "pixi.js";
import { SliderView } from "./index.js";

Expand Down Expand Up @@ -161,4 +163,44 @@ export class SliderViewUtil {
return localPos.y - dragStartPos.y;
}
}

/**
* リスナー関数を切り替える。
*
* @param shouldAdd リスナーを有効にするか否か
* @param target ターゲットオブジェクト
* @param event イベント名
* @param listener リスナー関数
* @returns
*/
static toggleEventListener(
shouldAdd: boolean,
target: Container | HTMLCanvasElement,
event: keyof ContainerEvents,
listener: EventEmitter.ListenerFn,
): void {
if (target instanceof HTMLCanvasElement) {
const method = shouldAdd ? "addEventListener" : "removeEventListener";
target[method](event as string, listener);
return;
}
const pixiMethod = shouldAdd ? "on" : "off";
target[pixiMethod](event, listener);
}

static addEventListenerToTarget(
target: Container | HTMLCanvasElement,
event: keyof ContainerEvents,
listener: EventEmitter.ListenerFn,
) {
this.toggleEventListener(true, target, event, listener);
}

static removeEventListenerFromTarget(
target: Container | HTMLCanvasElement,
event: keyof ContainerEvents,
listener: EventEmitter.ListenerFn,
) {
this.toggleEventListener(false, target, event, listener);
}
}
29 changes: 5 additions & 24 deletions src/scrollBar/InertialScrollManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,13 @@ export class InertialScrollManager extends EventEmitter<ScrollBarEventTypes> {
this.switchDragListener(false);
}

private switchDragListener(isOn: boolean): void {
private switchDragListener(shouldAdd: boolean): void {
const target = this.scrollBarView.contents.target;
const dragTarget = this.scrollBarView.canvas ?? target;
const switchListener = (
isOn: boolean,
dragTarget: Container | HTMLCanvasElement,
event: keyof ContainerEvents,
listener: EventEmitter.ListenerFn,
) => {
if (dragTarget instanceof HTMLCanvasElement) {
if (isOn) {
dragTarget.addEventListener(event as string, listener);
} else {
dragTarget.removeEventListener(event as string, listener);
}
} else {
if (isOn) {
dragTarget.on(event, listener);
} else {
dragTarget.off(event, listener);
}
}
};
switchListener(isOn, dragTarget, "pointermove", this.onMouseMove);
switchListener(isOn, target, "pointerup", this.onMouseUp);
switchListener(isOn, target, "pointerupoutside", this.onMouseUp);
const switchListener = SliderViewUtil.toggleEventListener;
switchListener(shouldAdd, dragTarget, "pointermove", this.onMouseMove);
switchListener(shouldAdd, target, "pointerup", this.onMouseUp);
switchListener(shouldAdd, target, "pointerupoutside", this.onMouseUp);
}

private getDragPos(e: PointerEvent): number {
Expand Down

0 comments on commit b598c66

Please sign in to comment.