Skip to content

Commit

Permalink
[Maps] convert SavedGisMap to TS (#72286)
Browse files Browse the repository at this point in the history
* [Maps] convert SavedGisMap to TS

* i18n translate new map title
  • Loading branch information
nreese committed Jul 17, 2020
1 parent 7aa600b commit dc7db09
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
*/

import _ from 'lodash';
import { createSavedObjectClass } from '../../../../../../../src/plugins/saved_objects/public';
import { SavedObjectReference } from 'kibana/public';
import { i18n } from '@kbn/i18n';
import {
createSavedObjectClass,
SavedObject,
SavedObjectKibanaServices,
} from '../../../../../../../src/plugins/saved_objects/public';
import {
getTimeFilters,
getMapZoom,
Expand All @@ -18,73 +24,82 @@ 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',
title: i18n.translate('xpack.maps.newMapTitle', {
defaultMessage: 'New Map',
}),
description: '',
},
});
this.showInRecentlyAccessed = true;
}

getFullPath() {
return getExistingMapPath(this.id);
this.getFullPath = () => {
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);
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/maps/public/selectors/map_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { ISource } from '../classes/sources/source';
import { ITMSSource } from '../classes/sources/tms_source';
import { IVectorSource } from '../classes/sources/vector_source';
import { ILayer } from '../classes/layers/layer';
import { ISavedGisMap } from '../routing/bootstrap/services/saved_gis_map';

function createLayerInstance(
layerDescriptor: LayerDescriptor,
Expand Down Expand Up @@ -419,12 +420,11 @@ export const areLayersLoaded = createSelector(

export function hasUnsavedChanges(
state: MapStoreState,
savedMap: unknown,
savedMap: ISavedGisMap,
initialLayerListConfig: LayerDescriptor[]
) {
const layerListConfigOnly = copyPersistentState(getLayerListRaw(state));

// @ts-expect-error
const savedLayerList = savedMap.getLayerList();

return !savedLayerList
Expand Down

0 comments on commit dc7db09

Please sign in to comment.