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

fix: really disable print buttons while print preview map is loading #1663

Merged
merged 3 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 19 additions & 6 deletions src/controls/print/print-component.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import olAttribution from 'ol/control/Attribution';
import olScaleLine from 'ol/control/ScaleLine';
import { unByKey } from 'ol/Observable';
import { getPointResolution } from 'ol/proj';
import TileImage from 'ol/source/TileImage';
import TileWMSSource from 'ol/source/TileWMS';
Expand Down Expand Up @@ -329,12 +330,16 @@ const PrintComponent = function PrintComponent(options = {}) {
const printInteractionToggle = PrintInteractionToggle({ map, target, mapInteractionsActive, pageSettings: viewer.getViewerOptions().pageSettings });

const printToolbar = PrintToolbar();
map.getAllLayers().forEach((l) => {
// if we begin loading any data we want to disable the print buttons...
l.getSource().on(['tileloadstart', 'imageloadstart'], () => printToolbar.setDisabled(true));
});
// ...they then get re-enabled when the map has finished rendering
map.on('rendercomplete', () => printToolbar.setDisabled(false));

let mapLoadListenRefs;

function disablePrintToolbar() {
printToolbar.setDisabled(true);
}

function enablePrintToolbar() {
printToolbar.setDisabled(false);
}

return Component({
name: 'printComponent',
Expand Down Expand Up @@ -462,6 +467,8 @@ const PrintComponent = function PrintComponent(options = {}) {
// eslint-disable-next-line no-underscore-dangle
map.pixelRatio_ = olPixelRatio;
}
unByKey(mapLoadListenRefs[0]);
unByKey(mapLoadListenRefs[1]);
// GH-1537: remove layers temporarily added for print and unhide layers hidden for print
viewer.getLayersByProperty('added-for-print', true).forEach((l) => viewer.removeLayer(l));
viewer.getLayersByProperty('hidden-for-print', true).forEach((l) => {
Expand Down Expand Up @@ -561,6 +568,12 @@ const PrintComponent = function PrintComponent(options = {}) {
}));
},
async onRender() {
function addMapLoadListeners() {
const startEvRef = map.on('loadstart', disablePrintToolbar);
const endEvRef = map.on('loadend', enablePrintToolbar);
return [startEvRef, endEvRef];
}
mapLoadListenRefs = addMapLoadListeners();
printScale = 0;
today = new Date(Date.now());
viewerMapTarget = map.getTarget();
Expand Down
13 changes: 9 additions & 4 deletions src/controls/print/print-toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const PrintToolbar = function PrintToolbar() {
pdfButton.on('click', this.dispatchPrint.bind(this));
},
dispatchExport() {
this.dispatch('PNG');
if (pngButton.getState() !== 'disabled') this.dispatch('PNG');
},
dispatchPrint() {
this.dispatch('PDF');
if (pdfButton.getState() !== 'disabled') this.dispatch('PDF');
},
onRender() {
this.dispatch('render');
Expand All @@ -37,8 +37,13 @@ const PrintToolbar = function PrintToolbar() {
</div>`;
},
setDisabled(disabled) {
pngButton.setState(disabled ? 'disabled' : 'initial');
pdfButton.setState(disabled ? 'disabled' : 'initial');
if (disabled) {
pngButton.setState('disabled');
pdfButton.setState('disabled');
} else {
pngButton.setState('initial');
pdfButton.setState('initial');
}
}
});
};
Expand Down