import React from 'react' ; import { distinctUntilChanged , filter , map , skip , take , takeUntil } from 'rxjs/operators' ; import { Subject } from 'rxjs' ; import { Action } from '../../../../../../../../src/plugins/ui_actions/public' ; import { reactToUiComponent , toMountPoint , } from '../../../../../../../../src/plugins/kibana_react/public' ; import { EmbeddableContext , ViewMode , CONTEXT_MENU_TRIGGER , } from '../../../../../../../../src/plugins/embeddable/public' ; import { txtDisplayName } from './i18n' ; import { MenuItem } from './menu_item' ; import { isEnhancedEmbeddable , embeddableEnhancedDrilldownGrouping , } from '../../../../../../embeddable_enhanced/public' ; import { StartDependencies } from '../../../../plugin' ; import { StartServicesGetter } from '../../../../../../../../src/plugins/kibana_utils/public' ; import { createDrilldownTemplatesFromSiblings , ensureNestedTriggers } from '../drilldown_shared' ; export const OPEN_FLYOUT_EDIT_DRILLDOWN = 'OPEN_FLYOUT_EDIT_DRILLDOWN' ; export interface FlyoutEditDrilldownParams { start : StartServicesGetter < Pick < StartDependencies , 'uiActionsEnhanced' > > ; } export class FlyoutEditDrilldownAction implements Action < EmbeddableContext > { public readonly type = OPEN_FLYOUT_EDIT_DRILLDOWN ; public readonly id = OPEN_FLYOUT_EDIT_DRILLDOWN ; public order = 10 ; public grouping = embeddableEnhancedDrilldownGrouping ; constructor ( protected readonly params ) { } public getDisplayName ( ) { return txtDisplayName ; } public getIconType ( ) { return 'list' ; } MenuItem = reactToUiComponent ( MenuItem ) ; public async isCompatible ( { embeddable } ) { if ( embeddable . getInput ( ) . viewMode !== ViewMode . EDIT ) return false ; if ( ! isEnhancedEmbeddable ( embeddable ) ) return false ; return embeddable . enhancements . dynamicActions . state . get ( ) . events . length > 0 ; } public async execute ( context ) { const { core , plugins } = this . params . start ( ) ; const { embeddable } = context ; if ( ! isEnhancedEmbeddable ( embeddable ) ) { throw new Error ( 'Need embeddable to be EnhancedEmbeddable to execute FlyoutEditDrilldownAction.' ) ; } const templates = createDrilldownTemplatesFromSiblings ( embeddable ) ; const closed$ = new Subject < true > ( ) ; const close = ( ) => { closed$ . next ( true ) ; handle . close ( ) ; } ; const handle = core . overlays . openFlyout ( toMountPoint ( < plugins . uiActionsEnhanced . DrilldownManager initialRoute = { '/manage' } dynamicActionManager = { embeddable . enhancements . dynamicActions } triggers = { [ ... ensureNestedTriggers ( embeddable . supportedTriggers ( ) ) , CONTEXT_MENU_TRIGGER ] } placeContext = { { embeddable } } templates = { templates } onClose = { close } / > ) , { ownFocus : true , 'data-test-subj' : 'editDrilldownFlyout' , } ) ; core . application . currentAppId$ . pipe ( takeUntil ( closed$ ) , skip ( 1 ) , take ( 1 ) ) . subscribe ( ( ) => { close ( ) ; } ) ; embeddable . getInput$ ( ) . pipe ( takeUntil ( closed$ ) , map ( ( input ) => input . viewMode ) , distinctUntilChanged ( ) , filter ( ( mode ) => mode !== ViewMode . EDIT ) , take ( 1 ) ) . subscribe ( ( ) => { close ( ) ; } ) ; } }