Skip to content

Commit

Permalink
Handle the case where a layer is added and re-added and destroying co…
Browse files Browse the repository at this point in the history
…ntrols when necessary

var myLayer;
function _setupSampleLayer(args) {
var layer = args.layer;
var target = args.target;
layer.setVisibility(true);
layer.opacity = 1;
for (var i = 0; i < 8; i++) {
var eLayerInfo = layer.layerInfos[i];
eLayerInfo.defaultVisibility = true
}
};
function _loadSampleLayer() {
var imageParameters = new esri.layers.ImageParameters();
imageParameters.layerIds = [0,1,2,3,4,5,6,7];
imageParameters.layerOption =
esri.layers.ImageParameters.LAYER_OPTION_SHOW;
myLayer = new

esri.layers.ArcGISDynamicMapServiceLayer("http://hougissrv02:6080/arcgis/rest/services/Integrations_iApps/DEN_iAppsBase/MapServer",
{id: "MyMapService",
visible : true,
imageParameters : imageParameters
}
);
myLayer.on("load", function(args) {
_setupSampleLayer(args);
});
myLayer.on("error", function(args) {
debugger;
});
this.app.map.addLayer(myLayer);
this.app.map.on("layer-add-result", function(args) {
var layer = args.layer;
var error = args.error;
var layerInfo = {
title : "My Sample Title",
type : "dynamic",
layer : layer
}
dojo.publish('layerControl/addLayerControls', [[layerInfo]]);
});
}

dojo.publish("layerControl/removeLayerControls", [[myLayer]])
  • Loading branch information
