Source: widgets/SpwCustomRegion.js

Retour à la documentation
/**
 * @class spw.widgets.SpwCustomRegion
 */
define(["dojo/_base/declare", "spw/api/SpwBaseWidget", "dojo/dom-construct", "dojo/dom-style", "dojo/_base/lang",
        "spw/api/MessageManager", "dijit/layout/TabContainer", "dijit/layout/ContentPane", "dojo/_base/array",
        "dojo/Evented"],
		function(declare, SpwBaseWidget, domConstruct, domStyle, lang, MessageManager, TabContainer, ContentPane, 
				array, Evented){

	var SpwCustomRegion = null;
	SpwCustomRegion = declare("spw.widgets.SpwCustomRegion", [SpwBaseWidget, Evented], /** @lends spw.widgets.SpwCustomRegion.prototype */{
		
		/* START from config */
		widgets: null,
		/* END from config */
		
		_widgetsDefinitions: null,
		widgetContainer: null,
		_tabContainer: null,
		_activeWidget: null,
		widgetClassesLoaded: false,
		
        /**
         * @constructs
         * @param config
         */
		constructor: function(){
			this._widgetsDefinitions = [];
		},
		
		postMixInProperties: function(){
			this.inherited(arguments);
		},
		
		buildRendering: function(){
			this.inherited(arguments);
            this.domNode.style.width = '100%';
            this.domNode.style.height = '100%';
            this.domNode.style.padding = '0';
            this.domNode.style.overflow = 'hidden';

			domStyle.set(this.domNode, {width:"100%", height:"100%", padding: 0, overflow: 'hidden'});
			var handler = null, cpt = 0;
			handler = require.on("error", lang.hitch(this, function(err){
				cpt++;
				if(cpt == this.widgets.length){
					handler.remove();
					this.widgetClassesLoaded = true;
					this.emit(SpwCustomRegion.events.widgetsLoaded);
				}
			}));
			array.forEach(this.widgets, lang.hitch(this, function(widget, idx){
				require([widget.className], lang.hitch(this, function(widgetClass){
					lang.mixin(widget.config, {position: "none", activated: false, inToolbar: false, closable: false, spwViewer: this.spwViewer});
					this._widgetsDefinitions[idx] = { widgetClass: widgetClass, widgetConfig: widget.config };
					cpt++;
					if(cpt == this.widgets.length){
						handler.remove();
						this.widgetClassesLoaded = true;
						this.emit(SpwCustomRegion.events.widgetsLoaded);
					}
				}));
			}));
		},
		
		startup: function() {
			this.inherited(arguments);
			
			if(this.widgetClassesLoaded){
				this.initializeWidgets();
			} else {
				this.on(SpwCustomRegion.events.widgetsLoaded, lang.hitch(this, this.initializeWidgets));
			}
		},
		
		initializeWidgets: function(){
			if(this.widgets && this.widgets.length > 0){
				if(this.widgets.length == 1){
					this.showUniqueWidget();
				} else {
					this.showWidgets();
				}
			}
		},
		
		showUniqueWidget: function() {
			var widgetDef = this._widgetsDefinitions[0];
			var widget = new widgetDef.widgetClass(widgetDef.widgetConfig);
			
			this.widgetContainer = new ContentPane({content: widget, title: widget.widgetTitle}, domConstruct.create("div", {style:"width:100%; height:100%;padding:0;"}, this.domNode));
		},
		
		showWidgets: function() {
			//domStyle.set(this.domNode, {"padding-top":"5px"});
            this._tabContainer = new TabContainer({style: "width:100%;height:100%;padding:0;"});

            array.forEach(this._widgetsDefinitions, lang.hitch(this, function(widgetDef){
				var widget = new widgetDef.widgetClass(widgetDef.widgetConfig);
				var contentPane = new ContentPane({content: widget, title: widget.widgetTitle}, domConstruct.create("div", {style:"width:100%; height:100%;padding:0;"}));
				this._tabContainer.addChild(contentPane);
			}));
            
            this._tabContainer.watch("selectedChildWidget", lang.hitch(this, function(name, oval, nval) {
                if (oval && oval.getChildren()[0] && oval.getChildren()[0].onDeactivate) {
                	oval.getChildren()[0].onDeactivate();
                	this._activeWidget = null;
                }
                if (nval && nval.getChildren()[0] && nval.getChildren()[0].onActivate) {
                	nval.getChildren()[0].onActivate();
                	this._activeWidget = nval.getChildren()[0];
                }
            }));

            this._tabContainer.placeAt(this.domNode);
            this._tabContainer.startup();
            
            //activate default selected widget
            var w = this._tabContainer.get("selectedChildWidget");
            if(w && w.getChildren()[0] && w.getChildren()[0].onActivate){
            	w.getChildren()[0].onActivate();
            	this._activeWidget = w;
            }
		},
		
		onDeactivate: function() {
			this.inherited(arguments);
			if(this._activeWidget && this._activeWidget.onDeactivate) {
				this._activeWidget.onDeactivate();
			}
		},
		
        resize: function() {
            if (this._tabContainer) {
    			this._tabContainer.resize();
            }
        }
        		
	});
	
	SpwCustomRegion.events = {
		widgetsLoaded: "widgetsLoaded"	
	};
	
	return SpwCustomRegion;
});