Message whenever the legend widget is updated

1227
4
Jump to solution
04-24-2017 06:04 AM
Labels (1)
GilbertoMatos
Occasional Contributor II

Hello!

Is there a specific event or location that I can include a code to display for example a message, every time the legend widget is updated?

Example: I've included an alert on the "onOpen" function of the legend widget. It works perfectly, but logically, only when the widget is reopened. What I need is that whenever the legend widget is updated, for example, when the zoom of the map is modified and other layers are displayed, that same message is displayed.

I tried to include the code inside the "refreshLegend" function, but I did not succeed.
I also debugged the zoomslider widget, but I also did not find where the legend widget is updated.

Thanks for any help.
Gilberto.

0 Kudos
1 Solution

Accepted Solutions
GilbertoMatos
Occasional Contributor II

Hi Robert!

Fortunately, I was able to solve the problem: what I did, was to insert a click event, throughout the legend widget.

After clicking on some area of the legend widget, my user wanted to get a popup message, with the description of the layer (metadata). With this, I check the item that was clicked, and I go through layer by layer until I find what corresponds according to the text contained in innerhtml.

It may not be the best way to solve the problem, but it's working and it's needed.

Note: include this code, within the "onOpen" function of the widget.js file of the legend widget.


Thanks for the help one more time.

Hug,
Gilberto.

--------------------------------------------------------------------------

...

this.legend.startup();
this._bindEvent();    

                  
this.own
(
      on
      (
            dom.byId(this.id + "_panel"),
            'click',
            lang.hitch
            (
                  this,
                  function(event)
                  {                                    
                        var sDescription = "";
                        for (var i = 0; i < this.legend.layerInfos.length; i++)
                        {
                              if (this.legend.layerInfos.title == event.srcElement.innerHTML)
                              {
                                    if
                                    (
                                          this.legend.layerInfos.layer.description == "" ||
                                          this.legend.layerInfos.layer.description == "undefined" ||
                                          this.legend.layerInfos.layer.description == "null"
                                    )
                                    {
                                          sDescription = "No information registered for the layer " + this.legend.layerInfos.title + ".";
                                    }
                                    else
                                    {
                                          sDescription = this.legend.layerInfos.layer.description;
                                    }
                              }
                        }
                                    
                        if (sDescription != "")
                        {
                              new Message
                              (
                                    {
                                          titleLabel:"Metadata",
                                          message: sDescription,
                                          width: 500
                                    }
                              );
                        }
                  }
            )
      )

);

View solution in original post

4 Replies
RobertScheitlin__GISP
MVP Emeritus

Gilberto,

  Internally in the JS API it attaches listeners to several event of the map and layer to determine if the legend needs updating. The legend widget does not have any events so the only way you can do this is attach your own event listeners for extent change, layer add, etc.

GilbertoMatos
Occasional Contributor II

Hi robert

I actually did a lot of debugging, I tried to control the methods "_onReceiveData", "fetchData", class "BaseWidget.js" and "DataManager", but I did not succeed.

Actually the legend widget does not fit into these functions. What I really need to do, that's what you said, know if the legend widget has been updated, and if so, I need to run a messaging implementation function for the user.

I have already done this successfully, including with your tips, but I did this implementation, in the onOpen function, and since the widget is always open, after the update, I end up losing those messages.

This update can be done by marking more layers in the layerlist widget, by zooming in zoomslider widget, etc.

I'll try to implement something as your tip, and as soon as I sort it out, I put the solution here, so I can help someone else.

Thank you!
Gilberto.

0 Kudos
GilbertoMatos
Occasional Contributor II

Hi Robert!

Fortunately, I was able to solve the problem: what I did, was to insert a click event, throughout the legend widget.

After clicking on some area of the legend widget, my user wanted to get a popup message, with the description of the layer (metadata). With this, I check the item that was clicked, and I go through layer by layer until I find what corresponds according to the text contained in innerhtml.

It may not be the best way to solve the problem, but it's working and it's needed.

Note: include this code, within the "onOpen" function of the widget.js file of the legend widget.


Thanks for the help one more time.

Hug,
Gilberto.

--------------------------------------------------------------------------

...

this.legend.startup();
this._bindEvent();    

                  
this.own
(
      on
      (
            dom.byId(this.id + "_panel"),
            'click',
            lang.hitch
            (
                  this,
                  function(event)
                  {                                    
                        var sDescription = "";
                        for (var i = 0; i < this.legend.layerInfos.length; i++)
                        {
                              if (this.legend.layerInfos.title == event.srcElement.innerHTML)
                              {
                                    if
                                    (
                                          this.legend.layerInfos.layer.description == "" ||
                                          this.legend.layerInfos.layer.description == "undefined" ||
                                          this.legend.layerInfos.layer.description == "null"
                                    )
                                    {
                                          sDescription = "No information registered for the layer " + this.legend.layerInfos.title + ".";
                                    }
                                    else
                                    {
                                          sDescription = this.legend.layerInfos.layer.description;
                                    }
                              }
                        }
                                    
                        if (sDescription != "")
                        {
                              new Message
                              (
                                    {
                                          titleLabel:"Metadata",
                                          message: sDescription,
                                          width: 500
                                    }
                              );
                        }
                  }
            )
      )

);

GilbertoMatos
Occasional Contributor II

Robert,

Correction: include the code snippet at the end of the "startup" function and not at "onOpen" as I had quoted. If it is in "onOpen", an event is included for each time the widget is opened.

Gilberto.