Source: widgets/SpwNavigation.js

Retour à la documentation
/**
 * @class spw.widgets.SpwNavigation
 */
define(["dojo/_base/declare", "dojo/_base/array", "spw/api/SpwBaseTemplatedWidget",
        "dojo/i18n!./nls/SpwNavigation","dojo/text!./templates/SpwNavigation.html",
        "spw/api/SpwMap","dijit/Tooltip",
        "dojo/_base/lang","dojo/on",
        "dijit/form/VerticalSlider",

        "dijit/form/ToggleButton","dijit/form/Button"],

        function(declare, array, SpwBaseTemplatedWidget,
        		labels, template,
        		SpwMap,Tooltip,
				lang, on,
				VerticalSlider){

	var SpwNavigation = null;

	SpwNavigation = declare("spw.widgets.SpwNavigation", [SpwBaseTemplatedWidget],{
		templateString: template,

		labels: labels,

		spwMap: null,

		zoomSliderType: null,

		_zoomSlider: null,

		_tooltipLabel : null,

		zoomFullLabel: 'Afficher toute la Wallonie',

        "widgetId": "idNavigation",
        "activated": true,
        "hideOnPrint": true,
        "position": "map",
        "tooltipDuration": 2500,
        "tooltipShowDelay": 300,
        "mapPosition": {
            "bottom": "10px",
            "left": "10px"
        },
        "scaleLabels": [
            {
                "maxScale": 500,
                "label": "Maison"
            },
            {
                "minScale": 501,
                "maxScale": 2500,
                "label": "Rue"
            },
            {
                "minScale": 2501,
                "maxScale": 15000,
                "label": "Quartier"
            },
            {
                "minScale": 15001,
                "maxScale": 50000,
                "label": "Commune"
            },
            {
                "minScale": 50001,
                "maxScale": 100000,
                "label": "Arrondissement"
            },
            {
                "minScale": 100001,
                "maxScale": 500000,
                "label": "Province"
            },
            {
                "minScale": 500001,
                "maxScale": 1500000,
                "label": "Région"
            },
            {
                "minScale": 1500001,
                "label": "Pays"
            }
        ],

		constructor: function(config) {
	 	},

	 	postMixInProperties: function(){
			this.inherited(arguments);
			this.spwMap = this.spwViewer.get("spwMap");

			this.tooltipDuration = typeof(this.tooltipDuration) !== "undefined" ? this.tooltipDuration : 3000;
			this.tooltipShowDelay = typeof(this.tooltipShowDelay) !== "undefined" ? this.tooltipShowDelay : 300;

			this.own(
				this.spwMap.on(SpwMap.events.MapZoomStart, lang.hitch(this, this._zoomStart)),
				this.spwMap.on(SpwMap.events.MapZoomEnd, lang.hitch(this, this._zoomEnd))
			);
	 	},

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

			new Tooltip({
				connectId: this.zoomFullButton.domNode,
				position: ['after', 'above'],
				label: this.zoomFullLabel
			});

            this.createSlider(this.spwViewer.get('spwMap').get('esriMap'));

            this.own(
                on(this.spwViewer.get('spwMap'), this.spwViewer.get('spwMap').events.MapCreated, lang.hitch(this, this.createSlider))
            );
		},

        checkBasemaps: function() {
            this._curService = null;

            array.some(this.spwViewer.get('spwMap').getMapServices({isBaseMap: true}), lang.hitch(this, function(ms) {
                if (ms.scaleLabels) {
                    this._curService = ms;
                    return true;
                }

                return false;
            }));
        },

        createSlider: function(map) {
            if (!map.loaded) {
                on.once(this.spwViewer.get('spwMap'), this.spwViewer.get('spwMap').events.MapLoaded, lang.hitch(this, this.createSlider, map));
                return;
            }

            this.checkBasemaps();

            if (this._zoomSlider) {
                this._zoomSlider.destroy();
                this.navigationSlider.destroy();
            }

            this._zoomSlider = new VerticalSlider({
                value: map.getZoom(),
                minimum: map.getMinZoom(),
                maximum: map.getMaxZoom(),
                discreteValues: map.getMaxZoom() - map.getMinZoom() + 1,
                intermediateChanges: false,
                style: "height:200px;",
                onChange: lang.hitch(this, this.onSliderChange)
            });

            this._zoomSlider.placeAt(this.verticalSliderDiv);

            this._zoomSlider.actionType = null;

            this._tooltipLabel = this.getCurLabel();
            this.navigationSlider = new Tooltip({
                connectId: [this._zoomSlider.sliderHandle],
                label: '<span class="spwNavigationV3 tooltipNavigation">' + this._tooltipLabel + '</span>',
                showDelay: this.tooltipShowDelay
            });
        },

        tooltipOpenTimeOut: null,
        tooltipCloseTimeOut: null,
        navigationSlider: null,

        onSliderChange: function(value) {
            this._tooltipLabel = value;

            if(this._zoomSlider.actionType === null){
                this._zoomSlider.actionType = "zoomWithSlider";
                this.spwMap.setZoom(value);
            }
            else {
                this._zoomSlider.actionType = null;
            }
        },

        showTooltip: function() {
            this._tooltipLabel = this.getCurLabel();

            this.navigationSlider.close();

            if (!this._tooltipLabel) { // vide ou null
                return;
            }

            this.navigationSlider.set("label", '<span class="spwNavigationV3 tooltipNavigation">' + this._tooltipLabel + '</span>');

            if(this.tooltipOpenTimeOut !== null){
                clearTimeout(this.tooltipOpenTimeOut);
            }

            this.tooltipOpenTimeOut = setTimeout(lang.hitch(this,function(){
                this.navigationSlider.open(this._zoomSlider.sliderHandle);
                if(this.tooltipCloseTimeOut !== null){
                    clearTimeout(this.tooltipCloseTimeOut);
                }
                this.tooltipCloseTimeOut = setTimeout(lang.hitch(this, function(){ this.navigationSlider.close(); }), this.tooltipDuration);
            }), this.tooltipShowDelay);
        },

        getCurLabel: function() {
            var label = null;

            var labels = this._curService && this._curService.scaleLabels ? this._curService.scaleLabels : this.scaleLabels;

            array.some(labels, lang.hitch(this, function(lbl) {
                var tmp;
                var ok = true;

                if (lbl.minScale || lbl.maxScale) {
                    tmp = Math.round(this.spwViewer.get('spwMap').getCurrentScale());
                    ok = (lbl.minScale == null || lbl.minScale <= tmp) && (lbl.maxScale == null || lbl.maxScale >= tmp);
                }
                else if (lbl.minLevel || lbl.maxLevel) {
                    tmp = Math.round(this.spwViewer.get('spwMap').getLevel());
                    ok = (lbl.minLevel == null || lbl.minLevel <= tmp) && (lbl.maxLevel == null || lbl.maxLevel >= tmp);
                }
                else if (lbl.minZoom || lbl.maxZoom) {
                    tmp = Math.round(this.spwViewer.get('spwMap').getZoom());
                    ok = (lbl.minZoom == null || lbl.minZoom <= tmp) && (lbl.maxZoom == null || lbl.maxZoom >= tmp);
                }
                else {
                    ok = false;
                }

                if (ok) {
                    label = lbl.label;
                }

                return ok;
            }));

            return label;
        },

		_getZoomSliderValue: function(){
            return this.spwMap.getZoom();
		},

		_zoomStart: function(){
			if(this.zoomTwoPointsButton.get("checked")){
				this._deactivateZoomTwoPoints();
			}

			if(this._zoomSlider.actionType === null){
				this._zoomSlider.actionType = "zoomWithoutSlider";
			}
		},

		_zoomEnd: function(){
			if(this._zoomSlider !== null) {
				if(this._zoomSlider.actionType === "zoomWithoutSlider"){
					this._zoomSlider.set("value", this._getZoomSliderValue());
				}
				else {
					this._zoomSlider.actionType = null;
				}

                this.showTooltip();
			}
		},

		zoomByTwoPoints:function(){

			if(this.zoomTwoPointsButton.get("checked")){
				this._activateZoomTwoPoints();
			}
			else{
				this._deactivateZoomTwoPoints();
			}
		},

		_activateZoomTwoPoints:function(){
			this.spwMap.activateNavigationMode(SpwMap.navigationModes.ZoomTwoPoints);

			var zoomCursor = "url(" + this.imagesPath + "zoom-in.cur), auto";
			this.spwMap.setCurrentToolMapCursor(zoomCursor, true);
		},

		_deactivateZoomTwoPoints:function(){
			this.zoomTwoPointsButton.set("checked",false);
			this.spwMap.deactivateNavigation();

			this.spwMap.setCurrentToolMapCursor("default", true);
		},

		zoomOnFullExtent: function(){
			this.spwMap.zoomToFullExtent();
		}

	  });

	return SpwNavigation;
});