-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Maps] convert SavedGisMap to TS #72286
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,12 @@ | |
*/ | ||
|
||
import _ from 'lodash'; | ||
import { createSavedObjectClass } from '../../../../../../../src/plugins/saved_objects/public'; | ||
import { SavedObjectReference } from 'kibana/public'; | ||
import { | ||
createSavedObjectClass, | ||
SavedObject, | ||
SavedObjectKibanaServices, | ||
} from '../../../../../../../src/plugins/saved_objects/public'; | ||
import { | ||
getTimeFilters, | ||
getMapZoom, | ||
|
@@ -18,73 +23,80 @@ import { | |
} from '../../../selectors/map_selectors'; | ||
import { getIsLayerTOCOpen, getOpenTOCDetails } from '../../../selectors/ui_selectors'; | ||
import { copyPersistentState } from '../../../reducers/util'; | ||
// @ts-expect-error | ||
import { extractReferences, injectReferences } from '../../../../common/migrations/references'; | ||
import { getExistingMapPath, MAP_SAVED_OBJECT_TYPE } from '../../../../common/constants'; | ||
// @ts-expect-error | ||
import { getStore } from '../../store_operations'; | ||
import { MapStoreState } from '../../../reducers/store'; | ||
import { LayerDescriptor } from '../../../../common/descriptor_types'; | ||
|
||
export interface ISavedGisMap extends SavedObject { | ||
layerListJSON?: string; | ||
mapStateJSON?: string; | ||
uiStateJSON?: string; | ||
getLayerList(): LayerDescriptor[]; | ||
syncWithStore(): void; | ||
} | ||
|
||
export function createSavedGisMapClass(services) { | ||
export function createSavedGisMapClass(services: SavedObjectKibanaServices) { | ||
const SavedObjectClass = createSavedObjectClass(services); | ||
|
||
class SavedGisMap extends SavedObjectClass { | ||
static type = MAP_SAVED_OBJECT_TYPE; | ||
class SavedGisMap extends SavedObjectClass implements ISavedGisMap { | ||
public static type = MAP_SAVED_OBJECT_TYPE; | ||
|
||
// Mappings are used to place object properties into saved object _source | ||
static mapping = { | ||
public static mapping = { | ||
title: 'text', | ||
description: 'text', | ||
mapStateJSON: 'text', | ||
layerListJSON: 'text', | ||
uiStateJSON: 'text', | ||
}; | ||
static fieldOrder = ['title', 'description']; | ||
static searchSource = false; | ||
public static fieldOrder = ['title', 'description']; | ||
public static searchSource = false; | ||
|
||
constructor(id) { | ||
public showInRecentlyAccessed = true; | ||
public layerListJSON?: string; | ||
public mapStateJSON?: string; | ||
public uiStateJSON?: string; | ||
|
||
constructor(id: string) { | ||
super({ | ||
type: SavedGisMap.type, | ||
mapping: SavedGisMap.mapping, | ||
searchSource: SavedGisMap.searchSource, | ||
extractReferences, | ||
injectReferences: (savedObject, references) => { | ||
injectReferences: (savedObject: ISavedGisMap, references: SavedObjectReference[]) => { | ||
const { attributes } = injectReferences({ | ||
attributes: { layerListJSON: savedObject.layerListJSON }, | ||
references, | ||
}); | ||
|
||
savedObject.layerListJSON = attributes.layerListJSON; | ||
|
||
const indexPatternIds = references | ||
.filter((reference) => { | ||
return reference.type === 'index-pattern'; | ||
}) | ||
.map((reference) => { | ||
return reference.id; | ||
}); | ||
savedObject.indexPatternIds = _.uniq(indexPatternIds); | ||
}, | ||
|
||
// if this is null/undefined then the SavedObject will be assigned the defaults | ||
id: id, | ||
id, | ||
|
||
// default values that will get assigned if the doc is new | ||
defaults: { | ||
title: 'New Map', | ||
description: '', | ||
}, | ||
}); | ||
this.showInRecentlyAccessed = true; | ||
} | ||
|
||
getFullPath() { | ||
return getExistingMapPath(this.id); | ||
this.getFullPath = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this function getting created in the constructor? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typescripting. Something about functional method versus prototypemethod. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds related to the fact that it's now an arrow function. Does it need to be? It doesn't look like this PR changes any calls to it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for typescripting purposes, yes. I had to copy what SavedDashboard was doing to get TS to be happy. |
||
return getExistingMapPath(this.id!); | ||
}; | ||
} | ||
|
||
getLayerList() { | ||
return this.layerListJSON ? JSON.parse(this.layerListJSON) : null; | ||
} | ||
|
||
syncWithStore() { | ||
const state = getStore().getState(); | ||
const state: MapStoreState = getStore().getState(); | ||
const layerList = getLayerListRaw(state); | ||
const layerListConfigOnly = copyPersistentState(layerList); | ||
this.layerListJSON = JSON.stringify(layerListConfigOnly); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
savedObject.indexPatternIds
was added when MapEmbeddable was first introduced in https://github.com/elastic/kibana/pull/31473/files#diff-1bb10565f2889677ad2ded8210390726R50. At the time MapEmbeddableFactory usedsavedObject.indexPatternIds
to obtain index patterns used by the map#35542 changed the way MapEmbeddableFactory obtained indexPatternIds to pulling them from store, acb78a6#diff-e347d1b056afd109215cc604d63d4aefL47. However, that PR failed to clean up setting indexPatternIds on savedMap.
Since savedMap.indexPatternIds is no longer used, it is safe to remove this code