Skip to content

Commit 48e0a51

Browse files
committed
hide map element in HTML pages when collections/items do not have spatial component
1 parent 9c5bbf0 commit 48e0a51

File tree

4 files changed

+100
-79
lines changed

4 files changed

+100
-79
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
66

77
Note: Minor version `0.X.0` update might break the API, It's recommended to pin `tipg` to minor version: `tipg>=0.1,<0.2`
88

9+
## [unreleased]
10+
11+
- hide map element in HTML pages when collections/items do not have spatial component
12+
913
## [0.4.4] - 2023-10-03
1014

1115
### fixed

tipg/templates/collection.html

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,30 @@ <h2>Links</h2>
4747

4848
<script>
4949
$(function() {
50-
var map = L.map('map').setView([0, 0], 1);
51-
map.addLayer(new L.TileLayer(
52-
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
53-
maxZoom: 18,
54-
attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
55-
}
56-
));
50+
var collection = {{ response|tojson }};
51+
if (collection.extent && collection.extent.spatial){
52+
var bbox = collection.extent.spatial.bbox[0]
53+
var bbox_polygon = L.polygon([
54+
[bbox[1], bbox[0]],
55+
[bbox[1], bbox[2]],
56+
[bbox[3], bbox[2]],
57+
[bbox[3], bbox[0]]
58+
]);
5759

58-
var bbox = {{ ('extent' in response and response.extent.spatial.bbox.0) or [-180,-90,180,90] }};
59-
var bbox_polygon = L.polygon([
60-
[bbox[1], bbox[0]],
61-
[bbox[1], bbox[2]],
62-
[bbox[3], bbox[2]],
63-
[bbox[3], bbox[0]]
64-
]);
60+
var map = L.map('map').setView([0, 0], 1);
61+
map.addLayer(new L.TileLayer(
62+
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
63+
maxZoom: 18,
64+
attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
65+
}
66+
));
67+
68+
map.addLayer(bbox_polygon);
69+
map.fitBounds(bbox_polygon.getBounds());
70+
} else {
71+
document.getElementById("map").style.display = "none";
72+
}
6573

66-
map.addLayer(bbox_polygon);
67-
map.fitBounds(bbox_polygon.getBounds());
6874
});
6975
</script>
7076

tipg/templates/item.html

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,60 +32,65 @@ <h2>Properties</h2>
3232

3333
<script>
3434
var geojson = {{ response|tojson }};
35-
var map = L.map('map').setView([0, 0], 1);
36-
map.addLayer(new L.TileLayer(
37-
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
38-
maxZoom: 18,
39-
attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
40-
}
41-
));
42-
43-
function displayValue(value) {
44-
switch (typeof value) {
45-
case 'string':
46-
return value;
47-
case 'number':
48-
return value.toString();
49-
case 'object':
50-
if (value instanceof Array) {
51-
return value.map(displayValue).join(', ');
52-
} else {
53-
return JSON.stringify(value);
54-
}
55-
default:
56-
return '';
35+
if (geojson.geometry) {
36+
var map = L.map('map').setView([0, 0], 1);
37+
map.addLayer(new L.TileLayer(
38+
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
39+
maxZoom: 18,
40+
attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
41+
}
42+
));
43+
44+
function displayValue(value) {
45+
switch (typeof value) {
46+
case 'string':
47+
return value;
48+
case 'number':
49+
return value.toString();
50+
case 'object':
51+
if (value instanceof Array) {
52+
return value.map(displayValue).join(', ');
53+
} else {
54+
return JSON.stringify(value);
55+
}
56+
default:
57+
return '';
58+
}
5759
}
58-
}
5960

60-
function addPopup(feature, layer) {
61-
if (feature.properties) {
62-
var popupElm = document.createElement('div');
63-
popupElm.style.overflowX = 'scroll';
61+
function addPopup(feature, layer) {
62+
if (feature.properties) {
63+
var popupElm = document.createElement('div');
64+
popupElm.style.overflowX = 'scroll';
6465

65-
Object.keys(geojson.properties).map(prop => {
66-
var propElm = document.createElement('div');
66+
Object.keys(geojson.properties).map(prop => {
67+
var propElm = document.createElement('div');
6768

68-
var bElm = document.createElement('b');
69-
bElm.innerText = prop;
70-
propElm.appendChild(bElm);
71-
var valueElm = document.createTextNode(` : ${displayValue(feature.properties[prop])}`);
72-
propElm.appendChild(valueElm);
69+
var bElm = document.createElement('b');
70+
bElm.innerText = prop;
71+
propElm.appendChild(bElm);
72+
var valueElm = document.createTextNode(` : ${displayValue(feature.properties[prop])}`);
73+
propElm.appendChild(valueElm);
7374

74-
var brElm = document.createElement('br');
75-
propElm.appendChild(brElm);
75+
var brElm = document.createElement('br');
76+
propElm.appendChild(brElm);
7677

77-
popupElm.appendChild(propElm);
78-
})
78+
popupElm.appendChild(propElm);
79+
})
7980

80-
layer.bindPopup(popupElm);
81+
layer.bindPopup(popupElm);
82+
}
8183
}
84+
85+
var features = L.geoJSON(geojson, {
86+
onEachFeature: addPopup
87+
}).addTo(map);
88+
89+
map.fitBounds(features.getBounds());
90+
} else {
91+
document.getElementById("map").style.display = "none";
8292
}
8393

84-
var features = L.geoJSON(geojson, {
85-
onEachFeature: addPopup
86-
}).addTo(map);
87-
88-
map.fitBounds(features.getBounds());
8994
</script>
9095

9196
{% include "footer.html" %}

tipg/templates/items.html

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,32 +84,38 @@ <h1>Collection Items: {{ response.title or response.id }}</h1>
8484
window.location.href = url;
8585
}
8686
$(function() {
87-
8887
//
8988
// mapping
9089
//
9190
var geojson = {{ response|tojson }};
92-
var map = L.map('map').setView([0, 0], 1);
93-
map.addLayer(new L.TileLayer(
94-
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
95-
maxZoom: 18,
96-
attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
91+
92+
var features = (geojson.features) ? geojson.features : [];
93+
var hasGeom = features.some(feat => feat.geometry);
94+
if (hasGeom) {
95+
var map = L.map('map').setView([0, 0], 1);
96+
map.addLayer(new L.TileLayer(
97+
'https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
98+
maxZoom: 18,
99+
attribution: 'Map data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>'
100+
}
101+
));
102+
103+
function addPopup(feature, layer) {
104+
var aElm = document.createElement('a');
105+
aElm.setAttribute('href', `${currentURL}/${feature.id}`);
106+
aElm.setAttribute('target', '_blank');
107+
aElm.innerText = feature.id;
108+
layer.bindPopup(aElm);
97109
}
98-
));
99-
100-
function addPopup(feature, layer) {
101-
var aElm = document.createElement('a');
102-
aElm.setAttribute('href', `${currentURL}/${feature.id}`);
103-
aElm.setAttribute('target', '_blank');
104-
aElm.innerText = feature.id;
105-
layer.bindPopup(aElm);
106-
}
107110

108-
var features = L.geoJSON(geojson, {
109-
onEachFeature: addPopup
110-
}).addTo(map);
111+
var features = L.geoJSON(geojson, {
112+
onEachFeature: addPopup
113+
}).addTo(map);
111114

112-
map.fitBounds(features.getBounds());
115+
map.fitBounds(features.getBounds());
116+
} else {
117+
document.getElementById("map").style.display = "none";
118+
}
113119

114120
//
115121
// paging

0 commit comments

Comments
 (0)