Extend Print Widget so that the user can toggle labels on/off for dynamic web maps.

418
0
10-12-2018 11:12 AM
Status: Open
TorbjornSvangård
New Contributor II

For some reason there is already an option to toggle labels on/off, but only appears if there is a layer with the class esri.layers.LabelLayer in the web map. To make this option more useful, I think it should add showLabels false to the layerDefinition in the Web_Map_to_JSON. Please check following patch, based on WAB 2.9, to understand what I mean.

diff --git a/server/apps/2/widgets/Print/Print.js b/server/apps/2/widgets/Print/Print.js
index be9b20d2..b3fea565 100644
--- a/server/apps/2/widgets/Print/Print.js
+++ b/server/apps/2/widgets/Print/Print.js
@@ -138,13 +138,8 @@ define([
         html.setStyle(this.titleTr, 'display', '');
       }
 
-      if (this._hasLabelLayer()) {
-        html.setStyle(this.labelsFormDijit.domNode, 'display', '');
-        html.setStyle(this.labelsTitleNode, 'display', '');
-      } else {
-        html.setStyle(this.labelsFormDijit.domNode, 'display', 'none');
-        html.setStyle(this.labelsTitleNode, 'display', 'none');
-      }
+      html.setStyle(this.labelsFormDijit.domNode, 'display', '');
+      html.setStyle(this.labelsTitleNode, 'display', '');
 
       LayerInfos.getInstance(this.map, this.map.itemInfo)
         .then(lang.hitch(this, function(layerInfosObj) {
@@ -188,6 +183,12 @@ define([
           false);
       }
       if (this.printTask._createOperationalLayers) {
+        aspect.after(
+          this.printTask,
+          '_createOperationalLayers',
+          lang.hitch(this, '_hideLabels')
+     );
+
         // if opLayers contains markerSymbol of map.infoWindow, the print job will failed
         aspect.after(
           this.printTask,
@@ -202,13 +203,6 @@ define([
       }
     },
 
-    _hasLabelLayer: function() {
-      return array.some(this.map.graphicsLayerIds, function(glid) {
-        var l = this.map.getLayer(glid);
-        return l && l.declaredClass === 'esri.layers.LabelLayer';
-      }, this);
-    },
-
     _getPrintTaskInfo: function() {
       // portal own print url: portalname/arcgis/sharing/tools/newPrint
       var def = new Deferred();
@@ -276,6 +270,19 @@ define([
       return def;
     },
 
+    _hideLabels: function(opLayers) {
+      checkbox = this.labelsFormDijit.get('value');
+      if ( !checkbox.showLabels[0] ) {
+        array.forEach(opLayers, function(ol) {
+          array.forEach(ol.layers, function(layer) {
+            layer.layerDefinition.drawingInfo = {};
+         layer.layerDefinition.drawingInfo.showLabels = false;
+          });
+        }, this);
+      }
+      return opLayers;
+    },
+
     _fixInvalidSymbol: function(opLayers) {
       array.forEach(opLayers, function(ol) {
         if (ol.id === 'map_graphics') {