Source: widgets/SpwContextList.js

Retour à la documentation
/**
 * @class spw.widgets.SpwContextList
 */
define(["dojo/_base/declare", "spw/api/SpwBaseTemplatedWidget", "dojo/text!./templates/SpwContextList.html",
        "spw/api/ConfigLoader", "dijit/Menu", "dijit/form/DropDownButton", "dijit/MenuItem", "dijit/MenuSeparator",
        "dojo/_base/lang", "dijit/PopupMenuItem", "spw/api/Utils", "spw/api/ContextManager", "dojo/i18n!./nls/SpwContextList",
        "dojo/dom-class"], 
		function(declare, SpwBaseTemplatedWidget, tmpl, ConfigLoader, Menu, DropDownButton, MenuItem, MenuSeparator,
				lang, PopupMenuItem, Utils, ContextManager, labels, domClass){
	
	return declare("spw.widgets.SpwContextList", [SpwBaseTemplatedWidget], /** @lends spw.widgets.SpwContextList.prototype */{
		
		templateString: tmpl,
		labels: labels,

		rejectedContexts: null,
		acceptedContexts: null,
		
		selectTitle: null,
		selectedViewerContext:null,
		tabViewerContexts:null,
		tabAppContexts:null,
		btDropDown:null,
		
        /**
         * @constructs
         * @param config
         */
		constructor: function() {
			this.tabAppContexts=new Array();
			this.tabViewerContexts=new Array();
		},
		
		postMixInProperties: function() {
			this.inherited(arguments);
			
			this.selectTitle = this.labels.selectTitle + " : " + ConfigLoader.getInstance().currentContext;
		},
	
		postCreate: function() {
			this.inherited(arguments);

			var contextes = ConfigLoader.getInstance().get('context');
			var dropDownMenu = new Menu({ style: "display: none;"});
			this.btDropDown = new DropDownButton({
	            label: "Aucun",
	            name: "ddBtContexts",
	            dropDown: dropDownMenu,
	            width:"100%"
	        });
			dropDownMenu.onOpen =  function(){
				domClass.add(this._popupWrapper, "comboListPopup");
			};
			
			for(var i=0; i<contextes.length; i++)
			{
				if(this.isContextAllowed(contextes[i].name)){
					dropDownMenu.addChild(this.processContextConfig(contextes[i]));
				}
			}
	
			dojo.style(this.btDropDown.domNode, "width", "100%");
			dojo.style(this.btDropDown._buttonNode, "width", "100%");
			dojo.style(this.btDropDown.containerNode, "width", "80%");
	
			this.contextChooserSelect.appendChild(this.btDropDown.domNode);
		},
		
		isContextAllowed: function(contextCode) {
			if(this.acceptedContexts){
				return this.acceptedContexts.indexOf(contextCode) > -1;
			} else if(this.rejectedContexts && this.rejectedContexts.indexOf(contextCode) > -1){
				return false;
			}
			return true;
		},
		
		processContextConfig : function(context){
			if(context.url){
				context._menuItem = new MenuItem({
		            label: context.label,
		            _contextObj:context
		        });
				context._menuItem.on('click', lang.hitch(this, function(e){ this.applyContext(context);}));
			} else if(context.contexts){
				var pSubMenu = new dijit.Menu();
				var i=0;
				while (i<context.contexts.length){
					if(this.isContextAllowed(context.contexts[i].name)){
						pSubMenu.addChild(this.processContextConfig(context.contexts[i]));
					}
					i++;
				}
				context._menuItem = new PopupMenuItem({
		            label: context.label,
		            popup: pSubMenu
		        });
				context._menuItem.popup.onOpen =  function(){
					domClass.add(this._popupWrapper, "subComboListPopup");
				};

			} else if(context.separator){
				context._menuItem = new MenuSeparator();
			} else {
				context._menuItem = new MenuItem({
		            label: context.label,
		            _contextObj:context,
		    		checked:false
		        });
				if(context.name == ConfigLoader.getInstance().currentContext){
					this.contextChooserSelect.title = this.labels.selectTitle + " : " + context.label;
					context._menuItem.set("checked", true);
					this.selectedViewerContext = context;
					this.btDropDown.set("label",context.label);
				}
				this.tabViewerContexts.push(context);
				context._menuItem.on('click', lang.hitch(this, function(e){ this.selectContextViewer(context);}));
			}
			return context._menuItem;
		},
		
		selectContextViewer:function(context){
			this.contextChooserSelect.title = this.labels.selectTitle + " : " + context.label;
			if (this.selectedViewerContext!=null && context._menuItem.get("checked") && this.selectedViewerContext!=context)
			{
				this.selectedViewerContext._menuItem.set("checked",false);
				this.selectedViewerContext._menuItem.set("iconClass","");
				this.selectedViewerContext = context;
			}
			else{
				context._menuItem.set("checked", true);
				if(context.type != "intern_context" && context.type != "app_context"){
					this.selectedViewerContext = context;
				}
			}
			this.btDropDown.set("label",context.label);
			this.applyContext(context);
		},
		
		applyContext:function(context){
			ContextManager.getInstance().changeContext(context.name);
		},
		
		contextChanged: function(){
			var selectedOption = this.contextChooserSelect.options[this.contextChooserSelect.selectedIndex];
			ContextManager.getInstance().changeContext(selectedOption.value);
			var i=0;
			while(i<this.contextChooserSelect.options.length){
				if(this.contextChooserSelect.options[i].value == ConfigLoader.getInstance().currentContext){
					this.contextChooserSelect.selectedIndex = i;
				}
				i++;
			}
		}
	});
});