Skip to content
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

[7.x] [Maps] Migrate SavedGisMap to use createSavedObjectClass (#53121) #53457

Merged
merged 1 commit into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
});
}