Skip to content

Commit

Permalink
Added support to add & remove layer(s) from the Layer Control
Browse files Browse the repository at this point in the history
//add new layer
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;
var layer = new
esri.layers.ArcGISDynamicMapServiceLayer("http://<server>/arcgis/rest/services/<service>/MapServer",
{id: "MyMapService",
visible : true,
imageParameters : imageParameters
}
);
layer.on("load", function(args) {
_setupSampleLayer(args);
});
layer.on("error", function(args) {
debugger;
});
this.app.map.addLayer(layer);
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]]);
});
}

//remove layer
dojo.publish("layerControl/removeLayerControls", [["My Sample Title"]])
  • Loading branch information
AlexThomasEOG committed Jan 14, 2016
1 parent 4cf83ff commit 18bbfdf
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions viewer/js/gis/dijit/LayerControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,22 @@ define([
this.overlayReorder = false;
this.vectorReorder = false;
}
// load only the modules we need
this._addLayerControls(this.layerInfos);
this._subscribeToTopics();
},
_subscribeToTopics() {
this._removeLayerControlsHandler = topic.subscribe('layerControl/removeLayerControls', lang.hitch(this, function (layerTitles) {
this._removeLayerControls(layerTitles);
}));
this._addLayerControlsHandler = topic.subscribe('layerControl/addLayerControls', lang.hitch(this, function (layerInfos) {
this._addLayerControls(layerInfos);
}));
},
_addLayerControls: function(layerInfos) {
// load only the modules we need
var modules = [];
// push layer control mods
array.forEach(this.layerInfos, function (layerInfo) {
array.forEach(layerInfos, function (layerInfo) {
// check if control is excluded
var controlOptions = layerInfo.controlOptions;
if (controlOptions && controlOptions.exclude === true) {
Expand All @@ -139,7 +151,7 @@ define([
}, this);
// load and go
require(modules, lang.hitch(this, function () {
array.forEach(this.layerInfos, function (layerInfo) {
array.forEach(layerInfos, function (layerInfo) {
// exclude from widget
var controlOptions = layerInfo.controlOptions;
if (controlOptions && controlOptions.exclude === true) {
Expand All @@ -152,6 +164,32 @@ define([
}, this);
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
var _filterList = function (entry) {
return layerTitles.reduce(function(prior,curr){ return (curr === entry.layerTitle)||prior}, false);
}
// get a list of ALL the layers that meet the criteria
var layerControlList = this._overlayContainer.getChildren().filter( function(c) { return _filterList(c)}).concat(
this._vectorContainer.getChildren().filter( function(c) { return _filterList(c)}).concat(
this.getChildren().filter( function(c) { return _filterList(c)})
)
);
// follow the same logic as when the layers were added
array.forEach(layerControlList, lang.hitch(this, function (layerControl) {
if (this.separated) {
if (layerControl._layerType === 'overlay') {
this._overlayContainer.removeChild(layerControl);
} else {
this._vectorContainer.removeChild(layerControl);
}
}
else {
this.removeChild(layerControl);
}
}));
},
// create layer control and add to appropriate _container
_addControl: function (layerInfo, Control) {
Expand Down

0 comments on commit 18bbfdf

Please sign in to comment.