52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { throttle } from 'lodash-es';
|
|
import { Timer } from '../utils/timer';
|
|
import { CardInteractionAPI } from './types';
|
|
|
|
export class InteractionManager {
|
|
private _timer = new Timer();
|
|
private _api: CardInteractionAPI;
|
|
private _interacted = false;
|
|
|
|
constructor(api: CardInteractionAPI) {
|
|
this._api = api;
|
|
}
|
|
|
|
// The mouse handler may be called continually, throttle it to at most once
|
|
// per second for performance reasons.
|
|
public reportInteraction = throttle(() => {
|
|
this._reportInteraction();
|
|
}, 1 * 1000);
|
|
|
|
public initialize(): void {
|
|
this._setInteraction(false);
|
|
}
|
|
|
|
public uninitialize(): void {
|
|
this._timer.stop();
|
|
this.reportInteraction.cancel();
|
|
}
|
|
|
|
public hasInteraction(): boolean {
|
|
return this._interacted;
|
|
}
|
|
|
|
private _setInteraction(val: boolean): void {
|
|
this._interacted = val;
|
|
this._api.getCardElementManager().getElement().toggleAttribute('interaction', val);
|
|
this._api.getConditionStateManager().setState({ interaction: val });
|
|
}
|
|
|
|
private _reportInteraction(): void {
|
|
this._timer.stop();
|
|
this._setInteraction(true);
|
|
|
|
const timeoutSeconds = this._api.getConfigManager().getConfig()
|
|
?.view.interaction_seconds;
|
|
if (timeoutSeconds) {
|
|
this._timer.start(timeoutSeconds, () => {
|
|
this._setInteraction(false);
|
|
});
|
|
}
|
|
}
|
|
}
|