diff --git a/umap/static/umap/js/modules/urls.mjs b/umap/static/umap/js/modules/urls.mjs index 12d6b1e91..7094cf4d1 100644 --- a/umap/static/umap/js/modules/urls.mjs +++ b/umap/static/umap/js/modules/urls.mjs @@ -3,22 +3,11 @@ export default class URLs { this.urls = serverUrls; } - /** - * Returns the URL to the requested resource. - * - * @param {String} urlName The URL name, as specified by the server. - * @param {String|Object} params If a string, the map id, otherwise the parameters passed to the URL template. - * @returns {String} The URL that has been built. - */ get(urlName, params) { - if (typeof params != 'object') - params = { map_id: params }; - if (typeof this[urlName] === "function") return this[urlName](params); if (this.urls.hasOwnProperty(urlName)) { - console.log("urls", this.urls, urlName, params); return L.Util.template(this.urls[urlName], params); } else { throw `Unable to find a URL for route ${urlName}`; @@ -26,19 +15,16 @@ export default class URLs { } // Update if map_id is passed, create otherwise. - map_save({ map_id }) { - if (map_id) - return this.get("map_update", map_id); + map_save(params) { + if (params.map_id) + return this.get("map_update", params); return this.get("map_create"); } // Update the layer if pk is passed, create otherwise. - datalayer_save({ map_id, pk }) { - if (pk) - return this.get( - "datalayer_update", - { map_id: map_id, pk: pk } - ); - return this.get("datalayer_create", map_id); + datalayer_save(params) { + if (params.pk) + return this.get("datalayer_update", params); + return this.get("datalayer_create", params); } } \ No newline at end of file diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index acede19af..b623f8034 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -77,12 +77,12 @@ L.U.Map.include({ const zoomControl = typeof geojson.properties.zoomControl !== 'undefined' ? geojson.properties.zoomControl - : true; + : true geojson.properties.zoomControl = false const fullscreenControl = typeof geojson.properties.fullscreenControl !== 'undefined' ? geojson.properties.fullscreenControl - : true; + : true geojson.properties.fullscreenControl = false L.Util.setBooleanFromQueryString(geojson.properties, 'scrollWheelZoom') @@ -90,7 +90,7 @@ L.U.Map.include({ // After calling parent initialize, as we are doing initCenter our-selves if (geojson.geometry) this.options.center = this.latLng(geojson.geometry) - this.urls = new window.URLs(this.options.urls); + this.urls = new window.URLs(this.options.urls) this.ui = new L.U.UI(this._container) this.xhr = new L.U.Xhr(this.ui) @@ -274,8 +274,8 @@ L.U.Map.include({ history.pushState({}, '', url) } if (L.Util.queryString('download')) { - let download_url = this.urls.get('map_download', this.options.umap_id); - window.location = download_url; + let download_url = this.urls.get('map_download', { map_id: this.options.umap_id }) + window.location = download_url } }) @@ -1094,7 +1094,7 @@ L.U.Map.include({ formData.append('name', this.options.name) formData.append('center', JSON.stringify(this.geometry())) formData.append('settings', JSON.stringify(geojson)) - this.post(this.urls.get('map_save', this.options.umap_id), { + this.post(this.urls.get('map_save', { map_id: this.options.umap_id }), { data: formData, context: this, callback: function (data) { @@ -1176,7 +1176,7 @@ L.U.Map.include({ const formData = new FormData() formData.append('email', email) - const url = this.urls.get('map_send_edit_link', this.options.umap_id) + const url = this.urls.get('map_send_edit_link', { map_id: this.options.umap_id }) this.post(url, { data: formData, }) @@ -1188,7 +1188,7 @@ L.U.Map.include({ content: L._('Please save the map first'), level: 'error', }) - let url = this.urls.get('map_star', this.options.umap_id); + let url = this.urls.get('map_star', { map_id: this.options.umap_id }) this.post(url, { context: this, callback: function (data) { @@ -1796,7 +1796,7 @@ L.U.Map.include({ del: function () { if (confirm(L._('Are you sure you want to delete this map?'))) { - const url = this.urls.get('map_delete', this.options.umap_id); + const url = this.urls.get('map_delete', { map_id: this.options.umap_id }) this.post(url) } }, @@ -1805,8 +1805,8 @@ L.U.Map.include({ if ( confirm(L._('Are you sure you want to clone this map and all its datalayers?')) ) { - const url = this.urls.get('map_clone', this.options.umap_id); - this.post(url); + const url = this.urls.get('map_clone', { map_id: this.options.umap_id }) + this.post(url) } }, @@ -1940,17 +1940,13 @@ L.U.Map.include({ }, openExternalRouting: function (e) { - let url = this.urls.get( - 'routing', - { - lat: e.latlng.lat, - lng: e.latlng.lng, - locale: L.locale, - zoom: this.getZoom(), - } - ); - if (url) - window.open(url) + let url = this.urls.get('routing', { + lat: e.latlng.lat, + lng: e.latlng.lng, + locale: L.locale, + zoom: this.getZoom(), + }) + if (url) window.open(url) }, getMap: function () { diff --git a/umap/static/umap/test/URLs.mjs b/umap/static/umap/test/URLs.mjs new file mode 100644 index 000000000..dd4a20376 --- /dev/null +++ b/umap/static/umap/test/URLs.mjs @@ -0,0 +1,48 @@ +import URLs from "../js/modules/urls.mjs"; + +describe('URLs', () => { + // Mock server URLs that will be used for testing + const mockServerUrls = { + 'map_create': '/maps/create', + 'map_update': '/maps/{map_id}/update', + 'datalayer_create': '/maps/{map_id}/datalayers/create', + 'datalayer_update': '/maps/{map_id}/datalayers/{pk}/update' + }; + + let urls = new URLs(mockServerUrls); + + describe('get()', () => { + it('should throw an error if the urlName does not exist', () => { + expect(() => urls.get('non_existent')).to.throw(); + }); + + it('should return the correct templated URL for known urlNames', () => { + expect(urls.get('map_create')).to.be.equal('/maps/create'); + expect(urls.get('map_update', { map_id: '123' })).to.be.equal('/maps/123/update'); + }); + + it('should return the correct templated URL when provided with parameters', () => { + expect(urls.get('datalayer_update', { map_id: '123', pk: '456' })).to.be.equal('/maps/123/datalayers/456/update'); + }); + }); + + describe('map_save()', () => { + it('should return the create URL if no map_id is provided', () => { + expect(urls.map_save({})).to.be.equal('/maps/create'); + }); + + it('should return the update URL if a map_id is provided', () => { + expect(urls.map_save({ map_id: '123' })).to.be.equal('/maps/123/update'); + }); + }); + + describe('datalayer_save()', () => { + it('should return the create URL if no pk is provided', () => { + expect(urls.datalayer_save({ map_id: '123' })).to.be.equal('/maps/123/datalayers/create'); + }); + + it('should return the update URL if a pk is provided', () => { + expect(urls.datalayer_save({ map_id: '123', pk: '456' })).to.be.equal('/maps/123/datalayers/456/update'); + }); + }); +}); \ No newline at end of file diff --git a/umap/static/umap/test/index.html b/umap/static/umap/test/index.html index 5f6549d30..bb1eb1617 100644 --- a/umap/static/umap/test/index.html +++ b/umap/static/umap/test/index.html @@ -88,6 +88,7 @@ +