summaryrefslogtreecommitdiff
path: root/web/static/app.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'web/static/app.mjs')
-rw-r--r--web/static/app.mjs70
1 files changed, 61 insertions, 9 deletions
diff --git a/web/static/app.mjs b/web/static/app.mjs
index 429bef7..b31177a 100644
--- a/web/static/app.mjs
+++ b/web/static/app.mjs
@@ -1,8 +1,40 @@
import { Client } from './client.mjs';
+import { catalogViews } from './catalog.mjs';
const $ = id => document.getElementById(id);
const text = new TextDecoder('utf-8', { fatal: true });
let client = null, current = null, currentPath = '/', busy = false, dirty = false;
let configuration = null;
+let activeView = 'files';
+const catalog = catalogViews(pathLink);
+
+async function loadConfiguration() {
+ if (!configuration) {
+ const response = await fetch('/_cloud9/config.json');
+ if (!response.ok) throw new Error('Unable to load server settings. Reload to try again.');
+ configuration = await response.json();
+ for (const view of ['sites', 'services']) $(`view-${view}`).hidden = !configuration.catalog;
+ }
+ return configuration;
+}
+function locationView() {
+ const view = new URLSearchParams(location.search).get('view');
+ return configuration?.catalog && ['sites', 'services'].includes(view) ? view : 'files';
+}
+function viewURL(view, path = currentPath) {
+ return pathURL(path) + (view === 'files' ? '' : `?view=${view}`);
+}
+function showView(view) {
+ activeView = view;
+ for (const name of ['files', 'sites', 'services']) {
+ $(`${name}-view`).hidden = view !== name;
+ const link = $(`view-${name}`);
+ link.classList.toggle('selected', view === name);
+ if (view === name) link.setAttribute('aria-current', 'page'); else link.removeAttribute('aria-current');
+ link.href = viewURL(name);
+ }
+ document.title = `${view === 'files' ? currentPath : view} — cloud9`;
+ catalog.show(view);
+}
function status(message, error = false) {
$('status').textContent = message;
@@ -17,7 +49,7 @@ function controls() {
$('delete').disabled = busy || currentPath === '/';
$('editor').readOnly = busy;
$('edit-status').textContent = dirty ? 'Unsaved changes.' : 'No unsaved changes.';
- document.querySelector('main').setAttribute('aria-busy', String(busy));
+ document.querySelector('#files-view main').setAttribute('aria-busy', String(busy));
}
async function action(operation) {
if (busy) return;
@@ -29,11 +61,7 @@ async function action(operation) {
}
async function connectedClient() {
if (client && !client.closed) return client;
- if (!configuration) {
- const response = await fetch('/_cloud9/config.json');
- if (!response.ok) throw new Error('Unable to load server settings. Reload to try again.');
- configuration = await response.json();
- }
+ await loadConfiguration();
try { client = await Client.connect('/_cloud9/9p', configuration.user, configuration.tree); }
catch { throw new Error('Unable to reach the file server. Reload to try again.'); }
return client;
@@ -105,10 +133,12 @@ function breadcrumbs(path) {
}
async function navigate(path, historyMode = 'push') {
path = normalizePath(path);
+ showView('files');
stopFollow();
status('Loading…');
const result = await readPath(path);
current = result; currentPath = path; dirty = false;
+ for (const view of ['files', 'sites', 'services']) $(`view-${view}`).href = viewURL(view);
$('path').value = path;
const url = pathURL(path);
if (historyMode === 'push' && location.pathname + location.search + location.hash !== url) history.pushState(null, '', url);
@@ -149,6 +179,17 @@ async function navigate(path, historyMode = 'push') {
}
function mayLeave() { return !dirty || window.confirm('Discard unsaved changes?'); }
document.addEventListener('click', event => {
+ const tab = event.target.closest('a[data-view]');
+ if (tab && event.button === 0 && !event.ctrlKey && !event.metaKey && !event.shiftKey && !event.altKey) {
+ event.preventDefault();
+ if (busy || tab.hidden) return;
+ const view = tab.dataset.view;
+ if (view === activeView) return;
+ history.pushState(null, '', viewURL(view));
+ showView(view);
+ if (view === 'files' && !current) action(() => navigate(currentPath, 'none')).then(loadTree);
+ return;
+ }
const link = event.target.closest('a[data-path]');
if (!link || event.button !== 0 || event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) return;
event.preventDefault();
@@ -180,8 +221,10 @@ $('download').onclick = () => {
setTimeout(() => URL.revokeObjectURL(url), 1000);
};
window.addEventListener('popstate', () => {
- if (busy || !mayLeave()) { history.pushState(null, '', pathURL(currentPath)); return; }
- action(() => navigate(locationPath(), 'none'));
+ const view = locationView(), path = locationPath();
+ if (busy || (view === 'files' && path !== currentPath && !mayLeave())) { history.pushState(null, '', viewURL(activeView)); return; }
+ showView(view);
+ if (view === 'files' && (!current || path !== currentPath)) action(() => navigate(path, 'none')).then(loadTree);
});
window.addEventListener('beforeunload', event => {
if (dirty) { event.preventDefault(); event.returnValue = ''; }
@@ -357,4 +400,13 @@ $('upload-input').onchange = () => {
$('upload-input').value = '';
};
-action(() => navigate(locationPath(), 'none')).then(loadTree);
+async function start() {
+ try {
+ await loadConfiguration();
+ currentPath = locationPath();
+ const view = locationView();
+ showView(view);
+ if (view === 'files') await action(() => navigate(currentPath, 'none')).then(loadTree);
+ } catch (error) { status(error.message, true); }
+}
+start();