mirror of https://github.com/abpframework/abp.git
Browse Source
Angular: Sort user menu items by base sorting function at `AbstractMenuService<T>`pull/19582/head
committed by
GitHub
4 changed files with 35 additions and 23 deletions
@ -0,0 +1,5 @@ |
|||||
|
export interface SortableItem { |
||||
|
id?: string | number; |
||||
|
name?: string; |
||||
|
order?: number; |
||||
|
} |
||||
@ -1,27 +1,33 @@ |
|||||
import { InjectionToken, inject } from '@angular/core'; |
import { InjectionToken, inject } from '@angular/core'; |
||||
|
import { SortableItem } from '../models'; |
||||
import { LocalizationService } from '../services'; |
import { LocalizationService } from '../services'; |
||||
|
|
||||
export const SORT_COMPARE_FUNC = new InjectionToken< 0 | 1 | -1 >('SORT_COMPARE_FUNC'); |
export const SORT_COMPARE_FUNC = new InjectionToken<(a: SortableItem, b: SortableItem) => number>( |
||||
|
'SORT_COMPARE_FUNC', |
||||
|
); |
||||
|
|
||||
export function compareFuncFactory() { |
export function compareFuncFactory() { |
||||
const localizationService = inject(LocalizationService) |
const localizationService = inject(LocalizationService); |
||||
const fn = (a,b) => { |
const fn = (a: SortableItem, b: SortableItem) => { |
||||
const aName = localizationService.instant(a.name); |
const aName = localizationService.instant(a.name); |
||||
const bName = localizationService.instant(b.name); |
const bName = localizationService.instant(b.name); |
||||
const aNumber = a.order; |
const aNumber = a.order; |
||||
const bNumber = b.order; |
const bNumber = b.order; |
||||
|
|
||||
if (!Number.isInteger(aNumber)) return 1; |
if (!Number.isInteger(aNumber)) return 1; |
||||
if (!Number.isInteger(bNumber)) return -1; |
if (!Number.isInteger(bNumber)) return -1; |
||||
|
|
||||
if (aNumber > bNumber) return 1 |
if (aNumber > bNumber) return 1; |
||||
if (aNumber < bNumber) return -1 |
if (aNumber < bNumber) return -1; |
||||
|
|
||||
if ( aName > bName ) return 1; |
if (aName > bName) return 1; |
||||
if ( aName < bName ) return -1; |
if (aName < bName) return -1; |
||||
|
|
||||
return 0 |
if (a.id > b.id) return 1; |
||||
} |
if (a.id < b.id) return -1; |
||||
|
|
||||
return fn |
return 0; |
||||
} |
}; |
||||
|
|
||||
|
return fn; |
||||
|
} |
||||
|
|||||
Loading…
Reference in new issue