name extends SavedObject . import { createSavedObjectClass , SavedObject , SavedObjectOpenSearchDashboardsServices , } from '../../../saved_objects/public' ; import { updateOldState } from '../legacy/vis_update_state' ; import { extractReferences , injectReferences } from './saved_visualization_references' ; import { IIndexPattern } from '../../../../plugins/data/public' ; import { ISavedVis , SerializedVis } from '../types' ; import { createSavedSearchesLoader } from '../../../discover/public' ; export const convertToSerializedVis = ( savedVis ) : SerializedVis => { const { id , title , description , visState , uiStateJSON , searchSourceFields } = savedVis ; const aggs = searchSourceFields && searchSourceFields . index ? visState . aggs || [ ] : visState . aggs ; return { id , title , type : visState . type , description , params : visState . params , uiState : JSON . parse ( uiStateJSON || '{}' ) , data : { aggs , searchSource : searchSourceFields ! , savedSearchId : savedVis . savedSearchId , } , } ; } ; export const convertFromSerializedVis = ( vis ) : ISavedVis => { return { id : vis . id , title : vis . title , description : vis . description , visState : { title : vis . title , type : vis . type , aggs : vis . data . aggs , params : vis . params , } , uiStateJSON : JSON . stringify ( vis . uiState ) , searchSourceFields : vis . data . searchSource , savedSearchId : vis . data . savedSearchId , } ; } ; export function createSavedVisClass ( services ) { const SavedObjectClass = createSavedObjectClass ( services ) ; const savedSearch = createSavedSearchesLoader ( services ) ; class SavedVis extends SavedObjectClass { public static type : string = 'visualization' ; public static mapping : Record < string , string > = { title : 'text' , visState : 'json' , uiStateJSON : 'text' , description : 'text' , savedSearchId : 'keyword' , version : 'integer' , } ; public static fieldOrder = [ 'title' , 'description' ] ; constructor ( opts = { } ) { if ( typeof opts !== 'object' ) { opts = { id : opts } ; } const visState = ! opts . type ? null : { type : opts . type } ; super ( { type : SavedVis . type , mapping : SavedVis . mapping , extractReferences , injectReferences , id : ( opts . id as string ) || '' , indexPattern : opts . indexPattern as IIndexPattern , defaults : { title : '' , visState , uiStateJSON : '{}' , description : '' , savedSearchId : opts . savedSearchId , version : 1 , } , afterOpenSearchResp : async ( savedObject ) => { const savedVis = ( savedObject as any ) as ISavedVis ; savedVis . visState = await updateOldState ( savedVis . visState ) ; if ( savedVis . searchSourceFields ?. index ) { await services . indexPatterns . get ( savedVis . searchSourceFields . index as any ) ; } if ( savedVis . savedSearchId ) { await savedSearch . get ( savedVis . savedSearchId ) ; } return ( savedVis as any ) as SavedObject ; } , } ) ; this . showInRecentlyAccessed = true ; this . getFullPath = ( ) => { return `/app/visualize#/edit/${ this . id }` ; } ; } } return SavedVis as new ( opts ) => SavedObject ; }