Source: widgets/SpwAdvancedToolBar.js

Retour à la documentation
/**
 * @class spw.widgets.SpwAdvancedToolBar
 */
define(["dojo/_base/declare","spw/api/SpwBaseWidget", "dojo/_base/lang",
        "spw/api/Utils", "dojo/_base/array", "dojo/dom-construct",
        "dijit/form/DropDownButton", "dijit/DropDownMenu", "dijit/MenuItem",
        "dojo/dom-style","dojo/query", "dojo/on", "dojo/mouse", "dijit/PopupMenuItem",
        "dijit/form/ToggleButton", "dijit/Toolbar", "dijit/ToolbarSeparator", "spw/widgets/SpwIWantTo",
        "dojo/dom-class", "dojo/window"],
        function(declare, SpwBaseWidget, lang, Utils, array, domConstruct,
                DropDownButton, DropDownMenu, MenuItem, domStyle, query, on,
                mouse, PopupMenuItem, Button, Toolbar, ToolbarSeparator,
                SpwIWantTo, domClass, win){

    var SpwAdvancedToolBar = null;

    SpwAdvancedToolBar = declare("spw.widgets.SpwAdvancedToolBar", [SpwBaseWidget], /** @lends spw.widgets.SpwAdvancedToolBar.prototype */{

        "content": [
            {
                "widgetId": "SpwQuickAddressSearchId",
                "integrated": true
            },
            {
                "label": "+",
                "tooltip": "Autres critères de localisation",
                "content": [
                    {
                        "label": "Adresse",
                        "content": [
                            {
                                "widgetId": "SpwAddressSearchId",
                                "integrated": true
                            }
                        ]
                    },
                    {
                        "label": "Coordonnées",
                        "content": [
                            {
                                "widgetId": "SpwCoordinatesSearchId",
                                "integrated": true
                            }
                        ]
                    },
                    {
                        "label": "Parcelle cadastrale",
                        "content": [
                            {
                                "widgetId": "SpwCadmapSearchId",
                                "integrated": true
                            }
                        ]
                    }
                ],
                "className": "otherLocalizeButton"
            },
            {
                "widgetId": "SpwMeasureId",
                "label": "Mesurer"
            },
            {
                "widgetId": "SpwDrawId",
                "label": "Dessiner"
            },
            {
                "widgetId": "SpwGoogleStreetViewId",
                "label": "StreetView"
            },
            {
                "widgetId": "SpwLegendId",
                "label": "Légende"
            },
            {
                "widgetId": "SpwIdentifyId",
                "label": "Infos"
            },
            {
                "label": "Imprimer",
                "widgetId": "SpwPrintId",
                "className": "printMenuButton"
            },
            {
                "label": "Partager",
                "content": [
                    {
                        "widgetId": "SpwShareLinkId",
                        "integrated": true
                    }
                ],
                "className": "ShareLinkMenuButton",
                "popupClass": "ShareLinkMenuPopup"
            },
            {
                "content": [
                    {
                        "widgetId": "SpwContextChooserId",
                        "integrated": true
                    }
                ],
                "label": "Vues prédéfinies",
                "className": "contextsMenuButton",
                "popupClass": "ShareLinkMenuPopup"
            },
            {
                "label": "Fond de plan   et voyage     dans le temps",
                "content": [
                    {
                        "widgetId": "SpwBaseMapChooserId",
                        "integrated": true
                    }
                ],
                "className": "fdpMenuButton",
                "popupClass": "fdpMenuPopup",
                "style": {
                    "position": "absolute",
                    "right": "10px",
                    "display": "inline-block"
                }
            }
        ],
        'class': 'SpwAdvancedToolBar',
        position: 'top',
        toolbarStyle: null,
        labelPosition: 'top',
        showLabel: true,
        activated: true,
        lightToolbar: false,

        buttonStyle: {
            fontSize: '10px'
        },

        spwWidgetsManager: null,
        _toolbar: null,

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

            this.spwWidgetsManager = this.spwViewer.get('spwWidgetsManager');

            this.widgetsInToolbar = [];
            this.inMoreMenu = [];

            this.own(on(window, 'resize', lang.hitch(this, this.onWinResize)));
        },

        postCreate: function() {
            this.inherited(arguments);
            this.onWinResize();
            if (this.lightToolbar) {
                domClass.add(this.domNode, 'light-toolbar');
            }
        },

        onWinResize: function() {
            var cpt = 0;

            while (this.domNode.clientHeight > this.domNode.parentNode.clientHeight) {
                var widget = this.widgetsInToolbar.pop();

                if (this.moreMenu == null) {
                    this.moreMenu = new SpwIWantTo({
                        spwViewer:this.spwViewer,
                        style:"display:inline-block;",
                        labelPosition: this.labelPosition,
                        showLabel: this.showLabel,
                        buttonStyle: this.buttonStyle,
                        lightTitle: true,
                        label: '⇩',
                        style: 'position: absolute; right: 10px; display: inline-block;',
                        className: "otherLocalizeButton",
                        content: null
                    });

                    this._toolbar.addChild(this.moreMenu);
                }

                this.inMoreMenu.push({
                    moreMenu: this.moreMenu.addWidgetToMainMenu(widget.contentInfo),
                    toolbar: widget
                });

                this._toolbar.removeChild(widget);

                cpt += 1;
            }

            if (!cpt && this.inMoreMenu && this.inMoreMenu.length > 0) {
                var toReput = this.inMoreMenu.pop();

                this.moreMenu.removeWidgetFromMainMenu(toReput.moreMenu);

                this.widgetsInToolbar.push(toReput.toolbar);

                if (toReput.toolbar.refresh) {
                    toReput.toolbar.refresh();
                }

                this._toolbar.addChild(toReput.toolbar);

                if (this.inMoreMenu.length === 0) {
                    this._toolbar.removeChild(this.moreMenu);
                    this.moreMenu.destroy();
                    this.moreMenu = null;
                }

                this.onWinResize();
            }
        },

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

            this._toolbar = new Toolbar({}, domConstruct.create("div", { }, this.domNode, "last"));
            this.watchers = [];

            if(this.toolbarStyle){
                try{
                    domStyle.set(this._toolbar.domNode, this.toolbarStyle);
                } catch(err){
                    console.error(err);
                }
            }

            if(this.spwWidgetsManager.widgetsCreated){
                this.buildContent(this._toolbar, this.content);
            } else {
                this.own(
                    this.spwWidgetsManager.on(this.spwWidgetsManager.events.widgetsCreated, lang.hitch(this,function(){
                        this.buildContent(this._toolbar, this.content);
                    }))
                );
            }
        },

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

            array.forEach(this.watchers, function(w) {
                w.unwatch();
            });

            this.watchers = null;

            if (this._toolbar) {
                array.forEach(this._toolbar.getChildren(), lang.hitch(this, function(child) {
                    this._toolbar.removeChild(child);
                }));

                this._toolbar.destroy();

                this._toolbar = null;
            }
        },

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

        buildContent: function(toolbar, content) {
            array.forEach(content, lang.hitch(this, function(contentInfo){
                if(contentInfo === "separator")
                {
                    this.createSeparator(toolbar);
                }
                else if(typeof(contentInfo) === 'string')
                {
                    this.createButtonFromWidgetId(toolbar, { widgetId: contentInfo });
                }
                else if(typeof(contentInfo.widgetId) !== 'undefined')
                {
                    this.createButtonFromWidgetId(toolbar, contentInfo);
                }
                else if(typeof(contentInfo.content) !== 'undefined')
                {
                    this.createButtonFromContent(toolbar, contentInfo);
                }
            }));
        },

        createSeparator: function(toolbar) {
            var sep = new ToolbarSeparator();
            toolbar.addChild(sep);

            this.widgetsInToolbar.push(sep);
        },

        createButtonFromWidgetId: function(toolbar, widgetInfo){
            var widget = this.spwWidgetsManager.getWidgets({
                widgetId:widgetInfo.widgetId
            });

            if(widget && widget.length){
                if(widgetInfo.integrated === true){
                    toolbar.addChild(widget[0]);

                    lang.mixin(widget[0], {
                        contentInfo: widgetInfo
                    });

                    this.widgetsInToolbar.push(widget[0]);
                } else {
                    var btn = this.createButton(widget[0], widgetInfo);
                    if(btn){
                        lang.mixin(btn, {
                            contentInfo: widgetInfo
                        });

                        toolbar.addChild(btn);

                        this.widgetsInToolbar.push(btn);
                    }
                }

                if (widget[0]) {
                    widget[0]._closeOnSolitary = true;
                }
            }
        },

        createButton: function(widget, widgetInfo) {
            var widgetTitle = widgetInfo.label || widget.get('widgetTitle');
            var btnParam = {
                onClick: function(){
                    if(widget.get('activated')){
                        widget.onDeactivate();
                    } else {
                        button.set('checked', false);
                        widget.onActivate();

                        if(widget.get('isOneShot')){
                            button.set('checked', false);
                            widget.set('activated', false);
                        }
                    }
                },
                checked:widget.get('activated'),
                iconClass: widget.iconClass ? widget.iconClass : "",
                title: this.lightToolbar ? widgetTitle : ''
            };

            if(widgetInfo.label) {
                btnParam.label = widgetInfo.label;
            } else {
                btnParam.label = widget.get('widgetTitle');
            }

            if(typeof(widgetInfo.showLabel) === 'boolean'){
                btnParam.showLabel = widgetInfo.showLabel;
            }
            else if(this.showLabel){
                btnParam.showLabel = true;
            }
            else {
                btnParam.showLabel = false;
            }

            var button = new Button(btnParam);

            if(widgetInfo.className) {
                domClass.add(button.domNode, widgetInfo.className);
            }

            query("span.dijitButtonText", button.domNode).forEach(lang.hitch(this, function(node){
                switch (this.labelPosition)
                {
                case 'bottom' : domStyle.set(node, "display", "block"); break;
                case 'top' : domStyle.set(node, "display", "block"); domConstruct.place(node, node.parentNode, "first"); break;
                case 'right' : domConstruct.place(node, node.parentNode, "first"); break;
                default: break;
                }
            }));

            if(widgetInfo.iconStyle) {
                query("span.dijitIcon", button.domNode).forEach(lang.hitch(this, function(node){
                    try{
                        domStyle.set(node, widgetInfo.iconStyle);
                    } catch(err){
                        console.error(err);
                    }
                }));
            } else {
                query("span.dijitIcon", button.domNode).forEach(lang.hitch(this, function(node){
                    domStyle.set(node, { width:"16px", height:"16px" });
                }));
            }

            query("span.dijitButtonNode", button.domNode).forEach(lang.hitch(this, function(node){
                try{
                    domStyle.set(node, this.buttonStyle);
                } catch(err){
                    console.error(err);
                }
            }));

            this.watchers.push(widget.watch("activated", function(prop, o, n){
                if(!widget.get('isOneShot')){
                    button.set('checked', n);
                }
            }));

            return button;
        },

        createButtonFromContent: function(toolbar, widgetInfo){
            var btn = new SpwIWantTo(lang.mixin({
                spwViewer:this.spwViewer, style:"display: inline-block;",
                labelPosition: this.labelPosition,
                lightTitle: true,
                showLabel: this.showLabel,
                buttonStyle: this.buttonStyle,
                title: this.lightToolbar && !widgetInfo.tooltip ? widgetInfo.label : ''
            }, widgetInfo));

            toolbar.addChild(btn);

            lang.mixin(btn, {
                contentInfo: widgetInfo
            });

            this.widgetsInToolbar.push(btn);
        }
    });

    return SpwAdvancedToolBar;
});