Source: widgets/SpwAddDataFromIFrame.js

Retour à la documentation
/**
 * @class spw.widgets.SpwAddDataFromIFrame
 */
define(["dojo/_base/declare","spw/api/SpwBaseTemplatedWidget", "dojo/dom-construct",
        "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dojo/on",
        "dojo/_base/lang", "spw/api/ConfigLoader", "dojo/_base/array", "dojo/query", "spw/api/MapServiceFactory", "esri/request", "dojo/NodeList-traverse"],
		function(declare, SpwBaseTemplatedWidget, domConstruct, BorderContainer, ContentPane,
				on, lang, ConfigLoader, array, query, MapServiceFactory, esriRequest){
	if(!String.prototype.startsWith){
		String.prototype.startsWith = function(prefix) {
		    return this.indexOf(prefix) === 0;
		}
	}

	if(!String.prototype.endsWith){
		String.prototype.endsWith = function(suffix) {
		    return this.match(suffix+"$") == suffix;
		};
	}
	
	return declare("spw.widgets.SpwAddDataFromIFrame", [SpwBaseTemplatedWidget], /** @lends spw.widgets.SpwAddDataFromIFrame.prototype */{

		templateString: '<div style="overflow:hidden;"></div>',

        "widgetTitle": "Ajouter des données du Géoportail de la Wallonie",
        "position": "none",
        "activated": false,
        "addDataUrl": "http://geoportail.wallonie.be/sites/geoportail/catalogue-walonmap.html",
        "style": {
            "width": "600px",
            "height": "100%"
        },

        /**
         * @constructs
         * @param config
         */
		constructor: function (config) {
		},

		postMixInProperties: function (config) {
			this.inherited(arguments);
		},

		buildRendering: function (config) {
			this.inherited(arguments);

			//todo : add event when frame content is loaded
			this.iframe = domConstruct.create('iframe', {
				src: this.addDataUrl,
				style: 'background-color: white; border: none; margin: 0;display: block; position: absolute; top: 40px;bottom: 0;left: 0;right: 0;width: 100px;min-width: 100%;height: 100px;min-height: calc(100% - 43px);'
			}, this.domNode);

			on(window, 'message', lang.hitch(this, this.onMessageReceived));
		},

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

			var parents = query(this.domNode).parents(".dijitPopup");
            var spanInTitle;
			parents.forEach(lang.hitch(this, function(parent){
				var temp = query(".dialogPopupTitle>span", parent);
				if(temp && temp[0]){
                    spanInTitle = temp;
                }
			}));

			if(spanInTitle && spanInTitle[0]){
				var clientHeight = spanInTitle[0].clientHeight;
				if(clientHeight > 40){
                    this.iframe.style.top = clientHeight+"px";
                }
			}

			// FIX: safari ne focus pas bien l'iframe -> impossible de cliquer dedans avant que la popup ne se ferme...
            setTimeout(lang.hitch(this, this.focusWhenReady), 100);
		},

        focusWhenReady: function(){
            var iframe = this.iframe;

            if (iframe.contentWindow != null) {
                iframe.focus();
            }
            else {
                setTimeout(lang.hitch(this, this.focusWhenReady), 100);
			}
        },

        onMessageReceived: function(mesg) {
			if (mesg.source !== this.iframe.contentWindow) {
				return;
			}

			var parsed = JSON.parse(mesg.data);

			if (parsed.loaded) {
				return;
			}

			if(parsed && this.spwViewer.get('spwMap').getMapServiceById(parsed.serviceId)){
				this.spwViewer.get('spwMap').removeMapService(parsed.serviceId, true);
			} else {
				var service = lang.mixin({
					"hasLegend": true,
					"alpha": 100,
					"toLoad": true,
					"visible": true,
					"identifiable": true
				}, parsed);
				
				if (service.url.startsWith("http://")){
					service.url = service.url.replace("http://", "//");
				}
				
				var catalogConfig = this._getCatalogServiceConfig(service.serviceId);
				if(catalogConfig){
					lang.mixin(service, catalogConfig);
				}

				//FIX FOR JAHIA AGS_TILED ARE SENT AS AGS_DYNAMIC
				//TEST IF SERVICE HAS TILE INFO, IF YES, THEN CHANGE SERVICE TYPE TO AGS_DYNAMIC
				if (service.type == MapServiceFactory.types.DYNAMIC) {
                    esriRequest({
                        url:  service.url +"/f=pjson",
                        content: {
                            f: "json"
                        },
                        handleAs: "json",
                        callbackParamName: "callback"
                    }).then(lang.hitch(this, function(res) {
                        this.onServiceInfoSuccess(res, service);
					}), lang.hitch(this, function(err) {
                        this.onServiceInfoError(err, service);
					}));
				} else if(service.type == MapServiceFactory.types.WMS){
					this.spwViewer.get('spwMap').addMapService(service);
				}
			}
		},

		onServiceInfoSuccess: function(res, service) {
        	if (res.tileInfo) {
        		service.type = MapServiceFactory.types.TILED;
			}
            this.spwViewer.get('spwMap').addMapService(service);
		},

		onServiceInfoError: function(err, service) {
        	console.error(err);
            this.spwViewer.get('spwMap').addMapService(service);
		},
		
		_getCatalogServiceConfig: function(id) {
            var catalog = ConfigLoader.getInstance().get('catalog');
            var toReturn = null;

            array.some(catalog, lang.hitch(this, function(serviceGroup) {
                var services = this.spwViewer.get('spwMap').getServicesFromGroup(serviceGroup);
                array.some(services, lang.hitch(this,function(service){
                    if (service.serviceId === id) {
                        toReturn = service;
                        return true;
                    }
                    return false;
                }));

                return toReturn;
            }));

            return toReturn;
        }
	});
});