2020-08-14 08:46:34 +02:00
|
|
|
import loading from '../../../components/loading/loading';
|
|
|
|
import dom from '../../../scripts/dom';
|
|
|
|
import globalize from '../../../scripts/globalize';
|
|
|
|
import imageHelper from '../../../scripts/imagehelper';
|
|
|
|
import { formatDistanceToNow } from 'date-fns';
|
|
|
|
import { localeWithSuffix } from '../../../scripts/dfnshelper';
|
|
|
|
import '../../../elements/emby-button/emby-button';
|
|
|
|
import '../../../elements/emby-itemscontainer/emby-itemscontainer';
|
|
|
|
import '../../../components/cardbuilder/card.css';
|
2020-07-09 14:23:41 +01:00
|
|
|
|
|
|
|
/* eslint-disable indent */
|
2018-10-23 01:05:09 +03:00
|
|
|
|
2020-07-11 19:21:34 -04:00
|
|
|
// Local cache of loaded
|
2020-07-11 17:08:52 -04:00
|
|
|
let deviceIds = [];
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
function canDelete(deviceId) {
|
2019-11-06 13:43:39 +03:00
|
|
|
return deviceId !== ApiClient.deviceId();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-07-11 17:08:52 -04:00
|
|
|
function deleteAllDevices(page) {
|
2020-10-17 22:21:47 -04:00
|
|
|
const msg = globalize.translate('DeleteDevicesConfirmation');
|
2020-07-11 17:08:52 -04:00
|
|
|
|
|
|
|
require(['confirm'], async function (confirm) {
|
|
|
|
await confirm({
|
|
|
|
text: msg,
|
|
|
|
title: globalize.translate('HeaderDeleteDevices'),
|
|
|
|
confirmText: globalize.translate('ButtonDelete'),
|
|
|
|
primary: 'delete'
|
|
|
|
});
|
|
|
|
|
|
|
|
loading.show();
|
|
|
|
await Promise.all(
|
2020-10-17 22:21:32 -04:00
|
|
|
deviceIds.filter(canDelete).map((id) => ApiClient.deleteDevice(id))
|
2020-07-11 17:08:52 -04:00
|
|
|
);
|
|
|
|
loadData(page);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
function deleteDevice(page, id) {
|
2020-07-11 11:26:24 +01:00
|
|
|
const msg = globalize.translate('DeleteDeviceConfirmation');
|
2019-11-06 13:43:39 +03:00
|
|
|
|
2020-08-14 08:46:34 +02:00
|
|
|
import('../../../components/confirm/confirm').then(({default: confirm}) => {
|
2018-10-23 01:05:09 +03:00
|
|
|
confirm({
|
|
|
|
text: msg,
|
2020-05-04 12:44:12 +02:00
|
|
|
title: globalize.translate('HeaderDeleteDevice'),
|
2020-08-13 21:23:51 +09:00
|
|
|
confirmText: globalize.translate('Delete'),
|
2020-05-04 12:44:12 +02:00
|
|
|
primary: 'delete'
|
2020-07-11 17:08:52 -04:00
|
|
|
}).then(async () => {
|
2019-11-06 13:43:39 +03:00
|
|
|
loading.show();
|
2020-10-17 22:21:32 -04:00
|
|
|
await ApiClient.deleteDevice(id);
|
2020-07-11 17:08:52 -04:00
|
|
|
loadData(page);
|
2019-11-06 13:43:39 +03:00
|
|
|
});
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function showDeviceMenu(view, btn, deviceId) {
|
2020-07-19 17:38:42 +02:00
|
|
|
const menuItems = [];
|
2019-11-06 13:43:39 +03:00
|
|
|
|
|
|
|
if (canEdit) {
|
|
|
|
menuItems.push({
|
2020-05-04 12:44:12 +02:00
|
|
|
name: globalize.translate('Edit'),
|
|
|
|
id: 'open',
|
|
|
|
icon: 'mode_edit'
|
2019-11-06 13:43:39 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (canDelete(deviceId)) {
|
|
|
|
menuItems.push({
|
2020-05-04 12:44:12 +02:00
|
|
|
name: globalize.translate('Delete'),
|
|
|
|
id: 'delete',
|
|
|
|
icon: 'delete'
|
2019-11-06 13:43:39 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-14 08:46:34 +02:00
|
|
|
import('../../../components/actionSheet/actionSheet').then(({default: actionsheet}) => {
|
2018-10-23 01:05:09 +03:00
|
|
|
actionsheet.show({
|
|
|
|
items: menuItems,
|
|
|
|
positionTo: btn,
|
2019-11-06 13:43:39 +03:00
|
|
|
callback: function (id) {
|
2018-10-23 01:05:09 +03:00
|
|
|
switch (id) {
|
2020-05-04 12:44:12 +02:00
|
|
|
case 'open':
|
|
|
|
Dashboard.navigate('device.html?id=' + deviceId);
|
2018-10-23 01:05:09 +03:00
|
|
|
break;
|
2019-11-06 13:43:39 +03:00
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
case 'delete':
|
2019-11-06 13:43:39 +03:00
|
|
|
deleteDevice(view, deviceId);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
}
|
2019-11-06 13:43:39 +03:00
|
|
|
});
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function load(page, devices) {
|
2020-07-11 11:26:24 +01:00
|
|
|
let html = '';
|
2019-11-06 13:43:39 +03:00
|
|
|
html += devices.map(function (device) {
|
2020-07-11 11:26:24 +01:00
|
|
|
let deviceHtml = '';
|
2019-07-01 13:24:49 -07:00
|
|
|
deviceHtml += "<div data-id='" + device.Id + "' class='card backdropCard'>";
|
|
|
|
deviceHtml += '<div class="cardBox visualCardBox">';
|
|
|
|
deviceHtml += '<div class="cardScalable">';
|
|
|
|
deviceHtml += '<div class="cardPadder cardPadder-backdrop"></div>';
|
2020-05-04 12:44:12 +02:00
|
|
|
deviceHtml += '<a is="emby-linkbutton" href="' + (canEdit ? 'device.html?id=' + device.Id : '#') + '" class="cardContent cardImageContainer">';
|
2020-07-11 11:26:24 +01:00
|
|
|
const iconUrl = imageHelper.getDeviceIcon(device);
|
2019-11-06 13:43:39 +03:00
|
|
|
|
2019-07-01 13:24:49 -07:00
|
|
|
if (iconUrl) {
|
|
|
|
deviceHtml += '<div class="cardImage" style="background-image:url(\'' + iconUrl + "');background-size: auto 64%;background-position:center center;\">";
|
2020-05-04 12:44:12 +02:00
|
|
|
deviceHtml += '</div>';
|
2019-07-01 13:24:49 -07:00
|
|
|
} else {
|
2020-04-26 02:37:28 +03:00
|
|
|
deviceHtml += '<span class="cardImageIcon material-icons tablet_android"></span>';
|
2019-07-01 13:24:49 -07:00
|
|
|
}
|
2019-11-06 13:43:39 +03:00
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
deviceHtml += '</a>';
|
|
|
|
deviceHtml += '</div>';
|
2019-07-01 13:24:49 -07:00
|
|
|
deviceHtml += '<div class="cardFooter">';
|
2019-11-06 13:43:39 +03:00
|
|
|
|
2019-07-01 13:24:49 -07:00
|
|
|
if (canEdit || canDelete(device.Id)) {
|
|
|
|
deviceHtml += '<div style="text-align:right; float:right;padding-top:5px;">';
|
2020-05-16 18:07:20 +02:00
|
|
|
deviceHtml += '<button type="button" is="paper-icon-button-light" data-id="' + device.Id + '" title="' + globalize.translate('Menu') + '" class="btnDeviceMenu"><span class="material-icons more_vert"></span></button>';
|
2020-05-04 12:44:12 +02:00
|
|
|
deviceHtml += '</div>';
|
2019-07-01 13:24:49 -07:00
|
|
|
}
|
2019-11-06 13:43:39 +03:00
|
|
|
|
2020-08-14 08:46:34 +02:00
|
|
|
deviceHtml += "<div class='c" +
|
|
|
|
"ardText'>";
|
2019-07-01 13:24:49 -07:00
|
|
|
deviceHtml += device.Name;
|
2020-05-04 12:44:12 +02:00
|
|
|
deviceHtml += '</div>';
|
2019-07-01 13:24:49 -07:00
|
|
|
deviceHtml += "<div class='cardText cardText-secondary'>";
|
2020-05-04 12:44:12 +02:00
|
|
|
deviceHtml += device.AppName + ' ' + device.AppVersion;
|
|
|
|
deviceHtml += '</div>';
|
2019-07-01 13:24:49 -07:00
|
|
|
deviceHtml += "<div class='cardText cardText-secondary'>";
|
2019-11-06 13:43:39 +03:00
|
|
|
|
2019-07-01 13:24:49 -07:00
|
|
|
if (device.LastUserName) {
|
|
|
|
deviceHtml += device.LastUserName;
|
2020-08-14 08:46:34 +02:00
|
|
|
deviceHtml += ', ' + formatDistanceToNow(Date.parse(device.DateLastActivity), localeWithSuffix);
|
2019-07-01 13:24:49 -07:00
|
|
|
}
|
2019-11-06 13:43:39 +03:00
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
deviceHtml += ' ';
|
|
|
|
deviceHtml += '</div>';
|
|
|
|
deviceHtml += '</div>';
|
|
|
|
deviceHtml += '</div>';
|
|
|
|
deviceHtml += '</div>';
|
2019-07-01 14:29:46 -07:00
|
|
|
return deviceHtml;
|
2020-05-04 12:44:12 +02:00
|
|
|
}).join('');
|
|
|
|
page.querySelector('.devicesList').innerHTML = html;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadData(page) {
|
2019-11-06 13:43:39 +03:00
|
|
|
loading.show();
|
2020-05-04 12:44:12 +02:00
|
|
|
ApiClient.getJSON(ApiClient.getUrl('Devices')).then(function (result) {
|
2019-11-06 13:43:39 +03:00
|
|
|
load(page, result.Items);
|
2020-07-11 17:08:52 -04:00
|
|
|
deviceIds = result.Items.map((device) => device.Id);
|
2019-11-06 13:43:39 +03:00
|
|
|
loading.hide();
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-11-06 13:43:39 +03:00
|
|
|
|
2020-07-11 11:26:24 +01:00
|
|
|
const canEdit = ApiClient.isMinServerVersion('3.4.1.31');
|
2020-07-09 14:23:41 +01:00
|
|
|
export default function (view, params) {
|
2020-05-04 12:44:12 +02:00
|
|
|
view.querySelector('.devicesList').addEventListener('click', function (e) {
|
2020-07-11 11:26:24 +01:00
|
|
|
const btnDeviceMenu = dom.parentWithClass(e.target, 'btnDeviceMenu');
|
2019-11-06 13:43:39 +03:00
|
|
|
|
|
|
|
if (btnDeviceMenu) {
|
2020-05-04 12:44:12 +02:00
|
|
|
showDeviceMenu(view, btnDeviceMenu, btnDeviceMenu.getAttribute('data-id'));
|
2019-11-06 13:43:39 +03:00
|
|
|
}
|
|
|
|
});
|
2020-05-04 12:44:12 +02:00
|
|
|
view.addEventListener('viewshow', function () {
|
2019-11-06 13:43:39 +03:00
|
|
|
loadData(this);
|
|
|
|
});
|
2020-07-09 14:23:41 +01:00
|
|
|
|
2020-07-11 17:08:52 -04:00
|
|
|
view.querySelector('#deviceDeleteAll').addEventListener('click', function() {
|
|
|
|
deleteAllDevices(view);
|
|
|
|
});
|
|
|
|
}
|
2020-07-09 14:23:41 +01:00
|
|
|
/* eslint-enable indent */
|