import { Component, computed, inject, OnInit, OnDestroy, AfterViewInit, ElementRef, ViewChild, Signal, signal } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { DynamicDialogRef, DynamicDialogConfig } from '@openng/optimus-ui/dynamicdialog'; import { AutoComplete, AutoCompleteSelectEvent } from '@openng/optimus-ui/autocomplete'; import { Button } from '@openng/optimus-ui/button'; import { Checkbox } from '@openng/optimus-ui/checkbox'; import { Subject, takeUntil } from 'rxjs'; import { BookService } from '../../service/book.service'; import { BookFileService } from '../../service/book-file.service'; import { Book } from '../../model/book.model'; import { MessageService } from '@openng/optimus-ui/api'; import { TranslocoDirective, TranslocoPipe, TranslocoService } from '@jsverse/transloco'; import { AppSettingsService } from '../../../../shared/service/app-settings.service'; export interface BookFileAttacherSourceBook { id: number; libraryId: number; metadata?: {title?: string; authors?: string[]}; primaryFile?: {extension?: string; bookType?: string; fileName?: string}; } @Component({ selector: 'app-book-file-attacher', standalone: true, imports: [ FormsModule, AutoComplete, Button, Checkbox, TranslocoDirective, TranslocoPipe, ], templateUrl: './book-file-attacher.component.html', styleUrls: ['./book-file-attacher.component.scss'] }) export class BookFileAttacherComponent implements OnInit, AfterViewInit, OnDestroy { private readonly t = inject(TranslocoService); private readonly appSettingsService = inject(AppSettingsService); private readonly dialogRef = inject(DynamicDialogRef); private readonly config = inject(DynamicDialogConfig); private readonly bookService = inject(BookService); private readonly bookFileService = inject(BookFileService); private readonly messageService = inject(MessageService); @ViewChild('autocompleteWrapper') autocompleteWrapper!: ElementRef; sourceBooks: readonly BookFileAttacherSourceBook[] = []; targetBook: Book | null = null; moveFiles = false; isAttaching = false; searchQuery = ''; filteredBooks: Book[] = []; autocomplePanelStyle: Record = {}; private readonly destroy$ = new Subject(); private allBooks: Signal = signal([]); ngAfterViewInit(): void { setTimeout(() => { const width = this.autocompleteWrapper?.nativeElement?.offsetWidth; if (width) { this.autocomplePanelStyle = { 'width': `${width}px`, 'max-width': `${width}px` }; } }); } ngOnInit(): void { // Support both single book and multiple books if (this.config.data.sourceBook) { this.sourceBooks = [this.config.data.sourceBook]; } else if (this.config.data.sourceBooks) { this.sourceBooks = this.config.data.sourceBooks; } if (this.sourceBooks.length === 0) { this.closeDialog(); return; } const settings = this.appSettingsService.appSettings(); if (settings) { this.moveFiles = settings.metadataPersistenceSettings?.moveFilesToLibraryPattern ?? false; } // Get the library ID from first source book (all should be same library) const libraryId = this.sourceBooks[0].libraryId; const sourceBookIds = new Set(this.sourceBooks.map(b => b.id)); this.allBooks = computed(() => this.bookService.books().filter(book => book.libraryId === libraryId && !sourceBookIds.has(book.id) ) ); this.filteredBooks = this.allBooks().slice(0, 20); } ngOnDestroy(): void { this.destroy$.next(); this.destroy$.complete(); } get isBulkMode(): boolean { return this.sourceBooks.length > 1; } filterBooks(event: { query: string; }): void { const query = event.query.toLowerCase().trim(); const books = this.allBooks(); if (!query) { this.filteredBooks = books.slice(0, 20); return; } this.filteredBooks = books .filter(book => { const title = book.metadata?.title?.toLowerCase() || ''; const authors = book.metadata?.authors?.join(' ').toLowerCase() || ''; return title.includes(query) || authors.includes(query); }) .slice(0, 20); } onBookSelect(event: AutoCompleteSelectEvent): void { this.targetBook = event.value as Book; } onBookClear(): void { this.targetBook = null; } getBookDisplayName(book: Book): string { const title = book.metadata?.title || `Book #${book.id}`; const authors = book.metadata?.authors?.join(', '); return authors ? `${title} - ${authors}` : title; } getSourceFileInfo(book: BookFileAttacherSourceBook): string { const file = book.primaryFile; if (!file) return this.t.translate('book.fileAttacher.unknownFile'); const format = file.extension?.toUpperCase() || file.bookType || this.t.translate('book.fileAttacher.unknownFormat'); return `${format} - ${file.fileName || this.t.translate('book.fileAttacher.unknownFilename')}`; } canAttach(): boolean { return !!this.targetBook && !this.isAttaching; } attach(): void { if (!this.targetBook) return; this.isAttaching = true; const sourceBookIds = this.sourceBooks.map(b => b.id); this.bookFileService.attachBookFiles( this.targetBook.id, sourceBookIds, this.moveFiles ).pipe( takeUntil(this.destroy$) ).subscribe({ next: () => { this.dialogRef.close({ success: true }); }, error: () => { this.isAttaching = false; } }); } closeDialog(): void { this.dialogRef.close(); } }