Skip to content

Commit

Permalink
Fix #1750.Add smarter shareURLs generator and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
offtherailz committed Apr 20, 2017
1 parent edf271a commit 87c6d04
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 17 deletions.
3 changes: 2 additions & 1 deletion docma-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@
"web/client/epics/search.js",

"web/client/utils/index.jsdoc",
"web/client/utils/PluginsUtils.js"
"web/client/utils/PluginsUtils.js",
"web/client/utils/ShareUtils.js"
],
"jsapi": "web/client/jsapi/MapStore2.js",
"plugins": [
Expand Down
6 changes: 5 additions & 1 deletion web/client/actions/fullscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
const TOGGLE_FULLSCREEN = "TOGGLE_FULLSCREEN";
/**
* when fullscreen have to be toggled
* @memberof actions.fullscreen
* @param {boolean} enable true for enable, false for disable
* @param {string} elementSelector querySelector string to use to get the element to fullscreen.
* @return {action} the action of type `TOGGLE_FULLSCREEN` with enable flag and element selector.
Expand All @@ -21,7 +22,10 @@ function toggleFullscreen(enable, elementSelector) {
elementSelector
};
}

/**
* Actions for FullScreen Plugin.
* @name actions.fullscreen
*/
module.exports = {
toggleFullscreen,
TOGGLE_FULLSCREEN
Expand Down
18 changes: 3 additions & 15 deletions web/client/plugins/Share.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,8 @@ const {Glyphicon} = require('react-bootstrap');
const Message = require('../components/I18N/Message');
const {toggleControl} = require('../actions/controls');
const ConfigUtils = require('../utils/ConfigUtils');
const ShareUtils = require('../utils/ShareUtils');

const Url = require('url');

const getApiUrl = (url) => {
let urlParsedObj = Url.parse(url, true);

return urlParsedObj.protocol + '//' + urlParsedObj.host + urlParsedObj.path;
};

const getConfigUrl = (url) => {
let urlParsedObj = Url.parse(url, true);

return urlParsedObj.protocol + '//' + (urlParsedObj.host + urlParsedObj.path + ConfigUtils.getConfigProp('geoStoreUrl') + 'data/' + urlParsedObj.hash.substring(urlParsedObj.hash.lastIndexOf('/') + 1, urlParsedObj.hash.lastIndexOf('?'))).replace('//', '/');
};
/**
* Share Plugin allows to share the current URL (location.href) in some different ways.
* You can share it on socials networks(facebook,twitter,google+,linkedin)
Expand All @@ -49,8 +37,8 @@ const getConfigUrl = (url) => {
const Share = connect((state) => ({
isVisible: state.controls && state.controls.share && state.controls.share.enabled,
shareUrl: location.href,
shareApiUrl: getApiUrl(location.href),
shareConfigUrl: getConfigUrl(location.href)
shareApiUrl: ShareUtils.getApiUrl(location.href),
shareConfigUrl: ShareUtils.getConfigUrl(location.href, ConfigUtils.getConfigProp('geoStoreUrl'))
}), {
onClose: toggleControl.bind(null, 'share', null)
})(require('../components/share/SharePanel'));
Expand Down
56 changes: 56 additions & 0 deletions web/client/utils/ShareUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2017, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
const Url = require('url');

/**
* Utility functions for Share tools
* @memberof utils
* @type {Object}
*/
var ShareUtils = {
/**
* get the absolute URL from the local url and the url to convert
* @param {string} localUrl the context where to evaluate the URL, typically location.href
* @param {string} urlToConvert the url to convert
* @return {string} the absolute url of the urlToConvert
*/
getAbsoluteURL: (localUrl, urlToConvert) => {
// case absolute URL
if (urlToConvert.indexOf("http") === 0 || urlToConvert.indexOf("//") === 0) {
return urlToConvert;
}
return Url.resolve(localUrl, urlToConvert);
},
/**
* get the url for the configuration in GeoStore parsing the hash string (`#/viewer/{maptype}/1`)
* @param {string} url the context where to evaluate the URL, typically location.href
* @param {string} geoStoreUrl the Base URL of GeoStore
* @return {string} the absolute url of the GeoStore Resource
*/
getConfigUrl: (url, geoStoreUrl) => {
let urlParsedObj = Url.parse(url, true);
if (!urlParsedObj.hash) {
return null;
}
const start = urlParsedObj.hash.lastIndexOf('/') + 1;
const end = urlParsedObj.hash.lastIndexOf('?') >= 0 ? urlParsedObj.hash.lastIndexOf('?') : urlParsedObj.hash.length;
const mapId = urlParsedObj.hash.substring(start, end);
return Url.resolve(ShareUtils.getAbsoluteURL(url, geoStoreUrl), ('data/' + mapId));
},
/**
* Parses the API url to get the proper base path where to retrieve the js for the api.
* @param {string} url the current context
* @return {string} the base path of mapstore where to retrieve the js api.
*/
getApiUrl: (url) => {
let urlParsedObj = Url.parse(url, false);
return urlParsedObj.protocol + '//' + urlParsedObj.host + urlParsedObj.pathname;
}
};

module.exports = ShareUtils;
52 changes: 52 additions & 0 deletions web/client/utils/__tests__/ShareUtils-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2017, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
var expect = require('expect');
var ShareUtils = require('../ShareUtils');


const MAPSTORE_PATH = "/mapstore/";
const MAP_HASH_PATH = "#/viewer/leaflet/";
const GEOSTORE_DATA_PATH = "data/";
const QUERY_STRING = "?config=myCustomConfig";
const MS_GEOSTORE_EMBEDDED_PATH = "/mapstore/rest/geostore/";
const STANDALONE_GEOSTORE_PATH = "/geostore/rest/";

const LOCALURL = "http://localhost:8081";
const SOMEHOST = "http://somehost.it";
const DEV_URL = "http://dev.mapstore2.geo-solutions.it";

const LOCALURL_PATH = LOCALURL + MAPSTORE_PATH;
const DEV_URL_PATH = DEV_URL + MAPSTORE_PATH;
const DEV_URL_MAP_PATH = DEV_URL_PATH + MAP_HASH_PATH;
const SOMEHOST_PATH = SOMEHOST + MAPSTORE_PATH;
const SOMEHOST_PATH_QUERY_STRING = SOMEHOST_PATH + QUERY_STRING;
const EXTERNAL_GEOSTORE = "http://dev.mapstore2.geo-solutions.it/geostore/rest/";

describe('ShareUtils test', () => {
it('getAbsoluteURL', () => {
expect(ShareUtils.getAbsoluteURL(LOCALURL, MS_GEOSTORE_EMBEDDED_PATH)).toBe( LOCALURL + MS_GEOSTORE_EMBEDDED_PATH );
expect(ShareUtils.getAbsoluteURL(DEV_URL, MS_GEOSTORE_EMBEDDED_PATH)).toBe(DEV_URL + MS_GEOSTORE_EMBEDDED_PATH);
expect(ShareUtils.getAbsoluteURL(DEV_URL_PATH, MS_GEOSTORE_EMBEDDED_PATH)).toBe(DEV_URL + MS_GEOSTORE_EMBEDDED_PATH);
expect(ShareUtils.getAbsoluteURL(SOMEHOST_PATH, STANDALONE_GEOSTORE_PATH)).toBe(SOMEHOST + STANDALONE_GEOSTORE_PATH);
expect(ShareUtils.getAbsoluteURL(SOMEHOST_PATH_QUERY_STRING, STANDALONE_GEOSTORE_PATH)).toBe(SOMEHOST + STANDALONE_GEOSTORE_PATH);
expect(ShareUtils.getAbsoluteURL(SOMEHOST_PATH_QUERY_STRING, EXTERNAL_GEOSTORE)).toBe(EXTERNAL_GEOSTORE);
});
it('getConfigUrl', () => {
expect(ShareUtils.getConfigUrl(DEV_URL_MAP_PATH + "1", MS_GEOSTORE_EMBEDDED_PATH)).toBe(DEV_URL + MS_GEOSTORE_EMBEDDED_PATH + GEOSTORE_DATA_PATH + "1");
expect(ShareUtils.getConfigUrl(DEV_URL_MAP_PATH + "11", MS_GEOSTORE_EMBEDDED_PATH)).toBe(DEV_URL + MS_GEOSTORE_EMBEDDED_PATH + GEOSTORE_DATA_PATH + "11");
expect(ShareUtils.getConfigUrl(DEV_URL_MAP_PATH + "111", MS_GEOSTORE_EMBEDDED_PATH)).toBe(DEV_URL + MS_GEOSTORE_EMBEDDED_PATH + GEOSTORE_DATA_PATH + "111");
expect(ShareUtils.getConfigUrl(DEV_URL_MAP_PATH + "111?abc=def", MS_GEOSTORE_EMBEDDED_PATH)).toBe(DEV_URL + MS_GEOSTORE_EMBEDDED_PATH + GEOSTORE_DATA_PATH + "111");
expect(ShareUtils.getConfigUrl(DEV_URL_MAP_PATH, MS_GEOSTORE_EMBEDDED_PATH)).toBe(DEV_URL + MS_GEOSTORE_EMBEDDED_PATH + GEOSTORE_DATA_PATH);
expect(ShareUtils.getConfigUrl(SOMEHOST_PATH, MS_GEOSTORE_EMBEDDED_PATH)).toBe(null);
});
it('getApiUrl', () => {
expect(ShareUtils.getApiUrl(DEV_URL_MAP_PATH)).toBe(DEV_URL_PATH);
expect(ShareUtils.getApiUrl(LOCALURL_PATH)).toBe(LOCALURL_PATH);
expect(ShareUtils.getApiUrl(LOCALURL_PATH + MAPSTORE_PATH + QUERY_STRING)).toBe(LOCALURL_PATH + MAPSTORE_PATH);
});
});

0 comments on commit 87c6d04

Please sign in to comment.