jellyfin-web/src/components/activitylog.js

173 lines
6.1 KiB
JavaScript
Raw Normal View History

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