Source: widgets/SpwGeolocalisationBatch.js

Retour à la documentation
/**
 * @class spw.widgets.SpwGeolocalisationBatch
 */
define(["dojo/_base/declare","spw/api/SpwBaseTemplatedWidget", "dojo/text!./templates/SpwGeolocalisationBatch.html",
        "dojo/_base/lang", "dojo/dom-construct", "dojo/_base/array", "dojo/dom", "dojo/query", "spw/api/MessageManager", 
        "dojo/on", "dojo/_base/window", "dojo/html", "dojo/aspect", "dojo/topic","dojo/request", "dojo/_base/Color",
        "spw/api/Utils","esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol"],
		function(declare, SpwBaseTemplatedWidget, template, 
				lang, domConstruct,	array, dom, query, MessageManager, 
				on, win, html, aspect, topic, request, Color,
				Utils, SimpleMarkerSymbol, SimpleLineSymbol){
	
	var SpwGeolocalisationBatch = null;
	SpwGeolocalisationBatch =  declare("spw.widgets.SpwGeolocalisationBatch", [SpwBaseTemplatedWidget],/** @lends spw.widgets.SpwGeolocalisationBatch.prototype */{
		
		templateString: template,

		/* START from config */
		
		geolocBaseUrl: null,
		
		/* END from config */
		
		useLocale: true,
		_config: null,
		_symbole: null,
		_graphics: new Array(),
		
		_viewBatchHandler: null,
		
//		SPW_GEOLOCALISATION_BATCH_WIDGET_LOAD_BATCH : "SPW_GEOLOCALISATION_BATCH_WIDGET:LOAD_BATCH",
//		SPW_GEOLOCALISATION_BATCH_WIDGET_BATCH_LOADED : "SPW_GEOLOCALISATION_BATCH_WIDGET:BATCH_LOADED",
		
		/** 
	     * @constructs
	     */ 
		constructor: function(config){
    		this.iconClass = "SpwGeolocalisationBatchWidgetIcon";
    		
    		this.height = 380; //this._config.height || 480;
    		this.width = 745;  //this._config.width || 745;
    		
			this._symbole = new SimpleMarkerSymbol(
					SimpleMarkerSymbol.STYLE_SQUARE,
					10,
					new SimpleLineSymbol(
							SimpleLineSymbol.STYLE_SOLID,
							new Color([255,0,0]),
							1),
							new Color([0,255,0,0.5]));
			
			topic.subscribe('BATCH_LOADED', lang.hitch(this, function(batchToken){
					this.onLoadBatch(batchToken);
				}
			));
			
			topic.subscribe('ZOOM', lang.hitch(this, function(x, y){
					this.onZoom(x, y);
				}
			));

			topic.subscribe('MOVE_POINT', lang.hitch(this, function(id, x, y){
					this.movePoint(id, x, y);
				}
			));
    	},
    	
    	onActivate: function(){
    		this.inherited(arguments);
    		
//    		this._viewBatchHandler = aspect.after(this.spwViewer.get("spwMap").get("esriMap").graphics,"onClick", lang.hitch(this, function(evt){
//    			if(evt.graphic) {
//    				this.spwViewer.get("spwMap").get("esriMap").infoWindow.show(evt.mapPoint);
//    			}
//    		}));
    		
//    		this._viewBatchHandler = on(this.spwViewer.get("spwMap").get("esriMap").graphics, 'onClick', lang.hitch(this, function(evt){
//    			
//    			console.log('view batch clicked');
//    			
//    			if(evt.graphic) {
//    				this.spwViewer.get("spwMap").get("esriMap").infoWindow.show(evt.mapPoint);
//    			}
//    		}));
    	},
    	
    	onDeactivate: function(){
    		this.inherited(arguments);
//    		this._viewBatchHandler.remove();
    	},
    	
    	postCreate: function() {
    		this.inherited(arguments);
    		
    		var iframe = domConstruct.create("iframe", {style: {height:'350px',width:'740px', overflow:'hidden'}, frameborder:0, marginwidth:0, marginheight:0}, this._SpwGeolocalisationBatchContentDiv);
    				
    		var batchToken = Utils.gup("batchToken");
    		
    		if(batchToken) {
    			this.onLoadBatch(batchToken);
    			iframe.src = this.geolocBaseUrl + "batch/view/" + batchToken;
    		} else {
    			iframe.src = this.geolocBaseUrl + "batch/";
    		}
    		
    		this._viewBatchHandler = on(window, "message", function(event) {
    			var cmd = event.data.split('#');
    			topic.publish(cmd[0], cmd[1], cmd[2], cmd[3]);
    	    });
    	},
    	
    	onLoadBatch: function(batchToken) {
    		this.spwViewer.get('spwMap').get('esriMap').graphics.clear();
    		this._graphics = new Array();
    		
    		request.get(this.geolocBaseUrl + "rest/batch/getLot/" + batchToken, {
	    		    handleAs: "json",
	    		    headers:{ 'Content-Type': '' }
	    		  }).then(lang.hitch(this, function(result){
	    		     this.onBatchLoaded(result);
	    		  }), function(e){
	    			  MessageManager.getInstance().notifyError(this.labels.dataLoadError);
    		  });
    	},
    	
    	onZoom : function(x, y){
    		this.spwViewer.get('spwMap').zoomToPoint(parseFloat(x), parseFloat(y), 150);
    	},
    	
    	onBatchLoaded: function(result) {
    		
			if(result.errorCode == 0) {
				var xmin = null;
				var xmax = null;
				var ymin = null;
				var ymax = null;
				
				var content = "<table>";
				
				content += "<tr><td>Adresse:</td><td>${_ADRESSE}</td></tr>";
				content += "<tr><td>Score:</td><td>${_SCORE}</td></tr>";
				content += "<tr><td>Message:</td><td>${_MESSAGE}</td></tr>";
				
//				for (var i=0;i<result.fields.length;i++) {
//					content += "<tr><td>"+result.fields[i]+":</td><td>${"+result.fields[i]+"}</td></tr>";
//				}
				
				array.forEach(result.fields, lang.hitch(this, function(field){
					content += "<tr><td>"+ field + ":</td><td>${" + field + "}</td></tr>";
				}));
				
				content += "</table>";
				
				this._infoTemplate = new esri.InfoTemplate(result.label, content);
	
				array.forEach(result.adresses, lang.hitch(this, function(adress){
					var x = adress.x;
					var y = adress.y;
					if(x && y) {
						xmin = (xmin == null) ? x : Math.min(xmin,x);
						xmax = (xmax == null) ? x : Math.max(xmax,x);
						ymin = (ymin == null) ? y : Math.min(ymin,y);
						ymax = (ymax == null) ? y : Math.max(ymax,y);
						
						var props = adress.props;
						props._ADRESSE = adress.adresse;
						props._SCORE = adress.score;
						props._MESSAGE = adress.message;
						
						var graphic = new esri.Graphic(
								new esri.geometry.Point(x, y),
								this._symbole,
								props,
								this._infoTemplate);
						graphic.id = adress.id;
						this._graphics.push(graphic);
						this.spwViewer.get("spwMap").get("esriMap").graphics.add(graphic);
					}
				}));
				
				if(xmin != null && xmax != null && ymin != null && ymax != null) {
					this.spwViewer.get("spwMap").zoomToBbox(xmin, xmax, ymin, ymax);
				}
			}
			else {
				MessageManager.getInstance().notifyError(result.errorMsg);
			}
		},
    	
    	movePoint: function(id, x, y) {
    		console.log('Moving point..');
    		for(var i=0;i<this._graphics.length;i++) {
    			if(this._graphics[i].id == id) {
    				this._graphics[i].geometry.x = x;
    				this._graphics[i].geometry.y = y;
    			}
    		}
    	}    	
	});

	return SpwGeolocalisationBatch;
});