Source: widgets/SpwIcarSearch.js

Retour à la documentation
/**
 * @class spw.widgets.SpwIcarSearch
 */
define([ "dojo/_base/declare", "spw/api/SpwBaseTemplatedWidget",
				"dojo/text!./templates/SpwIcarSearch.html",
				"dojo/_base/lang", "dojo/store/Memory",
				"dojo/data/ObjectStore", "spw/api/Utils",
				"spw/api/MessageManager", "dojo/_base/array",
				"dojo/i18n!./nls/SpwIcarSearch", "dojo/on", "dojo/query",
				"esri/request", "spw/api/GeometryConverter",
				"spw/api/ProjectionManager",

				"dijit/form/FilteringSelect", "dijit/form/Form",
				"dijit/form/TextBox", "dijit/form/Button"
		],
	function(declare, SpwBaseTemplatedWidget, template, lang, Memory,
			ObjectStore, Utils, MessageManager, array, labels, on, query,
			request, GeometryConverter, ProjectionManager) {

		return declare("spw.widgets.SpwIcarSearch", [ SpwBaseTemplatedWidget ],
				/** @lends spw.widgets.SpwIcarSearch.prototype */
				{

					templateString : template,
					labels : labels,

					localiteStore : null,
					rueOfLocaliteStore : null,

					selectLocalite : null,
					selectRue : null,
					numeroTextbox : null,
					goButton : null,

					apiLoaded : false,

					/* START widget config */
					useCors : true,
					closeOnZoom : false,

					hasCloseButton : true,
					widgetTitle : "Localiser une adresse",
					position : "none",
					style : {
						width : "400px"
					},
					/* END widget config */

					apiSRID : 31370,
					geocodeApiUrl : "//geoservices.wallonie.be/geocodeWS",

					postCreate : function() {
						this.inherited(arguments);

						this.selectLocalite.watch('item', function(prop, o, n){
							this.selectRue.set('disabled', this.selectLocalite.get('item') == null);
							if(!this.selectLocalite.get('item') || o != n) {
								this.selectRue.set('displayedValue', '');
							}
						}.bind(this));
						this.selectLocalite.watch('displayedValue', function(){
							if(!this.selectLocalite.get('displayedValue')){
								this.selectRue.set('disabled', true);
								this.selectRue.set('displayedValue', '');
								this.selectRue.set('item', null);
								this.selectRue.set('store', null);
							}
						}.bind(this));


						this.selectRue.watch('item', function(){
							this.selectNumero.set('disabled', this.selectRue.get('item') == null);
							if(!this.selectRue.get('item')){
								this.selectNumero.set('displayedValue', '');
							}

						}.bind(this));
						this.selectRue.watch('displayedValue', function(){
							if(!this.selectRue.get('displayedValue')){
								this.selectNumero.set('disabled', true);
								this.selectNumero.set('displayedValue', '');
								this.selectNumero.set('item', null);
								this.selectNumero.set('store', null);
							}
						}.bind(this));
						
						on(this.selectLocalite._buttonNode, 'click', function(){
							if(!this.selectLocalite.get('store') || !this.localiteStore)
								this.communeKeyup();
						}.bind(this));
						on(this.selectRue._buttonNode, 'click', function(){
							if(!this.selectRue.get('store') || !this.rueStore)
								this.streetKeyup();
						}.bind(this));
						on(this.selectNumero._buttonNode, 'click', function(){
							if(!this.selectNumero.get('store') || !this.numeroStore)
								this.numeroKeyup();
						}.bind(this));

						this.own(on(this._formNode, 'submit', lang.hitch(
								this, this.goButtonClicked)), on(
								this._formNode, 'reset', lang.hitch(this,
										this.onReset)));
					},
					
					geolist: function(content){
						//not working with google : content.crs = "EPSG:"+this.spwViewer.get('spwMap').get('esriMap').spatialReference.wkid;
						esriConfig.defaults.io.corsEnabledServers.push("localhost:8085");
						return request({
							url : this.geocodeApiUrl + "/geolist",
							useProxy : false,
							method : "GET",
							handleAs : 'json',
							content : content
						});
					},

					communeKeyup : function() {
						this.geolist({ 
							city : this.selectLocalite.get('displayedValue') ? this.selectLocalite.get('displayedValue') : '*'
						}).then(function(response) {
							this.createLocaliteStore(response);
						}.bind(this));
					},
					
					streetKeyup: function() { 
						this.geolist({
							city : this.selectLocalite.get('item').ident,
							street: this.selectRue.get('displayedValue') ? this.selectRue.get('displayedValue') : '*'
						}).then(function(response) {
							this.createRueStore(response);
						}.bind(this));
					},
					
					numeroKeyup: function() { 
						this.geolist({
							street: this.selectRue.get('item').ident,
							house: this.selectNumero.get('displayedValue') ? this.selectNumero.get('displayedValue') : '*'
						}).then(function(response) {
							this.createNumeroStore(response);
						}.bind(this));
					},

					onReset : function(e) {
						this.selectLocalite.reset();
						this.selectRue.reset();
						this.selectNumero.reset();
					},

					onActivate : function() {
						this.inherited(arguments);

						if (this.selectLocalite.item == null) {
							this.selectLocalite.reset();
						} else if (this.selectRue.item == null) {
							this.selectRue.reset();
						}
					},

					createLocaliteStore : function(data) {
						var localites = array.map(data.candidates, function(c) {
							return { ident: c.city.ident, displayLabel : c.city.name, searchLabel: c.city.name };
						});
						
						this.localiteStore = new ObjectStore({ objectStore : new Memory({ data : localites }) });
						this.selectLocalite.set('store', this.localiteStore);
						this.selectLocalite.loadAndOpenDropDown();
					},
					
					createRueStore: function(data){
						var rues = array.map(data.candidates, function(c) {
							return { ident: c.street.ident, displayLabel : c.street.name, searchLabel: c.street.name };
						});
						this.rueOfLocaliteStore = new ObjectStore({ objectStore : new Memory({ data : rues }) });
						this.selectRue.set('store', this.rueOfLocaliteStore);
						this.selectRue.loadAndOpenDropDown();
					},
					
					createNumeroStore: function(data) {
						var numeros = array.map(data.candidates, function(c) {
							return { ident: c.house.ident, displayLabel : c.house.name, searchLabel: c.house.name };
						});
						this.numeroStore = new ObjectStore({ objectStore : new Memory({ data : numeros }) });
						this.selectNumero.set('store', this.numeroStore);
						this.selectNumero.loadAndOpenDropDown();
					},

					onListLocaliteError : function(request, status, error) {
						MessageManager.getInstance().notifyError(
								this.labels.geolocalisationError + error);
					},

					goButtonClicked : function() {
						if(this.currentFeature) {
							this.spwViewer.spwMap.removeFeature(this.currentFeature);
							this.currentFeature = null;
						}
						var content = { geom: 'true' };
						if(this.selectNumero.get('item')){
							content.house = this.selectNumero.get('item').ident;
						} else if (this.selectRue.get('item')){
							content.street = this.selectRue.get('item').ident;
						} else if (this.selectLocalite.get('item')){
							content.city = this.selectLocalite.get('item').ident;
						} else {
							return false;
						}
						this.geolist(content).then(this.geolistGeomSuccess.bind(this));
						
						return false;
					},
					
					geolistGeomSuccess: function(response, fromGeotag){
						if(response && response.candidates && response.candidates.length > 0){
							var g = (response.candidates[0].house || response.candidates[0].street || response.candidates[0].city).geometry;
							if(g){
								
								g = GeometryConverter.geoJSONToEsri(g);
								
								if(g.spatialReference.wkid != this.spwViewer.get('spwMap').get('esriMap').spatialReference.wkid){
									g = ProjectionManager.getInstance().transform(31370, this.spwViewer.get('spwMap').get('esriMap').spatialReference.wkid, g);
								}
								
								this.currentFeature = this.spwViewer.get('spwMap').showGeometry(g);
								this.spwViewer.get('spwMap').zoomToFeature(this.currentFeature);
								
								if(fromGeotag){
									var msg = "ne possède pas de représentation cartographique. ";
									msg = fromGeotag === "house" ? "L'adresse " + msg : "La rue " + msg;
									msg += response.geotag === "street" ? "La rue a été mise en évidence." : "La commune a été mise en évidence";
									MessageManager.getInstance().notifyInfo(msg, null, 5000);
								}
                                this.closeOnZoom && this.closeWidget();
							} else {
								if(response.geotag === "house") {
									var content = { geom: true, street: response.candidates[0].street.ident };
									this.geolist(content).then(function(r){ this.geolistGeomSuccess(r, response.geotag); }.bind(this));
								} else if(response.geotag === "street"){
									var content = { geom: true, city: response.candidates[0].city.ident };
									this.geolist(content).then(function(r){ this.geolistGeomSuccess(r, fromGeotag || response.geotag); }.bind(this));
								} else if(response.geotag === "city"){
									MessageManager.getInstance().notifyInfo('La commune ne possède pas de représentation cartographique. La mise en évidence est impossible.', null, 5000);
								}
							}
						}
					},

					closeWidget : function() {
						var p = this.getParent();

						if (p && p.spwParentMenu
								&& (p = p.spwParentMenu.getParent())) {
							if (p.spwParentMenu
									&& (p = p.spwParentMenu.getParent())) {
								p._button.closeDropDown();
							}
						}
					},
					
					onDeactivate: function() {
						this.inherited(arguments);
						
						if(this.currentFeature) {
							this.spwViewer.spwMap.removeFeature(this.currentFeature);
							this.currentFeature = null;
						}

					}
				});
		});