Source: widgets/SpwIdentifyNew.js

Retour à la documentation
/**
 * @class spw.widgets.SpwIdentifyNew
 */
define(["dojo/_base/declare", "spw/api/SpwBaseWidget", "dojo/dom-construct", "dojo/_base/array", "dojo/_base/lang",
    "dojo/DeferredList", "esri/tasks/IdentifyTask", "esri/tasks/IdentifyParameters", "dijit/layout/ContentPane",
    "dijit/layout/TabContainer", "spw/api/CollapserItem", "dojo/on", "dojo/mouse", "spw/api/Utils", "dojo/i18n!./nls/SpwIdentify",
    "dojo/dom-style"],
        function(declare, SpwBaseWidget, domConstruct, array, lang,
                DeferredList, IdentifyTask, IdentifyParameters, ContentPane, TabContainer,
                CollapserItem, on, mouse, Utils, labels, style) {

            return declare("spw.widgets.SpwIdentifyNew", [SpwBaseWidget], {

                iconClass: "identifyIcon",
                labels: labels,
                _mapClickHandler: null,
                _identifiedServices: null,
                _tabContainer: null,
                _noServiceMessageDiv: null,
                _notIdentifiablePane: null,
                output: null,

                constructor: function(){
                	this.openPanelOnActivate = false;
                },
                onActivate: function() {
                    this.inherited(arguments);
                    this.spwViewer.get('spwMap').setMapCursor("help");
            		if (this._mapClickHandler) {
                        this._mapClickHandler.remove();
                    }
                    this._mapClickHandler = this.spwViewer.get('spwMap').get('esriMap').on("click", lang.hitch(this, this.executeIdentify));
                },
                onDeactivate: function() {
                    this.inherited(arguments);
                    this.spwViewer.get('spwMap').resetCursor();
                    if (this._mapClickHandler) {
                        this._mapClickHandler.remove();
                    }
                },
                executeIdentify: function(mouseEvent) {
                    this._identifiedServices = this.spwViewer.get('spwMap').getMapServices({ visible: true, isBaseMap: false });
                    this.clearOutput();
					array.forEach(this._identifiedServices, lang.hitch(this, function(service) {
                    	if(service.get('identifiable') && service.hasIdentifiableVisibleLayers(this.spwViewer.get('spwMap').getCurrentScale())){
                			service.identify({ tolerance: 5, returnGeometry: true, spwViewer: this.spwViewer, mapPoint: mouseEvent.mapPoint }, lang.hitch(this, this.identifySuccess), lang.hitch(this, this.identifyError));
                    	}
                    }));
                },

                identifyError: function(error) {
                    console.log("Identify Error : ", error);
                    return null;
                },

                identifySuccess: function(identifyResults, mapService, formattedResults){
                    this.doOutput(formattedResults);
                },

                doOutput: function(data){
                	if(this.output){
                		var outputWidget = this.spwViewer.get('spwWidgetsManager').getWidgets({widgetId:this.output});
                		if(outputWidget && outputWidget.length > 0 && outputWidget[0].addOutput){
                			outputWidget[0].addOutput(data, {});
                		}

                	}
                },

                clearOutput: function(){
                	if(this.output){
                		var outputWidget = this.spwViewer.get('spwWidgetsManager').getWidgets({widgetId:this.output});
                		if(outputWidget && outputWidget.length > 0 && outputWidget[0].clearOutput){
                			outputWidget[0].clearOutput();
                		}

                	}
                }
//
//                buildRendering: function() {
//                    this.inherited(arguments);
//
//                    this["class"] = 'SpwIdentifyClass';
//                    style.set(this.domNode, 'width', '100%');
//                    style.set(this.domNode, 'height', '100%');
//                    style.set(this.domNode, 'padding', '0');
//                    style.set(this.domNode, 'overflow', 'hidden');
//                },
//                postCreate: function() {
//                    this.inherited(arguments);
//                    this._esriMap = this.spwViewer.get('spwMap').get('esriMap');
//                },
//                onActivate: function() {
//                    this.inherited(arguments);
//
//                    this.spwViewer.get('spwMap').setMapCursor("help");
//
//            		if (this._mapClickHandler) {
//                        this._mapClickHandler.remove();
//                    }
//                    this._mapClickHandler = this._esriMap.on("click", lang.hitch(this, this.executeIdentify));
//                },
//                onDeactivate: function() {
//                    this.inherited(arguments);
//
//                    this.spwViewer.get('spwMap').resetCursor();
//
//                    if (this._mapClickHandler) {
//                        this._mapClickHandler.remove();
//                    }
//                    this.clearDisplay();
//                },
//                resize: function() {
//                    if (this._tabContainer) {
//                        this._tabContainer.resize();
//                    }
//                },
//
//                clearDisplay: function() {
//                    this.spwViewer.get('spwMap').get('esriMap').graphics.clear();
//
//                    if(this._noServiceMessageDiv){
//                    	style.set(this._noServiceMessageDiv, "display", "none");
//                    }
//
//                    if (this._tabContainer) {
//                        this._tabContainer.destroyRecursive();
//                        this._tabContainer = null;
//                    }
//                },
//
//                executeIdentify: function(mouseEvent) {
//                	this.show();
//
//                    var mapPoint = mouseEvent.mapPoint;
//                    this._identifiedServices = this.spwViewer.get('spwMap').getMapServices({ visible: true, isBaseMap: false/*, visibleAtCurrentScale: true*/ });
//
//                    var selectedServiceId = null;
//                    if(this._tabContainer) {
//	                    selectedServiceId = this._tabContainer.get('selectedChildWidget').get('id');
//                    }
//
//                    this.clearDisplay();
//
//                    if(this._identifiedServices && this._identifiedServices.length < 1){
//                    	if(!this._noServiceMessageDiv){
//                        	this._noServiceMessageDiv = domConstruct.create("div", { innerHTML: this.labels.noIdentifiableService, style: "width:100%;text-align:center; padding-top: 20px;"}, this.domNode, "first");
//                        } else {
//                        	style.set(this._noServiceMessageDiv, "display", "block");
//                        }
//                    }
//                    else
//                    {
//                    	if (!this._tabContainer) {
//
//	                        this._tabContainer = new TabContainer({style: "width:100%;height:100%;padding:0;"});
//	                        this._tabContainer.watch("selectedChildWidget", lang.hitch(this, function(name, oval, nval) {
//	                            if (oval && oval.identifyResult) {
//	                                array.forEach(oval.identifyResult, lang.hitch(this, function(result) {
//	                                    if (result.feature && result.feature.checkedInSelection) {
//	                                        this.spwViewer.get('spwMap').removeFeature(result.feature);
//	                                    }
//	                                }));
//	                            }
//	                            if (nval && nval.identifyResult) {
//	                                array.forEach(nval.identifyResult, lang.hitch(this, function(result) {
//	                                    if (result.feature && result.feature.checkedInSelection) {
//	                                        this.spwViewer.get('spwMap').showFeature(result.feature);
//	                                    }
//	                                }));
//	                            }
//	                        }));
//	                    }
//
//	                    this._notIdentifiablePane = null;
//	                    var handlers = new Array();
//						array.forEach(this._identifiedServices, lang.hitch(this, function(service) {
//	                    	if(service.get('identifiable')){
//	                    		var servicePane = new ContentPane({
//		                        	id: service.get('serviceId'),
//		                            title: service.get('label') + "<img src='"+this.imagesPath +"ajax-loader.gif' alt='"+this.labels.loading+"' style='width:12px; height:12px;margin-left:4px;' />",
//		                            content: "<img src='"+this.imagesPath +"ajax-loader.gif' alt='"+this.labels.loading+"' />"
//		                        });
//		                        this._tabContainer.addChild(servicePane);
//
//	                    		if(service.hasIdentifiableVisibleLayers(this.spwViewer.get('spwMap').getCurrentScale())){
//	                    			var identifyCB = service.identify({ tolerance: 5, returnGeometry: true, spwViewer: this.spwViewer, mapPoint: mapPoint }, this.identifySuccess(service), this.identifyError);
//	                    			handlers.push(identifyCB);
//	                    		}
//	                    		else{
//	                    			servicePane.set('title', service.get('label'));
//	                    			servicePane.set('content', domConstruct.create("div", { innerHTML: this.labels.noLayersVisible }));
//	                    		}
//
//	                    	} else {
//	                    		if(this._notIdentifiablePane== null){
//	                    			this._notIdentifiablePane = new ContentPane({
//			                        	id: 'notIdentifiableServicePane',
//			                            title: this.labels.notIdentifiablePaneTitle,
//			                            content: domConstruct.create("div", { innerHTML: this.labels.servicesNotIdentifiable })
//			                        });
//			                        this._tabContainer.addChild(this._notIdentifiablePane);
//	                    		}
//	                    		this._notIdentifiablePane.content.innerHTML += "<br/> - "+service.get('label');
//	                    	}
//	                    }));
//
//						this._tabContainer.placeAt(this.domNode);
//	                    this._tabContainer.startup();
//
//	                    if(selectedServiceId){
//							var pane = this.getTabPaneByServiceId(selectedServiceId);
//							if(pane != null)
//	                    		this._tabContainer.selectChild(pane);
//	                    }
//                    }
//                },
//                identifySuccess: function(service) {
//                	return lang.hitch(this, function(identifyResult){
//                		var tabPane = this.getTabPaneByServiceId(service.get('serviceId'));
//	                    tabPane.set('title', service.get('label'));
//	                    if (identifyResult && identifyResult.length > 0) {
//	                        lang.mixin(tabPane, {identifyResult: identifyResult});
//	                        this.displayServiceResult(service, identifyResult);
//	                    } else {
//	                        tabPane.set('content', domConstruct.create("div", { innerHTML: this.labels.noIdentifyResult }));
//	                    }
//	                    return identifyResult;
//                	});
//                },
//                identifySuccess: function(service) {
//                	return lang.hitch(this, function(identifyResult){
//	                    if (identifyResult && identifyResult.length > 0) {
//	                        this.displayServiceResult(service, identifyResult);
//	                    } else {
//                    		//no results
//	                    }
//	                    return identifyResult;
//                	});
//                },

//                displayServiceResult: function(service, result) {
//                    var contentWrapper = domConstruct.create("div");
//
//                    var struct = this._structureLayersResults(result,service);
//                    for (key in struct) {
//                        var collapser = this._createLayerCollapser(struct[key], contentWrapper);
//
//                        array.forEach(struct[key], lang.hitch(this, function(object) {
//                        	var objectResult = this._mergeResultAndService(object, service);
//                            var objectCollapser = this._createObjectCollapser(objectResult);
//                            collapser.addContent(objectCollapser.domNode);
//
//                            objectCollapser.check(true);
//                            lang.mixin(object.feature, {checkedInSelection : true});
//                            if (this._tabContainer && this._tabContainer.selectedChildWidget &&
//                                    this._tabContainer.selectedChildWidget.get('id') == service.get('serviceId')) {
//                                this.spwViewer.get('spwMap').showFeature(object.feature);
//                            }
//                        }));
//                    }
//                    var tabPane = this.getTabPaneByServiceId(service.get('serviceId'));
//                    tabPane.set('content', contentWrapper);
//                },
//
//                _mergeResultAndService: function(object, service){
//                	for(var key in object.feature.attributes){
//                		if(service && service.ignoreAttributes && service.ignoreAttributes.indexOf && service.ignoreAttributes.indexOf(key) > -1){
//                			delete object.feature.attributes[key];
//                			continue;
//                		}
//
//                		if(object.feature.attributes[key] == "Null" || object.feature.attributes[key] == "null"){
//                			object.feature.attributes[key] = "";
//                		}
//
//						if(this.isValueHyperLink(object.feature.attributes[key])){
//							var linkLabel = object.feature.attributes[key];
//							var layer = Utils.findInList(service.get('layers'), {layerId:object.layerId});
//							if(layer && layer.length > 0 && layer[0].fields){
//								var field = Utils.findInList(layer[0].fields, {name:key});
//								if(field && field.length > 0 && field[0].hyperlinkLabel){
//									linkLabel = field[0].hyperlinkLabel;
//								}
//							}
//    	    				object.feature.attributes[key] = this._createHyperLinkForField(object, key, linkLabel);
//    					}
//                	}
//
//                	var layers = service.get('layers');
//                	if(layers && layers.length > 0){
//                		var layer = this.getLayerById(layers, object.layerId);
//                		if(layer){
//                			if(layer.displayField){
//                				if(layer.displayField.indexOf("$") > -1){
//                					object.customFieldName = Utils.mergeTemplateWithFeature(layer.displayField, object.feature);
//                				} else {
//                					object.customFieldName = layer.displayField;
//                				}
//                			}
//                			else{
//								object.displayFieldName = object.value;
//							}
//                			if(layer.fields){
//
//                				var attributesToProcess = {};
//
//                				if(layer.identifiable || (layer.identifiable == undefined && service.identifiable)){
//                					// if layer identifiable -> only suppress identifiable false field
//                					attributesToProcess = object.feature.attributes;
//
//                					array.forEach(layer.fields, lang.hitch(this, function(field){
//                						if(field.name && (attributesToProcess[field.name] || attributesToProcess[field.name] == "")){
//                							if(field.identifiable != undefined && !field.identifiable){
//	                    						delete attributesToProcess[field.name];
//	                    					}
//                						}
//                					}));
//                				}
//                				else{
//                					// if layer not identifiable -> suppress all except identifiable true field
//                					array.forEach(layer.fields, lang.hitch(this, function(field){
//                						if(field.name && (object.feature.attributes[field.name] || object.feature.attributes[field.name] == "")){
//                							if(field.identifiable != undefined && field.identifiable){
//                								attributesToProcess[field.name] = object.feature.attributes[field.name];
//	                    					}
//                						}
//                					}));
//                				}
//
//                				var attributes = {};
//                				for(var fieldName in attributesToProcess){
//                					var field = Utils.findInList(layer.fields, {name:fieldName});
//                					if(field.length > 0 && field[0].alias){
//                						attributes[field[0].alias] = attributesToProcess[fieldName];
//                					} else{
//                						attributes[fieldName] = attributesToProcess[fieldName];
//                					}
//                				}
//                				object.feature.attributes = attributes;
//                			}
//                		}
//                	}
//                	return object;
//                },
//
//                _createHyperLinkForField: function(object, fieldName, linkLabel) {
//                	if(typeof(linkLabel) == 'undefined'){
//                		linkLabel = object.feature.attributes[fieldName];
//                	}
//
//					if(typeof(object.feature.attributes[fieldName]) == "string" &&
//							object.feature.attributes[fieldName].indexOf("http:") == -1){
//						object.feature.attributes[fieldName] = "http://" + object.feature.attributes[fieldName];
//					}
//
//					return "<a href='"+object.feature.attributes[fieldName]+"' target='_blank'>"+linkLabel+"</a>";
//                },
//
//                isValueHyperLink: function(value) {
//                	if(value && value.length && value.length > 0){
//                		return value.indexOf("http://") > -1 || value.indexOf("www.") > -1 || value.indexOf("https://") > -1;
//                	}
//                	return false;
//                },
//                getLayerById: function(layers, layerId){
//                	var _layer = null;
//                	array.every(layers, function(layer){
//                		if(layer.layerId == layerId){
//                			_layer = layer;
//                			return false;
//                		}
//            			return true;
//                	});
//                	return _layer;
//                },
//
//                _createLayerCollapser: function(layerObjects, contentWrapper) {
//                    var collapser = new CollapserItem({
//                        _label: key
//                    }, domConstruct.create("div", {}, contentWrapper));
//                    style.set(collapser.itemTitleImageBlock, "background", 'none');
//                    style.set(collapser.itemTitleImageBlock, "font-weight", 'bold');
//                    style.set(collapser.itemTitleImageBlock, "background-color", 'rgb(230,237,250)');
//
//                    return collapser;
//                },
//                _createObjectCollapser: function(object) {
//                    var objectCollapser = new CollapserItem({
//                        _label: (object.customFieldName ? object.customFieldName : (object.feature.attributes[object.displayFieldName] ? object.feature.attributes[object.displayFieldName] : object.value)),
//                        _labelCheckHandler: lang.hitch(this, function(checked) {
//                            lang.mixin(object.feature, {checkedInSelection : checked});
//                            if (checked) {
//                                this.spwViewer.get('spwMap').showFeature(object.feature);
//                            }
//                            else {
//                                this.spwViewer.get('spwMap').removeFeature(object.feature);
//                            }
//                        })
//                    }, domConstruct.create("div", {}));
//
//                    style.set(objectCollapser.itemTitleImageBlock, "background", 'none');
//                    style.set(objectCollapser.itemTitleImageBlock, "position", 'relative');
//                    style.set(objectCollapser.itemTitleImageBlock, "background-color", 'rgb(230,237,250)');
//                    objectCollapser.itemTitleImageBlock.appendChild(this.createZoomIcon(object));
//
//                    on(objectCollapser.itemWrapper, mouse.enter, lang.hitch(this, function() {
//                        if (objectCollapser.isChecked()) {
//                            this.spwViewer.get('spwMap').highlightFeature(object.feature);
//                        }
//                    }));
//                    on(objectCollapser.itemWrapper, mouse.leave, lang.hitch(this, function() {
//                        if (objectCollapser.isChecked()) {
//                            this.spwViewer.get('spwMap').unhighlightFeature(object.feature);
//                        }
//                    }));
//
//                    var tableAttr = domConstruct.create("table", {style: "margin-left:12px;word-break: break-word;"});
//                    for (attr in object.feature.attributes) {
//                        var row = domConstruct.create("tr", {}, tableAttr);
//                        domConstruct.create("td", {innerHTML: attr, style: "font-weight:bold;"}, row);
//                        domConstruct.create("td", {innerHTML: object.feature.attributes[attr] }, row);
//                    }
//                    objectCollapser.addContent(tableAttr);
//                    return objectCollapser;
//                },
//                createZoomIcon: function(object) {
//                    var zoomIcon = domConstruct.create("img", {src: this.imagesPath + "loupe.png", style: "position: absolute; top: 2px; right: 5px; height:16px;cursor:pointer;"});
//                    on(zoomIcon, "click", lang.hitch(this, function() {
//                        this.spwViewer.get('spwMap').zoomToFeature(object.feature);
//                    }));
//                    return zoomIcon;
//                },
//                getTabPaneByServiceId: function(serviceId) {
//                    var pane = null;
//                    var tabPanes = this._tabContainer.getChildren();
//                    array.every(tabPanes, lang.hitch(this, function(tabPane) {
//                        if (tabPane.get('id') == serviceId) {
//                            pane = tabPane;
//                            return false;
//                        }
//                        return true;
//                    }));
//                    return pane;
//                },
//                _structureLayersResults: function(result,service) {
//                	var structure = {};
//                    var layers = service.get('layers');
//                    array.forEach(result, function(object) {
//
//                    	var _layer = null;
//                    	array.every(layers, lang.hitch(this,function(layer){
//							if(layer.layerId == object.layerId){
//								_layer = layer;
//								return false;
//							}
//							return true;
//                    	}));
//
//                		if(_layer && _layer.displayLabel)
//                		{
//                			layerName = _layer.displayLabel;
//                		}
//                		else if(_layer && _layer.name)
//                		{
//                			layerName = _layer.name;
//                		}
//                		else
//                		{
//                			layerName = object.layerName;
//                		}
//
//                        if (structure[layerName]) {
//                            structure[layerName].push(object);
//                        } else {
//                            structure[layerName] = [object];
//                        }
//                    });
//                    return structure;
//                }

            });
        });