Source: widgets/SpwMousePosition.js

Retour à la documentation
/**
 * @class spw.widgets.SpwMousePosition
 */
define(["dojo/_base/declare", "spw/api/SpwBaseTemplatedWidget",
        "dojo/i18n!./nls/SpwMousePosition","dojo/text!./templates/SpwMousePosition.html",
        "spw/api/SpwMap","spw/api/ProjectionManager",
        "dojo/_base/lang","dojo/on","dojo/store/Memory",
        "dojo/keys", "dojo/number","dojo/dom-construct",

        "dijit/form/ComboBox"],

        function(declare, SpwBaseTemplatedWidget,
        		labels, template,
        		SpwMap,ProjectionManager,
				lang, on, Memory,
				keys, number, domConstruct){

	var SpwMousePosition = null;
	SpwMousePosition = declare("spw.widgets.SpwMousePosition", [SpwBaseTemplatedWidget],{
		templateString: template,

		labels: labels,

		spwMap: null,

        "widgetId": "idMousePosition",
        "activated": true,
        "hideOnPrint": true,
        "inToolbar": false,
        "solitary": false,
        "position": "map",
        "mapPosition": {
            "bottom": "10px",
            "left": "300px"
        },
        "projectionsAvailable": [
            {
                "label": "Lambert Belge 72",
                "SRID": 31370,
                "unit": "m",
                "precision": 0
            },
            {
                "label": "Lambert Belge 2008",
                "SRID": 3812,
                "unit": "m",
                "precision": 0
            },
            {
                "label": "WGS84 (DD)",
                "SRID": 4326,
                "unit": "",
                "precision": 5
            },
            {
                "label": "WGS84 (DMS)",
                "SRID": 4326,
                "dms": true,
                "unit": "",
                "precision": 5
            }
        ],

		constructor: function(config) {
	 	},

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

			this.spwMap = this.spwViewer.get("spwMap");

			this.own(
				this._mapMouseMoveHandler = this.spwMap.on(SpwMap.events.MapMouseMove, lang.hitch(this, this._mapMouseMove))
			);
	 	},

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

			this.mouseXCoordinate.on('focus', lang.hitch(this, function(){
				this.spwMap.get('esriMap').disableKeyboardNavigation();
				this._mapMouseMoveHandler.remove();
			}));
			this.mouseYCoordinate.on('focus', lang.hitch(this, function(){
				this.spwMap.get('esriMap').disableKeyboardNavigation();
				this._mapMouseMoveHandler.remove();
			}));
			this.mouseXCoordinate.on('blur', lang.hitch(this, function(){
				this.spwMap.get('esriMap').enableKeyboardNavigation();
				this._mapMouseMoveHandler = this.spwMap.on(SpwMap.events.MapMouseMove, lang.hitch(this, this._mapMouseMove));
			}));
			this.mouseYCoordinate.on('blur', lang.hitch(this, function(){
				this.spwMap.get('esriMap').enableKeyboardNavigation();
				this._mapMouseMoveHandler = this.spwMap.on(SpwMap.events.MapMouseMove, lang.hitch(this, this._mapMouseMove));
			}));
			this.mouseXCoordinate.watch('value', lang.hitch(this, function(propName, oval, nval){
				var span = domConstruct.create("span", {innerHTML: nval, style: "opacity:0" }, document.body);
				this.mouseXCoordinate.domNode.style.width = span.getBoundingClientRect().width + 6 + "px";
				domConstruct.destroy(span);
			}));
			this.mouseYCoordinate.watch('value', lang.hitch(this, function(propName, oval, nval){
				var span = domConstruct.create("span", {innerHTML: nval, style: "opacity:0" }, document.body);
				this.mouseYCoordinate.domNode.style.width = span.getBoundingClientRect().width + 6 + "px";
				domConstruct.destroy(span);
			}));

			this.mouseXCoordinate.set("value",0);
			this.mouseYCoordinate.set("value",0);

			this.own(
				this.mouseXCoordinate.on("keydown", lang.hitch(this, this._coordinateChangedByUser)),
				this.mouseYCoordinate.on("keydown", lang.hitch(this, this._coordinateChangedByUser))
			);

			if(this.projectionsAvailable === null || !this.projectionsAvailable.length){
				var viewerSRID = this.spwViewer.get('spatialReference').wkid;

				if (ProjectionManager.definitions[viewerSRID]) {
					this.projectionsAvailable = [{
						id: 0,
						label: 'EPSG:' + viewerSRID,
						precision: 0,
						unit: viewerSRID == 31370 ? "m" : "",
						SRID: viewerSRID
					}];
				}
				else {
                    this.projectionsAvailable = [{
                        id: 0,
                        label: this.labels.defaultProjectionLabel,
                        precision: 0,
                        SRID: "31370",
                        definition: "+proj=lcc +lat_1=51.16666723333333 +lat_2=49.8333339 +lat_0=90 +lon_0=4.367486666666666 +x_0=150000.013 +y_0=5400088.438 +ellps=intl +towgs84=106.869,-52.2978,103.724,-0.33657,0.456955,-1.84218,1 +units=m +no_defs"
                    }];
				}
			}
			else{
				for(var i=0;i<this.projectionsAvailable.length;i++){
					this.projectionsAvailable[i].id = i;
				}
			}

			var projectionsStore = new Memory({idProperty:"id",data: this.projectionsAvailable});
			this.projectionsAvailableComboBox.set("store",projectionsStore);
			this.projectionsAvailableComboBox.set("item",this.projectionsAvailable[0]);
		},

		_mapMouseMove: function(event){
			if(this.projectionsAvailableComboBox.get("item") !== null){

				var currentSpatialReferenceSRID = this.spwMap.getSpatialReferenceSRID();
				var mapPoint = event.mapPoint;

				if(typeof(this.projectionsAvailableComboBox.get("item").SRID) !== "undefined" && currentSpatialReferenceSRID === this.projectionsAvailableComboBox.get("item").SRID){
					var mult = Math.pow(10, this.projectionsAvailableComboBox.get("item").precision);
					this.mouseXCoordinate.set("value",Math.round(parseFloat(mapPoint.x)*mult)/mult + " " + this.projectionsAvailableComboBox.get("item").unit);
					this.mouseYCoordinate.set("value", Math.round(parseFloat(mapPoint.y)*mult)/mult + " " + this.projectionsAvailableComboBox.get("item").unit);
				}
				else{
					var pointProjected = null;
					if(typeof(this.projectionsAvailableComboBox.get("item").definition) !== "undefined"){
						pointProjected = ProjectionManager.getInstance().projectPoint(currentSpatialReferenceSRID,this.projectionsAvailableComboBox.get("item").definition,mapPoint.x,mapPoint.y);
					}
					else{
						if(typeof(this.projectionsAvailableComboBox.get("item").SRID) !== "undefined"){
							pointProjected = ProjectionManager.getInstance().projectPoint(currentSpatialReferenceSRID,this.projectionsAvailableComboBox.get("item").SRID,mapPoint.x,mapPoint.y);
						}
					}

					if(pointProjected !== null){
						var mult = Math.pow(10, this.projectionsAvailableComboBox.get("item").precision);
						if(this.projectionsAvailableComboBox.get("item").dms){
							var lon = parseFloat(pointProjected.x), lat = parseFloat(pointProjected.y), signlat=lat<0 ? -1:1, signlon=lon<0 ? -1:1, latAbs = Math.abs(Math.round(lat * 1000000.)), lonAbs = Math.abs( Math.round(lon * 1000000.));

							var latValue = ((Math.floor(latAbs / 1000000) * signlat) + '° ' + number.format(Math.floor(  ((latAbs/1000000) - Math.floor(latAbs/1000000)) * 60), {pattern: "00"})  + '\' ' +  number.format(( Math.floor(((((latAbs/1000000) - Math.floor(latAbs/1000000)) * 60) - Math.floor(((latAbs/1000000) - Math.floor(latAbs/1000000)) * 60)) * 100000) *60/100000 ), {pattern: "00.000"}) + '"'  );
							var lonValue = ((Math.floor(lonAbs / 1000000) * signlon) + '° ' + number.format(Math.floor(  ((lonAbs/1000000) - Math.floor(lonAbs/1000000)) * 60), {pattern: "00"})  + '\' ' +  number.format(( Math.floor(((((lonAbs/1000000) - Math.floor(lonAbs/1000000)) * 60) - Math.floor(((lonAbs/1000000) - Math.floor(lonAbs/1000000)) * 60)) * 100000) *60/100000 ), {pattern: "00.000"}) + '"'  );

							this.setLabel(true);

							this.mouseXCoordinate.set("value", lonValue + " " + this.projectionsAvailableComboBox.get("item").unit);
							this.mouseYCoordinate.set("value", latValue + " " + this.projectionsAvailableComboBox.get("item").unit);
						} else if (this.projectionsAvailableComboBox.get("item").unit === '' || this.projectionsAvailableComboBox.get("item").unit == null || this.projectionsAvailableComboBox.get("item").unit === '°') {
							this.setLabel(true);

							this.mouseXCoordinate.set("value", number.format(Math.round(parseFloat(pointProjected.x)*mult)/mult, {places: this.projectionsAvailableComboBox.get("item").precision}) + "° " + this.projectionsAvailableComboBox.get("item").unit);
							this.mouseYCoordinate.set("value", number.format(Math.round(parseFloat(pointProjected.y)*mult)/mult, {places: this.projectionsAvailableComboBox.get("item").precision}) + "° " + this.projectionsAvailableComboBox.get("item").unit);
						} else {
							this.setLabel();

							this.mouseXCoordinate.set("value", Math.round(parseFloat(pointProjected.x)*mult)/mult + " " + this.projectionsAvailableComboBox.get("item").unit);
							this.mouseYCoordinate.set("value", Math.round(parseFloat(pointProjected.y)*mult)/mult + " " + this.projectionsAvailableComboBox.get("item").unit);
						}
					}
					else {
						this.setLabel();
					}
				}
			}
		},

		setLabel: function(longLat) {
			if (longLat) {
				this.xLabelNode.innerHTML = ' Long. = ';
				this.yLabelNode.innerHTML = ' Lat. = ';
			}
			else {
				this.xLabelNode.innerHTML = ' X = ';
				this.yLabelNode.innerHTML = ' Y = ';
			}
		},

		_coordinateChangedByUser: function(event){
			var x = parseFloat(this.mouseXCoordinate.get("value"));
			var y = parseFloat(this.mouseYCoordinate.get("value"));

			if(event.keyCode === keys.ENTER && !isNaN(x) && !isNaN(parseFloat(y))){
				if(this.projectionsAvailableComboBox.get("item") !== null){
					var currentSpatialReferenceSRID = this.spwMap.getSpatialReferenceSRID();

					if(typeof(this.projectionsAvailableComboBox.get("item").SRID) !== "undefined" && currentSpatialReferenceSRID === this.projectionsAvailableComboBox.get("item").SRID){
						this.spwMap.centerAt(x,y);
					}
					else{
						var pointProjected = null;
						if(typeof(this.projectionsAvailableComboBox.get("item").definition) !== "undefined"){
							pointProjected = ProjectionManager.getInstance().projectPoint(this.projectionsAvailableComboBox.get("item").definition,currentSpatialReferenceSRID,x,y);
						}
						else{
							if(typeof(this.projectionsAvailableComboBox.get("item").SRID) !== "undefined"){
								pointProjected = ProjectionManager.getInstance().projectPoint(this.projectionsAvailableComboBox.get("item").SRID,currentSpatialReferenceSRID,x,y);
							}
						}

						if(pointProjected !== null){
							this.spwMap.centerAt(pointProjected.x,pointProjected.y);
						}
					}
				}
			}
		}
	});

	return SpwMousePosition;
});