diff --git a/projects/admin/src/app/menu/service/library-switch-menu.service.ts b/projects/admin/src/app/menu/service/library-switch-menu.service.ts index c02721df6..11db8363a 100644 --- a/projects/admin/src/app/menu/service/library-switch-menu.service.ts +++ b/projects/admin/src/app/menu/service/library-switch-menu.service.ts @@ -1,6 +1,6 @@ /* * RERO ILS UI - * Copyright (C) 2020-2022 RERO + * Copyright (C) 2020-2023 RERO * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -77,11 +77,18 @@ export class LibrarySwitchMenuService { .totalHits(results.hits.total) > 0 ? results.hits.hits : [] ), map((result: any) => { - const libraries = {}; + const libraries: ILibraryMenu[] = []; result.map((library: any) => { - libraries[library.metadata.pid] = library.metadata; + libraries.push({ + id: library.metadata.pid, + name: library.metadata.name, + metadata: library.metadata + }); + }); + // Sort libraries by name asc. + return libraries.sort((first: ILibraryMenu, second: ILibraryMenu) => { + return first.name >= second.name ? 1 : -1 }); - return libraries; })).subscribe((libraries: any) => this._generateMenu(libraries)); } @@ -89,27 +96,26 @@ export class LibrarySwitchMenuService { * Generate Menu * @param libraries - array of library record */ - private _generateMenu(libraries: any) { + private _generateMenu(libraries: ILibraryMenu[]) { this._librariesMenu = {}; const factory = new MenuFactory(); this._menu = factory.createItem('Switch Libraries'); - const currentLibrary = libraries[this._userService.user.currentLibrary]; - const libMenu = this._menu.addChild(currentLibrary.code) + const currentLibrary = libraries.find((library: ILibraryMenu) => library.id == this._userService.user.currentLibrary); + const libMenu = this._menu.addChild(currentLibrary.metadata.code) .setAttribute('class', 'dropdown-menu dropdown-menu-right') .setExtra('iconClass', 'fa fa-random'); this._currentLibrary = libMenu; - Object.keys(libraries).map((key: string) => { - const library = libraries[key]; + libraries.forEach((library: ILibraryMenu) => { const libraryLine = libMenu.addChild(library.name) - .setAttribute('id', `library-${library.pid}`) - .setPrefix(`[${library.code}]`, 'pr-2 text-dark small font-weight-bold') - .setExtra('id', library.pid) - .setExtra('code', library.code); - if (key === this._userService.user.currentLibrary) { + .setAttribute('id', `library-${library.metadata.pid}`) + .setPrefix(`[${library.metadata.code}]`, 'pr-2 text-dark small font-weight-bold') + .setExtra('id', library.metadata.pid) + .setExtra('code', library.metadata.code); + if (library.id === this._userService.user.currentLibrary) { this._setActive(libraryLine); } // Proxy library menu item - this._librariesMenu[key] = libraryLine; + this._librariesMenu[library.id] = libraryLine; }); } @@ -176,3 +182,10 @@ export class LibrarySwitchMenuService { }); } } + +// Interface for library menu line +export interface ILibraryMenu { + id: string; + name: string; + metadata: any +}