/** * This file is part of Radicale Server - Calendar Server * Copyright © 2017-2024 Unrud * Copyright © 2023-2024 Matthew Hana * Copyright © 2024-2025 Peter Bieringer * Copyright © 2026-2026 Max Berger * * 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 . */ import { add_share_by_map, add_share_by_token } from "../api/api.js"; import { ErrorHandler } from "../utils/error.js"; import { FormValidator, validate_href, validate_non_empty } from "../utils/form_validator.js"; import { onCleanHREFinput, random_uuid } from "../utils/misc.js"; import { Scene, pop_scene, scene_stack } from "./scene_manager.js"; /** * @implements {Scene} */ export class NewShareScene { /** * @param {string} user * @param {string} password * @param {string} pathMapped * @param {string} shareType * @param {function():void} onclose */ constructor(user, password, pathMapped, shareType, onclose) { /** @type {HTMLElement} */ let html_scene = document.getElementById("newshare"); /** @type {HTMLFormElement} */ let form = html_scene.querySelector("form"); /** @type {HTMLElement} */ let sharemapfields = html_scene.querySelector("[data-name=sharemapfields]"); /** @type {HTMLInputElement} */ let shareuser_input = html_scene.querySelector("[data-name=shareuser]"); /** @type {HTMLInputElement} */ let sharehref_input = html_scene.querySelector("[data-name=sharehref]"); /** @type {HTMLInputElement} */ let enabled_checkbox = html_scene.querySelector("[data-name=enabled]"); /** @type {HTMLInputElement} */ let hidden_checkbox = html_scene.querySelector("[data-name=hidden]"); let permissions_ro_radio = /** @type {HTMLInputElement} */ (document.getElementById("newshare_attr_permissions_ro")); let permissions_rw_radio = /** @type {HTMLInputElement} */ (document.getElementById("newshare_attr_permissions_rw")); /** @type {HTMLInputElement} */ let properties_input = html_scene.querySelector("[data-name=properties]"); /** @type {HTMLElement} */ let error_form = html_scene.querySelector("[data-name=error]"); /** @type {HTMLElement} */ let cancel_btn = html_scene.querySelector("[data-name=cancel]"); let errorHandler = new ErrorHandler(error_form); let map_validator = new FormValidator(errorHandler); map_validator.addValidator(shareuser_input, validate_non_empty(shareuser_input, "Share User")); map_validator.addValidator(sharehref_input, validate_href(sharehref_input, "Share Href")); sharehref_input.addEventListener("input", onCleanHREFinput); /** @type {?number} */ let scene_index = null; function oncancel() { try { if (scene_index !== null) { pop_scene(scene_index - 1); } if (onclose) onclose(); } catch (err) { console.error(err); } return false; } function onsubmit() { try { if (shareType === "map") { if (!map_validator.validate()) { return false; } } let enabled = enabled_checkbox.checked; let hidden = hidden_checkbox.checked; let permissions = permissions_rw_radio.checked ? "rw" : "r"; let properties = properties_input.value; let callback = function (/** @type {string} */ error) { if (scene_index === null) { return; } if (error) { errorHandler.setError(error); } else { pop_scene(scene_index - 1); if (onclose) onclose(); } }; if (shareType === "map") { let share_user = shareuser_input.value; let href = sharehref_input.value; add_share_by_map(user, password, pathMapped, permissions, enabled, hidden, properties, share_user, href, callback); } else { add_share_by_token(user, password, pathMapped, permissions, enabled, hidden, properties, callback); } } catch (err) { console.error(err); } return false; } this.show = function () { this.release(); scene_index = scene_stack.length - 1; html_scene.classList.remove("hidden"); cancel_btn.onclick = oncancel; form.onsubmit = onsubmit; shareuser_input.value = ""; enabled_checkbox.checked = true; hidden_checkbox.checked = false; permissions_ro_radio.checked = true; permissions_rw_radio.checked = false; properties_input.value = ""; if (shareType === "map") { sharehref_input.value = random_uuid(); sharemapfields.classList.remove("hidden"); map_validator.validate(); } else { sharehref_input.value = ""; sharemapfields.classList.add("hidden"); errorHandler.clearError(); } }; this.hide = function () { html_scene.classList.add("hidden"); cancel_btn.onclick = null; form.onsubmit = null; }; this.release = function () { scene_index = null; }; } }