Skip to content

Commit

Permalink
[Maps] Migrate SavedGisMap to use createSavedObjectClass (elastic#53121
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Dec 18, 2019
1 parent 68be6fb commit 00abd61
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@
* you may not use this file except in compliance with the Elastic License.
*/

import './saved_gis_map';
import { createSavedGisMapClass } from './saved_gis_map';
import { uiModules } from 'ui/modules';
import { SavedObjectLoader } from 'ui/saved_objects';
import { npStart } from '../../../../../../../src/legacy/ui/public/new_platform';

const module = uiModules.get('app/maps');

// This is the only thing that gets injected into controllers
module.service('gisMapSavedObjectLoader', function(SavedGisMap) {
module.service('gisMapSavedObjectLoader', function() {
const savedObjectsClient = npStart.core.savedObjects.client;
const services = {
savedObjectsClient,
indexPatterns: npStart.plugins.data.indexPatterns,
chrome: npStart.core.chrome,
overlays: npStart.core.overlays,
};
const SavedGisMap = createSavedGisMapClass(services);

return new SavedObjectLoader(SavedGisMap, npStart.core.savedObjects.client, npStart.core.chrome);
});
174 changes: 83 additions & 91 deletions x-pack/legacy/plugins/maps/public/angular/services/saved_gis_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
*/

import _ from 'lodash';
import { uiModules } from 'ui/modules';
import { createLegacyClass } from 'ui/utils/legacy_class';
import { SavedObjectProvider } from 'ui/saved_objects/saved_object';
import { createSavedObjectClass } from 'ui/saved_objects/saved_object';
import {
getTimeFilters,
getMapZoom,
Expand All @@ -24,95 +22,89 @@ import { copyPersistentState } from '../../reducers/util';
import { extractReferences, injectReferences } from '../../../common/migrations/references';
import { MAP_SAVED_OBJECT_TYPE } from '../../../common/constants';

const module = uiModules.get('app/maps');

module.factory('SavedGisMap', function(Private) {
const SavedObject = Private(SavedObjectProvider);
createLegacyClass(SavedGisMap).inherits(SavedObject);
function SavedGisMap(id) {
SavedGisMap.Super.call(this, {
type: SavedGisMap.type,
mapping: SavedGisMap.mapping,
searchSource: SavedGisMap.searchsource,
extractReferences,
injectReferences: (savedObject, references) => {
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,

// default values that will get assigned if the doc is new
defaults: {
title: 'New Map',
description: '',
export function createSavedGisMapClass(services) {
const SavedObjectClass = createSavedObjectClass(services);

class SavedGisMap extends SavedObjectClass {
static type = MAP_SAVED_OBJECT_TYPE;

// Mappings are used to place object properties into saved object _source
static mapping = {
title: 'text',
description: 'text',
mapStateJSON: 'text',
layerListJSON: 'text',
uiStateJSON: 'text',
bounds: {
type: 'object',
},
});
};
static fieldOrder = ['title', 'description'];
static searchSource = false;

constructor(id) {
super({
type: SavedGisMap.type,
mapping: SavedGisMap.mapping,
searchSource: SavedGisMap.searchSource,
extractReferences,
injectReferences: (savedObject, references) => {
const { attributes } = injectReferences({
attributes: { layerListJSON: savedObject.layerListJSON },
references,
});

this.showInRecentlyAccessed = true;
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,

// default values that will get assigned if the doc is new
defaults: {
title: 'New Map',
description: '',
},
});
this.showInRecentlyAccessed = true;
}
getFullPath() {
return `/app/maps#map/${this.id}`;
}
getLayerList() {
return this.layerListJSON ? JSON.parse(this.layerListJSON) : null;
}

syncWithStore(state) {
const layerList = getLayerListRaw(state);
const layerListConfigOnly = copyPersistentState(layerList);
this.layerListJSON = JSON.stringify(layerListConfigOnly);

this.mapStateJSON = JSON.stringify({
zoom: getMapZoom(state),
center: getMapCenter(state),
timeFilters: getTimeFilters(state),
refreshConfig: getRefreshConfig(state),
query: _.omit(getQuery(state), 'queryLastTriggeredAt'),
filters: getFilters(state),
});

this.uiStateJSON = JSON.stringify({
isLayerTOCOpen: getIsLayerTOCOpen(state),
openTOCDetails: getOpenTOCDetails(state),
});

this.bounds = convertMapExtentToPolygon(getMapExtent(state));
}
}

SavedGisMap.type = MAP_SAVED_OBJECT_TYPE;

// Mappings are used to place object properties into saved object _source
SavedGisMap.mapping = {
title: 'text',
description: 'text',
mapStateJSON: 'text',
layerListJSON: 'text',
uiStateJSON: 'text',
bounds: {
type: 'object',
},
};

SavedGisMap.fieldOrder = ['title', 'description'];

SavedGisMap.searchsource = false;

SavedGisMap.prototype.getFullPath = function() {
return `/app/maps#map/${this.id}`;
};

SavedGisMap.prototype.getLayerList = function() {
return this.layerListJSON ? JSON.parse(this.layerListJSON) : null;
};

SavedGisMap.prototype.syncWithStore = function(state) {
const layerList = getLayerListRaw(state);
const layerListConfigOnly = copyPersistentState(layerList);
this.layerListJSON = JSON.stringify(layerListConfigOnly);

this.mapStateJSON = JSON.stringify({
zoom: getMapZoom(state),
center: getMapCenter(state),
timeFilters: getTimeFilters(state),
refreshConfig: getRefreshConfig(state),
query: _.omit(getQuery(state), 'queryLastTriggeredAt'),
filters: getFilters(state),
});

this.uiStateJSON = JSON.stringify({
isLayerTOCOpen: getIsLayerTOCOpen(state),
openTOCDetails: getOpenTOCDetails(state),
});

this.bounds = convertMapExtentToPolygon(getMapExtent(state));
};

return SavedGisMap;
});
}

0 comments on commit 00abd61

Please sign in to comment.