Make left menu work

This commit is contained in:
aditya.chandel
2025-01-21 12:05:52 -07:00
parent efc2372a7e
commit 69c1c443d9
6 changed files with 178 additions and 144 deletions
@@ -50,43 +50,46 @@ public class LibraryService {
Set<String> deletedPaths = currentPaths.stream().filter(path -> !updatedPaths.contains(path)).collect(Collectors.toSet()); Set<String> deletedPaths = currentPaths.stream().filter(path -> !updatedPaths.contains(path)).collect(Collectors.toSet());
Set<String> newPaths = updatedPaths.stream().filter(path -> !currentPaths.contains(path)).collect(Collectors.toSet()); Set<String> newPaths = updatedPaths.stream().filter(path -> !currentPaths.contains(path)).collect(Collectors.toSet());
if (!deletedPaths.isEmpty()) { if(newPaths.isEmpty() && deletedPaths.isEmpty()) {
Set<LibraryPathEntity> pathsToRemove = library.getLibraryPaths().stream() return libraryMapper.toLibrary(libraryRepository.save(library));
.filter(pathEntity -> deletedPaths.contains(pathEntity.getPath())) } else {
.collect(Collectors.toSet()); if (!deletedPaths.isEmpty()) {
library.getLibraryPaths().removeAll(pathsToRemove); Set<LibraryPathEntity> pathsToRemove = library.getLibraryPaths().stream()
.filter(pathEntity -> deletedPaths.contains(pathEntity.getPath()))
.collect(Collectors.toSet());
library.getLibraryPaths().removeAll(pathsToRemove);
List<Long> books = bookRepository.findAllBookIdsByLibraryPathIdIn(pathsToRemove.stream().map(LibraryPathEntity::getId).collect(Collectors.toSet())); List<Long> books = bookRepository.findAllBookIdsByLibraryPathIdIn(pathsToRemove.stream().map(LibraryPathEntity::getId).collect(Collectors.toSet()));
if (!books.isEmpty()) { if (!books.isEmpty()) {
notificationService.sendMessage(Topic.BOOKS_REMOVE, books); notificationService.sendMessage(Topic.BOOKS_REMOVE, books);
}
libraryPathRepository.deleteAll(pathsToRemove);
libraryPathRepository.saveAll(library.getLibraryPaths());
libraryRepository.save(library);
} }
libraryPathRepository.deleteAll(pathsToRemove); if (!newPaths.isEmpty()) {
libraryPathRepository.saveAll(library.getLibraryPaths()); Set<LibraryPathEntity> newPathEntities = newPaths.stream()
libraryRepository.save(library); .map(path -> LibraryPathEntity.builder().path(path).library(library).build())
.collect(Collectors.toSet());
library.getLibraryPaths().addAll(newPathEntities);
libraryPathRepository.saveAll(library.getLibraryPaths());
libraryRepository.save(library);
Thread.startVirtualThread(() -> {
try {
libraryProcessingService.processLibrary(libraryId);
} catch (InvalidDataAccessApiUsageException e) {
log.warn("InvalidDataAccessApiUsageException - Library id: {}", libraryId);
} catch (IOException e) {
log.error("Error while parsing library books", e);
}
log.info("Parsing task completed!");
});
}
return libraryMapper.toLibrary(libraryRepository.save(library));
} }
if (!newPaths.isEmpty()) {
Set<LibraryPathEntity> newPathEntities = newPaths.stream()
.map(path -> LibraryPathEntity.builder().path(path).library(library).build())
.collect(Collectors.toSet());
library.getLibraryPaths().addAll(newPathEntities);
libraryPathRepository.saveAll(library.getLibraryPaths());
libraryRepository.save(library);
Thread.startVirtualThread(() -> {
try {
libraryProcessingService.processLibrary(libraryId);
} catch (InvalidDataAccessApiUsageException e) {
log.warn("InvalidDataAccessApiUsageException - Library id: {}", libraryId);
} catch (IOException e) {
log.error("Error while parsing library books", e);
}
log.info("Parsing task completed!");
});
}
return libraryMapper.toLibrary(library);
} }
public Library createLibrary(CreateLibraryRequest request) { public Library createLibrary(CreateLibraryRequest request) {
@@ -26,6 +26,14 @@
</i> </i>
</a> </a>
<a class="topbar-items topbar-item">
<i
class="pi pi-sort"
pTooltip="Select sorting"
tooltipPosition="top">
</i>
</a>
<a class="topbar-items topbar-item" (click)="toggleTableGrid()"> <a class="topbar-items topbar-item" (click)="toggleTableGrid()">
<i <i
[ngClass]="viewIcon" [ngClass]="viewIcon"
@@ -1,4 +1,4 @@
import {Component, OnInit} from '@angular/core'; import {Component, inject, OnInit} from '@angular/core';
import {AppMenuitemComponent} from './app.menuitem.component'; import {AppMenuitemComponent} from './app.menuitem.component';
import {AsyncPipe, NgForOf, NgIf} from '@angular/common'; import {AsyncPipe, NgForOf, NgIf} from '@angular/common';
import {MenuModule} from 'primeng/menu'; import {MenuModule} from 'primeng/menu';
@@ -7,6 +7,7 @@ import {Observable, of} from 'rxjs';
import {map} from 'rxjs/operators'; import {map} from 'rxjs/operators';
import {ShelfService} from '../../../book/service/shelf.service'; import {ShelfService} from '../../../book/service/shelf.service';
import {BookService} from '../../../book/service/book.service'; import {BookService} from '../../../book/service/book.service';
import {LibraryShelfMenuService} from '../../../book/service/library-shelf-menu.service';
@Component({ @Component({
selector: 'app-menu', selector: 'app-menu',
@@ -18,8 +19,11 @@ export class AppMenuComponent implements OnInit {
shelfMenu$: Observable<any> | undefined; shelfMenu$: Observable<any> | undefined;
homeMenu$: Observable<any> | undefined; homeMenu$: Observable<any> | undefined;
constructor(private libraryService: LibraryService, private shelfService: ShelfService, private bookService: BookService) { private libraryService = inject(LibraryService);
} private shelfService = inject(ShelfService);
private bookService = inject(BookService);
private libraryShelfMenuService = inject(LibraryShelfMenuService);
ngOnInit(): void { ngOnInit(): void {
this.libraryMenu$ = this.libraryService.libraryState$.pipe( this.libraryMenu$ = this.libraryService.libraryState$.pipe(
@@ -28,7 +32,9 @@ export class AppMenuComponent implements OnInit {
label: 'Library', label: 'Library',
separator: false, separator: false,
items: state.libraries?.map((library) => ({ items: state.libraries?.map((library) => ({
menu: this.libraryShelfMenuService.initializeLibraryMenuItems(library),
label: library.name, label: library.name,
type: 'Library',
icon: 'pi pi-' + library.icon, icon: 'pi pi-' + library.icon,
routerLink: [`/library/${library.id}/books`], routerLink: [`/library/${library.id}/books`],
bookCount$: this.libraryService.getBookCount(library.id ?? 0), bookCount$: this.libraryService.getBookCount(library.id ?? 0),
@@ -43,7 +49,9 @@ export class AppMenuComponent implements OnInit {
label: 'Shelves', label: 'Shelves',
separator: false, separator: false,
items: state.shelves?.map((shelf) => ({ items: state.shelves?.map((shelf) => ({
menu: this.libraryShelfMenuService.initializeLibraryMenuItems(shelf),
label: shelf.name, label: shelf.name,
type: 'Shelf',
icon: 'pi pi-' + shelf.icon, icon: 'pi pi-' + shelf.icon,
routerLink: [`/shelf/${shelf.id}/books`], routerLink: [`/shelf/${shelf.id}/books`],
bookCount$: this.shelfService.getBookCount(shelf.id ?? 0), bookCount$: this.shelfService.getBookCount(shelf.id ?? 0),
@@ -66,6 +74,7 @@ export class AppMenuComponent implements OnInit {
}, },
{ {
label: 'All Books', label: 'All Books',
type: 'All Books',
icon: 'pi pi-fw pi-book', icon: 'pi pi-fw pi-book',
routerLink: ['/all-books'], routerLink: ['/all-books'],
bookCount$: of(bookState.books ? bookState.books.length : 0), bookCount$: of(bookState.books ? bookState.books.length : 0),
@@ -28,19 +28,30 @@
</span> </span>
<i class="pi pi-fw pi-angle-down layout-submenu-toggler" *ngIf="item.items"></i> <i class="pi pi-fw pi-angle-down layout-submenu-toggler" *ngIf="item.items"></i>
</a> </a>
<span *ngIf="(item.routerLink && !item.items) && item.visible !== false" class="book-count"> <span *ngIf="(item.routerLink && !item.items) && item.visible !== false">
<p-button <ng-container *ngIf="item.type === 'Library' || item.type === 'Shelf'">
(mouseover)="hovered = true" <p-button
(mouseleave)="hovered = false" (click)="entitymenu.toggle($event)"
[text]="true" (mouseover)="hovered = true"
[outlined]="false" (mouseleave)="hovered = false"
[rounded]="true" [text]="true"
[label]="hovered ? '' : (item.bookCount$ | async)?.toString() || ''" [outlined]="false"
[icon]="hovered ? 'pi pi-ellipsis-v' : ''" [rounded]="true"
[style.width]="'30px'" [label]="hovered ? '' : (item.bookCount$ | async)?.toString() || ''"
[style.height]="'30px'" [icon]="hovered ? 'pi pi-ellipsis-v' : ''"
[style.display]="'flex'"> [style.width]="'30px'"
</p-button> [style.height]="'30px'"
[style.display]="'flex'">
</p-button>
<p-menu #entitymenu [model]="item.menu" [popup]="true" appendTo="body"></p-menu>
</ng-container>
<ng-container *ngIf="item.type === 'All Books'">
<p
class="text-[var(--primary-color)] pr-1">
{{ (item.bookCount$ | async)?.toString() || '0' }}
</p>
</ng-container>
</span> </span>
</div> </div>
@@ -7,119 +7,122 @@ import {MenuService} from './service/app.menu.service';
import {AsyncPipe, NgClass, NgForOf, NgIf} from '@angular/common'; import {AsyncPipe, NgClass, NgForOf, NgIf} from '@angular/common';
import {Ripple} from 'primeng/ripple'; import {Ripple} from 'primeng/ripple';
import {Button} from 'primeng/button'; import {Button} from 'primeng/button';
import {Menu} from 'primeng/menu';
import {MenuItem} from 'primeng/api';
@Component({ @Component({
selector: '[app-menuitem]', selector: '[app-menuitem]',
templateUrl: './app.menuitem.component.html', templateUrl: './app.menuitem.component.html',
styleUrl: './app.menuitem.component.scss', styleUrl: './app.menuitem.component.scss',
imports: [ imports: [
RouterLink, RouterLink,
RouterLinkActive, RouterLinkActive,
NgClass, NgClass,
NgForOf, NgForOf,
Ripple, Ripple,
NgIf, NgIf,
AsyncPipe, AsyncPipe,
Button Button,
], Menu
animations: [ ],
trigger('children', [ animations: [
state('collapsed', style({ trigger('children', [
height: '0' state('collapsed', style({
})), height: '0'
state('expanded', style({ })),
height: '*' state('expanded', style({
})), height: '*'
transition('collapsed <=> expanded', animate('400ms cubic-bezier(0.86, 0, 0.07, 1)')) })),
]) transition('collapsed <=> expanded', animate('400ms cubic-bezier(0.86, 0, 0.07, 1)'))
] ])
]
}) })
export class AppMenuitemComponent implements OnInit, OnDestroy { export class AppMenuitemComponent implements OnInit, OnDestroy {
@Input() item: any; @Input() item: any;
@Input() index!: number; @Input() index!: number;
@Input() @HostBinding('class.layout-root-menuitem') root!: boolean; @Input() @HostBinding('class.layout-root-menuitem') root!: boolean;
@Input() parentKey!: string; @Input() parentKey!: string;
hovered = false; hovered = false;
active = false; active = false;
menuSourceSubscription: Subscription; menuSourceSubscription: Subscription;
menuResetSubscription: Subscription; menuResetSubscription: Subscription;
key: string = ""; key: string = "";
constructor(public router: Router, private menuService: MenuService) { constructor(public router: Router, private menuService: MenuService) {
this.menuSourceSubscription = this.menuService.menuSource$.subscribe(value => { this.menuSourceSubscription = this.menuService.menuSource$.subscribe(value => {
Promise.resolve(null).then(() => { Promise.resolve(null).then(() => {
if (value.routeEvent) { if (value.routeEvent) {
this.active = (value.key === this.key || value.key.startsWith(this.key + '-')) ? true : false; this.active = (value.key === this.key || value.key.startsWith(this.key + '-')) ? true : false;
} else { } else {
if (value.key !== this.key && !value.key.startsWith(this.key + '-')) { if (value.key !== this.key && !value.key.startsWith(this.key + '-')) {
this.active = false;
}
}
});
});
this.menuResetSubscription = this.menuService.resetSource$.subscribe(() => {
this.active = false; this.active = false;
}); }
}
});
});
this.router.events.pipe(filter(event => event instanceof NavigationEnd)) this.menuResetSubscription = this.menuService.resetSource$.subscribe(() => {
.subscribe(() => { this.active = false;
if (this.item.routerLink) { });
this.updateActiveStateFromRoute();
}
});
}
ngOnInit() { this.router.events.pipe(filter(event => event instanceof NavigationEnd))
this.key = this.parentKey ? this.parentKey + '-' + this.index : String(this.index); .subscribe(() => {
if (this.item.routerLink) { if (this.item.routerLink) {
this.updateActiveStateFromRoute(); this.updateActiveStateFromRoute();
} }
} });
}
updateActiveStateFromRoute() { ngOnInit() {
const activeRoute = this.router.isActive(this.item.routerLink[0], { this.key = this.parentKey ? this.parentKey + '-' + this.index : String(this.index);
paths: 'exact', if (this.item.routerLink) {
queryParams: 'ignored', this.updateActiveStateFromRoute();
matrixParams: 'ignored',
fragment: 'ignored'
});
if (activeRoute) {
this.menuService.onMenuStateChange({key: this.key, routeEvent: true});
}
} }
}
itemClick(event: Event) { updateActiveStateFromRoute() {
if (this.item.disabled) { const activeRoute = this.router.isActive(this.item.routerLink[0], {
event.preventDefault(); paths: 'exact',
return; queryParams: 'ignored',
} matrixParams: 'ignored',
if (this.item.command) { fragment: 'ignored'
this.item.command({originalEvent: event, item: this.item}); });
} if (activeRoute) {
if (this.item.items) { this.menuService.onMenuStateChange({key: this.key, routeEvent: true});
this.active = !this.active;
}
this.menuService.onMenuStateChange({key: this.key});
} }
}
get submenuAnimation() { itemClick(event: Event) {
return this.root ? 'expanded' : (this.active ? 'expanded' : 'collapsed'); if (this.item.disabled) {
event.preventDefault();
return;
} }
if (this.item.command) {
this.item.command({originalEvent: event, item: this.item});
}
if (this.item.items) {
this.active = !this.active;
}
this.menuService.onMenuStateChange({key: this.key});
}
@HostBinding('class.active-menuitem') get submenuAnimation() {
get activeClass() { return this.root ? 'expanded' : (this.active ? 'expanded' : 'collapsed');
return this.active && !this.root; }
}
ngOnDestroy() { @HostBinding('class.active-menuitem')
if (this.menuSourceSubscription) { get activeClass() {
this.menuSourceSubscription.unsubscribe(); return this.active && !this.root;
} }
if (this.menuResetSubscription) {
this.menuResetSubscription.unsubscribe(); ngOnDestroy() {
} if (this.menuSourceSubscription) {
this.menuSourceSubscription.unsubscribe();
} }
if (this.menuResetSubscription) {
this.menuResetSubscription.unsubscribe();
}
}
} }
@@ -19,7 +19,7 @@
<div class="topbar-right-items"> <div class="topbar-right-items">
<app-book-searcher></app-book-searcher> <app-book-searcher></app-book-searcher>
<ul class="topbar-items"> <ul class="topbar-items pl-6">
<div class="flex gap-6"> <div class="flex gap-6">
<li> <li>
<a class="topbar-item"> <a class="topbar-item">