/** * NoteWidget - Paginated note widget for the homepage. * * Fixed-height 6-item viewport with Up / Down navigation buttons. * Extends NoteBase for shared loading, sorting, and action logic. */ import { NoteBase } from './NoteBase.js'; import { Router } from '../services/Router.js'; export class NoteWidget extends NoteBase { #offset = 0; #navContainer = null; #itemsContainer = null; #upBtn = null; #downBtn = null; _getTemplateId() { return 'pockist-note-widget'; } connectedCallback() { super.connectedCallback(); this.#navContainer = this.querySelector('.note-widget-nav-container'); this.#itemsContainer = this.querySelector('.note-widget-items'); } _setupEventListeners() { const newNoteBtn = this.querySelector('.note-widget-new-btn'); newNoteBtn?.addEventListener('click', () => this._createNewNote()); this.#itemsContainer?.addEventListener('note-open', (e) => { Router.go(`/note/${e.detail.noteId}`); }); this.#itemsContainer?.addEventListener('note-more-actions', (e) => { this._showNoteActions(e.detail.noteId); }); } _renderContent() { if (!this.#itemsContainer) return; this.#itemsContainer.innerHTML = ''; if (this._notes.length === 0) { this.#itemsContainer.innerHTML = '
No notes yet.
'; this.#renderNav(0, 0); return; } const total = this._notes.length; this.#clampOffset(); const clampedOffset = this.#offset; const startIndex = clampedOffset; const endIndex = Math.min(clampedOffset + 6, total); const visibleNotes = this._notes.slice(startIndex, endIndex); visibleNotes.forEach(note => { const el = this._createNoteItem(note); el.classList.add('note-item--enter'); this.#itemsContainer.appendChild(el); requestAnimationFrame(() => { el.classList.remove('note-item--enter'); }); }); this.#renderNav(total, clampedOffset); } _createNoteItem(note) { const el = document.createElement('note-item'); el.setAttribute('note-id', note.id); el.setAttribute('title', this._extractTitle(note.content)); el.setAttribute('preview', this._getPreview(note.content)); el.setAttribute('date', this._formatDate(note.meta?.updatedAt || note.meta?.createdAt)); return el; } #clampOffset() { const total = this._notes.length || 0; if (total <= 6) { this.#offset = 0; } else { const maxOffset = total - 6; this.#offset = Math.max(0, Math.min(this.#offset, maxOffset)); } } #createNavButton(direction) { const btn = document.createElement('button'); btn.type = 'button'; btn.className = 'note-widget-nav-btn btn btn-ghost'; const svg = direction === -1 ? '' : ''; btn.innerHTML = svg + (direction === -1 ? ' Up' : ' Down'); return btn; } #renderNav(total, offset) { if (!this.#navContainer) return; if (total <= 6) { this.#navContainer.innerHTML = ''; this.#upBtn = null; this.#downBtn = null; return; } const maxOffset = total - 6; // Create once, update disabled state on subsequent renders if (!this.#upBtn) { this.#upBtn = this.#createNavButton(-1); this.#upBtn.addEventListener('click', () => { this.#offset = Math.max(0, this.#offset - 1); this._renderContent(); }); this.#navContainer.appendChild(this.#upBtn); } if (!this.#downBtn) { this.#downBtn = this.#createNavButton(1); this.#downBtn.addEventListener('click', () => { this.#offset = Math.min(maxOffset, this.#offset + 1); this._renderContent(); }); this.#navContainer.appendChild(this.#downBtn); } this.#upBtn.disabled = offset === 0; this.#downBtn.disabled = offset >= maxOffset; } } customElements.define('pockist-note-widget', NoteWidget);