Add: clickHandler supporting hold and click actions

pull/16/head
Juraj Nyíri 3 years ago
parent e164c080da
commit 75257220cd

@ -18893,6 +18893,7 @@ class PlayController {
return foundResult; return foundResult;
}; };
this.play = async (data, instantPlay = false) => { this.play = async (data, instantPlay = false) => {
console.log(data);
if (lodash.isArray(this.runBefore)) { if (lodash.isArray(this.runBefore)) {
await this.hass.callService(this.runBefore[0], this.runBefore[1], {}); await this.hass.callService(this.runBefore[0], this.runBefore[1], {});
} }
@ -19205,7 +19206,49 @@ const findTrailerURL = (movieData) => {
} }
return foundURL; return foundURL;
}; };
const clickHandler = (elem, clickFunction, holdFunction) => {
let longpress = false;
let presstimer = null;
const cancel = (e) => {
e.stopPropagation();
if (presstimer !== null) {
clearTimeout(presstimer);
presstimer = null;
}
};
const click = (e) => {
e.stopPropagation();
if (presstimer !== null) {
clearTimeout(presstimer);
presstimer = null;
}
if (longpress) {
return false;
}
clickFunction(e);
return true;
};
const start = (e) => {
e.stopPropagation();
if (e.type === 'click' && e.button !== 0) {
return;
}
longpress = false;
presstimer = setTimeout(() => {
holdFunction(e);
longpress = true;
}, 1000);
};
elem.addEventListener('mousedown', start);
elem.addEventListener('touchstart', start);
elem.addEventListener('click', click);
elem.addEventListener('mouseout', cancel);
elem.addEventListener('touchend', cancel);
elem.addEventListener('touchleave', cancel);
elem.addEventListener('touchcancel', cancel);
};
const createEpisodesView = (playController, plex, data) => { const createEpisodesView = (playController, plex, data) => {
console.log(data);
const episodeContainer = document.createElement('div'); const episodeContainer = document.createElement('div');
episodeContainer.className = 'episodeContainer'; episodeContainer.className = 'episodeContainer';
episodeContainer.style.width = `${CSS_STYLE.episodeWidth}px`; episodeContainer.style.width = `${CSS_STYLE.episodeWidth}px`;
@ -21140,11 +21183,15 @@ class PlexMeetsHomeAssistant extends HTMLElement {
interactiveArea.append(playButton); interactiveArea.append(playButton);
} }
movieElem.append(interactiveArea); movieElem.append(interactiveArea);
playButton.addEventListener('click', event => { clickHandler(playButton, (event) => {
console.log('click');
event.stopPropagation(); event.stopPropagation();
if (this.hassObj && this.playController) { if (this.hassObj && this.playController) {
this.playController.play(data, true); this.playController.play(data, true);
} }
}, (event) => {
console.log('hold');
event.stopPropagation();
}); });
const titleElem = document.createElement('div'); const titleElem = document.createElement('div');
if (lodash.isEqual(data.type, 'episode')) { if (lodash.isEqual(data.type, 'episode')) {

@ -96,6 +96,7 @@ class PlayController {
}; };
play = async (data: Record<string, any>, instantPlay = false): Promise<void> => { play = async (data: Record<string, any>, instantPlay = false): Promise<void> => {
console.log(data);
if (_.isArray(this.runBefore)) { if (_.isArray(this.runBefore)) {
await this.hass.callService(this.runBefore[0], this.runBefore[1], {}); await this.hass.callService(this.runBefore[0], this.runBefore[1], {});
} }

@ -104,8 +104,57 @@ const findTrailerURL = (movieData: Record<string, any>): string => {
} }
return foundURL; return foundURL;
}; };
const clickHandler = (elem: HTMLButtonElement, clickFunction: Function, holdFunction: Function): void => {
let longpress = false;
let presstimer: any = null;
const cancel = (e: any): void => {
e.stopPropagation();
if (presstimer !== null) {
clearTimeout(presstimer);
presstimer = null;
}
};
const click = (e: any): boolean => {
e.stopPropagation();
if (presstimer !== null) {
clearTimeout(presstimer);
presstimer = null;
}
if (longpress) {
return false;
}
clickFunction(e);
return true;
};
const start = (e: any): void => {
e.stopPropagation();
if (e.type === 'click' && e.button !== 0) {
return;
}
longpress = false;
presstimer = setTimeout(() => {
holdFunction(e);
longpress = true;
}, 1000);
};
elem.addEventListener('mousedown', start);
elem.addEventListener('touchstart', start);
elem.addEventListener('click', click);
elem.addEventListener('mouseout', cancel);
elem.addEventListener('touchend', cancel);
elem.addEventListener('touchleave', cancel);
elem.addEventListener('touchcancel', cancel);
};
const createEpisodesView = (playController: PlayController, plex: Plex, data: Record<string, any>): HTMLElement => { const createEpisodesView = (playController: PlayController, plex: Plex, data: Record<string, any>): HTMLElement => {
console.log(data);
const episodeContainer = document.createElement('div'); const episodeContainer = document.createElement('div');
episodeContainer.className = 'episodeContainer'; episodeContainer.className = 'episodeContainer';
episodeContainer.style.width = `${CSS_STYLE.episodeWidth}px`; episodeContainer.style.width = `${CSS_STYLE.episodeWidth}px`;
@ -206,5 +255,6 @@ export {
hasEpisodes, hasEpisodes,
getOldPlexServerErrorMessage, getOldPlexServerErrorMessage,
getWidth, getWidth,
getDetailsBottom getDetailsBottom,
clickHandler
}; };

@ -15,7 +15,8 @@ import {
isVideoFullScreen, isVideoFullScreen,
hasEpisodes, hasEpisodes,
getOldPlexServerErrorMessage, getOldPlexServerErrorMessage,
getDetailsBottom getDetailsBottom,
clickHandler
} from './modules/utils'; } from './modules/utils';
import style from './modules/style'; import style from './modules/style';
@ -1362,13 +1363,21 @@ class PlexMeetsHomeAssistant extends HTMLElement {
movieElem.append(interactiveArea); movieElem.append(interactiveArea);
playButton.addEventListener('click', event => { clickHandler(
event.stopPropagation(); playButton,
(event: any): void => {
console.log('click');
event.stopPropagation();
if (this.hassObj && this.playController) { if (this.hassObj && this.playController) {
this.playController.play(data, true); this.playController.play(data, true);
}
},
(event: any): void => {
console.log('hold');
event.stopPropagation();
} }
}); );
const titleElem = document.createElement('div'); const titleElem = document.createElement('div');
if (_.isEqual(data.type, 'episode')) { if (_.isEqual(data.type, 'episode')) {

Loading…
Cancel
Save