diff --git a/src/controls/print/print-component.js b/src/controls/print/print-component.js index 625b33a51..56d48e205 100644 --- a/src/controls/print/print-component.js +++ b/src/controls/print/print-component.js @@ -430,10 +430,18 @@ const PrintComponent = function PrintComponent(options = {}) { printMapComponent.dispatch('change:togglePrintLegend', { showPrintLegend }); }, close() { + // 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) => { + l.setVisible(true); + l.unset('hidden-for-print'); + }); + if (suppressNewDPIMethod === false) { printResize.resetLayers(); printResize.setResolution(150); } + // Restore scales if (!supressResolutionsRecalculation) { const viewerResolutions = viewer.getResolutions(); @@ -528,6 +536,28 @@ const PrintComponent = function PrintComponent(options = {}) { map.setTarget(printMapComponent.getId()); this.removeViewerControls(); await printMapComponent.addPrintControls(); + + // GH-1537: temporarily add layers for print and hide their original versions + viewer.getLayersByProperty('printRenderMode', 'image').forEach((layer) => { + if (layer.getVisible() && !layer.get('added-for-print') && !layer.get('hidden-for-print')) { + // hide the original, tiled, layer + layer.setVisible(false); + layer.set('hidden-for-print', true); + + // create a duplicate of the layer with a different renderMode + const { map: _, type, name, sourceName, ...properties } = layer.getProperties(); + const newLayer = viewer.addLayer({ + ...properties, + source: sourceName, + renderMode: 'image', + type: type === 'AGS_TILE' ? 'AGS_MAP' : type, + name: `${name}-forPrint`, + visible: true + }, layer); + newLayer.set('added-for-print', true); + } + }); + if (!supressResolutionsRecalculation) { updateResolutions(); } diff --git a/src/viewer.js b/src/viewer.js index 8df2e6477..2129cad1d 100644 --- a/src/viewer.js +++ b/src/viewer.js @@ -395,13 +395,23 @@ const Viewer = function Viewer(targetOption, options = {}) { } }; - const addLayer = function addLayer(layerProps) { + const addLayer = function addLayer(layerProps, insertBefore) { const layer = Layer(layerProps, this); addLayerStylePicker(layerProps); - map.addLayer(layer); + if (insertBefore) { + map.getLayers().insertAt(map.getLayers().getArray().indexOf(insertBefore), layer); + } else { + map.addLayer(layer); + } this.dispatch('addlayer', { layerName: layerProps.name }); + return layer; + }; + + const removeLayer = function removeLayer(layer) { + this.dispatch('removelayer', { layerName: layer.get('name') }); + map.removeLayer(layer); }; const addLayers = function addLayers(layersProps) { @@ -644,6 +654,7 @@ const Viewer = function Viewer(targetOption, options = {}) { getUrlParams, getViewerOptions, removeGroup, + removeLayer, removeOverlays, setStyle, zoomToExtent,