consbio / Leaflet.HtmlLegend

A simple Leaflet plugin for creating legends with HTML elements
ISC License
56 stars 26 forks source link

allow no label and still have a collapsible item #11

Open brentfraser opened 4 years ago

brentfraser commented 4 years ago

Currently if the legend entry does not have a label value , the entry is not collapsible. And a missing label key causes "undefined" to be shown on the legend.

ka7eh commented 4 years ago

Thanks for reporting this. I'll take a look later. Also, feel free to submit a PR if you have a fix for it.

ka7eh commented 4 years ago

The first part of the issue is intentional. If you want to legend with a single entry and no label to be collapsible, you can set collapseSimple to true in its options.

As for the second part, I couldn't reproduce the issue. Can you provide an example for it? Thanks.

brentfraser commented 4 years ago

Simply remove/rename the "name" key from the legends list and the item is no longer collapsible. Below is an edit of the Example to show this. Note the name is not shown (as expected), but there is no ability to collapse the elements (a problem):

`

var htmlLegend1and2 = L.control.htmllegend({
    position: 'bottomright',
    legends: [{
        nameTEST: 'Layer 1',
        layer: layer1,
        elements: [{
            label: 'Rectangle',
            html: '',
            style: {
                'background-color': 'red',
                'width': '10px',
                'height': '10px'
            }
        }, {
            label: 'Triangle',
            html: '',
            style: {
                'background-color': 'green',
                'width': '10px',
                'height': '10px'
            }
        }]
    }],
    collapseSimple: true,
    detectStretched: true,
    collapsedOnInit: true,
    defaultOpacity: 0.7,
    visibleIcon: 'icon icon-eye',
    hiddenIcon: 'icon icon-eye-slash'
})
map.addControl(htmlLegend1and2)

`

brentfraser commented 4 years ago

One work-around is to use name: ' ',

brentfraser commented 4 years ago

As for the "undefined", it appears when the html key is not included (not the label key as I originally reported above)

ka7eh commented 4 years ago

Thanks for clarifying this. I looked at the first case again and the current behavior is intentional because right now I don't see an elegant way of having a collapsible legend without a label. I fixed the second part in #13.