Files
notifications-admin/app/templates/views/broadcast/partials/area-map-javascripts.html
T

132 lines
3.8 KiB
HTML
Raw Normal View History

2020-09-03 10:18:28 +01:00
<script src="{{ asset_url('javascripts/leaflet.js') }}"></script>
<script>
var polygons = []
{% for polygon in broadcast_message.simple_polygons_with_bleed.as_coordinate_pairs_lat_long %}
2020-09-03 10:18:28 +01:00
polygons.push(
L.polygon({{polygon}}, {
2020-09-24 15:52:38 +01:00
opacity: 1,
color: '#005ea5',
fillColor: '#2B8CC4',
2020-08-26 10:43:55 +01:00
fillOpacity: 0.15,
2020-09-24 15:52:38 +01:00
weight: 2,
dashArray: [6, 7],
lineCap: 'butt'
2020-09-03 10:18:28 +01:00
})
);
{% endfor %}
{% for polygon in broadcast_message.polygons.as_coordinate_pairs_lat_long %}
polygons.push(
L.polygon({{polygon}}, {
2020-09-24 15:52:38 +01:00
color: '#0b0b0c',
2020-10-02 14:15:40 +01:00
fillColor: '#2B8CC4',
2020-09-24 15:52:38 +01:00
fillOpacity: 0.3,
2020-09-03 10:18:28 +01:00
weight: 2
})
);
{% endfor %}
2020-09-03 10:55:01 +01:00
2020-10-05 13:47:21 +01:00
var mapElement = document.getElementById('area-list-map');
2021-09-01 20:26:12 +01:00
var grandparent = mapElement.parentNode.parentNode;
var isInDetails = grandparent.className.indexOf('govuk-details') !== -1;
2021-08-27 11:18:56 +01:00
var areas = document.querySelectorAll('.area-list > .area-list-item');
var trimRegExp = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
var details;
var label;
2020-09-03 10:55:01 +01:00
// if element is inside a details element then to make the map render correctly we open the details element
// and set up the map before closing the details map after
2021-08-27 11:18:56 +01:00
if (isInDetails) {
2020-09-03 10:55:01 +01:00
details = grandparent;
details.open = true;
}
2021-09-01 20:26:12 +01:00
function addAriaLabel (mapElement) {
function getLabelPrefix (areas) {
2021-09-02 13:52:37 +01:00
return [
'Map of the United Kingdom, showing the ',
(areas.length === 1) ? 'area' : 'areas',
' for'
].join('');
2021-09-01 20:26:12 +01:00
};
function getStringOfAreas (areas) {
var areasLen = areas.length;
var areaStrings = [];
var idx;
function getAreaName (area) {
var areaString = '';
var childNodesLen = area.childNodes.length;
2021-09-02 13:52:37 +01:00
var idx;
2021-09-01 20:26:12 +01:00
var childNode;
2021-09-02 13:52:37 +01:00
for (idx = 0; idx < childNodesLen; idx++) {
childNode = area.childNodes[idx];
2021-09-01 20:26:12 +01:00
if (childNode.nodeType === 3) { areaString += childNode.nodeValue; } // only use text nodes
}
return areaString.replace(trimRegExp, '')
};
if (areasLen === 1) { return getAreaName(areas[0]); }
for (idx = 0; idx < areasLen; idx++) {
areaStrings.push(getAreaName(areas[idx]));
}
// always join last 2 areas with 'and', the rest with commas
return areaStrings.slice(0, areasLen - 1).join(', ') + ' and ' + areaStrings[areasLen - 1];
};
label = getLabelPrefix(areas) + " " + getStringOfAreas(areas);
mapElement.setAttribute('aria-label', label);
};
function addAriaDescription (mapElement) {
var mapContainer = mapElement.parentNode;
var description = document.createElement('p');
var descriptionText = 'Use the arrow keys to move the map. Use the buttons to zoom the map in or out';
description.appendChild(
document.createTextNode(descriptionText)
);
description.setAttribute('id', 'area-list-map__description');
description.className = 'govuk-visually-hidden';
mapContainer.insertBefore(description, mapElement.nextSibling);
mapElement.setAttribute('aria-describedby', 'area-list-map__description');
};
2020-09-03 10:55:01 +01:00
mapElement.style.height = Math.max(320, window.innerHeight - mapElement.offsetTop - 100) + 'px';
2021-08-27 11:18:56 +01:00
mapElement.setAttribute('role', 'region');
2021-09-01 20:26:12 +01:00
addAriaLabel(mapElement);
addAriaDescription(mapElement);
2020-09-03 10:55:01 +01:00
var mymap = L.map(
2020-10-05 13:47:21 +01:00
'area-list-map',
2020-09-03 10:55:01 +01:00
{
scrollWheelZoom: false
}
)
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(mymap);
2020-09-03 10:18:28 +01:00
var polygonGroup = L.featureGroup(polygons).addTo(mymap);
mymap.fitBounds(
polygonGroup.getBounds(),
{padding: [1, 1]}
);
2020-09-03 10:55:01 +01:00
// if element is inside a details element then close the details element
2021-08-27 11:18:56 +01:00
if (isInDetails) {
2020-09-03 10:55:01 +01:00
details.open = false;
}
2020-09-03 10:18:28 +01:00
</script>