Select to view content in your preferred language

How to deactivate a Tool to use another Tool?

1226
3
10-09-2013 04:20 PM
IanPeebles
Frequent Contributor
I have two toolsets. . one is used to modify features (move, rotate, scale, edit vertices) on the map and the other is used to delete features on the map. The problem is that my delete tool will not work with the modify features toolset. If the move feature button is checked from the modify features toolset, then I can delete a feature, but I want these tools to work separately.

My question is, can I disable a tool using a button?

I have attached a code sample. Any feedback will greatly be appreciated.
0 Kudos
3 Replies
ManishkumarPatel
Deactivated User
I have two toolsets. . one is used to modify features (move, rotate, scale, edit vertices) on the map and the other is used to delete features on the map. The problem is that my delete tool will not work with the modify features toolset. If the move feature button is checked from the modify features toolset, then I can delete a feature, but I want these tools to work separately.

My question is, can I disable a tool using a button?

I have attached a code sample. Any feedback will greatly be appreciated.


Hi Ian,

what you can do is assign the event listener to some variable and use dojo.disconnect method to remove the eventlistener on the button click.

here's an example:

//global variable
var PSFLEventListener;
var NewEventListener;

//use the below to attach the event listener
PSFLEventListener = dojo.connect(plantingsitesFL, "onClick", function(evt) {
               dojo.stopEvent(evt);
               activateEditToolbar(evt.graphic);
               activeFeatureLayer = plantingsitesFL;
            });


//use the below to disconnect on button click and attach the new using the same method.

function button_onclick(){

      dojo.disconnect(PSFLEventListener);

      NewEventListener =  dojo.connect(publictreeinventoryFL, "onClick", function(evt) {
              dojo.stopEvent(evt);
              activateEditToolbar(evt.graphic);
              activeFeatureLayer = publictreeinventoryFL;
            });
}


Hope this helps.

Regards,
Manish
0 Kudos
IanPeebles
Frequent Contributor
Hi Ian,

what you can do is assign the event listener to some variable and use dojo.disconnect method to remove the eventlistener on the button click.

here's an example:

//global variable
var PSFLEventListener;
var NewEventListener;

//use the below to attach the event listener
PSFLEventListener = dojo.connect(plantingsitesFL, "onClick", function(evt) {
               dojo.stopEvent(evt);
               activateEditToolbar(evt.graphic);
               activeFeatureLayer = plantingsitesFL;
            });


//use the below to disconnect on button click and attach the new using the same method.

function button_onclick(){

      dojo.disconnect(PSFLEventListener);

      NewEventListener =  dojo.connect(publictreeinventoryFL, "onClick", function(evt) {
              dojo.stopEvent(evt);
              activateEditToolbar(evt.graphic);
              activeFeatureLayer = publictreeinventoryFL;
            });
}


Hope this helps.

Regards,
Manish


Thanks for the feedback how would that work with the code block below?  The two tools are identify by the comments.

  // EDITING - CONNECT MAP WITH EDITOR TOOLBAR
  dojo.connect(map, "onLoad", createEditToolbar);
  
  // EDITING - CONNECT MAP WITH DELETE FEATURE TOOL
                dojo.connect(map, "onLayersAddResult", editingDeleteFeature);



//  *****************************************************************************
//     *   EDIT FEATURES - MOVE, EDIT VERTICES, SCALE, ROTATE, OPTIONS                                          *
//  *****************************************************************************
        function createEditToolbar() {
  var activeFeatureLayer = null;
         var landuseLayerFL = map.getLayer("landuseLayer");
  var militaryareasFL = map.getLayer("militaryareas");
                editModifyFeaturesTools = new esri.toolbars.Edit(map);

            // ACTIVATE TOOLBAR WHEN CLICKING ON LAND USE
            dojo.connect(landuseLayerFL, "onClick", function(evt) {
               dojo.stopEvent(evt);
               activateEditToolbar(evt.graphic);
               activeFeatureLayer = landuseLayerFL;
            });
  
            // ACTIVATE TOOLBAR WHEN CLICKING ON MILITARY SITES
               dojo.connect(militaryareasFL, "onClick", function(evt) {
               dojo.stopEvent(evt);
               activateEditToolbar(evt.graphic);
        activeFeatureLayer = militaryareasFL;
            });
   
            // DEACTIVATE TOOLBAR
            dojo.connect(map,"onClick", function(evt){
            editModifyFeaturesTools.deactivate();
            });
   
     // POST EDITS AFTER TOOL IS DEACTIVATED
     dojo.connect(editModifyFeaturesTools, "onDeactivate", function(editTool,graphic) {
                activeFeatureLayer.applyEdits(null, [graphic], null);
         alert("Feature successfully updated.");    
            });
    
        }
     // ACTIVATE TOOLBAR EDITING
     function activateEditToolbar(graphic) {
  
            var editTool = 0;

            if (dijit.byId("tool_move").checked) {
                editTool = editTool | esri.toolbars.Edit.MOVE;    
            }
            if (dijit.byId("tool_vertices").checked) {
                editTool = editTool | esri.toolbars.Edit.EDIT_VERTICES; 
            }
            if (dijit.byId("tool_scale").checked) {
                editTool = editTool | esri.toolbars.Edit.SCALE;     
            }
            if (dijit.byId("tool_rotate").checked) {
                editTool = editTool | esri.toolbars.Edit.ROTATE;     
            }
   
           // SPECIFY TOOLBAR OPTIONS
            var options = {
                allowAddVertices: dijit.byId("vtx_ca").checked,
                allowDeleteVertices: dijit.byId("vtx_cd").checked,
                uniformScaling: dijit.byId("uniform_scaling").checked
            };
            editModifyFeaturesTools.activate(editTool, graphic, options);
        }   

//  *****************************************************************************
//     *   DELETE FEATURES                                                                                                            *
//  *****************************************************************************  
   function editingDeleteFeature(results) {
    var layers = dojo.map(results, function (result) {
           return result.layer;
    });

    // DELETE FEATURE USING ONCLICK
    dojo.forEach(layers, function (deleteFeatureslLayer) {
       dojo.connect(deleteFeatureslLayer, "onClick", function (evt) {
       dojo.stopEvent(evt);

        // DELETE FEATURE IS DELETE BUTTON IS CHECKED (ACTIVE)
        if (dijit.byId("tool_delete").checked) {
            deleteFeatureslLayer.applyEdits(null, null, [evt.graphic], function () {
              operation = new esri.dijit.editing.Delete({
                    featureLayer: deleteFeatureslLayer,
                    deletedGraphics: [evt.graphic]
     });
     // ALERT WHEN FEATURE IS DELETED
     alert("Feature deleted.")
    });
   }
  });
 });
}
0 Kudos
IanPeebles
Frequent Contributor
I was just thinking, could I have a button that activates the dojo.connect and a button that disconnects the dojo.connect?

Using this:

  // EDITING - CONNECT MAP WITH EDITOR TOOLBAR
  dojo.connect(map, "onLoad", createEditToolbar);
 
  // EDITING - CONNECT MAP WITH DELETE FEATURE TOOL
                dojo.connect(map, "onLayersAddResult", editingDeleteFeature);
0 Kudos