import { CoreStart , CoreSetup , Plugin , PluginInitializerContext } from 'src/core/public' ; import { SavedObjectAttributes } from 'kibana/public' ; import { EmbeddableFactory , EmbeddableFactoryDefinition , EmbeddableInput , EmbeddableOutput , EmbeddableSetup , EmbeddableStart , IEmbeddable , defaultEmbeddableFactoryProvider , EmbeddableContext , PANEL_NOTIFICATION_TRIGGER , } from '../../../../src/plugins/embeddable/public' ; import { EnhancedEmbeddable } from './types' ; import { EmbeddableActionStorage , EmbeddableWithDynamicActions , } from './embeddables/embeddable_action_storage' ; import { UiActionsEnhancedDynamicActionManager as DynamicActionManager , AdvancedUiActionsSetup , AdvancedUiActionsStart , } from '../../ui_actions_enhanced/public' ; import { PanelNotificationsAction } from './actions' ; export interface SetupDependencies { embeddable : EmbeddableSetup ; uiActionsEnhanced : AdvancedUiActionsSetup ; } export interface StartDependencies { embeddable : EmbeddableStart ; uiActionsEnhanced : AdvancedUiActionsStart ; } export interface SetupContract { } export interface StartContract { } export class EmbeddableEnhancedPlugin implements Plugin < SetupContract , StartContract , SetupDependencies , StartDependencies > { constructor ( protected readonly context ) { } private uiActions ? : StartDependencies [ 'uiActionsEnhanced' ] ; public setup ( core , plugins ) { this . setCustomEmbeddableFactoryProvider ( plugins ) ; const panelNotificationAction = new PanelNotificationsAction ( ) ; plugins . uiActionsEnhanced . registerAction ( panelNotificationAction ) ; plugins . uiActionsEnhanced . attachAction ( PANEL_NOTIFICATION_TRIGGER , panelNotificationAction . id ) ; return { } ; } public start ( core , plugins ) { this . uiActions = plugins . uiActionsEnhanced ; return { } ; } public stop ( ) { } private setCustomEmbeddableFactoryProvider ( plugins ) { plugins . embeddable . setCustomEmbeddableFactoryProvider ( < I extends EmbeddableInput = EmbeddableInput , O extends EmbeddableOutput = EmbeddableOutput , E extends IEmbeddable < I , O > = IEmbeddable < I , O > , T extends SavedObjectAttributes = SavedObjectAttributes > ( def ) : EmbeddableFactory < I , O , E , T > => { const factory = defaultEmbeddableFactoryProvider < I , O , E , T > ( def ) ; return { ... factory , create : async ( ... args ) => { const embeddable = await factory . create ( ... args ) ; if ( ! embeddable ) return embeddable ; return this . enhanceEmbeddableWithDynamicActions ( embeddable ) ; } , createFromSavedObject : async ( ... args ) => { const embeddable = await factory . createFromSavedObject ( ... args ) ; if ( ! embeddable ) return embeddable ; return this . enhanceEmbeddableWithDynamicActions ( embeddable ) ; } , } ; } ) ; } private readonly isEmbeddableContext = ( context ) : context is EmbeddableContext => { if ( ! ( context as EmbeddableContext ) ?. embeddable ) { console . warn ( 'For drilldowns to work action context should contain .embeddable field.' ) ; return false ; } return true ; } ; private enhanceEmbeddableWithDynamicActions < E extends IEmbeddable > ( embeddable ) { const enhancedEmbeddable = embeddable as EnhancedEmbeddable < E > ; const storage = new EmbeddableActionStorage ( embeddable as EmbeddableWithDynamicActions ) ; const dynamicActions = new DynamicActionManager ( { isCompatible : async ( context ) => { if ( ! this . isEmbeddableContext ( context ) ) return false ; return context . embeddable . runtimeId === embeddable . runtimeId ; } , storage , uiActions : this . uiActions ! , } ) ; dynamicActions . start ( ) . catch ( ( error ) => { console . log ( 'Failed to start embeddable dynamic actions' , embeddable ) ; console . error ( error ) ; } ) ; const stop = ( ) => { dynamicActions . stop ( ) . catch ( ( error ) => { console . log ( 'Failed to stop embeddable dynamic actions' , embeddable ) ; console . error ( error ) ; } ) ; } ; embeddable . getInput$ ( ) . subscribe ( { next : ( ) => { storage . reload$ . next ( ) ; } , error : stop , complete : stop , } ) ; enhancedEmbeddable . enhancements = { ... enhancedEmbeddable . enhancements , dynamicActions , } ; return enhancedEmbeddable ; } }