Skip to content

Commit

Permalink
library switch menu: fix sort by name
Browse files Browse the repository at this point in the history
* Closes rero/rero-ils#2397.

Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
Garfield-fr committed Apr 12, 2023
1 parent a405e64 commit 7c6be44
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions projects/admin/src/app/menu/service/library-switch-menu.service.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -77,39 +77,45 @@ 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));
}

/**
* 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;
});
}

Expand Down Expand Up @@ -176,3 +182,10 @@ export class LibrarySwitchMenuService {
});
}
}

// Interface for library menu line
export interface ILibraryMenu {
id: string;
name: string;
metadata: any
}

0 comments on commit 7c6be44

Please sign in to comment.