Skip to content

Commit

Permalink
Add style to the selected city or prefecture
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbarbosa committed Dec 11, 2023
1 parent f7d19e6 commit 61f8663
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
13 changes: 13 additions & 0 deletions css/maps.css
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,16 @@
#prefectures.active svg text {
visibility: visible;
}

#maps .circle {
display: none;
}

#maps #cities .active .circle {
display: block;
opacity: 0.5;
}

#maps #regions .active {
opacity: 0.5;
}
19 changes: 16 additions & 3 deletions js/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export function drawCities(data, callback) {
cityGroup?.setAttribute('data-nationalCapital', cityData.types.includes('nationalCapital') ? 'true' : 'false');
cityGroup?.setAttribute('data-name', cityData.name.en);
cityGroup?.addEventListener('click', () => {
document.querySelectorAll('#cities svg g').forEach(elm => elm.classList.remove('active'));
cityGroup?.classList.add('active');
setInfo('city', cityData);
});

Expand All @@ -96,7 +98,7 @@ export function drawCities(data, callback) {
city.setAttribute('y', `${y - textHeight * 0.7}`);
}

addStroke(city);
addStroke(city, cityData.bottom);
})

if (callback) {
Expand Down Expand Up @@ -138,8 +140,19 @@ export function drawPrefectures(data, callback) {
prefecture.setAttribute('text-anchor', prefectureData.textAnchor);
}

prefecture.parentElement?.addEventListener('click', () => {
setInfo('prefecture', prefectureData);
const prefectureGroup = prefecture.parentElement;

const logicalname = `F#feature#1#0#JP-${prefectureData.code}#0`;

prefectureGroup?.addEventListener('click', () => {
document.querySelectorAll('#regions svg path').forEach(elm => {
if (elm.logicalname === logicalname) {
elm.classList.add('active');
} else {
elm.classList.remove('active');
}
setInfo('prefecture', prefectureData);
});
});

addStroke(prefecture);
Expand Down
11 changes: 10 additions & 1 deletion js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function extractPrefectures(data, filter = null) {
return dataArray;
}

export function addStroke(elm) {
export function addStroke(elm, cityBottom = false) {
const strokePos = [
{ x: 1, y: 1 },
{ x: 1, y: -1 },
Expand All @@ -113,6 +113,15 @@ export function addStroke(elm) {
const x = parseInt(elm.getAttribute('x'));
const y = parseInt(elm.getAttribute('y'));

const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
circle.setAttribute('cx', `${x - 1}`);
circle.setAttribute('cy', `${cityBottom ? y - 14 : y + 9}`);
circle.setAttribute('r', '30');
circle.setAttribute('fill', 'none');
circle.setAttribute('stroke', 'black');
circle.setAttribute('class', 'circle');
elm.parentElement?.appendChild(circle);

strokePos.forEach(pos => {
const stroke = elm.cloneNode(true);
stroke.setAttribute('fill', 'white');
Expand Down

0 comments on commit 61f8663

Please sign in to comment.