Hi all and Merry Christmas,
I created a very simple widget that clears the map's graphics layer. It only opens a jimu/dijits/Message which ask if you want to clear the features and two buttons, OK and CANCEL. The message dijit is created in the onOpen method and it is closed either when you click OK or CANCEL.
The weird thing is that when you start the app and click the widget button the first time, it opens two messages popups. This is because the onOpen method is called twice. The sequence is as follows:
postCreate - startup - onOpen - onClose - onOpen
However that double onOpen is not repeated for the next times you click the widget's button. I'd like to avoid that repetition.
In this widget, the only method developed is onOpen
onOpen: function(){
console.log('ClearGraphics::onOpen');
var msg = new Message({
type: "question",
titleLabel: "Clear Map",
message: "Want to remove features?",
buttons:[{
label: "YES",
onClick: lang.hitch(this, lang.hitch(this, function(){
if (this.map.graphics.graphics.length > 0){
this.map.graphics.clear();
}
msg.close();
WidgetManager.getInstance().closeWidget(this.id);
}))
},{
label: "CANCEL",
onClick: lang.hitch(this, function(){
msg.close();
WidgetManager.getInstance().closeWidget(this.id);
})
}]
});
}
and the widgets properties in the manifest are:
"properties": {
"inPanel": false,
"hasLocale": true,
"hasStyle": false,
"hasConfig": true,
"hasUIFile": false,
"hasSettingPage": false,
"hasSettingUIFile": false,
"hasSettingLocale": false,
"hasSettingStyle": false,
"IsController": false
}
Another question. Is it normal that the map's graphics layer starts with one graphic by default?
Happy Holidays and Thanks,
Alfonso
Solved! Go to Solution.
Alfonso,
In my ToggleLayerButton widget I overcome this by using a check in the code.
var clazz = declare([BaseWidget], {
name: 'LayerToggleButton',
baseClass: 'widget-layertogglebutton',
isToggling: false,
...
onOpen: function() {
this.setToggleLayer();
var lObjs = [];
array.map(this.toggleLayerIds, lang.hitch(this, function(id){
lObjs.push(this.operLayerInfos.getLayerInfoById(id));
}));
if (!this.isToggling) {
this.isToggling = true;
array.map(lObjs, lang.hitch(this, function(lObj){
this.toggleLayer(lObj);
}));
setTimeout(lang.hitch(this, function() {
this.isToggling = false;
WidgetManager.getInstance().closeWidget(this);
if(lObjs[0]._visible){
domClass.add(this.parentContainer, "jimu-state-selected");
}else{
domClass.remove(this.parentContainer, "jimu-state-selected");
}
}), 300);
}
}
I get the same the onOpen firing twice when first opening.
The first onOpen sets the this.isToggling to true and thus the second onOpen just bypasses the code because the this.isToggling is still true;
Some others have mentioned that the maps.graphics has a graphic by default. I have not noticed because I almost never use the map.graphics. I always create my own GraphicsLayer.
Alfonso,
Can I see your whole widget code?
Alfonso,
In my ToggleLayerButton widget I overcome this by using a check in the code.
var clazz = declare([BaseWidget], {
name: 'LayerToggleButton',
baseClass: 'widget-layertogglebutton',
isToggling: false,
...
onOpen: function() {
this.setToggleLayer();
var lObjs = [];
array.map(this.toggleLayerIds, lang.hitch(this, function(id){
lObjs.push(this.operLayerInfos.getLayerInfoById(id));
}));
if (!this.isToggling) {
this.isToggling = true;
array.map(lObjs, lang.hitch(this, function(lObj){
this.toggleLayer(lObj);
}));
setTimeout(lang.hitch(this, function() {
this.isToggling = false;
WidgetManager.getInstance().closeWidget(this);
if(lObjs[0]._visible){
domClass.add(this.parentContainer, "jimu-state-selected");
}else{
domClass.remove(this.parentContainer, "jimu-state-selected");
}
}), 300);
}
}
I get the same the onOpen firing twice when first opening.
The first onOpen sets the this.isToggling to true and thus the second onOpen just bypasses the code because the this.isToggling is still true;
Some others have mentioned that the maps.graphics has a graphic by default. I have not noticed because I almost never use the map.graphics. I always create my own GraphicsLayer.
Thanks Robert, that worked.