2020-09-23 23:40:52 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { CoreStart } from '../../../../src/core/public';
|
|
|
|
import { LensPluginStartDependencies } from './plugin';
|
2020-10-12 15:51:39 +02:00
|
|
|
import { AttributeService } from '../../../../src/plugins/embeddable/public';
|
2020-09-23 23:40:52 +02:00
|
|
|
import {
|
|
|
|
LensSavedObjectAttributes,
|
|
|
|
LensByValueInput,
|
|
|
|
LensByReferenceInput,
|
|
|
|
} from './editor_frame_service/embeddable/embeddable';
|
2020-10-19 17:21:25 +02:00
|
|
|
import { SavedObjectIndexStore } from './persistence';
|
2020-10-06 09:31:55 +02:00
|
|
|
import { checkForDuplicateTitle, OnSaveProps } from '../../../../src/plugins/saved_objects/public';
|
2020-10-19 17:21:25 +02:00
|
|
|
import { DOC_TYPE } from '../common';
|
2020-09-23 23:40:52 +02:00
|
|
|
|
|
|
|
export type LensAttributeService = AttributeService<
|
|
|
|
LensSavedObjectAttributes,
|
|
|
|
LensByValueInput,
|
|
|
|
LensByReferenceInput
|
|
|
|
>;
|
|
|
|
|
|
|
|
export function getLensAttributeService(
|
|
|
|
core: CoreStart,
|
|
|
|
startDependencies: LensPluginStartDependencies
|
|
|
|
): LensAttributeService {
|
|
|
|
const savedObjectStore = new SavedObjectIndexStore(core.savedObjects.client);
|
2020-10-12 15:51:39 +02:00
|
|
|
return startDependencies.embeddable.getAttributeService<
|
2020-09-23 23:40:52 +02:00
|
|
|
LensSavedObjectAttributes,
|
|
|
|
LensByValueInput,
|
|
|
|
LensByReferenceInput
|
|
|
|
>(DOC_TYPE, {
|
2020-10-06 09:31:55 +02:00
|
|
|
saveMethod: async (
|
2020-09-23 23:40:52 +02:00
|
|
|
type: string,
|
|
|
|
attributes: LensSavedObjectAttributes,
|
|
|
|
savedObjectId?: string
|
|
|
|
) => {
|
|
|
|
const savedDoc = await savedObjectStore.save({
|
|
|
|
...attributes,
|
|
|
|
savedObjectId,
|
|
|
|
type: DOC_TYPE,
|
|
|
|
});
|
|
|
|
return { id: savedDoc.savedObjectId };
|
|
|
|
},
|
2020-10-06 09:31:55 +02:00
|
|
|
unwrapMethod: async (savedObjectId: string): Promise<LensSavedObjectAttributes> => {
|
|
|
|
const savedObject = await core.savedObjects.client.get<LensSavedObjectAttributes>(
|
|
|
|
DOC_TYPE,
|
|
|
|
savedObjectId
|
|
|
|
);
|
2020-09-23 23:40:52 +02:00
|
|
|
return {
|
|
|
|
...savedObject.attributes,
|
|
|
|
references: savedObject.references,
|
|
|
|
};
|
|
|
|
},
|
2020-10-06 09:31:55 +02:00
|
|
|
checkForDuplicateTitle: (props: OnSaveProps) => {
|
|
|
|
const savedObjectsClient = core.savedObjects.client;
|
|
|
|
const overlays = core.overlays;
|
|
|
|
return checkForDuplicateTitle(
|
|
|
|
{
|
|
|
|
title: props.newTitle,
|
|
|
|
copyOnSave: false,
|
|
|
|
lastSavedTitle: '',
|
|
|
|
getEsType: () => DOC_TYPE,
|
|
|
|
getDisplayName: () => DOC_TYPE,
|
|
|
|
},
|
|
|
|
props.isTitleDuplicateConfirmed,
|
|
|
|
props.onTitleDuplicate,
|
|
|
|
{
|
|
|
|
savedObjectsClient,
|
|
|
|
overlays,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2020-09-23 23:40:52 +02:00
|
|
|
});
|
|
|
|
}
|