mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 15:05:44 +01:00
Sorted extension point entries with supplied extensionName
This commit is contained in:
@@ -3,6 +3,7 @@ type Predicate = (props: any) => boolean;
|
||||
type ExtensionRegistration = {
|
||||
predicate: Predicate;
|
||||
extension: any;
|
||||
extensionName: string;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -25,13 +26,14 @@ export class Binder {
|
||||
* @param extension provided extension
|
||||
* @param predicate to decide if the extension gets rendered for the given props
|
||||
*/
|
||||
bind(extensionPoint: string, extension: any, predicate?: Predicate) {
|
||||
bind(extensionPoint: string, extension: any, predicate?: Predicate, extensionName?: string) {
|
||||
if (!this.extensionPoints[extensionPoint]) {
|
||||
this.extensionPoints[extensionPoint] = [];
|
||||
}
|
||||
const registration = {
|
||||
predicate: predicate ? predicate : () => true,
|
||||
extension
|
||||
extension,
|
||||
extensionName: extensionName ? extensionName : ""
|
||||
};
|
||||
this.extensionPoints[extensionPoint].push(registration);
|
||||
}
|
||||
@@ -61,6 +63,7 @@ export class Binder {
|
||||
if (props) {
|
||||
registrations = registrations.filter(reg => reg.predicate(props || {}));
|
||||
}
|
||||
registrations.sort(this.sortExtensions);
|
||||
return registrations.map(reg => reg.extension);
|
||||
}
|
||||
|
||||
@@ -70,6 +73,27 @@ export class Binder {
|
||||
hasExtension(extensionPoint: string, props?: object): boolean {
|
||||
return this.getExtensions(extensionPoint, props).length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort extensions in ascending order.
|
||||
*/
|
||||
sortExtensions = (a: ExtensionRegistration, b: ExtensionRegistration) => {
|
||||
const regA = a.extensionName ? a.extensionName.toUpperCase() : "";
|
||||
const regB = b.extensionName ? b.extensionName.toUpperCase() : "";
|
||||
|
||||
if (regA === "" && regB === "") {
|
||||
return 0;
|
||||
} else if (regA === "") {
|
||||
return 1;
|
||||
} else if (regB === "") {
|
||||
return -1;
|
||||
} else if (regA > regB) {
|
||||
return 1;
|
||||
} else if (regA < regB) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
|
||||
// singleton binder
|
||||
|
||||
Reference in New Issue
Block a user