import { api } from '../api.js'; export class ShareModal extends HTMLElement { constructor() { super(); this.fileId = null; this.folderId = null; this.handleEscape = this.handleEscape.bind(this); this.render(); this.attachListeners(); } render() { this.innerHTML = `
`; } attachListeners() { const closeBtn = this.querySelector('#close-share'); closeBtn.addEventListener('click', () => this.hide()); const form = this.querySelector('#share-form'); form.addEventListener('submit', (e) => this.handleCreateShare(e)); const copyBtn = this.querySelector('#copy-link-btn'); copyBtn.addEventListener('click', () => this.copyLink()); } handleEscape(e) { if (e.key === 'Escape') { const modal = this.querySelector('#share-modal'); if (modal.style.display === 'flex') { this.hide(); } } } show(fileId = null, folderId = null) { this.fileId = fileId; this.folderId = folderId; this.querySelector('#share-modal').style.display = 'flex'; this.querySelector('#share-result').style.display = 'none'; this.querySelector('#share-form').reset(); document.addEventListener('keydown', this.handleEscape); } hide() { this.querySelector('#share-modal').style.display = 'none'; this.fileId = null; this.folderId = null; document.removeEventListener('keydown', this.handleEscape); } async handleCreateShare(e) { e.preventDefault(); const form = e.target; const formData = new FormData(form); const errorDiv = this.querySelector('#share-error'); try { const permissionLevel = formData.get('permission_level'); const password = formData.get('password') || null; const expiresAt = formData.get('expires_at') || null; const share = await api.createShare( this.fileId, this.folderId, expiresAt, password, permissionLevel ); const shareLink = `${window.location.origin}/share/${share.token}`; this.querySelector('#share-link').value = shareLink; this.querySelector('#share-result').style.display = 'block'; errorDiv.textContent = ''; } catch (error) { errorDiv.textContent = 'Failed to create share link: ' + error.message; } } copyLink() { const linkInput = this.querySelector('#share-link'); linkInput.select(); document.execCommand('copy'); alert('Link copied to clipboard'); } } customElements.define('share-modal', ShareModal);