From a591ae781aeccb023e2a574e000493edcd2ade78 Mon Sep 17 00:00:00 2001 From: Steffen van Bergerem Date: Wed, 10 Aug 2016 10:54:59 +0200 Subject: [PATCH] Refactor tileLayer creation --- .../javascripts/app/helpers/locations.js | 25 ++++++++++++++++ .../javascripts/app/views/location_stream.js | 25 +--------------- .../single_post_content_view.js | 25 +--------------- .../javascripts/app/helpers/locations_spec.js | 29 +++++++++++++++++++ .../views/single_post_content_view_spec.js | 1 - 5 files changed, 56 insertions(+), 49 deletions(-) create mode 100644 app/assets/javascripts/app/helpers/locations.js create mode 100644 spec/javascripts/app/helpers/locations_spec.js diff --git a/app/assets/javascripts/app/helpers/locations.js b/app/assets/javascripts/app/helpers/locations.js new file mode 100644 index 000000000..4909dbe9c --- /dev/null +++ b/app/assets/javascripts/app/helpers/locations.js @@ -0,0 +1,25 @@ +(function() { + app.helpers.locations = { + getTiles: function() { + // If the mapbox option is enabled in the diaspora.yml, the mapbox tiles with the podmin's credentials are used. + if (gon.appConfig.map.mapbox.enabled) { + return L.tileLayer("https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}", { + id: gon.appConfig.map.mapbox.id, + accessToken: gon.appConfig.map.mapbox.access_token, + attribution: "Map data © OpenStreetMap contributors, " + + "CC-BY-SA, " + + "Imagery © Mapbox", + maxZoom: 18 + }); + } + + // maptiles from the Heidelberg University are used by default. + return L.tileLayer("http://korona.geog.uni-heidelberg.de/tiles/roads/x={x}&y={y}&z={z}", { + attribution: "Map data © OpenStreetMap contributors, " + + "rendering " + + "GIScience Research Group @ Heidelberg University", + maxZoom: 18 + }); + } + }; +})(); diff --git a/app/assets/javascripts/app/views/location_stream.js b/app/assets/javascripts/app/views/location_stream.js index 1db156dea..925ee1f2f 100644 --- a/app/assets/javascripts/app/views/location_stream.js +++ b/app/assets/javascripts/app/views/location_stream.js @@ -14,31 +14,8 @@ app.views.LocationStream = app.views.Content.extend({ mapContainer.css("height", "150px"); if (location.lat) { - // If map function is enabled the maptiles from the Heidelberg University are used by default. - var map = L.map(mapContainer[0]).setView([location.lat, location.lng], 14); - - var tiles = L.tileLayer("http://korona.geog.uni-heidelberg.de/tiles/roads/x={x}&y={y}&z={z}", { - attribution: "Map data © OpenStreetMap contributors, " + - "rendering " + - "GIScience Research Group @ Heidelberg University", - maxZoom: 18, - }); - - // If the mapbox option is enabled in the diaspora.yml, the mapbox tiles with the podmin's credentials are used. - if (gon.appConfig.map.mapbox.enabled) { - - tiles = L.tileLayer("https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}", { - id: gon.appConfig.map.mapbox.id, - /* jshint camelcase: false */ - accessToken: gon.appConfig.map.mapbox.access_token, - /* jshint camelcase: true */ - attribution: "Map data © OpenStreetMap contributors, " + - "CC-BY-SA, " + - "Imagery © Mapbox", - maxZoom: 18, - }); - } + var tiles = app.helpers.locations.getTiles(); tiles.addTo(map); diff --git a/app/assets/javascripts/app/views/single-post-viewer/single_post_content_view.js b/app/assets/javascripts/app/views/single-post-viewer/single_post_content_view.js index 2b1e1d2ca..c58cf08af 100644 --- a/app/assets/javascripts/app/views/single-post-viewer/single_post_content_view.js +++ b/app/assets/javascripts/app/views/single-post-viewer/single_post_content_view.js @@ -34,31 +34,8 @@ app.views.SinglePostContent = app.views.Base.extend({ // get location data and render map var location = this.model.get("location"); - // If map function is enabled the maptiles from the Heidelberg University are used by default. - var map = L.map(mapContainer[0]).setView([location.lat, location.lng], 14); - - var tiles = L.tileLayer("http://korona.geog.uni-heidelberg.de/tiles/roads/x={x}&y={y}&z={z}", { - attribution: "Map data © OpenStreetMap contributors, " + - "rendering " + - "GIScience Research Group @ Heidelberg University", - maxZoom: 18, - }); - - // If the mapbox option is enabled in the diaspora.yml, the mapbox tiles with the podmin's credentials are used. - if (gon.appConfig.map.mapbox.enabled) { - - tiles = L.tileLayer("https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}", { - id: gon.appConfig.map.mapbox.id, - /* jshint camelcase: false */ - accessToken: gon.appConfig.map.mapbox.access_token, - /* jshint camelcase: true */ - attribution: "Map data © OpenStreetMap contributors, " + - "CC-BY-SA, " + - "Imagery © Mapbox", - maxZoom: 18, - }); - } + var tiles = app.helpers.locations.getTiles(); tiles.addTo(map); diff --git a/spec/javascripts/app/helpers/locations_spec.js b/spec/javascripts/app/helpers/locations_spec.js new file mode 100644 index 000000000..f9848a7dc --- /dev/null +++ b/spec/javascripts/app/helpers/locations_spec.js @@ -0,0 +1,29 @@ +describe("app.helpers.locations", function() { + describe("getTiles", function() { + context("with mapbox disabled", function() { + beforeEach(function() { + gon.appConfig = {map: {mapbox: {enabled: false}}}; + }); + + it("returns tiles from the Heidelberg University", function() { + var tiles = app.helpers.locations.getTiles(); + expect(tiles._url).toMatch("http://korona.geog.uni-heidelberg.de/"); + expect(tiles._url).not.toMatch("https://api.tiles.mapbox.com/"); + }); + }); + + context("with mapbox enabled", function() { + beforeEach(function() { + /* eslint-disable camelcase */ + gon.appConfig = {map: {mapbox: {enabled: true, id: "yourID", access_token: "yourAccessToken"}}}; + /* eslint-enable camelcase */ + }); + + it("returns tiles from mapbox", function() { + var tiles = app.helpers.locations.getTiles(); + expect(tiles._url).toMatch("https://api.tiles.mapbox.com/"); + expect(tiles._url).not.toMatch("http://korona.geog.uni-heidelberg.de/"); + }); + }); + }); +}); diff --git a/spec/javascripts/app/views/single_post_content_view_spec.js b/spec/javascripts/app/views/single_post_content_view_spec.js index ddf36a505..956866424 100644 --- a/spec/javascripts/app/views/single_post_content_view_spec.js +++ b/spec/javascripts/app/views/single_post_content_view_spec.js @@ -2,7 +2,6 @@ describe("app.views.SinglePostContent", function() { beforeEach(function(){ this.post = factory.post(); this.view = new app.views.SinglePostContent({model : this.post}); - gon.appConfig = { map: {mapbox: {enabled: true, id: "yourID", accessToken: "yourAccessToken" }}}; }); describe("map", function() {