tabs/resources/index.html

312 lines
10 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tabs</title>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css"
integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<link rel="stylesheet" href="layout.css">
<link rel="stylesheet" href="index.css">
</head>
<body>
<div id="navigation">
<div id="history">
<button id="back"><i class="fas fa-arrow-left"></i></button>
<button id="forward" class="disabled"><i class="fas fa-arrow-right"></i></button>
</div>
<ul id="service-selector"></ul>
<button id="add-button"><i class="fa fa-plus"></i></button>
<button id="settings-button"><i class="fa fa-cog"></i></button>
</div>
<div id="services">
<div class="loader"></div>
</div>
<script>
const {
remote,
ipcRenderer,
} = require('electron');
const {
Menu,
MenuItem,
2020-01-04 15:49:23 +01:00
dialog,
} = remote;
const icons = [];
let services = [];
let selectedService = 0;
let forwardButton;
let backButton;
let addButton;
// Service context menu
const serviceContextMenu = new Menu();
2020-01-04 15:49:23 +01:00
serviceContextMenu.append(new MenuItem({
label: 'Edit', click: () => {
ipcRenderer.send('openServiceSettings', serviceContextMenu.serviceId);
}
2020-01-04 15:49:23 +01:00
}));
serviceContextMenu.append(new MenuItem({
label: 'Delete', click: () => {
dialog.showMessageBox(remote.getCurrentWindow(), {
type: 'question',
title: 'Confirm',
message: 'Are you sure you want to delete this service?',
buttons: ['Cancel', 'Confirm'],
cancelId: 0,
}).then(result => {
if (result.response === 1) {
ipcRenderer.send('deleteService', serviceContextMenu.serviceId);
}
}).catch(console.error);
}
}));
function openServiceContextMenu(event, index) {
event.preventDefault();
serviceContextMenu.serviceId = index;
serviceContextMenu.popup({window: remote.getCurrentWindow()});
}
ipcRenderer.on('data', (event, brandIcons, solidIcons, actualServices, actualSelectedService) => {
for (const icon of brandIcons) {
icons.push(icon);
}
for (const icon of solidIcons) {
icons.push(icon);
}
console.log('Updating services ...');
services = actualServices;
const nav = document.querySelector('#service-selector');
while (nav.children.length > 0) {
nav.removeChild(nav.children[0]);
}
const serviceContainer = document.querySelector('#services');
serviceContainer.querySelectorAll(":scope > webview").forEach(w => serviceContainer.removeChild(w));
for (let i = 0; i < services.length; i++) {
createService(i);
}
2020-01-04 15:49:23 +01:00
if (actualSelectedService < 0 || actualSelectedService >= services.length) {
actualSelectedService = 0;
}
setActiveService(actualSelectedService);
});
ipcRenderer.on('updateService', (e, id, data) => {
if (id === null) {
services.push(data);
createService(services.length - 1);
} else {
2020-01-04 15:49:23 +01:00
const nav = document.querySelector('#service-selector');
// Remove nav
const oldNavButton = nav.querySelector('li:nth-of-type(' + (id + 1) + ')');
const nextNavButton = oldNavButton.nextSibling;
nav.removeChild(oldNavButton);
// Remove webview
if (services[id].view) {
const serviceContainer = document.querySelector('#services');
serviceContainer.removeChild(services[id].view);
}
// Create new service
services[id] = data;
createService(id, nextNavButton);
if (parseInt(selectedService) === id) {
setActiveService(id);
}
}
});
2020-01-04 15:49:23 +01:00
ipcRenderer.on('deleteService', (e, id) => {
const nav = document.querySelector('#service-selector');
// Remove nav
const navButton = nav.querySelector('li:nth-of-type(' + (id + 1) + ')');
if (navButton) {
nav.removeChild(navButton);
}
// Remove webview
if (services[id].view) {
const serviceContainer = document.querySelector('#services');
serviceContainer.removeChild(services[id].view);
}
if (parseInt(selectedService) === id) {
setActiveService(0);
}
delete services[id];
services = services.filter(s => s !== null);
});
function createService(index, nextNavButton) {
let service = services[index];
let li = document.createElement('li');
service.li = li;
let button = document.createElement('button');
button.dataset.serviceId = '' + index;
button.dataset.tooltip = service.name;
button.addEventListener('click', () => {
setActiveService(button.dataset.serviceId);
ipcRenderer.send('setActiveService', button.dataset.serviceId);
});
button.addEventListener('contextmenu', e => openServiceContextMenu(e, index));
let icon;
if (service.useFavicon && service.favicon != null) {
icon = document.createElement('img');
icon.src = service.favicon;
icon.alt = service.name;
} else if (service.isImage) {
icon = document.createElement('img');
icon.src = service.icon;
icon.alt = service.name;
} else {
icon = document.createElement('i');
const iconProperties = icons.find(i => i.name === service.icon);
if (iconProperties) {
iconProperties.faIcon.split(' ').forEach(cl => {
icon.classList.add(cl);
});
}
}
button.appendChild(icon);
li.appendChild(button);
li.button = button;
const nav = document.querySelector('#service-selector');
if (nextNavButton === nav || nextNavButton === undefined) {
nav.appendChild(li);
} else {
nav.insertBefore(li, nextNavButton);
}
if (service.autoLoad) {
loadService(index, service);
}
}
document.addEventListener('DOMContentLoaded', () => {
forwardButton = document.querySelector('#forward');
forwardButton.addEventListener('click', () => goForward());
backButton = document.querySelector('#back');
backButton.addEventListener('click', () => goBack());
addButton = document.querySelector('#add-button');
addButton.addEventListener('click', () => ipcRenderer.send('openServiceSettings', null));
});
function setActiveService(serviceId) {
let currentService = services[serviceId];
process.nextTick(() => {
loadService(serviceId, currentService);
// Hide previous service
2020-01-04 15:49:23 +01:00
if (services[selectedService] && services[selectedService].view) {
services[selectedService].view.classList.remove('active');
}
// Show service
currentService.view.classList.add('active');
// Save active service ID
selectedService = serviceId;
// Refresh navigation
updateNavigation();
});
}
function loadService(serviceId, service) {
// Load service if not loaded yet
if (!service.view) {
service.view = document.createElement('webview');
service.view.setAttribute('src', service.url);
service.view.setAttribute('partition', 'persist:service_' + service.partition);
service.view.setAttribute('autosize', "true");
document.querySelector('#services').appendChild(service.view);
service.view.addEventListener('dom-ready', () => {
service.viewReady = true;
updateNavigation();
});
service.view.addEventListener('page-favicon-updated', event => {
console.debug('Loaded favicons for', service.name, event.favicons);
if (event.favicons.length > 0) {
ipcRenderer.send('setServiceFavicon', serviceId, event.favicons[0]);
if (service.useFavicon) {
const img = document.createElement('img');
img.src = event.favicons[0];
img.alt = service.name;
img.onload = () => {
service.li.button.innerHTML = '';
service.li.button.appendChild(img);
};
}
}
});
service.li.classList.add('loaded');
}
}
function updateNavigation() {
console.debug('Updating navigation');
// Update active list element
for (let i = 0; i < services.length; i++) {
let service = services[i];
if (parseInt(selectedService) === i) {
service.li.classList.add('active');
} else {
service.li.classList.remove('active');
}
}
if (services[selectedService].viewReady) {
console.debug('Updating navigation buttons because view is ready');
// Update history navigation
let view = services[selectedService].view;
if (view && view.canGoForward()) forwardButton.classList.remove('disabled');
else forwardButton.classList.add('disabled');
if (view && view.canGoBack()) backButton.classList.remove('disabled');
else backButton.classList.add('disabled');
}
}
function goForward() {
let view = services[selectedService].view;
if (view) view.getWebContents().goForward();
}
function goBack() {
let view = services[selectedService].view;
if (view) view.getWebContents().goBack();
}
</script>
</body>
</html>