From a332515f520987b084af6b831907d741ef7ace6f Mon Sep 17 00:00:00 2001 From: ashutosh16 Date: Thu, 22 Aug 2024 13:07:07 -0700 Subject: [PATCH] add actio nmenu ext --- .../app/shared/services/extensions-service.ts | 61 ++++++++++++++++--- 1 file changed, 52 insertions(+), 9 deletions(-) diff --git a/ui/src/app/shared/services/extensions-service.ts b/ui/src/app/shared/services/extensions-service.ts index 0a03a3bc6f356..0f3ad41b360e8 100644 --- a/ui/src/app/shared/services/extensions-service.ts +++ b/ui/src/app/shared/services/extensions-service.ts @@ -3,7 +3,34 @@ import * as minimatch from 'minimatch'; import {Application, ApplicationTree, State} from '../models'; +type ExtensionsEventType = 'resource' | 'systemLevel' | 'appView' | 'statusPanel'| 'topBar'; +type ExtensionsType = ResourceTabExtension | SystemLevelExtension | AppViewExtension | StatusPanelExtension | TopBarExtension; + +class ExtensionsEventTarget { + private listeners: Map void>> = new Map(); + + addEventListener(eventName: ExtensionsEventType, listener: (extension: ExtensionsType) => void) { + if (!this.listeners.has(eventName)) { + this.listeners.set(eventName, []); + } + this.listeners.get(eventName)?.push(listener); + } + + removeEventListener(eventName: ExtensionsEventType, listenerToRemove: (extension: ExtensionsType) => void) { + const listeners = this.listeners.get(eventName); + if (!listeners) return; + + const filteredListeners = listeners.filter(listener => listener !== listenerToRemove); + this.listeners.set(eventName, filteredListeners); + } + + emit(eventName: ExtensionsEventType, extension: ExtensionsType) { + this.listeners.get(eventName)?.forEach(listener => listener(extension)); + } +} + const extensions = { + eventTarget: new ExtensionsEventTarget(), resourceExtentions: new Array(), systemLevelExtensions: new Array(), appViewExtensions: new Array(), @@ -12,23 +39,33 @@ const extensions = { }; function registerResourceExtension(component: ExtensionComponent, group: string, kind: string, tabTitle: string, opts?: {icon: string}) { - extensions.resourceExtentions.push({component, group, kind, title: tabTitle, icon: opts?.icon}); + const ext = {component, group, kind, title: tabTitle, icon: opts?.icon}; + extensions.resourceExtentions.push(ext); + extensions.eventTarget.emit('resource', ext); } function registerSystemLevelExtension(component: ExtensionComponent, title: string, path: string, icon: string) { - extensions.systemLevelExtensions.push({component, title, icon, path}); + const ext = {component, title, icon, path}; + extensions.systemLevelExtensions.push(ext); + extensions.eventTarget.emit('systemLevel', ext); } function registerAppViewExtension(component: ExtensionComponent, title: string, icon: string) { - extensions.appViewExtensions.push({component, title, icon}); + const ext = {component, title, icon}; + extensions.appViewExtensions.push(ext); + extensions.eventTarget.emit('appView', ext); } -function registerStatusPanelExtension(component: StatusPanelExtensionComponent, title: string, id: string, flyout?: ExtensionComponent, isMiddle = false, isNarrow = false) { - extensions.statusPanelExtensions.push({component, flyout, title, id, isMiddle, isNarrow}); +function registerStatusPanelExtension(component: StatusPanelExtensionComponent, title: string, id: string, flyout?: ExtensionComponent) { + const ext = {component, flyout, title, id}; + extensions.statusPanelExtensions.push(ext); + extensions.eventTarget.emit('statusPanel', ext); } function registerTopBarExtension(component: TopBarExtensionComponent, title: string, id: string, flyout?: ExtensionComponent, isMiddle = false, isNarrow = false) { - extensions.TopBarExtensions.push({component, flyout, title, id, isMiddle, isNarrow}); + const ext = {component, flyout, title, id, isMiddle, isNarrow}; + extensions.TopBarExtensions.push(ext); + extensions.eventTarget.emit('topBar', ext); } let legacyInitialized = false; @@ -71,8 +108,6 @@ export interface StatusPanelExtension { flyout?: StatusPanelExtensionFlyoutComponent; title: string; id: string; - isMiddle: boolean; - isNarrow: boolean; } export interface TopBarExtension { @@ -129,6 +164,14 @@ export interface TopBarExtensionFlyoutProps { } export class ExtensionsService { + public addEventListener(evtType: ExtensionsEventType, cb: (ext: ExtensionsType) => void) { + extensions.eventTarget.addEventListener(evtType, cb); + } + + public removeEventListener(evtType: ExtensionsEventType, cb: (ext: ExtensionsType) => void) { + extensions.eventTarget.removeEventListener(evtType, cb); + } + public getResourceTabs(group: string, kind: string): ResourceTabExtension[] { initLegacyExtensions(); const items = extensions.resourceExtentions.filter(extension => minimatch(group, extension.group) && minimatch(kind, extension.kind)).slice(); @@ -161,4 +204,4 @@ export class ExtensionsService { registerStatusPanelExtension, registerTopBarExtension }; -})(window); +})(window); \ No newline at end of file