Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced Feature to DnD widget #92

Open
ERS-Long opened this issue Jan 30, 2017 · 0 comments
Open

Enhanced Feature to DnD widget #92

ERS-Long opened this issue Jan 30, 2017 · 0 comments

Comments

@ERS-Long
Copy link
Contributor

ERS-Long commented Jan 30, 2017

@tmcgee Used your new Legend.js code as base and changed the code LayerControl.js a little, now we can have this function: if user adds a layer using DnD widget, the legend should show up in the Legend widget too, if user removes it, the legend is also removed from the Legend widget.

The Change in the LayerControl.js is the following, I am not sure if this is needed or not ( i did not use that this.layerInfos, i am afraid it may break other thing). Please review it.

var layerInfosAdded; //declared a new variable,

in postCreate function I do

        layerInfosAdded = this.layerInfos;

// the main code is in the following function

    _subscribeToTopics: function () {
        this._removeLayerControlsHandler = topic.subscribe('layerControl/removeLayerControls', lang.hitch(this, function (layers) {
            this._removeLayerControls(layers);
            //console.log(layers);
            var index=-1;
            for (var i=0; i<layerInfosAdded.length; i++)
            {
               // console.log(layers[0].id);
               // console.log(window.layerInfosAdded[i].layer.id);
                if (layers[0].id === layerInfosAdded[i].layer.id)
                {
                    index = i;
                    break;
                }
            }
            if (index > -1)
            {
                layerInfosAdded.splice(index, 1);
                topic.publish('LayerControl/refreshLegend', {layerInfos:layerInfosAdded});
            }
        }));
        this._addLayerControlsHandler = topic.subscribe('layerControl/addLayerControls', lang.hitch(this, function (layerInfos) {
            this._addLayerControls(layerInfos);
            layerInfosAdded.push(layerInfos[0]);
            topic.publish('LayerControl/refreshLegend', {layerInfos:layerInfosAdded});
        }));
    },

For the Legend.js, i have to change it otherwise the topic call throws scoping error for the legend object.

define([
'dojo/_base/declare',
'dijit/_WidgetBase',
'dojo/_base/lang',
'esri/dijit/Legend',
'dojo/topic'
], function (
declare,
_WidgetBase,
lang,
Legend,
topic
) {
var legend;
return declare([_WidgetBase], {
startup: function () {
this.inherited(arguments);

        legend = new Legend({
            arrangement: this.arrangement || Legend.ALIGN_LEFT,
            autoUpdate: this.autoUpdate || true,
            id: this.id + '_legend',
            layerInfos: this.layerInfos,
            map: this.map,
            respectCurrentMapScale: this.respectCurrentMapScale || true
        }, this.domNode);
        legend.startup();

        this.map.on('update-end', lang.hitch(this, function () {
            legend.refresh();
        }));

        topic.subscribe('LayerControl/refreshLegend', function (layerInfos) {
        //    console.log(layerInfos.layerInfos);
            legend.refresh(layerInfos.layerInfos);
        });
    }
});

});

Very last change is in the config file, we use the gis instead of esri.

        legend: {
            include: true,
            id: 'legend',
            type: 'titlePane',
            path: 'gis/dijit/Legend',
            title: '<i class="icon-large icon-legend"></i>&nbsp;&nbsp;Legend',
            open: true,
            position: 0,
            placeAt: 'right',
            options: {
                map: true,
                legendLayerInfos: true
            }
        },

Please review the logic and refine it.

My test works just fine.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant