Source: widgets/SpwCatalog.js

Retour à la documentation
/**
 * @class spw.widgets.SpwCatalog
 */
define(["dojo/_base/declare","spw/api/SpwBaseWidget","spw/api/SpwBaseTemplatedWidget", "spw/api/ConfigLoader",
        "dojo/_base/lang", "dojo/dom-construct", "dojo/_base/array", "spw/api/CollapserItem", "spw/api/Utils", "spw/api/MapService",
        "dojo/text!./templates/CatalogItem.html", "dojo/dom", "spw/api/MessageManager", "dojo/Evented", "dojo/on", "dojo/html", "dojo/i18n!./nls/SpwCatalog",
        "dijit/Dialog", "dojo/dom-attr"],
		function(declare, SpwBaseWidget, SpwBaseTemplatedWidget, ConfigLoader, lang, domConstruct, array,
				CollapserItem, Utils, MapService, CatalogItemTmpl, dom, MessageManager, Evented, on, html, labels,
				Dialog, domAttr){

    var SpwCatalog = null;
    SpwCatalog = declare("spw.widgets.SpwCatalog", [SpwBaseWidget, Evented], /** @lends spw.widgets.SpwCatalog.prototype */{

    	/* START widget config */
    	infoText: null,
    	collapseAllByDefault: false,
    	rejectedServiceIds: null,
    	/* END widget config */

		labels: labels,
    	_catalogGroupItems: null,

        /**
         * @constructs
         * @param config
         */
    	constructor: function(config, domNode){
    		this._catalogGroupItems = [];
    	},

		buildRendering: function(){
            this.domNode = domConstruct.create("div");

            this.buildCatalogView();

            if(this.collapseAllByDefault)
            	this.collapseAll();

			var spwMap = this.spwViewer.get('spwMap');
			this.own(
				spwMap.on(spwMap.events.MapServiceAdded, lang.hitch(this, this.onMapServiceAdded)),
				spwMap.on(spwMap.events.MapServiceRemoved, lang.hitch(this, this.onMapServiceRemoved))
			);
		},

		isMapServiceAllowed: function(mapServiceId) {
			return !(this.rejectedServiceIds && this.rejectedServiceIds.indexOf(mapServiceId) > -1);
		},

		onMapServiceAdded: function(mapService) {
			if(this.isMapServiceAllowed(mapService.get('serviceId'))){
				var catalogItem = this.getCatalogItemById(mapService.get('serviceId'));
				if(catalogItem)
					catalogItem.visuallyCheckService(true);
			}
		},

		onMapServiceRemoved: function(mapService) {
			var catalogItem = this.getCatalogItemById(mapService.get('serviceId'));
			if(catalogItem)
				catalogItem.visuallyCheckService(false);
		},

		buildCatalogView : function() {
			array.forEach(ConfigLoader.getInstance().get("catalog"), lang.hitch(this, function(serviceGroup){
				lang.mixin(serviceGroup, {spwViewer: this.spwViewer, labels:this.labels, imagesPath: this.imagesPath});
				var catalogGroupItem = new CatalogGroupItem(lang.mixin(serviceGroup, {catalog: this}),
						domConstruct.create("div", {}, this.domNode));
				this._catalogGroupItems.push(catalogGroupItem);
	        }));

			this.checkAlreadyAddedMapServices();

			if(this.infoText){
				dojo.create("div", { innerHTML: this.infoText, className: "ajoutGeodonneeTextDiv" }, this.domNode);
			}

			this.buildInfoDiv();
		},

		checkAlreadyAddedMapServices: function(){
			var spwMap = this.spwViewer.get('spwMap');
			array.forEach(spwMap.getMapServices({isBaseMap: false}), lang.hitch(this, function(mapService){
				var catalogItem = this.getCatalogItemById(mapService.get('serviceId'));
				if(catalogItem){
					catalogItem.visuallyCheckService(true);
				}
			}));
		},

		buildInfoDiv: function() {
			var infoDiv = domConstruct.create("div", { style: "float:right; font-size:10px; margin-top: 2px; margin-right: 5px;" }, this.domNode, "first");
			var openAll = domConstruct.create("span", { 'class': 'link', innerHTML: "Tout ouvrir"}, infoDiv);
			on(openAll, "click", lang.hitch(this, this.expendAll));
			domConstruct.create("span", { innerHTML: " / "}, infoDiv);
			var closeAll = domConstruct.create("span", { 'class': 'link', innerHTML: "Tout fermer"}, infoDiv);
			on(closeAll, "click", lang.hitch(this, this.collapseAll));
			domConstruct.create("div", { style: "height:6px;clear: both;"}, infoDiv, "after");
		},

		expendAll: function() {
			array.forEach(this._catalogGroupItems, lang.hitch(this, function(catalogGroupItem){
				catalogGroupItem.expend();
			}));
		},

		collapseAll: function() {
			array.forEach(this._catalogGroupItems, lang.hitch(this, function(catalogGroupItem){
				catalogGroupItem.collapse();
			}));
		},

		getCatalogItemById: function(serviceId) {
			var matchingCatalogItem = null;
			array.every(this._catalogGroupItems, lang.hitch(this, function(catalogGroupItem){
				if((matchingCatalogItem = catalogGroupItem.getCatalogItemById(serviceId)) != null){
					return false;
				}
				return true;
			}));
			return matchingCatalogItem;
		}
	});

    var CatalogGroupItem = null;
    CatalogGroupItem = declare("spw.widgets.SpwCatalogGroupItem", [SpwBaseWidget, Evented], {

		/*START from Config : catalog.json*/
    	tooltip: null,
    	code: null,
    	label: null,
    	mapServices: null,
    	groups: null,
		/*END from Config : catalog.json*/

    	_collapser: null,
    	_checkAllButton: null,
    	_catalogItems: null,
    	_groupItems : null,
    	catalog: null,

    	constructor: function(config, domNode){
    		this._catalogItems = [];
    		this._groupItems = [];
    	},

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

    		this._collapser = new CollapserItem({
    			_label: this.get('label'),
    			_labelCheckHandler: lang.hitch(this, this.onSelDeselAll),
    			closable: false
    		}, domConstruct.create("div", {}, this.domNode));

    		if(this.get('label').length > 0 && (this.get('mapServices') || this.get('groups'))){
	    		this._collapser.itemTitleImageBlock.style.display = "block";
    		} else {
	    		this._collapser.itemTitleImageBlock.style.display = "none";
    		}

    		if(this.get('tooltip')){
    			this._collapser.set('title', this.get('tooltip'));
    		}

    		var services = [];
    		if(services = this.get('mapServices')){
    			array.forEach(services, lang.hitch(this, function(service){
    				lang.mixin(service, { spwViewer: this.spwViewer, labels: this.labels, imagesPath: this.imagesPath });
    				if(this.catalog.isMapServiceAllowed(service.serviceId)){
        				var catalogItem = new CatalogItem(service);
        				catalogItem.on(SpwCatalog.events.CatalogItemChecked, dojo.hitch(this, this.onCatalogChanged));
        				catalogItem.on(SpwCatalog.events.CatalogItemUnchecked, dojo.hitch(this, this.onCatalogChanged));
        				this._catalogItems.push(catalogItem);
        				this._collapser.addContent(catalogItem.domNode);
    				}
    			}));
    		}

    		var groups = [];
    		if(groups = this.get('groups')){

    			array.forEach(groups, lang.hitch(this, function(serviceGroup){
    				lang.mixin(serviceGroup, {spwViewer: this.spwViewer, labels:this.labels, imagesPath: this.imagesPath});
    				var catalogGroupItem = new CatalogGroupItem(lang.mixin(serviceGroup, {catalog: this.catalog}),
							domConstruct.create("div", {}, this._collapser.itemContentBlock));
    				catalogGroupItem.on(SpwCatalog.events.CatalogGroupChecked, dojo.hitch(this, this.onCatalogChanged));
    				this._groupItems.push(catalogGroupItem);
    			}));
    		}

    		if(this._catalogItems.length === 0 && this._groupItems.length === 0){
    			this.destroy();
    		}
    	},

    	onCatalogChanged: function() {
    		var checked = 0;
			array.forEach(this._catalogItems, lang.hitch(this, function(_catalogItem){
				if(_catalogItem.isChecked()){
					checked++;
				}
			}));
			array.forEach(this._groupItems, lang.hitch(this, function(_catalogGroup){
				if(_catalogGroup.get('_collapser').get('labelCheckbox').get('checked')){
					checked++;
				}
			}));
			this._collapser.check(checked == (this._catalogItems.length + this._groupItems.length));
			this.emit(SpwCatalog.events.CatalogGroupChecked, {catalogGroup: this});
    	},

    	onSelDeselAll: function(checked) {
    		array.forEach(this._catalogItems, lang.hitch(this, function(catalogItem){
    			catalogItem.checkService(checked);
    		}));
    		array.forEach(this._groupItems, lang.hitch(this, function(groupItem){
    			groupItem.onSelDeselAll(checked);
    		}));
    		this.emit(SpwCatalog.events.CatalogGroupChecked, {catalogGroup: this});
    	},

    	expend: function(){
    		this._collapser.expend();
    		array.forEach(this._groupItems,lang.hitch(this,function(groupItem){
    			groupItem.expend();
    		}));
    	},

    	collapse: function(){
    		this._collapser.collapse();
    		array.forEach(this._groupItems,lang.hitch(this,function(groupItem){
    			groupItem.collapse();
    		}));
    	},

    	getCatalogItemById: function(serviceId){
    		var matchingCatalogItem = null;
    		array.every(this._catalogItems,lang.hitch(this,function(catalogItem){
    			if(catalogItem.get('serviceId') == serviceId){
    				matchingCatalogItem = catalogItem;
    				return false;
    			}
    			return true;
    		}));

    		if(matchingCatalogItem == null){
    			array.every(this._groupItems,lang.hitch(this,function(groupItem){
    				matchingCatalogItem = groupItem.getCatalogItemById(serviceId);
    				if(matchingCatalogItem != null){
    					return false;
    				}
    				return true;
        		}));
    		}
    		return matchingCatalogItem;
    	}
    });

    var CatalogItem = null;
    CatalogItem = declare("spw.widgets.SpwCatalogItem", [SpwBaseTemplatedWidget, Evented], {

    	templateString: CatalogItemTmpl,

		/*START from Config : catalog.json*/
    	serviceId: null,
    	label: null,
    	visible: null,
    	metadataUrl: null,
    	/*END from Config : catalog.json*/

    	_service: null,
    	labels: {},

    	constructor: function(config){
    		this._service = config;
    	},

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

    		if(this.metadataUrl == null){
    			domConstruct.destroy(this.spwTocMetawalDiv);
    		}
    	},

    	postCreate: function(){
    		this.inherited(arguments);
			this.visibleCheck.set('checked', this.get('toLoad'));

			if(this._service.metadataPopup){
				domAttr.set(this.metawalLink, {href: "javascript:void(0);", target:"_self"});
				this.own(
					on(this.metawalLink, "click", lang.hitch(this,function(){
						var width = this._service.metadataPopup.width != null ? this._service.metadataPopup.width : "750px";
						var height = this._service.metadataPopup.height != null ? this._service.metadataPopup.height : "800px";
						new Dialog({ title: this.labels.geodataInfo, content: "<iframe width="+width+" height="+height+" src="+this.metadataUrl+"></iframe>"}).show();
					}))
				);
			}
    	},

    	onClickCheckbox: function() {
    		if(this.isChecked()){
    			this._service.toLoad = true;
    			this.spwViewer.get('spwMap').addMapService(this._service);
    			this.emit(SpwCatalog.events.CatalogItemChecked, {catalogItem: this});
    		} else {
    			this.spwViewer.get('spwMap').removeMapService(this._service.serviceId);
    			this.emit(SpwCatalog.events.CatalogItemUnchecked, {catalogItem: this});
    		}
    	},

    	checkService: function(checked) {
    		if(this.visibleCheck.get('checked') != checked){
        		this.visibleCheck.set('checked', checked);
        		if(this.isChecked()){
        			this._service.toLoad = true;
        			this.spwViewer.get('spwMap').addMapService(this._service);
        			this.emit(SpwCatalog.events.CatalogItemChecked, {catalogItem: this});
        		} else {
        			this.spwViewer.get('spwMap').removeMapService(this._service.serviceId);
        			this.emit(SpwCatalog.events.CatalogItemUnchecked, {catalogItem: this});
        		}
    		}
    	},

    	visuallyCheckService: function(checked) {
    		this.visibleCheck.set('checked', checked);
    		if(this.isChecked()){
    			this.emit(SpwCatalog.events.CatalogItemChecked, {catalogItem: this});
    		} else {
    			this.emit(SpwCatalog.events.CatalogItemUnchecked, {catalogItem: this});
    		}
    	},

    	isChecked: function() {
    		return this.visibleCheck.get('checked');
    	}
    });


	SpwCatalog.events = {
			CatalogGroupChecked: "CatalogGroupChecked",
			CatalogItemChecked: "CatalogItemChecked",
			CatalogItemUnchecked: "CatalogItemUnchecked"
	};

	return SpwCatalog;
});