import { html, LitElement } from "lit"; import { customElement, property } from "lit/decorators.js"; import { Ref, createRef, ref } from 'lit/directives/ref.js'; import { escapeXml, SVG_ICON_CALENDAR, SVG_ICON_INTERNET } from "."; import { getTimezones } from "./timezones.ts"; @customElement("create-calendar-form") export class CreateCalendarForm extends LitElement { constructor() { super() this.resetForm() this.fetchTimezones() } resetForm() { this.form.value?.reset() this.principal = this.user this.cal_id = self.crypto.randomUUID() this.displayname = '' this.description = '' this.timezone_id = '' this.color = '' this.isSubscription = false this.subscriptionUrl = null this.components = new Set(["VEVENT", "VTODO"]) } async fetchTimezones() { this.timezones = await getTimezones() } protected override createRenderRoot() { return this } @property() user: string = '' @property() principal: string @property() cal_id: string @property() displayname: string @property() description: string @property() timezone_id: string @property() color: string @property() isSubscription: boolean @property() subscriptionUrl: string @property() components: Set<"VEVENT" | "VTODO" | "VJOURNAL"> dialog: Ref = createRef() form: Ref = createRef() @property() timezones: Array = [] override render() { return html` this.resetForm()}>

Create calendar









${this.isSubscription ? html`

`: html``}
${["VEVENT", "VTODO", "VJOURNAL"].map(comp => html`
`)}

` } async submit(e: SubmitEvent) { e.preventDefault() if (!this.cal_id) { alert("Empty id") return } if (!this.displayname) { alert("Empty displayname") return } if (!this.components.size) { alert("No calendar components selected") return } if (this.isSubscription && !this.subscriptionUrl) { alert("Invalid subscription url") return } let response = await fetch(`/caldav/principal/${this.principal || this.user}/${this.cal_id}`, { method: 'MKCOL', headers: { 'Content-Type': 'application/xml' }, body: ` ${escapeXml(this.displayname)} ${this.timezone_id ? `${escapeXml(this.timezone_id)}` : ''} ${this.description ? `${escapeXml(this.description)}` : ''} ${this.color ? `${escapeXml(this.color)}` : ''} ${(this.isSubscription && this.subscriptionUrl) ? `${escapeXml(this.subscriptionUrl)}` : ''} ${Array.from(this.components.keys()).map(comp => ``).join('\n')} ` }) if (response.status >= 400) { alert(`Error ${response.status}: ${await response.text()}`) return null } window.location.reload() return null } } declare global { interface HTMLElementTagNameMap { 'create-calendar-form': CreateCalendarForm } }