mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
export class DoubleClickEvent
|
|
{
|
|
constructor(handler, singleClickHandler = null)
|
|
{
|
|
this.handler = handler;
|
|
this.singleClickHandler = singleClickHandler;
|
|
this.elements = [];
|
|
this.clickedOnce = false;
|
|
this.doubleClickHandler = e =>
|
|
{
|
|
if (!this.clickedOnce)
|
|
{
|
|
this.clickedOnce = true;
|
|
setTimeout(() =>
|
|
{
|
|
if (this.clickedOnce)
|
|
{
|
|
this.clickedOnce = false;
|
|
this.singleClickHandler && this.singleClickHandler(e);
|
|
}
|
|
}, 200);
|
|
}
|
|
else
|
|
{
|
|
this.clickedOnce = false;
|
|
this.handler && this.handler(e);
|
|
}
|
|
};
|
|
}
|
|
bind(element)
|
|
{
|
|
if (this.elements.indexOf(element) === -1)
|
|
{
|
|
this.elements.push(element);
|
|
element.addEventListener("click", this.doubleClickHandler);
|
|
}
|
|
}
|
|
unbind(element)
|
|
{
|
|
const index = this.elements.indexOf(element);
|
|
if (index === -1)
|
|
{
|
|
return;
|
|
}
|
|
this.elements.splice(index, 1);
|
|
element.removeEventListener("click", this.doubleClickHandler);
|
|
}
|
|
} |