2019-10-13 22:59:28 +02:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
2020-01-07 11:48:38 +01:00
|
|
|
<meta charset="UTF-8">
|
2020-01-03 13:01:50 +01:00
|
|
|
<title>Tabs</title>
|
2019-10-13 22:59:28 +02:00
|
|
|
|
|
|
|
<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">
|
2020-01-07 11:48:38 +01:00
|
|
|
<link rel="stylesheet" href="index.css">
|
2019-10-13 22:59:28 +02:00
|
|
|
</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 {
|
2020-01-04 15:20:02 +01:00
|
|
|
remote,
|
|
|
|
ipcRenderer,
|
2019-10-13 22:59:28 +02:00
|
|
|
} = require('electron');
|
2020-01-04 15:20:02 +01:00
|
|
|
const {
|
|
|
|
Menu,
|
|
|
|
MenuItem,
|
2020-01-04 15:49:23 +01:00
|
|
|
dialog,
|
2020-01-04 15:20:02 +01:00
|
|
|
} = remote;
|
|
|
|
|
|
|
|
const icons = [];
|
2019-10-13 22:59:28 +02:00
|
|
|
|
|
|
|
let services = [];
|
|
|
|
let selectedService = 0;
|
|
|
|
let forwardButton;
|
|
|
|
let backButton;
|
|
|
|
let addButton;
|
|
|
|
|
2020-01-04 15:20:02 +01:00
|
|
|
|
|
|
|
// Service context menu
|
|
|
|
const serviceContextMenu = new Menu();
|
2020-01-07 12:02:39 +01:00
|
|
|
serviceContextMenu.append(new MenuItem({
|
|
|
|
label: 'Close', click: () => {
|
|
|
|
unloadService(serviceContextMenu.serviceId);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
serviceContextMenu.append(new MenuItem({type: "separator"}));
|
2020-01-04 15:49:23 +01:00
|
|
|
serviceContextMenu.append(new MenuItem({
|
2020-01-04 15:20:02 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}));
|
2020-01-04 15:20:02 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-10-13 22:59:28 +02:00
|
|
|
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++) {
|
2020-01-04 15:20:02 +01:00
|
|
|
createService(i);
|
|
|
|
}
|
2020-01-04 15:49:23 +01:00
|
|
|
|
|
|
|
if (actualSelectedService < 0 || actualSelectedService >= services.length) {
|
|
|
|
actualSelectedService = 0;
|
|
|
|
}
|
2020-01-04 15:20:02 +01:00
|
|
|
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');
|
2020-01-04 15:20:02 +01:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
|
2020-01-04 15:20:02 +01:00
|
|
|
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 => {
|
2019-10-13 22:59:28 +02:00
|
|
|
icon.classList.add(cl);
|
|
|
|
});
|
|
|
|
}
|
2020-01-04 15:20:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
button.appendChild(icon);
|
|
|
|
li.appendChild(button);
|
|
|
|
li.button = button;
|
2019-10-13 22:59:28 +02:00
|
|
|
|
2020-01-04 15:20:02 +01:00
|
|
|
const nav = document.querySelector('#service-selector');
|
|
|
|
if (nextNavButton === nav || nextNavButton === undefined) {
|
2019-10-13 22:59:28 +02:00
|
|
|
nav.appendChild(li);
|
2020-01-04 15:20:02 +01:00
|
|
|
} else {
|
|
|
|
nav.insertBefore(li, nextNavButton);
|
2019-10-13 22:59:28 +02:00
|
|
|
}
|
2020-01-07 11:48:38 +01:00
|
|
|
|
|
|
|
if (service.autoLoad) {
|
|
|
|
loadService(index, service);
|
|
|
|
}
|
2020-01-04 15:20:02 +01:00
|
|
|
}
|
2019-10-13 22:59:28 +02:00
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
forwardButton = document.querySelector('#forward');
|
|
|
|
forwardButton.addEventListener('click', () => goForward());
|
|
|
|
|
|
|
|
backButton = document.querySelector('#back');
|
|
|
|
backButton.addEventListener('click', () => goBack());
|
|
|
|
|
|
|
|
addButton = document.querySelector('#add-button');
|
2020-01-04 15:20:02 +01:00
|
|
|
addButton.addEventListener('click', () => ipcRenderer.send('openServiceSettings', null));
|
2019-10-13 22:59:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
function setActiveService(serviceId) {
|
2020-01-07 12:02:39 +01:00
|
|
|
const currentService = services[serviceId];
|
2019-10-13 22:59:28 +02:00
|
|
|
process.nextTick(() => {
|
2020-01-07 11:48:38 +01:00
|
|
|
loadService(serviceId, currentService);
|
2019-10-13 22:59:28 +02:00
|
|
|
|
|
|
|
// Hide previous service
|
2020-01-04 15:49:23 +01:00
|
|
|
if (services[selectedService] && services[selectedService].view) {
|
2019-10-13 22:59:28 +02:00
|
|
|
services[selectedService].view.classList.remove('active');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show service
|
|
|
|
currentService.view.classList.add('active');
|
|
|
|
|
|
|
|
// Save active service ID
|
|
|
|
selectedService = serviceId;
|
|
|
|
|
|
|
|
// Refresh navigation
|
|
|
|
updateNavigation();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-07 11:48:38 +01:00
|
|
|
function loadService(serviceId, service) {
|
|
|
|
// Load service if not loaded yet
|
2020-01-07 12:02:39 +01:00
|
|
|
if (!service.view && !service.viewReady) {
|
|
|
|
document.querySelector('#services > .loader').classList.remove('hidden');
|
2020-01-07 11:48:38 +01:00
|
|
|
service.view = document.createElement('webview');
|
|
|
|
service.view.setAttribute('src', service.url);
|
|
|
|
service.view.setAttribute('partition', 'persist:service_' + service.partition);
|
|
|
|
service.view.setAttribute('autosize', "true");
|
|
|
|
|
2020-01-07 12:52:41 +01:00
|
|
|
// Append element to DOM
|
2020-01-07 11:48:38 +01:00
|
|
|
document.querySelector('#services').appendChild(service.view);
|
2020-01-07 12:52:41 +01:00
|
|
|
|
|
|
|
// On load event
|
2020-01-07 11:48:38 +01:00
|
|
|
service.view.addEventListener('dom-ready', () => {
|
2020-01-07 12:52:41 +01:00
|
|
|
if (service.customCSS) {
|
|
|
|
service.view.insertCSS(service.customCSS);
|
|
|
|
}
|
|
|
|
|
2020-01-07 12:02:39 +01:00
|
|
|
document.querySelector('#services > .loader').classList.add('hidden');
|
2020-01-07 11:48:38 +01:00
|
|
|
updateNavigation();
|
2020-01-07 12:02:39 +01:00
|
|
|
service.li.classList.add('loaded');
|
|
|
|
service.viewReady = true;
|
2020-01-07 11:48:38 +01:00
|
|
|
});
|
2020-01-07 12:52:41 +01:00
|
|
|
|
|
|
|
// Load favicon
|
2020-01-07 11:48:38 +01:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-01-07 12:02:39 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-07 11:48:38 +01:00
|
|
|
|
2020-01-07 12:02:39 +01:00
|
|
|
function unloadService(serviceId) {
|
|
|
|
const service = services[serviceId];
|
|
|
|
if (service.view && service.viewReady) {
|
|
|
|
service.view.remove();
|
|
|
|
service.view = null;
|
|
|
|
service.li.classList.remove('loaded');
|
|
|
|
service.viewReady = false;
|
|
|
|
|
|
|
|
if (parseInt(selectedService) === serviceId) {
|
|
|
|
selectedService = null;
|
|
|
|
for (let i = 0; i < services.length; i++) {
|
|
|
|
if (services[i].view && services[i].viewReady) {
|
|
|
|
setActiveService(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (selectedService === null) {
|
|
|
|
updateNavigation();
|
|
|
|
}
|
|
|
|
}
|
2020-01-07 11:48:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 22:59:28 +02:00
|
|
|
function updateNavigation() {
|
|
|
|
console.debug('Updating navigation');
|
|
|
|
// Update active list element
|
|
|
|
for (let i = 0; i < services.length; i++) {
|
2020-01-07 12:02:39 +01:00
|
|
|
const service = services[i];
|
2019-10-13 22:59:28 +02:00
|
|
|
if (parseInt(selectedService) === i) {
|
|
|
|
service.li.classList.add('active');
|
|
|
|
} else {
|
|
|
|
service.li.classList.remove('active');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 12:02:39 +01:00
|
|
|
if (selectedService !== null && services[selectedService].viewReady) {
|
2019-10-13 22:59:28 +02:00
|
|
|
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>
|