Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zoom persistence to OpenLayers plugin #1920

Merged
merged 2 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add zoom persistence
  • Loading branch information
texodus committed Jul 27, 2022
commit 084074446029fb8b47f75a9566f7d420db9d5a5e
10 changes: 10 additions & 0 deletions packages/perspective-viewer-openlayers/src/js/plugin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ views.forEach(async (plugin) => {
get config_column_names() {
return plugin.plugin.initial.names;
}

save() {
return mapView.save(this.shadowRoot.children[1]);
}

async restore(token) {
mapView.restore(this.shadowRoot.children[1], token);
}

delete() {}
}
);

Expand Down
36 changes: 30 additions & 6 deletions packages/perspective-viewer-openlayers/src/js/views/base-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ baseMap.restyle = (container) => {
}
};

baseMap.restore = (container, token) => {
if (container[PRIVATE]) {
container[PRIVATE].next_zoom_state = token;
}
};

baseMap.save = (container) => {
if (container[PRIVATE]) {
const view = container[PRIVATE].map.getView();
return {
center: view.getCenter(),
zoom: view.getZoom(),
};
}
};

baseMap.initializeView = (container, extent) => {
initializeView(container, extent);
};
Expand All @@ -56,7 +72,7 @@ function getOrCreateMap(container) {
map,
tileLayer,
tooltip,
initialisedExtent: false,
invalid_extents: true,
};
}
removeVectorLayer(container);
Expand All @@ -65,13 +81,21 @@ function getOrCreateMap(container) {
}

function initializeView(container, vectorSource) {
// if (!container[PRIVATE].initialisedExtent) {
const extents = vectorSource.getExtent();
const map = container[PRIVATE].map;
map.getView().fit(extents, {size: map.getSize()});
const extents = vectorSource.getExtent();
if (container[PRIVATE]?.next_zoom_state) {
map.getView().setZoom(container[PRIVATE].next_zoom_state.zoom);
map.getView().setCenter(container[PRIVATE].next_zoom_state.center);
container[PRIVATE].next_zoom_state = undefined;
} else if (
map.getView().getCenter().some(isNaN) ||
(!!container[PRIVATE] && container[PRIVATE].invalid_extents)
) {
const map = container[PRIVATE].map;
map.getView().fit(extents, {size: map.getSize()});
}

// container[PRIVATE].initialisedExtent = true;
// }
container[PRIVATE].invalid_extents = extents.some(isNaN);
}

function removeVectorLayer(container) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ mapView.restyle = (container) => {
baseMap.restyle(container);
};

mapView.save = (container) => {
return baseMap.save(container);
};

mapView.restore = (container, token) => {
baseMap.restore(container, token);
};

function featureFromPoint(point, colorMap, sizeMap, shapeMap) {
const feature = new Feature(new Point(fromLonLat(point.cols)));
const fillAndStroke = colorMap(point);
Expand Down