Skip to content

Commit

Permalink
Merge pull request #517 from AlexAThomas/subLayerSupport
Browse files Browse the repository at this point in the history
Sub layer support
  • Loading branch information
tmcgee committed Mar 26, 2016
2 parents 7aa96da + edc9cfa commit fc7e160
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 21 deletions.
74 changes: 73 additions & 1 deletion viewer/js/gis/dijit/LayerControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,17 @@ define([
},
// create layer control and add to appropriate _container
_addControl: function (layerInfo, Control) {
var layer = (typeof layerInfo.layer === 'string') ? this.map.getLayer(layerInfo.layer) : layerInfo.layer;
if (layerInfo.controlOptions && (layerInfo.type === 'dynamic' || layerInfo.type === 'feature')) {
if (layer.loaded) {
this._applyLayerControlOptions(layerInfo.controlOptions, layer);
} else {
layer.on('load', lang.hitch(this, '_applyLayerControlOptions', layer.controlOptions));
}
}
var layerControl = new Control({
controller: this,
layer: (typeof layerInfo.layer === 'string') ? this.map.getLayer(layerInfo.layer) : layerInfo.layer, // check if we have a layer or just a layer id
layer: layer,
layerTitle: layerInfo.title,
controlOptions: lang.mixin({
noLegend: null,
Expand All @@ -226,6 +234,70 @@ define([
this.addChild(layerControl, position);
}
},
_applyLayerControlOptions: function (controlOptions, layer) {
if (typeof controlOptions.includeUnspecifiedLayers === 'undefined' && typeof controlOptions.subLayerInfos === 'undefined' && typeof controlOptions.excludedLayers === 'undefined') {
return;
}
var esriLayerInfos = [];
// Case 1: only show the layers that are explicitly listed
if (!controlOptions.includeUnspecifiedLayers && controlOptions.subLayerInfos && controlOptions.subLayerInfos.length !== 0) {
var subLayerInfos = array.map(controlOptions.subLayerInfos, function (sli) {
return sli.id;
});
array.forEach(layer.layerInfos, function (li) {
if (array.indexOf(subLayerInfos, li.id) !== -1) {
esriLayerInfos.push(li);
}
});
// Case 2: show ALL layers except those in the excluded list
} else if (controlOptions.excludedLayers) {
array.forEach(layer.layerInfos, function (li) {
if (array.indexOf(controlOptions.excludedLayers, li.id) === -1) {
esriLayerInfos.push(li);
}
});
// Case 3: just override the values found in the subLayerInfos
} else if (controlOptions.subLayerInfos) {
// show ALL layers that are in the map service's layerInfos, but take care to override the properties of each subLayerInfo as configured
this._mixinLayerInfos(layer.layerInfos, controlOptions.subLayerInfos);
this._setSublayerVisibilities(layer);
return;
}
// Finally, if we made use of the esriLayerInfos, make sure to apply all the subLayerInfos that were defined to our new array of esri layer infos
if (controlOptions.subLayerInfos) {
this._mixinLayerInfos(esriLayerInfos, controlOptions.subLayerInfos);
}
layer.layerInfos = esriLayerInfos;
this._setSublayerVisibilities(layer);
},
_setSublayerVisibilities: function (layer) {
var visibleIds = array.map(array.filter(layer.layerInfos, function (li) {
return li.defaultVisibility;
}), function (l) {
return l.id;
});
layer.setVisibleLayers(visibleIds);
},
_mixinLayerInfos: function (esriLayerInfos, subLayerInfos) {
// for each of the sublayers, go through the subLayerInfos from the controlOptions and see if defaultVisiblity is set to true or false
// then set each of the layer.layerInfos defaultVisibility appropriately
// assume defaultVisibility is true if it's not defined
if (subLayerInfos && subLayerInfos.length !== 0) {
array.forEach(subLayerInfos, function (sli) {
if (typeof sli.defaultVisibility === 'undefined') {
sli.defaultVisibility = true;
}
});
array.forEach(esriLayerInfos, function (li) {
var sli = array.filter(subLayerInfos, function (s) {
return s.id === li.id;
}).shift();
if (sli) {
lang.mixin(li, sli);
}
});
}
},
// move control up in controller and layer up in map
_moveUp: function (control) {
var id = control.layer.id,
Expand Down
47 changes: 29 additions & 18 deletions viewer/js/gis/dijit/LayerControl/controls/Dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,40 @@ define([
_createSublayers: function (layer) {
// check for single sublayer - if so no sublayer/folder controls
if (layer.layerInfos.length > 1) {
var allLayers = array.map(layer.layerInfos, function (l) {
return l.id;
});
array.forEach(layer.layerInfos, lang.hitch(this, function (info) {
// see if there was any override needed from the subLayerInfos array in the controlOptions
var sublayerInfo = array.filter(this.controlOptions.subLayerInfos, function (sli) {
return sli.id === info.id;
}).shift();
lang.mixin(info, sublayerInfo);
var pid = info.parentLayerId,
slids = info.subLayerIds,
controlId = layer.id + '-' + info.id + '-sublayer-control',
control;
if (pid === -1 && slids === null) {
// it's a top level sublayer
control = new DynamicSublayer({
id: controlId,
control: this,
sublayerInfo: info,
icons: this.icons
});
domConst.place(control.domNode, this.expandNode, 'last');
} else if (pid === -1 && slids !== null) {
// it's a top level folder
control = new DynamicFolder({
id: controlId,
control: this,
sublayerInfo: info,
icons: this.icons
});
domConst.place(control.domNode, this.expandNode, 'last');
// it's a top level
if (pid === -1 || allLayers.indexOf(pid) === -1) {
if (slids === null) {
// it's a top level sublayer
control = new DynamicSublayer({
id: controlId,
control: this,
sublayerInfo: info,
icons: this.icons
});
domConst.place(control.domNode, this.expandNode, 'last');
} else if (slids !== null) {
// it's a top level folder
control = new DynamicFolder({
id: controlId,
control: this,
sublayerInfo: info,
icons: this.icons
});
domConst.place(control.domNode, this.expandNode, 'last');
}
} else if (pid !== -1 && slids !== null) {
// it's a nested folder
control = new DynamicFolder({
Expand Down
9 changes: 9 additions & 0 deletions viewer/js/gis/dijit/LayerControl/controls/_DynamicFolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ define([
_handlers: [],
postCreate: function () {
this.inherited(arguments);
// Should the control be visible or hidden (depends on subLayerInfos)?
if (this.control.controlOptions.subLayerInfos && !this.control.controlOptions.includeUnspecifiedLayers) {
var subLayerInfos = array.map(this.control.controlOptions.subLayerInfos, function (sli) {
return sli.id;
});
if (array.indexOf(subLayerInfos, this.sublayerInfo.id) < 0) {
domClass.add(this.domNode, 'layerControlHidden');
}
}
// Should the control be visible or hidden?
if (this.control.controlOptions.layerIds && array.indexOf(this.control.controlOptions.layerIds, this.sublayerInfo.id) < 0) {
domClass.add(this.domNode, 'layerControlHidden');
Expand Down
10 changes: 9 additions & 1 deletion viewer/js/gis/dijit/LayerControl/controls/_DynamicSublayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ define([
_handlers: [],
postCreate: function () {
this.inherited(arguments);
// Should the control be visible or hidden (depends on subLayerInfos)?
if (this.control.controlOptions.subLayerInfos && !this.control.controlOptions.includeUnspecifiedLayers) {
var subLayerInfos = array.map(this.control.controlOptions.subLayerInfos, function (sli) {
return sli.id;
});
if (array.indexOf(subLayerInfos, this.sublayerInfo.id) < 0) {
domClass.add(this.domNode, 'layerControlHidden');
}
}
// Should the control be visible or hidden?
if (this.control.controlOptions.layerIds && array.indexOf(this.control.controlOptions.layerIds, this.sublayerInfo.id) < 0) {
domClass.add(this.domNode, 'layerControlHidden');
Expand All @@ -56,7 +65,6 @@ define([
if (array.indexOf(this.control.layer.visibleLayers, this.sublayerInfo.id) !== -1) {
this._setSublayerCheckbox(true, checkNode);
} else {

this._setSublayerCheckbox(false, checkNode);
}
this._handlers.push(on(checkNode, 'click', lang.hitch(this, function () {
Expand Down
1 change: 0 additions & 1 deletion viewer/js/viewer/_MapMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ define([
}
}
},

initMapComplete: function (warnings) {
if (warnings && warnings.length > 0) {
this.handleError({
Expand Down

0 comments on commit fc7e160

Please sign in to comment.