Skip to content

Commit 32f5b31

Browse files
committed
enh: improve zoom control functionality and error handling; increase timeout for legend panel setup, add retry logic for missing elements, and enhance logging for user interactions
1 parent 86e861e commit 32f5b31

1 file changed

Lines changed: 117 additions & 21 deletions

File tree

js/app.js

Lines changed: 117 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -596,21 +596,30 @@ class ShelterAccessApp {
596596
* Setup custom zoom and fullscreen controls integrated into legend panel
597597
*/
598598
setupCustomControls() {
599-
// Wait for legend panel to be created
599+
// Wait for legend panel to be created with increased timeout
600600
setTimeout(() => {
601601
this.addControlsToLegend();
602-
}, 100);
602+
}, 500);
603603
}
604604

605605
/**
606606
* Add zoom and fullscreen controls to legend panel
607607
*/
608608
addControlsToLegend() {
609609
const legendPanel = document.querySelector('.map-legend-panel');
610-
if (!legendPanel) return;
610+
if (!legendPanel) {
611+
console.warn('Legend panel not found, retrying in 200ms...');
612+
setTimeout(() => this.addControlsToLegend(), 200);
613+
return;
614+
}
611615

612616
// Check if controls already exist
613-
if (legendPanel.querySelector('.legend-controls-section')) return;
617+
if (legendPanel.querySelector('.legend-controls-section')) {
618+
console.log('Zoom controls already exist');
619+
return;
620+
}
621+
622+
console.log('Adding zoom controls to legend panel...');
614623

615624
// Create controls section
616625
const controlsSection = document.createElement('div');
@@ -636,31 +645,120 @@ class ShelterAccessApp {
636645
const zoomOutBtn = controlsSection.querySelector('.zoom-out-btn');
637646
const fullscreenBtn = controlsSection.querySelector('.fullscreen-btn');
638647

639-
zoomInBtn?.addEventListener('click', () => this.zoomIn());
640-
zoomOutBtn?.addEventListener('click', () => this.zoomOut());
641-
fullscreenBtn?.addEventListener('click', () => this.toggleFullscreen());
648+
if (zoomInBtn) {
649+
zoomInBtn.addEventListener('click', () => {
650+
console.log('Zoom in button clicked');
651+
this.zoomIn();
652+
});
653+
}
654+
655+
if (zoomOutBtn) {
656+
zoomOutBtn.addEventListener('click', () => {
657+
console.log('Zoom out button clicked');
658+
this.zoomOut();
659+
});
660+
}
661+
662+
if (fullscreenBtn) {
663+
fullscreenBtn.addEventListener('click', () => {
664+
console.log('Fullscreen button clicked');
665+
this.toggleFullscreen();
666+
});
667+
}
668+
669+
console.log('Zoom controls added successfully');
642670
}
643671

644672
/**
645673
* Zoom in functionality
646674
*/
647675
zoomIn() {
648-
const currentViewState = this.deckgl.viewState || this.deckgl.props.initialViewState;
649-
const newZoom = Math.min(currentViewState.zoom + 1, 19);
650-
const newViewState = { ...currentViewState, zoom: newZoom };
651-
this.deckgl.setProps({ viewState: newViewState });
652-
this.handleViewStateChange(newViewState);
676+
console.log('Attempting to zoom in...');
677+
try {
678+
if (!this.deckgl) {
679+
console.error('Deck.gl instance not available');
680+
return;
681+
}
682+
683+
// Use the stored current view state instead of trying to get it from deck.gl
684+
const currentViewState = this._currentViewState || this.deckgl.props.initialViewState;
685+
if (!currentViewState) {
686+
console.error('No current view state available');
687+
return;
688+
}
689+
690+
const currentZoom = currentViewState.zoom || 12;
691+
const newZoom = Math.min(currentZoom + 1, 19);
692+
693+
console.log(`Zooming from ${currentZoom} to ${newZoom}`);
694+
695+
const newViewState = {
696+
...currentViewState,
697+
zoom: newZoom,
698+
transitionDuration: 300,
699+
transitionEasing: t => t * t
700+
};
701+
702+
// Update the view state directly
703+
this.deckgl.setProps({ viewState: newViewState });
704+
705+
// Update our stored view state
706+
this._currentViewState = newViewState;
707+
this._currentZoom = newZoom;
708+
709+
// Update scale bar
710+
this.updateScaleBar();
711+
712+
console.log('Zoom in successful');
713+
} catch (error) {
714+
console.error('Error during zoom in:', error);
715+
}
653716
}
654717

655718
/**
656719
* Zoom out functionality
657720
*/
658721
zoomOut() {
659-
const currentViewState = this.deckgl.viewState || this.deckgl.props.initialViewState;
660-
const newZoom = Math.max(currentViewState.zoom - 1, 7);
661-
const newViewState = { ...currentViewState, zoom: newZoom };
662-
this.deckgl.setProps({ viewState: newViewState });
663-
this.handleViewStateChange(newViewState);
722+
console.log('Attempting to zoom out...');
723+
try {
724+
if (!this.deckgl) {
725+
console.error('Deck.gl instance not available');
726+
return;
727+
}
728+
729+
// Use the stored current view state instead of trying to get it from deck.gl
730+
const currentViewState = this._currentViewState || this.deckgl.props.initialViewState;
731+
if (!currentViewState) {
732+
console.error('No current view state available');
733+
return;
734+
}
735+
736+
const currentZoom = currentViewState.zoom || 12;
737+
const newZoom = Math.max(currentZoom - 1, 7);
738+
739+
console.log(`Zooming from ${currentZoom} to ${newZoom}`);
740+
741+
const newViewState = {
742+
...currentViewState,
743+
zoom: newZoom,
744+
transitionDuration: 300,
745+
transitionEasing: t => t * t
746+
};
747+
748+
// Update the view state directly
749+
this.deckgl.setProps({ viewState: newViewState });
750+
751+
// Update our stored view state
752+
this._currentViewState = newViewState;
753+
this._currentZoom = newZoom;
754+
755+
// Update scale bar
756+
this.updateScaleBar();
757+
758+
console.log('Zoom out successful');
759+
} catch (error) {
760+
console.error('Error during zoom out:', error);
761+
}
664762
}
665763

666764
/**
@@ -1146,8 +1244,7 @@ class ShelterAccessApp {
11461244
this.elements.legendItems.appendChild(legendItem);
11471245
});
11481246

1149-
// Ensure controls are added to legend panel
1150-
setTimeout(() => this.addControlsToLegend(), 50);
1247+
// Controls are handled by setupCustomControls() function
11511248
}
11521249

11531250

@@ -1218,8 +1315,7 @@ class ShelterAccessApp {
12181315
distanceInfo.textContent = `Within ${this.coverageRadius}m radius`;
12191316
this.elements.legendItems.appendChild(distanceInfo);
12201317

1221-
// Ensure controls are added to legend panel
1222-
setTimeout(() => this.addControlsToLegend(), 50);
1318+
// Controls are handled by setupCustomControls() function
12231319
}
12241320

12251321
/**

0 commit comments

Comments
 (0)