npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

leaflet-control-accordionlegend

v1.2.1

Published

an attractive accordion legend for Leaflet

Downloads

4

Readme

L.Control.AccordionLegend

An attractive accordion legend for Leaflet. Features include:

  • Checkboxes to toggle layer visibility
  • Expanding and collapsing sections for grouping the layers
  • Opacity sliders

https://github.com/GreenInfo-Network/L.Control.AccordionLegend

http://greeninfo-network.github.io/L.Control.AccordionLegend

Basic Usage

The content parameter is a structure for the legend. This consists of a list of "sections" and within each section a list of "layers" This is best illustrated with an example.

    var LAYERS_AND_LEGENDS = [
        {
            'title': "Base Maps",
            layers: [
                {
                    'title': "OSM Basemap",
                    'layer': L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {}),
                    'legend': [
                        { 'type':'line', 'color':'#e99dae', 'text':"Interstate Highway" },
                        { 'type':'line', 'color':'#f2baa8', 'text':"State Highway" },
                        { 'type':'line', 'color':'#e1c6db', 'text':"State Boundary" },
                        { 'type':'square', 'color':'#bdd3d3', 'text':"Water" },
                    ],
                },
                // add more layer objects to this section
            ]
        },
        // add more sections, with a 'section' title and a list of layers:[]
        {
            'title': "Second Section",
            layers: [ ... add layers to this accordion section ... ]
        },
    ];

    new L.Control.AccordionLegend({
        content: LAYERS_AND_LEGENDS,
    }).addTo(map);

A more formal description is as follows:

  • The structure consists of a list of Section objects. These will be drawn in the sequence given.
  • Each Section consists of the following:
    • A title attribute, this being the title of the section. This is used as an internal reference and therefore must be unique among Sections.
    • A layers attribute, this being a list of Layer objects.
  • Each Layer object is structured as follows:
    • A title attribute, which is the title displayed in the legend. This is used as an internal reference and therefore must be unique among other Layers.
    • A layer attribute, which is the L.TileLayer instance to be managed by this legend entry.
    • A legend attribute, this being the list of Legend objects to be displayed for this layer.
  • The list of Legend objects, are as follows:
    • A type attribute, which affects the legend swatch generated.
      • square -- The legend swatch will be a square.
      • circle -- The legend swatch will be a circle.
      • line -- The legend swatch will be a line.
      • image -- The legend swatch will be an image. The image will be sized to 1em X 1em, so should be about that size (15px or so) in order to look good.
    • A color attribute, this being the color of the swatch for this classification. This is only effective for square, line, and circle types.
    • A url attribute, this being the URL of the image. This is only effective for image types.
    • A text attribute, this being the text to display for this classification.
    • The Legend may be an empty list, in order to generate no visible legend.

Inline Legends (Single Swatch Next to Checkbox)

If a Layer has only one Legend entry, and this entry has an empty text attribute, then the legend will be treated differently.

Instead of creating a collapsing legend section underneath the opacity slider, containing one swatch and no text, it will create a smaller "inline" legend, a single swatch between the checkbox and the layer title.

Example:

    // one legend classification with no text
    // the red circle will appear next to the checkbox for a more compact layout
    {
        'title': "Sample Stations",
        'layer': L.tileLayer('https://{s}.somedomain.org/{z}/{x}/{y}.png', {}),
        'legend': [
            { 'type':'circle', 'color':'#FF0000', 'text':"" },
        ],
    },

If you really do want your single unlabeled swatch to appear in a section beneath the opacity slider, simply make the text attribute not blank, e.g. one single space.

Other Methods and Tricks

toggleLayer(layertitle,onoff)

Toggle the specified layer to the specified on/off status.

setOpacity(layertitle,opacity)

Adjust the opacity of the specified layer. Opacity values are from 0.0 to 1.0 per standard Leaflet behavior.

collapseUI()

Collapse the control, hiding the panel of sections and layers.

expandUI(sectiontitle)

Expand the control, showing the panel of sections and layers. As a convenience, you may optionally specify the title of a Section and this will call expandSection(sectiontitle) for you.

toggleUI()

Calls either expandUI() or collapseUI() to show/hide the panel.

expandSection(sectiontitle)

Within the panel, expand the given section to show its layers. Since only one sections stays open at a time, this would implicitly close other sections.

collapseSection(sectiontitle)

Within the panel, collapse the given section.

toggleSection(sectiontitle)

Within the panel, expand the given section (f it's collapsed) or collapse the given section (if it's expanded).

listLayersStates()

Returns an object keyed by layertitle with values being that layer's opacity. This would be used to "peek" at the opacity and on/off state of all layers, in case you wanted to construct some additional control or usage. An example would be some external need to loop over all visible layers, or to construct a list of layers-and-opacity pairings for serializing the map state.