Method to detect opening of an application widget from a second widget

1449
7
Jump to solution
07-26-2017 12:54 PM
Jay_Gregory
Occasional Contributor III

I'm developing an enhanced Google Analytics widget, one that tracks specific layer interactions, map interactions, etc. etc.  I'm curious if there is a way to code the Google Analytics widget to detect and track when a user opens any other (or chosen subset) of application widgets.  Given the Google Analytics widget essentially is always running, can I include code in its Widget.js that listens for the creation, opening, or instantiation of other widgets in the application.  I looked in the widget manager class, but am unsure if widgets have access to each other (because it makes sense if they are compartmentalized). 

Thanks!

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

No, you would just subscribe to the openWidget topic in your widget

topic.subscribe('openWidget', function(){
  //do something
});

View solution in original post

0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Emeritus

Jay,

   Sure you can. The WidgetManager class publishes the topic 'openWidget' and 'widgetCreated' and 'widgetActived' that you can subscribe to in your widget.

0 Kudos
Jay_Gregory
Occasional Contributor III

Okay, thanks - now that I'm looking at the WidgetManagerClass, I see a triggerWidgetOpen function that publishes an openWidget topic.  However, I didn't find any information about that in the documentation.  I might need a little help with the code here - supposed I have a widget in my application called "LayerList", and I want to set an event listener in some other widget that triggers when LayerList is opened.  I'm unclear what the code would look like. 

var widgetManager = WidgetManager.getInstance()

on.widgetManager.triggerWidgetOpen("LayerList")('openWidget', function(a){

do some stuff;

});

I know that's not correct - I'm just not sure how these topics and methods should be used.  Any advice would be very helpful.  

Thanks!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

No, you would just subscribe to the openWidget topic in your widget

topic.subscribe('openWidget', function(){
  //do something
});
0 Kudos
Jay_Gregory
Occasional Contributor III

But what I want to do is subscribe to the opening of any widget from my Google Analytics widget.  So something like: 

var myWidgetNames = ["LayerList","AttributeTable","Legend","Draw"];
for (var i=0; i<myWidgetNames.lenth; i++){
var widget = WidgetManager.getInstance().getWidget(widgetName);
   widget.topic.subscribe('openWidget', function(widetName){
     GoogleAnalytics.log('widgetOpen', widgetName);
   });
  });‍‍‍‍‍‍

Does that make sense?  I want to detect the opening of any widget in the application from the source code of a single widget in the application...

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Jay,

   Topic is a dojo class that does not need to be tied to a specific widget for just use the code I posted and it will listen for all widget opens in the app.

0 Kudos
Jay_Gregory
Occasional Contributor III

Ahh, thanks again.  So I couldn't get it to work with 'openWidget' but I could get an event to fire with 'widgetActived'.  It's interesting - the callback only occurs if the user clicks within the widget panel itself (as if to begin using the widget).  Just clicking the on screen widget button that displays the panel doesn't trigger the callback itself - the user must click inside the widget and begin actually using it.  It's not intuitive, but for my purposes it's actually more useful.  

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Jay,

   Turns out I can not get the openWidget topic to fire either but widgetCreated and widgetActived both work.

Don't forget to mark this question as answered by clicking on the "Correct Answer" link on the reply that answered your question.

0 Kudos