Files
Radicale/radicale/web/internal_data/js/scenes/CollectionsScene.js
2026-03-15 00:33:22 +01:00

246 lines
11 KiB
JavaScript

/**
* This file is part of Radicale Server - Calendar Server
* Copyright © 2017-2024 Unrud <unrud@outlook.com>
* Copyright © 2023-2024 Matthew Hana <matthew.hana@gmail.com>
* Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
* Copyright © 2026-2026 Max Berger <max@berger.name>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { get_collections } from "../api/api.js";
import { discover_server_features } from "../api/sharing.js";
import { SERVER } from "../constants.js";
import { Collection, CollectionType } from "../models/collection.js";
import { bytesToHumanReadable } from "../utils/misc.js";
import { CreateEditCollectionScene } from "./CreateEditCollectionScene.js";
import { DeleteCollectionScene } from "./DeleteCollectionScene.js";
import { IncomingSharingScene } from "./IncomingSharingScene.js";
import { LoadingScene } from "./LoadingScene.js";
import { Scene, is_current_scene, pop_scene, push_scene } from "./scene_manager.js";
import { ShareCollectionScene, maybe_enable_sharing_options } from "./ShareCollectionScene.js";
import { UploadCollectionScene } from "./UploadCollectionScene.js";
/**
* @implements {Scene}
*/
export class CollectionsScene {
/**
* @param {string} user
* @param {string} password
* @param {Collection} principal_collection The princial collection
* @param {function(string):void} onerror Called when an error occurs, before the
* scene is popped.
*/
constructor(user, password, principal_collection, onerror) {
/** @type {HTMLElement} */ let html_scene = document.getElementById("collectionsscene");
/** @type {HTMLElement} */ let template = html_scene.querySelector("[data-name=collectiontemplate]");
/** @type {HTMLElement} */ let new_btn = html_scene.querySelector("[data-name=new]");
/** @type {HTMLElement} */ let upload_btn = html_scene.querySelector("[data-name=upload]");
/** @type {HTMLElement} */ let incomingshares_btn = html_scene.querySelector("[data-name=incomingshares]");
/** @type {?XMLHttpRequest} */ let collections_req = null;
/** @type {?Array<Collection>} */ let child_collections = null;
/** @type {Array<HTMLElement>} */ let nodes = [];
function onnew() {
try {
let create_collection_scene = new CreateEditCollectionScene(user, password, principal_collection);
push_scene(create_collection_scene);
} catch (err) {
console.error(err);
}
return false;
}
function onupload() {
try {
let upload_scene = new UploadCollectionScene(user, password, principal_collection);
push_scene(upload_scene);
} catch (err) {
console.error(err);
}
return false;
}
function onincomingshares() {
try {
let incoming_sharing_scene = new IncomingSharingScene(user, password);
push_scene(incoming_sharing_scene);
} catch (err) {
console.error(err);
}
return false;
}
/**
* @param {Collection} collection
*/
function onedit(collection) {
try {
let edit_collection_scene = new CreateEditCollectionScene(user, password, collection);
push_scene(edit_collection_scene);
} catch (err) {
console.error(err);
}
return false;
}
/**
* @param {Collection} collection
*/
function onshare(collection) {
try {
let share_collection_scene = new ShareCollectionScene(user, password, collection);
push_scene(share_collection_scene);
} catch (err) {
console.error(err);
}
return false;
}
/**
* @param {Collection} collection
*/
function ondelete(collection) {
try {
let delete_collection_scene = new DeleteCollectionScene(user, password, collection);
push_scene(delete_collection_scene);
} catch (err) {
console.error(err);
}
return false;
}
/**
* @param {any[]} collections
*/
function show_collections(collections) {
/** @type {HTMLElement} */ let navBar = document.querySelector("#logoutview");
let heightOfNavBar = navBar.offsetHeight + "px";
html_scene.style.marginTop = heightOfNavBar;
html_scene.style.height = "calc(100vh - " + heightOfNavBar + ")";
collections.forEach(function (/** @type {Collection} */ collection) {
/** @type {HTMLElement} */ let node = /** @type {HTMLElement} */(template.cloneNode(true));
node.classList.remove("hidden");
/** @type {HTMLElement} */ let title_form = node.querySelector("[data-name=title]");
/** @type {HTMLElement} */ let description_form = node.querySelector("[data-name=description]");
/** @type {HTMLElement} */ let contentcount_form = node.querySelector("[data-name=contentcount]");
/** @type {HTMLInputElement} */ let url_form = node.querySelector("[data-name=url]");
/** @type {HTMLElement} */ let color_form = node.querySelector("[data-name=color]");
/** @type {HTMLElement} */ let delete_btn = node.querySelector("[data-name=delete]");
/** @type {HTMLElement} */ let edit_btn = node.querySelector("[data-name=edit]");
/** @type {HTMLElement} */ let share_btn = node.querySelector("[data-name=share]");
/** @type {HTMLAnchorElement} */ let download_btn = node.querySelector("[data-name=download]");
if (collection.color) {
color_form.style.background = collection.color;
}
let possible_types = [CollectionType.ADDRESSBOOK, CollectionType.WEBCAL];
[CollectionType.CALENDAR, ""].forEach(function (e) {
[CollectionType.union(e, CollectionType.JOURNAL), e].forEach(function (e) {
[CollectionType.union(e, CollectionType.TASKS), e].forEach(function (e) {
if (e) {
possible_types.push(e);
}
});
});
});
possible_types.forEach(function (e) {
if (e !== collection.type) {
node.querySelector("[data-name=" + e + "]").classList.add("hidden");
}
});
title_form.textContent = collection.displayname || collection.href;
if (title_form.textContent.length > 30) {
title_form.classList.add("smalltext");
}
description_form.textContent = collection.description;
if (description_form.textContent.length > 150) {
description_form.classList.add("smalltext");
}
if (collection.type != CollectionType.WEBCAL) {
let contentcount_form_txt = (collection.contentcount > 0 ? Number(collection.contentcount).toLocaleString() : "No") + " item" + (collection.contentcount == 1 ? "" : "s") + " in collection";
if (collection.contentcount > 0) {
contentcount_form_txt += " (" + bytesToHumanReadable(collection.size) + ")";
}
contentcount_form.textContent = contentcount_form_txt;
}
let href = SERVER + collection.href;
url_form.value = href;
download_btn.href = href;
if (collection.type == CollectionType.WEBCAL) {
download_btn.parentElement.classList.add("hidden");
}
delete_btn.onclick = function () { return ondelete(collection); };
edit_btn.onclick = function () { return onedit(collection); };
share_btn.onclick = function () { return onshare(collection); };
node.classList.remove("hidden");
nodes.push(node);
template.parentNode.insertBefore(node, template);
});
}
function update() {
let loading_scene = new LoadingScene();
push_scene(loading_scene);
collections_req = get_collections(user, password, principal_collection, function (child_collections_, error) {
if (!is_current_scene(loading_scene)) {
return;
}
collections_req = null;
if (error) {
onerror(error);
pop_scene();
} else {
child_collections = child_collections_;
pop_scene();
}
});
}
this.show = function () {
html_scene.classList.remove("hidden");
new_btn.onclick = onnew;
upload_btn.onclick = onupload;
incomingshares_btn.onclick = onincomingshares;
if (child_collections === null) {
update();
discover_server_features(user, password, maybe_enable_sharing_options);
} else {
// from update loading scene
show_collections(child_collections);
}
};
this.hide = function () {
html_scene.classList.add("hidden");
new_btn.onclick = null;
upload_btn.onclick = null;
incomingshares_btn.onclick = null;
child_collections = null;
// remove collection
nodes.forEach(function (node) {
node.parentNode.removeChild(node);
});
nodes = [];
};
this.release = function () {
if (collections_req !== null) {
collections_req.abort();
collections_req = null;
}
child_collections = null;
};
}
}