import { SavedObjectLoader , SavedObjectOpenSearchDashboardsServices , } from '../../../saved_objects/public' ; import { findListItems } from './find_list_items' ; import { createSavedVisClass } from './_saved_vis' ; import { TypesStart } from '../vis_types' ; export interface SavedObjectOpenSearchDashboardsServicesWithVisualizations extends SavedObjectOpenSearchDashboardsServices { visualizationTypes : TypesStart ; } export type SavedVisualizationsLoader = ReturnType < typeof createSavedVisLoader > ; export function createSavedVisLoader ( services ) { const { savedObjectsClient , visualizationTypes } = services ; class SavedObjectLoaderVisualize extends SavedObjectLoader { mapHitSource = ( source , id ) => { const visTypes = visualizationTypes ; source . id = id ; source . url = this . urlFor ( id ) ; let typeName = source . typeName ; if ( source . visState ) { try { typeName = JSON . parse ( String ( source . visState ) ) . type ; } catch ( e ) { } } if ( ! typeName || ! visTypes . get ( typeName ) ) { source . error = 'Unknown visualization type' ; return source ; } source . type = visTypes . get ( typeName ) ; source . savedObjectType = 'visualization' ; source . icon = source . type . icon ; source . image = source . type . image ; source . typeTitle = source . type . title ; source . editUrl = `/edit/${ id }` ; return source ; } ; urlFor ( id ) { return `#/edit/${ encodeURIComponent ( id ) }` ; } findListItems ( search = '' , size = 100 ) { return findListItems ( { search , size , mapSavedObjectApiHits : this . mapSavedObjectApiHits . bind ( this ) , savedObjectsClient , visTypes : visualizationTypes . getAliases ( ) , } ) ; } } const SavedVis = createSavedVisClass ( services ) ; return new SavedObjectLoaderVisualize ( SavedVis , savedObjectsClient ) as SavedObjectLoader & { findListItems : ( search : string , size : number ) => any ; } ; }