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

170 lines
5.7 KiB
JavaScript
Raw Normal View History

2022-01-30 00:27:26 +03:00
import escapeHtml from 'escape-html';
import Events from '../utils/events.ts';
2024-08-15 02:33:50 -04:00
import globalize from '../lib/globalize';
2020-08-14 08:46:34 +02:00
import dom from '../scripts/dom';
2022-11-03 00:52:12 -04:00
import { formatRelative } from 'date-fns';
2020-08-14 08:46:34 +02:00
import serverNotifications from '../scripts/serverNotifications';
import '../elements/emby-button/emby-button';
2021-01-26 16:25:38 -05:00
import './listview/listview.scss';
import ServerConnections from './ServerConnections';
import alert from './alert';
import { getLocale } from '../utils/dateFnsLocale.ts';
2022-04-12 12:18:12 -04:00
import { toBoolean } from '../utils/string.ts';
2023-04-19 01:56:05 -04:00
function getEntryHtml(entry, apiClient) {
let html = '';
html += '<div class="listItem listItem-border">';
let color = '#00a4dc';
let icon = 'notifications';
if (entry.Severity == 'Error' || entry.Severity == 'Fatal' || entry.Severity == 'Warn') {
color = '#cc0000';
icon = 'notification_important';
}
2019-10-09 19:19:20 +03:00
2023-04-19 01:56:05 -04:00
if (entry.UserId && entry.UserPrimaryImageTag) {
html += '<span class="listItemIcon material-icons dvr" aria-hidden="true" style="width:2em!important;height:2em!important;padding:0;color:transparent;background-color:' + color + ";background-image:url('" + apiClient.getUserImageUrl(entry.UserId, {
type: 'Primary',
tag: entry.UserPrimaryImageTag
}) + "');background-repeat:no-repeat;background-position:center center;background-size: cover;\"></span>";
} else {
html += '<span class="listItemIcon material-icons ' + icon + '" aria-hidden="true" style="background-color:' + color + '"></span>';
}
2019-10-09 19:19:20 +03:00
2023-04-19 01:56:05 -04:00
html += '<div class="listItemBody three-line">';
html += '<div class="listItemBodyText">';
html += escapeHtml(entry.Name);
html += '</div>';
html += '<div class="listItemBodyText secondary">';
html += formatRelative(Date.parse(entry.Date), Date.now(), { locale: getLocale() });
html += '</div>';
html += '<div class="listItemBodyText secondary listItemBodyText-nowrap">';
html += escapeHtml(entry.ShortOverview || '');
html += '</div>';
html += '</div>';
if (entry.Overview) {
html += `<button type="button" is="paper-icon-button-light" class="btnEntryInfo" data-id="${entry.Id}" title="${globalize.translate('Info')}">
2022-02-24 20:15:24 +03:00
<span class="material-icons info" aria-hidden="true"></span>
2020-05-14 23:55:23 +02:00
</button>`;
2023-04-19 01:56:05 -04:00
}
2019-10-09 19:19:20 +03:00
2023-04-19 01:56:05 -04:00
html += '</div>';
2020-05-14 23:55:23 +02:00
2023-04-19 01:56:05 -04:00
return html;
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function renderList(elem, apiClient, result) {
elem.innerHTML = result.Items.map(function (i) {
return getEntryHtml(i, apiClient);
}).join('');
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function reloadData(instance, elem, apiClient, startIndex, limit) {
if (startIndex == null) {
startIndex = parseInt(elem.getAttribute('data-activitystartindex') || '0', 10);
}
2019-10-09 19:19:20 +03:00
2023-04-19 01:56:05 -04:00
limit = limit || parseInt(elem.getAttribute('data-activitylimit') || '7', 10);
const minDate = new Date();
const hasUserId = toBoolean(elem.getAttribute('data-useractivity'), true);
2019-10-09 19:19:20 +03:00
2023-04-19 01:56:05 -04:00
// TODO: Use date-fns
if (hasUserId) {
minDate.setTime(minDate.getTime() - 24 * 60 * 60 * 1000); // one day back
} else {
minDate.setTime(minDate.getTime() - 7 * 24 * 60 * 60 * 1000); // one week back
}
2019-10-09 19:19:20 +03:00
2023-04-19 01:56:05 -04:00
ApiClient.getJSON(ApiClient.getUrl('System/ActivityLog/Entries', {
startIndex: startIndex,
limit: limit,
minDate: minDate.toISOString(),
hasUserId: hasUserId
})).then(function (result) {
elem.setAttribute('data-activitystartindex', startIndex);
elem.setAttribute('data-activitylimit', limit);
if (!startIndex) {
const activityContainer = dom.parentWithClass(elem, 'activityContainer');
if (activityContainer) {
if (result.Items.length) {
activityContainer.classList.remove('hide');
} else {
activityContainer.classList.add('hide');
2019-10-09 19:19:20 +03:00
}
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2019-10-09 19:19:20 +03:00
2023-04-19 01:56:05 -04:00
instance.items = result.Items;
renderList(elem, apiClient, result);
});
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function onActivityLogUpdate(e, apiClient) {
const options = this.options;
2019-10-09 19:19:20 +03:00
2023-04-19 01:56:05 -04:00
if (options && options.serverId === apiClient.serverId()) {
reloadData(this, options.element, apiClient);
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 onListClick(e) {
const btnEntryInfo = dom.parentWithClass(e.target, 'btnEntryInfo');
2019-10-09 19:19:20 +03:00
2023-04-19 01:56:05 -04:00
if (btnEntryInfo) {
const id = btnEntryInfo.getAttribute('data-id');
const items = this.items;
2019-10-09 19:19:20 +03:00
2023-04-19 01:56:05 -04:00
if (items) {
const item = items.filter(function (i) {
return i.Id.toString() === id;
})[0];
2019-10-09 19:19:20 +03:00
2023-04-19 01:56:05 -04:00
if (item) {
showItemOverview(item);
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 showItemOverview(item) {
alert({
text: item.Overview
});
}
2018-10-23 01:05:09 +03:00
class ActivityLog {
constructor(options) {
2018-10-23 01:05:09 +03:00
this.options = options;
2020-07-20 08:40:13 +01:00
const element = options.element;
2020-05-04 12:44:12 +02:00
element.classList.add('activityLogListWidget');
element.addEventListener('click', onListClick.bind(this));
const apiClient = ServerConnections.getApiClient(options.serverId);
2018-10-23 01:05:09 +03:00
reloadData(this, element, apiClient);
2020-07-20 08:40:13 +01:00
const onUpdate = onActivityLogUpdate.bind(this);
2019-10-09 19:19:20 +03:00
this.updateFn = onUpdate;
2020-09-08 02:05:02 -04:00
Events.on(serverNotifications, 'ActivityLogEntry', onUpdate);
2020-05-04 12:44:12 +02:00
apiClient.sendMessage('ActivityLogEntryStart', '0,1500');
2018-10-23 01:05:09 +03:00
}
destroy() {
2020-07-20 08:40:13 +01:00
const options = this.options;
2019-10-09 19:19:20 +03:00
2018-10-23 01:05:09 +03:00
if (options) {
2020-05-04 12:44:12 +02:00
options.element.classList.remove('activityLogListWidget');
ServerConnections.getApiClient(options.serverId).sendMessage('ActivityLogEntryStop', '0,1500');
2018-10-23 01:05:09 +03:00
}
2019-10-09 19:19:20 +03:00
2020-07-20 08:40:13 +01:00
const onUpdate = this.updateFn;
2019-10-09 19:19:20 +03:00
if (onUpdate) {
2020-09-08 02:05:02 -04:00
Events.off(serverNotifications, 'ActivityLogEntry', onUpdate);
2019-10-09 19:19:20 +03:00
}
this.items = null;
this.options = null;
}
}
export default ActivityLog;