2020-08-14 08:46:34 +02:00
|
|
|
import serverNotifications from '../../scripts/serverNotifications';
|
|
|
|
import globalize from '../../scripts/globalize';
|
2022-10-14 10:53:16 -04:00
|
|
|
import Events from '../../utils/events.ts';
|
2020-08-14 08:46:34 +02:00
|
|
|
import EmbyButtonPrototype from '../../elements/emby-button/emby-button';
|
2020-10-17 19:08:56 +01:00
|
|
|
import ServerConnections from '../../components/ServerConnections';
|
2020-07-08 18:01:19 +01:00
|
|
|
|
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');
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
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');
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2023-04-19 01:56:05 -04:00
|
|
|
}
|
2022-01-18 14:51:19 +03: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);
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
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');
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
function bindEvents(button) {
|
|
|
|
clearEvents(button);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
button.addEventListener('click', onClick);
|
|
|
|
addNotificationEvent(button, 'UserDataChanged', onUserDataChanged);
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
const EmbyPlaystateButtonPrototype = Object.create(EmbyButtonPrototype);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
EmbyPlaystateButtonPrototype.createdCallback = function () {
|
|
|
|
// base method
|
|
|
|
if (EmbyButtonPrototype.createdCallback) {
|
|
|
|
EmbyButtonPrototype.createdCallback.call(this);
|
|
|
|
}
|
|
|
|
};
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
EmbyPlaystateButtonPrototype.attachedCallback = function () {
|
|
|
|
// base method
|
|
|
|
if (EmbyButtonPrototype.attachedCallback) {
|
|
|
|
EmbyButtonPrototype.attachedCallback.call(this);
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
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);
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
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');
|
2019-01-10 15:39:37 +03:00
|
|
|
clearEvents(this);
|
2023-04-19 01:56:05 -04:00
|
|
|
}
|
|
|
|
};
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2023-04-19 01:56:05 -04:00
|
|
|
document.registerElement('emby-playstatebutton', {
|
|
|
|
prototype: EmbyPlaystateButtonPrototype,
|
|
|
|
extends: 'button'
|
|
|
|
});
|
2020-07-08 18:01:19 +01:00
|
|
|
|