AlexThomasEOG committed Jan 19, 2016
1 parent cdf2533 commit f1fa53c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 42 deletions.
15 changes: 8 additions & 7 deletions viewer/js/gis/dijit/LayerControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ define([
this._subscribeToTopics();
},
_subscribeToTopics: function () {
this._removeLayerControlsHandler = topic.subscribe('layerControl/removeLayerControls', lang.hitch(this, function (layerTitles) {
this._removeLayerControls(layerTitles);
this._removeLayerControlsHandler = topic.subscribe('layerControl/removeLayerControls', lang.hitch(this, function (layers) {
this._removeLayerControls(layers);
}));
this._addLayerControlsHandler = topic.subscribe('layerControl/addLayerControls', lang.hitch(this, function (layerInfos) {
this._addLayerControls(layerInfos);
Expand Down Expand Up @@ -165,12 +165,12 @@ define([
this._checkReorder();
}));
},
// remove the control given an array of layerTitles
_removeLayerControls: function (layerTitles) {
// helper function to determine which children's title have a match in the layerTitles parameter
// remove the control given an array of layers
_removeLayerControls: function (layers) {
// helper function to determine which children's layer have a match in the layers parameter
function _filterList (entry) {
return layerTitles.reduce(function (prior, curr) {
return (curr === entry.layerTitle) || prior;
return layers.reduce(function (prior, curr) {
return (curr === entry.layer) || prior;
}, false);
}
// get a list of ALL the layers that meet the criteria
Expand All @@ -196,6 +196,7 @@ define([
} else {
this.removeChild(layerControl);
}
layerControl.destroy();
}));
},
// create layer control and add to appropriate _container
Expand Down
17 changes: 12 additions & 5 deletions viewer/js/gis/dijit/LayerControl/controls/_DynamicFolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ define([
// ^args
templateString: folderTemplate,
_expandClickHandler: null,
_handlers: [],
postCreate: function () {
this.inherited(arguments);
// Should the control be visible or hidden?
Expand All @@ -47,26 +48,26 @@ define([
} else {
this._setSublayerCheckbox(false, checkNode);
}
on(checkNode, 'click', lang.hitch(this, function () {
this._handlers.push(on(checkNode, 'click', lang.hitch(this, function () {
if (domAttr.get(checkNode, 'data-checked') === 'checked') {
this._setSublayerCheckbox(false, checkNode);
} else {
this._setSublayerCheckbox(true, checkNode);
}
this.control._setVisibleLayers();
this._checkboxScaleRange();
}));
})));
html.set(this.labelNode, this.sublayerInfo.name);
this._expandClick();
if (this.sublayerInfo.minScale !== 0 || this.sublayerInfo.maxScale !== 0) {
this._checkboxScaleRange();
this.control.layer.getMap().on('zoom-end', lang.hitch(this, '_checkboxScaleRange'));
this._handlers.push(this.control.layer.getMap().on('zoom-end', lang.hitch(this, '_checkboxScaleRange')));
}
},
// add on event to expandClickNode
_expandClick: function () {
var i = this.icons;
this._expandClickHandler = on(this.expandClickNode, 'click', lang.hitch(this, function () {
this._handlers.push(this._expandClickHandler = on(this.expandClickNode, 'click', lang.hitch(this, function () {
var expandNode = this.expandNode,
iconNode = this.expandIconNode;
if (domStyle.get(expandNode, 'display') === 'none') {
Expand All @@ -82,7 +83,7 @@ define([
}).play();
domClass.replace(iconNode, i.folder, i.folderOpen);
}
}));
})));
},
// set checkbox based on layer so it's always in sync
_setSublayerCheckbox: function (checked, checkNode) {
Expand All @@ -106,6 +107,12 @@ define([
if ((min !== 0 && scale > min) || (max !== 0 && scale < max)) {
domClass.add(node, 'layerControlCheckIconOutScale');
}
},
destroy: function () {
this.inherited(arguments);
this._handlers.forEach(function (h) {
h.remove();
});
}
});
return _DynamicFolder;
Expand Down
50 changes: 29 additions & 21 deletions viewer/js/gis/dijit/LayerControl/controls/_DynamicSublayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ define([
'dojo/text!./templates/Sublayer.html',
'dojo/i18n!./../nls/resource'
], function (
declare,
lang,
array,
on,
domClass,
domStyle,
domAttr,
fx,
html,
Menu,
MenuItem,
topic,
WidgetBase,
TemplatedMixin,
sublayerTemplate,
i18n
) {
declare,
lang,
array,
on,
domClass,
domStyle,
domAttr,
fx,
html,
Menu,
MenuItem,
topic,
WidgetBase,
TemplatedMixin,
sublayerTemplate,
i18n
) {
var _DynamicSublayer = declare([WidgetBase, TemplatedMixin], {
control: null,
sublayerInfo: null,
Expand All @@ -43,6 +43,7 @@ define([
templateString: sublayerTemplate,
i18n: i18n,
_expandClickHandler: null,
_handlers: [],
postCreate: function () {
this.inherited(arguments);
// Should the control be visible or hidden?
Expand All @@ -58,24 +59,24 @@ define([

this._setSublayerCheckbox(false, checkNode);
}
on(checkNode, 'click', lang.hitch(this, function () {
this._handlers.push(on(checkNode, 'click', lang.hitch(this, function () {
if (domAttr.get(checkNode, 'data-checked') === 'checked') {
this._setSublayerCheckbox(false, checkNode);
} else {
this._setSublayerCheckbox(true, checkNode);
}
this.control._setVisibleLayers();
this._checkboxScaleRange();
}));
})));
html.set(this.labelNode, this.sublayerInfo.name);
this._expandClick();
if (this.sublayerInfo.minScale !== 0 || this.sublayerInfo.maxScale !== 0) {
this._checkboxScaleRange();
this.control.layer.getMap().on('zoom-end', lang.hitch(this, '_checkboxScaleRange'));
this._handlers.push(this.control.layer.getMap().on('zoom-end', lang.hitch(this, '_checkboxScaleRange')));
}
//set up menu
if (this.control.controlOptions.menu &&
this.control.controlOptions.menu.length) {
this.control.controlOptions.menu.length) {
domClass.add(this.labelNode, 'menuLink');
domClass.add(this.iconNode, 'menuLink');
this.menu = new Menu({
Expand Down Expand Up @@ -120,6 +121,7 @@ define([
domClass.replace(iconNode, i.expand, i.collapse);
}
}));
this._handlers.push(this._expandClickHandler);
},
// set checkbox based on layer so it's always in sync
_setSublayerCheckbox: function (checked, checkNode) {
Expand All @@ -143,6 +145,12 @@ define([
if ((min !== 0 && scale > min) || (max !== 0 && scale < max)) {
domClass.add(node, 'layerControlCheckIconOutScale');
}
},
destroy: function () {
this.inherited(arguments);
this._handlers.forEach(function (h) {
h.remove();
});
}
});
return _DynamicSublayer;
Expand Down
22 changes: 13 additions & 9 deletions viewer/js/gis/dijit/LayerControl/plugins/legendUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,19 @@ define([
}, row, 'last');

domConst.place(this._image(legend, layerId, layer), symbol);
}, this);
// place legend in the appropriate sublayer expandNode
// or if a single layer use control expandNode
if (layer.layerInfos.length > 1) {
var sublayerExpandNode = registry.byId(layer.id + '-' + _layer.layerId + '-sublayer-control').expandNode;
html.set(sublayerExpandNode, ''); //clear "No Legend" placeholder
domConst.place(table, sublayerExpandNode);
} else {
domConst.place(table, expandNode);
}, this);
if (layer.layerInfos.reduce(function (prior, curr) {
return (curr.id === _layer.layerId) || prior;
}, false)) {
// place legend in the appropriate sublayer expandNode
// or if a single layer use control expandNode
if (layer.layerInfos.length > 1) {
var sublayerExpandNode = registry.byId(layer.id + '-' + _layer.layerId + '-sublayer-control').expandNode;
html.set(sublayerExpandNode, ''); //clear "No Legend" placeholder
domConst.place(table, sublayerExpandNode);
} else {
domConst.place(table, expandNode);
}
}
}, this);
},
Expand Down

0 comments on commit f1fa53c

Please sign in to comment.