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

152 lines
4.6 KiB
JavaScript
Raw Normal View History

2020-08-14 08:46:34 +02:00
import serverNotifications from '../../scripts/serverNotifications';
import globalize from '../../scripts/globalize';
import Events from '../../utils/events.ts';
2020-08-14 08:46:34 +02:00
import EmbyButtonPrototype from '../../elements/emby-button/emby-button';
import ServerConnections from '../../components/ServerConnections';
2023-04-19 01:56:05 -04:00
function addNotificationEvent(instance, name, handler) {
const localHandler = handler.bind(instance);
Events.on(serverNotifications, name, localHandler);
instance[name] = localHandler;
}
function removeNotificationEvent(instance, name) {
const handler = instance[name];
if (handler) {
Events.off(serverNotifications, name, handler);
instance[name] = null;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
function onClick() {
const button = this;
const id = button.getAttribute('data-id');
const serverId = button.getAttribute('data-serverid');
const apiClient = ServerConnections.getApiClient(serverId);
if (!button.classList.contains('playstatebutton-played')) {
apiClient.markPlayed(apiClient.getCurrentUserId(), id, new Date());
setState(button, true);
} else {
apiClient.markUnplayed(apiClient.getCurrentUserId(), id, new Date());
setState(button, false);
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function onUserDataChanged(e, apiClient, userData) {
const button = this;
if (userData.ItemId === button.getAttribute('data-id')) {
setState(button, userData.Played);
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function setState(button, played, updateAttribute) {
let icon = button.iconElement;
if (!icon) {
button.iconElement = button.querySelector('.material-icons');
icon = button.iconElement;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
if (played) {
button.classList.add('playstatebutton-played');
if (icon) {
icon.classList.add('playstatebutton-icon-played');
icon.classList.remove('playstatebutton-icon-unplayed');
}
2023-04-19 01:56:05 -04:00
} else {
button.classList.remove('playstatebutton-played');
if (icon) {
icon.classList.remove('playstatebutton-icon-played');
icon.classList.add('playstatebutton-icon-unplayed');
}
2023-04-19 01:56:05 -04:00
}
2023-04-19 01:56:05 -04:00
if (updateAttribute !== false) {
button.setAttribute('data-played', played);
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
setTitle(button, button.getAttribute('data-type'), played);
}
2023-04-19 01:56:05 -04:00
function setTitle(button, itemType, played) {
if (itemType !== 'AudioBook' && itemType !== 'AudioPodcast') {
button.title = played ? globalize.translate('Watched') : globalize.translate('MarkPlayed');
} else {
button.title = played ? globalize.translate('Played') : globalize.translate('MarkPlayed');
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
const text = button.querySelector('.button-text');
if (text) {
text.innerText = button.title;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function clearEvents(button) {
button.removeEventListener('click', onClick);
removeNotificationEvent(button, 'UserDataChanged');
}
2023-04-19 01:56:05 -04:00
function bindEvents(button) {
clearEvents(button);
2023-04-19 01:56:05 -04:00
button.addEventListener('click', onClick);
addNotificationEvent(button, 'UserDataChanged', onUserDataChanged);
}
2023-04-19 01:56:05 -04:00
const EmbyPlaystateButtonPrototype = Object.create(EmbyButtonPrototype);
2023-04-19 01:56:05 -04:00
EmbyPlaystateButtonPrototype.createdCallback = function () {
// base method
if (EmbyButtonPrototype.createdCallback) {
EmbyButtonPrototype.createdCallback.call(this);
}
};
2023-04-19 01:56:05 -04:00
EmbyPlaystateButtonPrototype.attachedCallback = function () {
// base method
if (EmbyButtonPrototype.attachedCallback) {
EmbyButtonPrototype.attachedCallback.call(this);
}
2023-04-19 01:56:05 -04:00
const itemId = this.getAttribute('data-id');
const serverId = this.getAttribute('data-serverid');
if (itemId && serverId) {
setState(this, this.getAttribute('data-played') === 'true', false);
bindEvents(this);
}
};
EmbyPlaystateButtonPrototype.detachedCallback = function () {
// base method
if (EmbyButtonPrototype.detachedCallback) {
EmbyButtonPrototype.detachedCallback.call(this);
}
2023-04-19 01:56:05 -04:00
clearEvents(this);
this.iconElement = null;
};
EmbyPlaystateButtonPrototype.setItem = function (item) {
if (item) {
this.setAttribute('data-id', item.Id);
this.setAttribute('data-serverid', item.ServerId);
this.setAttribute('data-type', item.Type);
2023-07-06 13:39:48 -04:00
const played = item.UserData?.Played;
2023-04-19 01:56:05 -04:00
setState(this, played);
bindEvents(this);
} else {
this.removeAttribute('data-id');
this.removeAttribute('data-serverid');
this.removeAttribute('data-type');
this.removeAttribute('data-played');
clearEvents(this);
2023-04-19 01:56:05 -04:00
}
};
2023-04-19 01:56:05 -04:00
document.registerElement('emby-playstatebutton', {
prototype: EmbyPlaystateButtonPrototype,
extends: 'button'
});