Skip to content

Commit

Permalink
replace require with import and export and update mapstore submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
allyoucanmap committed Nov 24, 2020
1 parent 25f5b94 commit a12cc61
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 193 deletions.
2 changes: 1 addition & 1 deletion geonode_mapstore_client/client/MapStore2
Submodule MapStore2 updated 1546 files
92 changes: 38 additions & 54 deletions geonode_mapstore_client/client/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,25 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
require('react-widgets/dist/css/react-widgets.css');
const assign = require("object-assign");
const ConfigUtils = require('@mapstore/framework/utils/ConfigUtils');
const LocaleUtils = require('@mapstore/framework/utils/LocaleUtils');
const LayersUtils = require('@mapstore/framework/utils/LayersUtils');
LayersUtils.setRegGeoserverRule(/\/[\w- ]*geoserver[\w- ]*\/|\/[\w- ]*gs[\w- ]*\//);
const {keyBy, values} = require('lodash');

require('react-select/dist/react-select.css');
/**
* Add custom (overriding) translations with:
*
* ConfigUtils.setConfigProp('translationsPath', ['./MapStore2/web/client/translations', './translations']);
*/
ConfigUtils.setConfigProp('themePrefix', 'msgapi');
const Persistence = require("@mapstore/framework/api/persistence");
Persistence.addApi("geonode", require("./api/geonode"));
Persistence.setApi("geonode");
import assign from "object-assign";
import keyBy from 'lodash/keyBy';
import values from 'lodash/values';
import { setConfigProp } from '@mapstore/framework/utils/ConfigUtils';
import { getSupportedLocales, setSupportedLocales } from '@mapstore/framework/utils/LocaleUtils';
import { setRegGeoserverRule } from '@mapstore/framework/utils/LayersUtils';
import { addApi, setApi } from "@mapstore/framework/api/persistence";
import geoNodeAPI from "@js/api/geonode";
import MapStore2JSAPI from '@mapstore/framework/jsapi/MapStore2';
import 'react-widgets/dist/css/react-widgets.css';
import 'react-select/dist/react-select.css';

/**
* Use a custom plugins configuration file with:
*
* ConfigUtils.setLocalConfigurationFile('localConfig.json');
*/
// ConfigUtils.setLocalConfigurationFile('MapStore2/web/client/localConfig.json');
setRegGeoserverRule(/\/[\w- ]*geoserver[\w- ]*\/|\/[\w- ]*gs[\w- ]*\//);

/**
* Use a custom application configuration file with:
*
* const appConfig = require('./appConfig');
*
* Or override the application configuration file with (e.g. only one page with a mapviewer):
*
* const appConfig = assign({}, require('../MapStore2/web/client/product/appConfig'), {
* pages: [{
* name: "mapviewer",
* path: "/",
* component: require('../MapStore2/web/client/product/pages/MapViewer')
* }]
* });
*/
// const appConfig = require('../MapStore2/web/client/product/appConfig');
setConfigProp('themePrefix', 'msgapi');

addApi("geonode", geoNodeAPI);
setApi("geonode");

const getScriptPath = function() {
const scriptEl = document.getElementById('ms2-api');
Expand All @@ -61,8 +37,8 @@ const axios = require('@mapstore/framework/libs/ajax');
axios.defaults.xsrfHeaderName = "X-CSRFToken";
axios.defaults.xsrfCookieName = "csrftoken";

const createMapStore2Api = function(plugins) {
const MapStore2 = require('@mapstore/framework/jsapi/MapStore2').withPlugins(plugins, {
const createMapStore2Api = function(plugins, type) {
const MapStore2 = MapStore2JSAPI.withPlugins(plugins, {
theme: {
path: getScriptPath() + '/themes'
},
Expand All @@ -72,7 +48,9 @@ const createMapStore2Api = function(plugins) {
// window.MapStore2 = MapStore2;
return assign({}, MapStore2, { create: function(container, opts) {
if (opts && opts.localConfig) {
Object.keys(opts.localConfig).map(function(c) {ConfigUtils.setConfigProp(c, opts.localConfig[c]); });
Object.keys(opts.localConfig).map(function(c) {setConfigProp(c, opts.localConfig[c]); });
// select mapLayout based on type
setConfigProp('mapLayout', opts.localConfig?.mapLayout?.[type]);
}
return MapStore2.create(container, opts);
}
Expand All @@ -83,23 +61,29 @@ window.initMapstore2Api = function(config, resolve) {

// force supported locales to the selected one
const setLocale = (localeKey) => {
const supportedLocales = LocaleUtils.getSupportedLocales();
const supportedLocales = getSupportedLocales();
const locale = supportedLocales[localeKey]
? { [localeKey]: supportedLocales[localeKey] }
: { en: supportedLocales.en };
LocaleUtils.setSupportedLocales(locale);
setSupportedLocales(locale);
};

require(`./components/${maptype}/ArcGisMapServer`);// eslint-disable-line
if (config === 'preview') {
require.ensure('./previewPlugins', function() {
resolve(createMapStore2Api(require('./previewPlugins')), { setLocale });
});
} else {
require.ensure('./plugins', function() {
resolve(createMapStore2Api(require('./plugins')), { setLocale });
// Note: maptype is provided by the page template
import(`./components/${maptype}/ArcGisMapServer`) // eslint-disable-line
.then(() => {
if (config === 'preview') {
import('./previewPlugins')
.then((mod) => {
const previewPlugins = mod.default;
resolve(createMapStore2Api(previewPlugins, 'preview'), { setLocale });
});
} else {
import('./plugins')
.then((mod) => {
const plugins = mod.default;
resolve(createMapStore2Api(plugins, 'viewer'), { setLocale });
});
}
});
}
};
const createConfigObj = (cfg = []) => keyBy(cfg, (o) => o.name || o);

Expand Down
35 changes: 17 additions & 18 deletions geonode_mapstore_client/client/js/api/geonode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
* LICENSE file in the root directory of this source tree.
*/

const Rx = require('rxjs');
const axios = require('@mapstore/framework/libs/ajax');
const ConfigUtils = require("@mapstore/framework/utils/ConfigUtils");

import Rx from 'rxjs';
import axios from '@mapstore/framework/libs/ajax';
import { getConfigProp } from "@mapstore/framework/utils/ConfigUtils";

const createAttributesFromMetadata = ({ name, description }) => {
return [{
Expand All @@ -35,25 +34,25 @@ const addThumbToAttributes = ({ data }, attributes) => {
];
};
const patchResource = (id, resource) => {
const baseUrl = ConfigUtils.getConfigProp("genode_rest_api") || "./";
const baseUrl = getConfigProp("genode_rest_api") || "./";
return axios.patch(`${baseUrl}resources/${id}/?full=true`, resource);
};

const postResource = (data, metadata, { thumbnail }) => {
const baseUrl = ConfigUtils.getConfigProp("genode_rest_api") || "./";
const baseUrl = getConfigProp("genode_rest_api") || "./";
const attributes = thumbnail ? addThumbToAttributes(thumbnail, createAttributesFromMetadata(metadata)) : createAttributesFromMetadata(metadata);
return axios.post(`${baseUrl}resources/?full=true`, { data, attributes, name: metadata.name }, { timeout: 10000 });
};
const getLayerEditPerimissions = (name) => {
const baseUrl = ConfigUtils.getConfigProp("geonode_url") || "./";
const baseUrl = getConfigProp("geonode_url") || "./";
return axios.get(`${baseUrl}gs/${name}/edit-check`);
};
const getStyleEditPerimissions = (name) => {
const baseUrl = ConfigUtils.getConfigProp("geonode_url") || "./";
const baseUrl = getConfigProp("geonode_url") || "./";
return axios.get(`${baseUrl}gs/${name}/style-check`);
};
const postThumbnail = (endPoint, id, body = {}) => {
const baseUrl = ConfigUtils.getConfigProp("geonode_url") || "./";
const baseUrl = getConfigProp("geonode_url") || "./";
return axios.post(`${baseUrl}${endPoint}/${id}/thumbnail`, body, { timeout: 10000 });
};

Expand All @@ -64,23 +63,23 @@ const postThumbnail = (endPoint, id, body = {}) => {
* @param {object} API the API to use
* @return and observable that emits the resource
*/
const getResource = () => Rx.Observable.empty();
export const getResource = () => Rx.Observable.empty();


/*
* @param {resource} param0 resource content
* @return an observable that emits the id of the resource
*/
const createResource = ({ data, metadata, linkedResources = {} }) =>
export const createResource = ({ data, metadata, linkedResources = {} }) =>
Rx.Observable.defer(() => postResource(data, metadata, linkedResources))
.pluck('data')
.do((res) => window.location.href = `${ConfigUtils.getConfigProp("geonode_url")}maps/${res.id}/edit`) // eslint-disable-line no-return-assign
.do((res) => window.location.href = `${getConfigProp("geonode_url")}maps/${res.id}/edit`) // eslint-disable-line no-return-assign
.filter(() => false);
/**
* Patch a resource
* @param {resource} param0 the resource to update (must contain the id)
*/
const updateResource = ({ id, data } = {}) =>
export const updateResource = ({ id, data } = {}) =>
Rx.Observable.defer(
() => patchResource(id, { id, data }))
.switchMap(
Expand All @@ -93,22 +92,22 @@ const updateResource = ({ id, data } = {}) =>
* @param {object} options options
* @param {object} API the API to use
*/
const deleteResource = () => Rx.Observable.empty();
export const deleteResource = () => Rx.Observable.empty();
/**
* Retrieve layer's edit permission from local gs otherwise are false
*/
const layerEditPermissions = (layer) =>
export const layerEditPermissions = (layer) =>
Rx.Observable.defer(() => getLayerEditPerimissions(layer.name))
.pluck("data")
.map(({ authorized }) => ({ canEdit: authorized }));
const styleEditPermissions = (layer) =>
export const styleEditPermissions = (layer) =>
Rx.Observable.defer(() => getStyleEditPerimissions(layer.name))
.pluck("data")
.map(({ authorized }) => ({ canEdit: authorized }));
const updateThumb = (endPoint, id, body) =>
export const updateThumb = (endPoint, id, body) =>
Rx.Observable.defer(() => postThumbnail(endPoint, id, body));

module.exports = {
export default {
getResource,
createResource,
updateResource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* LICENSE file in the root directory of this source tree.
*/

const Layers = require('@mapstore/framework/utils/leaflet/Layers');
var L = require('leaflet');
import { registerType } from '@mapstore/framework/utils/leaflet/Layers';
import L from 'leaflet';

Layers.registerType('arcgis', (options) => {
registerType('arcgis', (options) => {
return L.esri.dynamicMapLayer({
url: options.url,
opacity: options.opacity || 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* LICENSE file in the root directory of this source tree.
*/

import Layers from '@mapstore/framework/utils/openlayers/Layers';
import { registerType } from '@mapstore/framework/utils/openlayers/Layers';

import TileLayer from 'ol/layer/Tile';
import TileArcGISRest from 'ol/source/TileArcGISRest';

Layers.registerType('arcgis', {
registerType('arcgis', {
create: (options) => {
return new TileLayer({
opacity: options.opacity !== undefined ? options.opacity : 1,
Expand Down
69 changes: 32 additions & 37 deletions geonode_mapstore_client/client/js/epics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,37 @@
/**
* Epics needed to adapt mapstore2 to geonode backend
*/
const Rx = require("rxjs");

const { SELECT_NODE } = require("@mapstore/framework/actions/layers");
const { setPermission } = require("@mapstore/framework/actions/featuregrid");
const { setEditPermissionStyleEditor, INIT_STYLE_SERVICE } = require("@mapstore/framework/actions/styleeditor");
const { layerEditPermissions, styleEditPermissions, updateThumb } = require("../api/geonode");
const { getSelectedLayer, layersSelector } = require("@mapstore/framework/selectors/layers");
const { mapSelector } = require("@mapstore/framework/selectors/map");
const ConfigUtils = require("@mapstore/framework/utils/ConfigUtils");

const { updateMapLayout } = require('@mapstore/framework/actions/maplayout');
const { TOGGLE_CONTROL, SET_CONTROL_PROPERTY, SET_CONTROL_PROPERTIES } = require('@mapstore/framework/actions/controls');
const { MAP_CONFIG_LOADED } = require('@mapstore/framework/actions/config');
const { SIZE_CHANGE, CLOSE_FEATURE_GRID, OPEN_FEATURE_GRID } = require('@mapstore/framework/actions/featuregrid');
const { CLOSE_IDENTIFY, ERROR_FEATURE_INFO, TOGGLE_MAPINFO_STATE, LOAD_FEATURE_INFO, EXCEPTIONS_FEATURE_INFO, NO_QUERYABLE_LAYER } = require('@mapstore/framework/actions/mapInfo');
const { SHOW_SETTINGS, HIDE_SETTINGS } = require('@mapstore/framework/actions/layers');
const { PURGE_MAPINFO_RESULTS } = require('@mapstore/framework/actions/mapInfo');
const { isMapInfoOpen } = require('@mapstore/framework/selectors/mapInfo');

const { isFeatureGridOpen, getDockSize } = require('@mapstore/framework/selectors/featuregrid');
const { head, get } = require('lodash');
// const {updateMapLayoutEpic} = require('@mapstore/framework/epics/maplayout');

// const {basicError} = require('@mapstore/framework/utils/NotificationUtils');
import Rx from "rxjs";

import { setEditPermissionStyleEditor, INIT_STYLE_SERVICE } from "@mapstore/framework/actions/styleeditor";
import { layerEditPermissions, styleEditPermissions, updateThumb } from "@js/api/geonode";
import { getSelectedLayer, layersSelector } from "@mapstore/framework/selectors/layers";
import { mapSelector } from "@mapstore/framework/selectors/map";
import { getConfigProp } from "@mapstore/framework/utils/ConfigUtils";

import { updateMapLayout } from '@mapstore/framework/actions/maplayout';
import { TOGGLE_CONTROL, SET_CONTROL_PROPERTY, SET_CONTROL_PROPERTIES } from '@mapstore/framework/actions/controls';
import { MAP_CONFIG_LOADED } from '@mapstore/framework/actions/config';
import { SIZE_CHANGE, CLOSE_FEATURE_GRID, OPEN_FEATURE_GRID, setPermission } from '@mapstore/framework/actions/featuregrid';
import { CLOSE_IDENTIFY, ERROR_FEATURE_INFO, TOGGLE_MAPINFO_STATE, LOAD_FEATURE_INFO, EXCEPTIONS_FEATURE_INFO, PURGE_MAPINFO_RESULTS } from '@mapstore/framework/actions/mapInfo';
import { SHOW_SETTINGS, HIDE_SETTINGS, SELECT_NODE } from '@mapstore/framework/actions/layers';
import { isMapInfoOpen } from '@mapstore/framework/selectors/mapInfo';

import { isFeatureGridOpen, getDockSize } from '@mapstore/framework/selectors/featuregrid';
import head from 'lodash/head';
import get from 'lodash/get';

/**
* We need to include missing epics. The plugins that normally include this epic is not used.
*/
const { mapSaveMapResourceEpic } = require("@mapstore/framework/epics/maps");
const { showCoordinateEditorSelector } = require('@mapstore/framework/selectors/controls');

import { mapSaveMapResourceEpic } from "@mapstore/framework/epics/maps";
import { showCoordinateEditorSelector } from '@mapstore/framework/selectors/controls';

/**
* When a user selects a layer, the app checks for layer editing permission.
*/
const _setFeatureEditPermission = (action$, { getState } = {}) =>
action$.ofType(SELECT_NODE).filter(({ nodeType }) => nodeType === "layer" && !ConfigUtils.getConfigProp("disableCheckEditPermissions"))
export const _setFeatureEditPermission = (action$, { getState } = {}) =>
action$.ofType(SELECT_NODE).filter(({ nodeType }) => nodeType === "layer" && !getConfigProp("disableCheckEditPermissions"))
.switchMap(() => {
const layer = getSelectedLayer(getState() || {});
return layer ? layerEditPermissions(layer)
Expand All @@ -56,11 +51,11 @@ const _setFeatureEditPermission = (action$, { getState } = {}) =>
* When a user selects a layer, the app checks for style editing permission.
* INIT_STYLE_SERVICE si needed for map editing, it ensures an user has permission to edit style of a specific layer retrieved from catalog
*/
const _setStyleEditorPermission = (action$, { getState } = {}) =>
export const _setStyleEditorPermission = (action$, { getState } = {}) =>
action$.ofType(INIT_STYLE_SERVICE, SELECT_NODE)
.filter(({ nodeType }) =>
nodeType && nodeType === "layer" && !ConfigUtils.getConfigProp("disableCheckEditPermissions")
|| !nodeType && !ConfigUtils.getConfigProp("disableCheckEditPermissions"))
nodeType && nodeType === "layer" && !getConfigProp("disableCheckEditPermissions")
|| !nodeType && !getConfigProp("disableCheckEditPermissions"))
.switchMap((action) => {
const layer = getSelectedLayer(getState() || {});
return layer
Expand All @@ -73,7 +68,7 @@ const _setStyleEditorPermission = (action$, { getState } = {}) =>
/**
* Update geonode thumbnail for layers or maps
*/
const _setThumbnail = (action$, { getState } = {}) =>
export const _setThumbnail = (action$, { getState } = {}) =>
action$.ofType("GEONODE:CREATE_MAP_THUMBNAIL", "GEONODE:CREATE_LAYER_THUMBNAIL")
.do(() => {
try {
Expand Down Expand Up @@ -133,9 +128,9 @@ const _setThumbnail = (action$, { getState } = {}) =>
});
});
// Modified to accept map-layout from Config diff less NO_QUERYABLE_LAYERS, SET_CONTROL_PROPERTIES more action$.ofType(PURGE_MAPINFO_RESULTS)
const updateMapLayoutEpic = (action$, store) =>
export const updateMapLayoutEpic = (action$, store) =>

action$.ofType(MAP_CONFIG_LOADED, SIZE_CHANGE, NO_QUERYABLE_LAYER, SET_CONTROL_PROPERTIES, CLOSE_FEATURE_GRID, OPEN_FEATURE_GRID, CLOSE_IDENTIFY, TOGGLE_MAPINFO_STATE, LOAD_FEATURE_INFO, EXCEPTIONS_FEATURE_INFO, TOGGLE_CONTROL, SET_CONTROL_PROPERTY, SHOW_SETTINGS, HIDE_SETTINGS, ERROR_FEATURE_INFO, PURGE_MAPINFO_RESULTS)
action$.ofType(MAP_CONFIG_LOADED, SIZE_CHANGE, SET_CONTROL_PROPERTIES, CLOSE_FEATURE_GRID, OPEN_FEATURE_GRID, CLOSE_IDENTIFY, TOGGLE_MAPINFO_STATE, LOAD_FEATURE_INFO, EXCEPTIONS_FEATURE_INFO, TOGGLE_CONTROL, SET_CONTROL_PROPERTY, SHOW_SETTINGS, HIDE_SETTINGS, ERROR_FEATURE_INFO, PURGE_MAPINFO_RESULTS)
.switchMap(() => {
const state = store.getState();

Expand All @@ -150,7 +145,7 @@ const updateMapLayoutEpic = (action$, store) =>
}));
}

const mapLayout = ConfigUtils.getConfigProp("mapLayout") || { left: { sm: 300, md: 500, lg: 600 }, right: { md: 658 }, bottom: { sm: 30 } };
const mapLayout = getConfigProp("mapLayout") || { left: { sm: 300, md: 500, lg: 600 }, right: { md: 658 }, bottom: { sm: 30 } };

if (get(state, "mode") === 'embedded') {
const height = { height: 'calc(100% - ' + mapLayout.bottom.sm + 'px)' };
Expand Down Expand Up @@ -202,7 +197,7 @@ const updateMapLayoutEpic = (action$, store) =>
boundingMapRect
}));
});
module.exports = {
export default {
mapSaveMapResourceEpic,
_setFeatureEditPermission,
_setThumbnail,
Expand Down
Loading

0 comments on commit a12cc61

Please sign in to comment.