Fix: Increase SVG size and improve click event handling with load listener
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
.svg-object {
|
.svg-object {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: auto;
|
height: 600px;
|
||||||
max-width: 100%;
|
max-width: 900px;
|
||||||
pointer-events: all;
|
pointer-events: all;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,30 +12,47 @@ export class MapSvgComponent implements AfterViewInit {
|
|||||||
constructor(private elementRef: ElementRef) {}
|
constructor(private elementRef: ElementRef) {}
|
||||||
|
|
||||||
ngAfterViewInit(): void {
|
ngAfterViewInit(): void {
|
||||||
// Wait for object to load
|
const objectElement = this.elementRef.nativeElement.querySelector('object');
|
||||||
setTimeout(() => {
|
|
||||||
this.attachClickHandlers();
|
if (objectElement) {
|
||||||
}, 500);
|
objectElement.addEventListener('load', () => {
|
||||||
|
this.attachClickHandlers();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private attachClickHandlers(): void {
|
private attachClickHandlers(): void {
|
||||||
const objectElement = this.elementRef.nativeElement.querySelector('object');
|
const objectElement = this.elementRef.nativeElement.querySelector('object');
|
||||||
if (!objectElement || !objectElement.contentDocument) {
|
if (!objectElement || !objectElement.contentDocument) {
|
||||||
console.error('SVG object not loaded');
|
console.error('SVG object not loaded or no contentDocument');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const svgDoc = objectElement.contentDocument;
|
const svgDoc = objectElement.contentDocument;
|
||||||
const paths = svgDoc.querySelectorAll('path[id]');
|
const paths = svgDoc.querySelectorAll('path[id]');
|
||||||
|
|
||||||
|
console.log(`Found ${paths.length} country paths`);
|
||||||
|
|
||||||
paths.forEach((path: Element) => {
|
paths.forEach((path: Element) => {
|
||||||
const countryCode = path.getAttribute('id');
|
const countryCode = path.getAttribute('id');
|
||||||
if (countryCode) {
|
if (countryCode) {
|
||||||
path.setAttribute('class', 'country-path');
|
path.setAttribute('class', 'country-path');
|
||||||
|
path.setAttribute('style', 'cursor: pointer;');
|
||||||
|
|
||||||
path.addEventListener('click', () => {
|
path.addEventListener('click', () => {
|
||||||
|
console.log(`Clicked country: ${countryCode}`);
|
||||||
this.countryClick.emit(countryCode.toUpperCase());
|
this.countryClick.emit(countryCode.toUpperCase());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
path.addEventListener('mouseenter', () => {
|
||||||
|
(path as SVGPathElement).style.fill = '#3498db';
|
||||||
|
(path as SVGPathElement).style.opacity = '0.8';
|
||||||
|
});
|
||||||
|
|
||||||
|
path.addEventListener('mouseleave', () => {
|
||||||
|
(path as SVGPathElement).style.fill = '';
|
||||||
|
(path as SVGPathElement).style.opacity = '';
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,16 +3,18 @@
|
|||||||
min-height: 100vh;
|
min-height: 100vh;
|
||||||
gap: 2rem;
|
gap: 2rem;
|
||||||
padding: 2rem;
|
padding: 2rem;
|
||||||
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
|
|
||||||
.map-column {
|
.map-column {
|
||||||
flex: 2;
|
flex: 3;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
}
|
}
|
||||||
|
|
||||||
.data-column {
|
.data-column {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
|
min-width: 300px;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
background-color: #f5f5f5;
|
background-color: #f5f5f5;
|
||||||
@@ -26,13 +28,8 @@
|
|||||||
display: flex;
|
display: flex;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
overflow: auto;
|
width: 100%;
|
||||||
}
|
min-height: 500px;
|
||||||
|
|
||||||
.world-map {
|
|
||||||
max-width: 100%;
|
|
||||||
height: auto;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.country-info {
|
.country-info {
|
||||||
|
|||||||
@@ -17,18 +17,21 @@ export class MapComponent {
|
|||||||
constructor(private worldBankService: WorldBankService) {}
|
constructor(private worldBankService: WorldBankService) {}
|
||||||
|
|
||||||
onCountrySelected(countryCode: string): void {
|
onCountrySelected(countryCode: string): void {
|
||||||
|
console.log('Country selected:', countryCode);
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
this.error = null;
|
this.error = null;
|
||||||
|
this.countryData = null;
|
||||||
|
|
||||||
this.worldBankService.getCountryData(countryCode).subscribe({
|
this.worldBankService.getCountryData(countryCode).subscribe({
|
||||||
next: (data) => {
|
next: (data) => {
|
||||||
|
console.log('Received data:', data);
|
||||||
this.countryData = data;
|
this.countryData = data;
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
},
|
},
|
||||||
error: (err) => {
|
error: (err) => {
|
||||||
|
console.error('API Error:', err);
|
||||||
this.error = 'Failed to load country data';
|
this.error = 'Failed to load country data';
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
console.error('API Error:', err);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user