From 9d20b1189a90636d39993c0c002629ca3b6fd67b Mon Sep 17 00:00:00 2001 From: Tim McGee Date: Mon, 22 Sep 2014 12:56:51 -0700 Subject: [PATCH] restored some functionality related to templates and the Identify UI that was misplaced during the big push to develop branch last week. split 2 of the larger functions (executeIdentifyTask & getInfoTemplate) into multiple functions to assist with debugging and reduce their complexity. Overall complexity of the Class has increased as a result but worth the tradeoff. --- viewer/js/gis/dijit/Identify.js | 280 ++++++++++++++++++++------------ 1 file changed, 175 insertions(+), 105 deletions(-) diff --git a/viewer/js/gis/dijit/Identify.js b/viewer/js/gis/dijit/Identify.js index 9cdd84d81..07f5bd8b2 100644 --- a/viewer/js/gis/dijit/Identify.js +++ b/viewer/js/gis/dijit/Identify.js @@ -17,7 +17,7 @@ define([ 'dijit/form/FilteringSelect', 'xstyle/css!./Identify/css/Identify.css' ], function (declare, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, MenuItem, lang, array, all, topic, Memory, IdentifyTask, IdentifyParameters, PopupTemplate, IdentifyTemplate) { - var Identify = declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], { + return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], { widgetsInTemplate: true, templateString: IdentifyTemplate, baseClass: 'gis_IdentifyDijit', @@ -93,13 +93,7 @@ define([ } })); if (this.mapRightClickMenu) { - this.map.on('MouseDown', lang.hitch(this, function (evt) { - this.mapRightClick = evt; - })); - this.mapRightClickMenu.addChild(new MenuItem({ - label: 'Identify here', - onClick: lang.hitch(this, 'handleRightClick') - })); + this.addRightClickMenu(); } // rebuild the layer selection list when the map is updated @@ -111,46 +105,89 @@ define([ })); } }, + addRightClickMenu: function () { + this.map.on('MouseDown', lang.hitch(this, function (evt) { + this.mapRightClick = evt; + })); + this.mapRightClickMenu.addChild(new MenuItem({ + label: 'Identify here', + onClick: lang.hitch(this, 'handleRightClick') + })); + }, executeIdentifyTask: function (evt) { + if (!this.checkForGraphicInfoTemplate(evt)) { + return; + } + + this.map.infoWindow.hide(); + this.map.infoWindow.clearFeatures(); + + var mapPoint = evt.mapPoint; + var identifyParams = this.createIdentifyParams(mapPoint); + var identifies = []; + var identifiedlayers = []; + var selectedLayer = this.getSelectedLayer(); + + array.forEach(this.layers, lang.hitch(this, function (layer) { + var layerIds = this.getLayerIds(layer, selectedLayer); + if (layerIds.length > 0) { + var params = lang.clone(identifyParams); + params.layerIds = layerIds; + identifies.push(layer.identifyTask.execute(params)); + identifiedlayers.push(layer); + } + })); + + if (identifies.length > 0) { + this.map.infoWindow.setTitle('Identifying...'); + this.map.infoWindow.setContent('
'); + this.map.infoWindow.show(mapPoint); + all(identifies).then(lang.hitch(this, 'identifyCallback', identifiedlayers), lang.hitch(this, 'identifyError')); + } + }, + + checkForGraphicInfoTemplate: function (evt) { if (evt.graphic) { // handle feature layers that come from a feature service // and may already have an info template var layer = evt.graphic._layer; if (layer.infoTemplate || (layer.capabilities && layer.capabilities.toLowerCase().indexOf('data') < 0)) { - return; + return false; } if (!this.ignoreOtherGraphics) { // handles graphic from another type of graphics layer // added to the map and so the identify is not found if (!this.identifies.hasOwnProperty(layer.id)) { - return; + return false; } // no layerId (graphics) or sublayer not defined if (isNaN(layer.layerId) || !this.identifies[layer.id].hasOwnProperty(layer.layerId)) { - return; + return false; } } + } - this.map.infoWindow.hide(); - this.map.infoWindow.clearFeatures(); + return true; + }, + createIdentifyParams: function (point) { var identifyParams = new IdentifyParameters(); - var mapPoint = evt.mapPoint; identifyParams.tolerance = this.identifyTolerance; identifyParams.returnGeometry = true; identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_VISIBLE; - identifyParams.geometry = mapPoint; + identifyParams.geometry = point; identifyParams.mapExtent = this.map.extent; identifyParams.width = this.map.width; identifyParams.height = this.map.height; identifyParams.spatialReference = this.map.spatialReference; - var identifies = []; - var identifiedlayers = []; - var selectedLayer = this.allLayersId; // default is all layers + return identifyParams; + }, + getSelectedLayer: function () { + var selectedLayer = this.allLayersId; // default is all layers // if we have a UI, then get the selected layer if (this.parentWidget) { var form = this.identifyFormDijit.get('value'); @@ -160,49 +197,45 @@ define([ selectedLayer = form.identifyLayer; } } + return selectedLayer; + }, + getLayerIds: function (layer, selectedLayer) { var arrIds = selectedLayer.split(this.layerSeparator); var allLayersId = this.allLayersId; - array.forEach(this.layers, lang.hitch(this, function (layer) { - var ref = layer.ref, - selectedIds = layer.layerInfo.layerIds; - if (ref.visible) { - if (arrIds[0] === allLayersId || ref.id === arrIds[0]) { - var layerIds = []; - if (arrIds.length > 1 && arrIds[1]) { // layer explicity requested - layerIds = [arrIds[1]]; - } else if ((ref.declaredClass === 'esri.layers.FeatureLayer') && !isNaN(ref.layerId)) { // feature layer - // do not allow feature layer that does not support - // Identify (Feature Service) - if (ref.capabilities && ref.capabilities.toLowerCase().indexOf('data') > 0) { - layerIds = [ref.layerId]; - } - } else if (ref.layerInfos) { - layerIds = []; - array.forEach(ref.layerInfos, lang.hitch(this, function (layerInfo) { - if (!this.includeSubLayer(layerInfo, ref, selectedIds)) { - return; - } - layerIds.push(layerInfo.id); - })); - } - if (layerIds.length > 0) { - var params = lang.clone(identifyParams); - params.layerIds = layerIds; - identifies.push(layer.identifyTask.execute(params)); - identifiedlayers.push(layer); + var ref = layer.ref, + selectedIds = layer.layerInfo.layerIds; + var layerIds = []; + if (ref.visible) { + if (arrIds[0] === allLayersId || ref.id === arrIds[0]) { + if (arrIds.length > 1 && arrIds[1]) { // layer explicity requested + layerIds = [arrIds[1]]; + } else if ((ref.declaredClass === 'esri.layers.FeatureLayer') && !isNaN(ref.layerId)) { // feature layer + // do not allow feature layer that does not support + // Identify (Feature Service) + if (ref.capabilities && ref.capabilities.toLowerCase().indexOf('data') > 0) { + layerIds = [ref.layerId]; } + } else if (ref.layerInfos) { + layerIds = this.getLayerInfos(ref, selectedIds); } } + } + return layerIds; + }, + + getLayerInfos: function (ref, selectedIds) { + var layerIds = []; + array.forEach(ref.layerInfos, lang.hitch(this, function (layerInfo) { + if (!this.includeSubLayer(layerInfo, ref, selectedIds)) { + return; + } + layerIds.push(layerInfo.id); })); + return layerIds; - if (identifies.length > 0) { - this.map.infoWindow.setTitle('Identifying...'); - this.map.infoWindow.setContent('
'); - this.map.infoWindow.show(mapPoint); - all(identifies).then(lang.hitch(this, 'identifyCallback', identifiedlayers), lang.hitch(this, 'identifyError')); - } }, + identifyCallback: function (identifiedlayers, responseArray) { var fSet = []; array.forEach(responseArray, function (response, i) { @@ -210,7 +243,7 @@ define([ array.forEach(response, function (result) { result.feature.geometry.spatialReference = this.map.spatialReference; //temp workaround for ags identify bug. remove when fixed. if (result.feature.infoTemplate === undefined) { - var infoTemplate = this.getInfoTemplate(ref, result); + var infoTemplate = this.getInfoTemplate(ref, null, result); if (infoTemplate) { result.feature.setInfoTemplate(infoTemplate); } else { @@ -230,21 +263,17 @@ define([ }); }, handleRightClick: function () { - if ((this.mapClickMode.current === 'identify') && (this.mapRightClick)) { - this.executeIdentifyTask(this.mapRightClick); - } + this.executeIdentifyTask(this.mapRightClick); }, - getInfoTemplate: function (layer, result) { + + getInfoTemplate: function (layer, layerId, result) { var popup = null; - var layerId = layer.layerId; - var layerName = layer.name; - if (layer._titleForLegend) { - layerName = layer._titleForLegend; - } if (result) { layerId = result.layerId; - layerName = result.layerName; + } else if (layerId === null) { + layerId = layer.layerId; } + // see if we have a Popup config defined for this layer if (this.identifies.hasOwnProperty(layer.id)) { if (this.identifies[layer.id].hasOwnProperty(layerId)) { @@ -260,53 +289,73 @@ define([ // if no Popup config found, create one with all attributes or layer fields if (!popup) { - var fieldInfos = []; - if (result && result.feature) { - var attributes = result.feature.attributes; - if (attributes) { - for (var prop in attributes) { - if (attributes.hasOwnProperty(prop)) { - fieldInfos.push({ - fieldName: prop, - visible: true - }); - } - } - } - } else if (layer._outFields && (layer._outFields.length) && (layer._outFields[0] !== '*')) { - var fields = layer.fields; - array.forEach(layer._outFields, function (fieldName) { - var foundField = array.filter(fields, function (field) { - return (field.name === fieldName); - }); - if (foundField.length > 0) { + popup = this.createInfoTemplate(layer, layerId, result); + } + + return popup; + }, + + createInfoTemplate: function (layer, layerId, result) { + var popup = null, fieldInfos = []; + + var layerName = this.getLayerName(layer); + if (result) { + layerName = result.layerName; + } + + // from the results + if (result && result.feature) { + var attributes = result.feature.attributes; + if (attributes) { + for (var prop in attributes) { + if (attributes.hasOwnProperty(prop)) { fieldInfos.push({ - fieldName: foundField[0].name, - label: foundField[0].alias, + fieldName: prop, visible: true }); } + } + } + + // from the outFields of the layer + } else if (layer._outFields && (layer._outFields.length) && (layer._outFields[0] !== '*')) { + + var fields = layer.fields; + array.forEach(layer._outFields, function (fieldName) { + var foundField = array.filter(fields, function (field) { + return (field.name === fieldName); }); - } else if (layer.fields) { - array.forEach(layer.fields, function (field) { + if (foundField.length > 0) { fieldInfos.push({ - fieldName: field.name, - label: field.alias, + fieldName: foundField[0].name, + label: foundField[0].alias, visible: true }); - }); - } - if (fieldInfos.length > 0) { - popup = new PopupTemplate({ - title: layerName, - fieldInfos: fieldInfos, - showAttachments: (layer.hasAttachments) - }); - if (!this.identifies[layer.id]) { - this.identifies[layer.id] = {}; } - this.identifies[layer.id][layerId] = popup; + }); + + // from the fields layer + } else if (layer.fields) { + + array.forEach(layer.fields, function (field) { + fieldInfos.push({ + fieldName: field.name, + label: field.alias, + visible: true + }); + }); + } + + if (fieldInfos.length > 0) { + popup = new PopupTemplate({ + title: layerName, + fieldInfos: fieldInfos, + showAttachments: (layer.hasAttachments) + }); + if (!this.identifies[layer.id]) { + this.identifies[layer.id] = {}; } + this.identifies[layer.id][layerId] = popup; } return popup; @@ -323,7 +372,7 @@ define([ selectedIds = layer.layerInfo.layerIds; // only include layers that are currently visible if (ref.visible) { - var name = ref._titleForLegend; + var name = this.getLayerName(layer); if ((ref.declaredClass === 'esri.layers.FeatureLayer') && !isNaN(ref.layerId)) { // feature layer identifyItems.push({ name: name, @@ -371,6 +420,7 @@ define([ this.identifyLayerDijit.set('store', identify); this.identifyLayerDijit.set('value', id); }, + includeSubLayer: function (layerInfo, ref, selectedIds) { // exclude group layers if (layerInfo.subLayerIds !== null) { @@ -405,6 +455,28 @@ define([ return true; }, + getLayerName: function (layer) { + var name = null; + if (layer.layerInfo) { + name = layer.layerInfo.title; + } + if (!name) { + array.forEach(this.layers, function (lyr) { + if (lyr.ref.id === layer.id) { + name = lyr.layerInfo.title; + return; + } + }); + } + if (!name) { + name = layer.name; + if (!name && layer.ref) { + name = layer.ref._titleForLegend; // fall back to old method using title from legend + } + } + return name; + }, + layerVisibleAtCurrentScale: function (layer) { var mapScale = this.map.getScale(); return !(((layer.maxScale !== 0 && mapScale < layer.maxScale) || (layer.minScale !== 0 && mapScale > layer.minScale))); @@ -422,8 +494,8 @@ define([ if (this.infoTemplates[layer.id]) { layer.infoTemplate = lang.clone(this.infoTemplates[layer.id]); } - // remove any infoTemplates that might - // interfere with clicking on a feature + // remove any infoTemplates that might + // interfere with clicking on a feature } else { if (layer.infoTemplate) { this.infoTemplates[layer.id] = lang.clone(layer.infoTemplate); @@ -434,6 +506,4 @@ define([ }, this); } }); - - return Identify; }); \ No newline at end of file