import _ from 'lodash' ; import { Action , IncompatibleActionError } from '../../services/ui_actions' ; import { ViewMode , PanelState , IEmbeddable , PanelNotFoundError , EmbeddableInput , isReferenceOrValueEmbeddable , isErrorEmbeddable , } from '../../services/embeddable' ; import { NotificationsStart } from '../../../../../core/public' ; import { dashboardUnlinkFromLibraryAction } from '../../dashboard_strings' ; import { DashboardPanelState , DASHBOARD_CONTAINER_TYPE , DashboardContainer } from '..' ; export const ACTION_UNLINK_FROM_LIBRARY = 'unlinkFromLibrary' ; export interface UnlinkFromLibraryActionContext { embeddable : IEmbeddable ; } export class UnlinkFromLibraryAction implements Action < UnlinkFromLibraryActionContext > { public readonly type = ACTION_UNLINK_FROM_LIBRARY ; public readonly id = ACTION_UNLINK_FROM_LIBRARY ; public order = 15 ; constructor ( private deps ) { } public getDisplayName ( { embeddable } ) { if ( ! embeddable . getRoot ( ) || ! embeddable . getRoot ( ) . isContainer ) { throw new IncompatibleActionError ( ) ; } return dashboardUnlinkFromLibraryAction . getDisplayName ( ) ; } public getIconType ( { embeddable } ) { if ( ! embeddable . getRoot ( ) || ! embeddable . getRoot ( ) . isContainer ) { throw new IncompatibleActionError ( ) ; } return 'folderExclamation' ; } public async isCompatible ( { embeddable } ) { return Boolean ( ! isErrorEmbeddable ( embeddable ) && embeddable . getInput ( ) ?. viewMode !== ViewMode . VIEW && embeddable . getRoot ( ) && embeddable . getRoot ( ) . isContainer && embeddable . getRoot ( ) . type === DASHBOARD_CONTAINER_TYPE && isReferenceOrValueEmbeddable ( embeddable ) && embeddable . inputIsRefType ( embeddable . getInput ( ) ) ) ; } public async execute ( { embeddable } ) { if ( ! isReferenceOrValueEmbeddable ( embeddable ) ) { throw new IncompatibleActionError ( ) ; } const newInput = await embeddable . getInputAsValueType ( ) ; embeddable . updateInput ( newInput ) ; const dashboard = embeddable . getRoot ( ) as DashboardContainer ; const panelToReplace = dashboard . getInput ( ) . panels [ embeddable . id ] as DashboardPanelState ; if ( ! panelToReplace ) { throw new PanelNotFoundError ( ) ; } const newPanel = { type : embeddable . type , explicitInput : { ... newInput , title : embeddable . getTitle ( ) } , } ; _ . unset ( newPanel , 'explicitInput.attributes.title' ) ; dashboard . replacePanel ( panelToReplace , newPanel , true ) ; const title = dashboardUnlinkFromLibraryAction . getSuccessMessage ( embeddable . getTitle ( ) ? `'${ embeddable . getTitle ( ) }'` : '' ) ; this . deps . toasts . addSuccess ( { title , 'data-test-subj' : 'unlinkPanelSuccess' , } ) ; } }