Source: api/SpwBaseToolBar.js

Retour à la documentation
/**
 * @class spw.api.SpwBaseToolBar
 * @classdesc Elément UI définissant la barre d'outil standard du viewer.
 */
define(["dojo/_base/declare", "dijit/_WidgetBase", "dijit/_TemplatedMixin", "dijit/_WidgetsInTemplateMixin", "dojo/dom-class",
        "dojo/text!./templates/SpwBaseToolBar.html", "dojo/_base/lang", "dojo/_base/array", "dijit/form/ToggleButton", "dijit/registry"],
		function(declare, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, domClass, template, lang, array, ToggleButton, registry) {
	return declare("spw.api.SpwBaseToolBar", [_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], /** @lends spw.api.SpwBaseToolBar.prototype */{
		
		
		/**
	     * @private
	     * @type String
	     */
		templateString: template,
		
		/**
		 * Position sur la carte
		 */
		mapPosition: null,
		
		/**
		 * Indique si la barre d'outil est légèrement transparente lorsque le curseur n'est pas au dessus.
		 */
		reactive: false,
		
		/**
		 * Tableau des widgets présents dans la toolbar
	     * @private
	     * @type spw.api.SpwBaseTool[]
	     */
		_toolWidgetList : [],
		
		buildRendering: function() {
			this.inherited(arguments);
			if(this.reactive){
				domClass.add(this.topToolBar, "reactive");
			} else if (domClass.contains(this.topToolBar, "reactive")){
				domClass.remove(this.topToolBar, "reactive");
			}
		},
		
		/**
		 * Ajoute un widget dans la toolbar
		 */
		addWidget: function(widget) {
			this._toolWidgetList.push(widget);
			if(widget.get('inToolbar')){
				this.createWidgetButton(widget);
			} else {
				this.placeInToolbar(widget, null);
			}
		},
		
		/**
		 * Retire un widget de la toolbar
		 */
		removeWidget: function(widget) {
			var widgetIndex = -1;
			for(var i=0;i<this._toolWidgetList.length;i++){
				if(this._toolWidgetList[i].id == widget.id){
					widgetIndex = i;
				}
			}
			if(widgetIndex > -1){
				this._toolWidgetList.splice(widgetIndex, 1);
			}
			if(widget.get('inToolbar') && widget.get('toolBarButton')){
				widget.get('toolBarButton').destroy();
				widget.set('toolBarButton', null);
			}
		},
		
		/**
		 * Permet de créer un boutton dans la barre d'outils pour un outil donné
		 * @param {spw.api.SpwBaseWidget> widget
	    */
		createWidgetButton: function(widget)
		{
			var btn = new ToggleButton({
				id:widget.get('name'),
				iconClass:widget.get('iconClass'),
				label:widget.get('widgetTitle'),
				showLabel: false,
				onClick: lang.hitch(this, function(){
					if(!widget.get('isOneShot')){
						var widgetActivated = widget.get('activated');
						
						if(widget.get('solitary')) {
							this.deactivateTools();
						}
						
						if(!widgetActivated){
							widget.onActivate();
						}
						else {
							widget.onDeactivate();
						}
					} else {
						widget.onActivate();
						btn.set("checked", false);
					}
				})
			});
			
			this.placeInToolbar(widget, btn);
			
			if(widget.get('activated')){
				btn.set("checked", true);
			}
		},
		
		/**
		 * Place le widget dans la toolbar
		 */
		placeInToolbar: function(widget, btn){
			if(widget.get('toolBarIndex') != null && widget.get('toolBarIndex') > -1) {
				var previousElement = null;
				var previousWidget = this.getPreviousWidget(widget);
				if(previousWidget){
					previousElement = previousWidget.get('toolBarButton') ? previousWidget.get('toolBarButton') : previousWidget;
				}

				if(previousElement){
					(btn ? btn : widget).placeAt(previousElement, "after");
				} else {
					(btn ? btn : widget).placeAt(this.topToolBar, "first");
				}
			} else {
				(btn ? btn : widget).placeAt(this.topToolBar, "last");
			}
			if(btn){
				widget.set('toolBarButton', btn);
			}
		},
		
		/**
		 * Obtient le widget précédent dans la toolbar
		 */
		getPreviousWidget: function(widget){
			var widgetIndex = widget.get('toolBarIndex');
			var prevWidget = null;
			array.forEach(this._toolWidgetList, lang.hitch(this, function(w){
				if(prevWidget && prevWidget.get('toolBarIndex') < w.get('toolBarIndex') && w.get('toolBarIndex') < widgetIndex){
					prevWidget = w;
				} else if(!prevWidget && w.get('toolBarIndex') < widgetIndex){
					prevWidget = w;
				}
			}));
			return prevWidget ? prevWidget : null;
		},
		
		/**
		 * Permet de désactiver tous les outils
	    */
		deactivateTools: function(){
			array.forEach(this._toolWidgetList, function(widget){
				if(widget.get('activated')){
					widget.onDeactivate();
				}
			});
		}
	});
});