Source: api/ContextManager.js

Retour à la documentation
define(["dojo/_base/declare", "dojo/DeferredList", "dojo/_base/config", "dojo/topic", "dojo/request/xhr",
        "dojo/_base/lang", "spw/api/SpwViewer", "spw/api/MessageManager", "dojo/_base/array",
        "spw/api/ConfigLoader", "spw/api/Utils", "spw/api/StateManager", "dojo/_base/window", "dojo/hash",
        "esri/geometry/Extent"],
        function (declare, DeferredList, config, topic, xhr, lang, SpwViewer, MessageManager,
                array, ConfigLoader, Utils, StateManager, win, hash, Extent) {

    var ContextManager = null;
    ContextManager = declare("spw.api.ContextManager", null, /** @lends spw.api.ContextManager.prototype */{

        /**
         * Référence vers l'instance du Geoviewer
         * @type spw.api.SpwViewer
         */
        spwViewer: null,

        /**
         * Contextes définis dans la configuration
         * @type Array.<Object>
         */
        contexts: null,

        /**
         * Le constructeur ne doit pas être appelé directement. ContextManager est une classe statique et doit être accédée via la méthode {@link spw.api.ContextManager.getInstance getInstance}.
         * @classdesc Manipule les configurations des contextes et gère le changement de contexte.
         * @constructs
         */
        constructor: function(config) {
            this.spwViewer = SpwViewer.getInstance();
            this.contexts = [];

            array.forEach(Utils.findInList(ConfigLoader.getInstance().get('context')), lang.hitch(this, function(context){
                if(context && context.contexts && context.contexts.length > 0){
                    this.contexts = this.contexts.concat(array.map(context.contexts, function(internContext){ return internContext; }));
                }
                this.contexts.push(context);
            }));

            topic.subscribe("/dojo/hashchange", lang.hitch(this, function(changedHash){
                if(Utils.gua("CTX") && ConfigLoader.getInstance().currentContext != Utils.gua("CTX"))
                {
                    ConfigLoader.getInstance().load(Utils.gua("CTX"));
                    this.applyContext();
                }
            }));
        },

        /**
         * Change un contexte dynamiquement sans rechargement de page ou l'ouvre dans un nouvel onglet suivant la configuration du contexte.
         * @param name le nom du contexte à charger
         */
        changeContext: function(name){
            var ctx = Utils.findInList(this.contexts, {name: name})[0];

            StateManager.getInstance().cleanUrl();

            if(ctx.url){
                var extent = this.spwViewer.get('spwMap').getCurrentExtent();
                var url = win.doc.createElement("a");
                //url.target = '_blank';
                url.href = ctx.url ? ctx.url.replace("${xmin}", Math.round(extent.xmin)).replace("${xmax}", Math.round(extent.xmax)).replace("${ymin}", Math.round(extent.ymin)).replace("${ymax}", Math.round(extent.ymax)) : win.global.location.href;
                win.global.open(url.href, '_blank');
            } else {
                Utils.sua("CTX", ctx.name);
            }
        },

        /**
         * Applique le contexte courant. Recharge les fonds de plan, les services initiaux et les widgets.
         */
        applyContext: function() {
            //Supprime tous les services
			array.forEach(this.spwViewer.get('spwMap').getMapServices({ "isBaseMap": false }), lang.hitch(this, function(mapService) {
				this.spwViewer.get('spwMap').removeMapService(mapService.get('serviceId'));
			}));

            this.spwViewer.get('spwWidgetsManager').destroyWidgets();

            var viewerCfg = ConfigLoader.getInstance().get('viewer');

            this.spwViewer.set('maxScale', viewerCfg.maxScale);
            this.spwViewer.set('minScale', viewerCfg.minScale);

            this.spwViewer.get('spwWidgetsManager').loadWidgetClasses();
            //spwMap.createServices();

//            var spwMap = this.spwViewer.get('spwMap');
//
//            //Supprime les basemaps
//            spwMap.removeBaseMaps();
//
//            var handler = null;
//            handler = spwMap.watch("hasBaseMap", lang.hitch(this, function(){
//                if(handler){
//                    handler.remove();
//                }
//                //Supprime tous les services
//                array.forEach(spwMap.getMapServices({ "isBaseMap": false }), lang.hitch(this, function(mapService){
//                    spwMap.removeMapService(mapService.get('serviceId'));
//                }));
//
//                this.spwViewer.get('spwWidgetsManager').destroyWidgets();
//
//                var viewerCfg = ConfigLoader.getInstance().get('viewer');
//
//                this.spwViewer.set('maxScale', viewerCfg.maxScale);
//                this.spwViewer.set('minScale', viewerCfg.minScale);
//
//                this.spwViewer.get('spwWidgetsManager').loadWidgetClasses();
//                spwMap.createServices();
//            }));
//
//            spwMap.createBaseMap(true);
//
//            var bbox = Utils.gua("BBOX");
//
//            if(bbox != null && bbox != "") {
//                var extentSplit = bbox.split(",");
//                if(extentSplit != null && extentSplit.length == 4){
//                    var ko = false;
//                    var tmp = [];
//
//                    ko = array.some(extentSplit, function(item) {
//                        var cvt = +item;
//
//                        if (isNaN(cvt)) {
//                            return true;
//                        }
//
//                        tmp.push(cvt);
//                        return false;
//                    });
//
//                    if (!ko) {
//                        spwMap.zoomToBbox.apply(spwMap, tmp);
//                    }
//                }
//            }
        }

    });

    var _INSTANCE = null;
    /**
     * Permet de récupérer l'instance du ContextManager
     * @method getInstance
     * @memberof spw.api.ContextManager
     * @returns spw.api.ContextManager
     */
    ContextManager.getInstance = function(){
        if(_INSTANCE == null){
            _INSTANCE = new ContextManager();
        }
        return _INSTANCE;
    };

    return ContextManager;
});