1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/scripts/inputManager.js

160 lines
5.3 KiB
JavaScript
Raw Normal View History

2020-01-21 12:51:33 +03:00
define(['playbackManager', 'focusManager', 'appRouter', 'dom', 'apphost'], function (playbackManager, focusManager, appRouter, dom, appHost) {
'use strict';
var lastInputTime = new Date().getTime();
2018-10-23 01:05:09 +03:00
function notify() {
lastInputTime = new Date().getTime();
handleCommand('unknown');
2018-10-23 01:05:09 +03:00
}
function notifyMouseMove() {
lastInputTime = new Date().getTime();
2018-10-23 01:05:09 +03:00
}
function idleTime() {
return new Date().getTime() - lastInputTime;
2018-10-23 01:05:09 +03:00
}
function select(sourceElement) {
sourceElement.click();
2018-10-23 01:05:09 +03:00
}
var eventListenerCount = 0;
2018-10-23 01:05:09 +03:00
function on(scope, fn) {
2020-01-17 11:50:46 +03:00
eventListenerCount++;
2019-09-11 04:00:07 -07:00
dom.addEventListener(scope, 'command', fn, {});
2018-10-23 01:05:09 +03:00
}
function off(scope, fn) {
2019-09-25 09:53:09 +09:00
if (eventListenerCount) {
eventListenerCount--;
}
2019-09-11 04:00:07 -07:00
dom.removeEventListener(scope, 'command', fn, {});
2018-10-23 01:05:09 +03:00
}
var commandTimes = {};
2018-10-23 01:05:09 +03:00
function checkCommandTime(command) {
var last = commandTimes[command] || 0;
var now = new Date().getTime();
if ((now - last) < 1000) {
return false;
}
commandTimes[command] = now;
return true;
2018-10-23 01:05:09 +03:00
}
2020-04-07 00:09:07 +02:00
function handleCommand(commandName, options) {
lastInputTime = new Date().getTime();
var sourceElement = (options ? options.sourceElement : null);
if (sourceElement) {
sourceElement = focusManager.focusableParent(sourceElement);
}
2020-03-14 22:29:58 +03:00
if (!sourceElement) {
sourceElement = document.activeElement || window;
var dlg = document.querySelector('.dialogContainer .dialog.opened');
if (dlg && (!sourceElement || !dlg.contains(sourceElement))) {
sourceElement = dlg;
}
}
if (eventListenerCount) {
2018-10-23 01:05:09 +03:00
var customEvent = new CustomEvent("command", {
detail: {
2020-04-07 00:09:07 +02:00
command: commandName
2018-10-23 01:05:09 +03:00
},
bubbles: true,
cancelable: true
2018-10-23 01:05:09 +03:00
});
var eventResult = sourceElement.dispatchEvent(customEvent);
if (!eventResult) {
// event cancelled
return;
}
2018-10-23 01:05:09 +03:00
}
2020-04-07 00:09:07 +02:00
const keyActions = (command) => ({
'up': focusManager.moveUp(sourceElement),
'down': focusManager.moveDown(sourceElement),
'left': focusManager.moveLeft(sourceElement),
'right': focusManager.moveRight(sourceElement),
'home': appRouter.goHome(),
'settings': appRouter.showSettings(),
'back': () => {
2020-01-21 12:51:33 +03:00
if (appRouter.canGoBack()) {
appRouter.back();
} else if (appHost.supports('exit')) {
appHost.exit();
}
2020-04-07 00:09:07 +02:00
},
'select': select(sourceElement),
'nextchapter': playbackManager.nextChapter(),
'next': playbackManager.nextTrack(),
'nexttrack': playbackManager.nextTrack(),
'previous': playbackManager.previousTrack(),
'previoustrack': playbackManager.previousTrack(),
'previouschapter': playbackManager.previousChapter(),
'guide': appRouter.showGuide(),
'recordedtv': appRouter.showRecordedTV(),
'livetv': appRouter.showLiveTV(),
'mute': playbackManager.setMute(true),
'unmute': playbackManager.setMute(false),
'togglemute': playbackManager.toggleMute(),
'channelup': playbackManager.channelUp(),
'channeldown': playbackManager.channelDown(),
'volumedown': playbackManager.volumeDown(),
'volumeup': playbackManager.volumeUp(),
'play': playbackManager.unpause(),
'pause': playbackManager.pause(),
'playpause': playbackManager.playPause(),
'stop': () => {
if (checkCommandTime('stop')) {
playbackManager.stop();
}
2020-04-07 00:09:07 +02:00
},
'changezoom': playbackManager.toggleAspectRatio(),
'changeaudiotrack': playbackManager.changeAudioStream(),
'changesubtitletrack': playbackManager.changeSubtitleStream(),
'search': appRouter.showSearch(),
'favorites': appRouter.showFavorites(),
'fastforward': playbackManager.fastForward(),
'rewind': playbackManager.rewind(),
'togglefullscreen': playbackManager.toggleFullscreen(),
'disabledisplaymirror': playbackManager.enableDisplayMirroring(false),
'enabledisplaymirror': playbackManager.enableDisplayMirroring(true),
'toggledisplaymirror': playbackManager.toggleDisplayMirroring(),
'nowplaying': appRouter.showNowPlaying(),
'repeatnone': playbackManager.setRepeatMode('RepeatNone'),
'repeatall': playbackManager.setRepeatMode('RepeatAll'),
'repeatone': playbackManager.setRepeatMode('RepeatOne')
})[command];
keyActions(commandName);
2018-10-23 01:05:09 +03:00
}
dom.addEventListener(document, 'click', notify, {
passive: true
});
return {
2018-10-23 01:05:09 +03:00
trigger: handleCommand,
handle: handleCommand,
notify: notify,
notifyMouseMove: notifyMouseMove,
idleTime: idleTime,
on: on,
off: off
};
2019-09-24 23:23:43 +09:00
});