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

ol-contextmenu

v5.5.0

Published

Custom Context Menu for Openlayers

Downloads

31,105

Readme

OpenLayers Custom Context Menu

A contextmenu extension for OpenLayers. Requires OpenLayers v7.0.0 or higher.

contextmenu anim

Demo

JSFiddle CodeSandbox

How to use it?

NPM

npm install ol-contextmenu

CDN Hosted - jsDelivr

Load CSS and Javascript:

<link href="https://cdn.jsdelivr.net/npm/ol-contextmenu@latest/dist/ol-contextmenu.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/ol-contextmenu"></script>
CDN Hosted - UNPKG

Load CSS and Javascript:

<link href="https://unpkg.com/ol-contextmenu/dist/ol-contextmenu.css" rel="stylesheet">
<script src="https://unpkg.com/ol-contextmenu"></script>
Self hosted

Download latest release and (obviously) load CSS and Javascript.

Instantiate with some options and add the Control
const contextmenu = new ContextMenu({
    width: 170,
    defaultItems: true, // defaultItems are (for now) Zoom In/Zoom Out
    items: [
        {
            text: 'Center map here',
            classname: 'some-style-class', // add some CSS rules
            callback: center, // `center` is your callback function
        },
        {
            text: 'Add a Marker',
            classname: 'some-style-class', // you can add this icon with a CSS class
            // instead of `icon` property (see next line)
            icon: 'img/marker.png', // this can be relative or absolute
            callback: marker,
        },
        '-', // this is a separator
    ],
});
map.addControl(contextmenu);
You can add a (nested) submenu like this:

If you provide items {Array} a submenu will be created as a child of the current item.

const all_items = [
    {
        text: 'Some Actions',
        items: [
            // <== this is a submenu
            {
                text: 'Action 1',
                callback: action,
            },
            {
                text: 'Other action',
                callback: action2,
            },
        ],
    },
    {
        text: 'Add a Marker',
        icon: 'img/marker.png',
        callback: marker,
    },
    '-', // this is a separator
];
Would you like to propagate custom data to the callback handler?
const removeMarker = function (obj) {
    vectorLayer.getSource().removeFeature(obj.data.marker);
};
const removeMarkerItem = {
    text: 'Remove this Marker',
    icon: 'img/marker.png',
    callback: removeMarker,
};

let restore = false;
contextmenu.on('open', function (evt) {
    const feature = map.forEachFeatureAtPixel(evt.pixel, function (ft, l) {
        return ft;
    });
    if (feature) {
        contextmenu.clear();
        removeMarkerItem.data = { marker: feature };
        contextmenu.push(removeMarkerItem);
        restore = true;
    } else if (restore) {
        contextmenu.clear();
        contextmenu.extend(contextmenu_items);
        contextmenu.extend(contextmenu.getDefaultItems());
        restore = false;
    }
});

API

Constructor

new ContextMenu(options)

options is an object with the following possible properties:
  • eventType: contextmenu; The listening event type (You could use 'click', 'dblclick')
  • defaultItems: true; Whether the default items (which are: Zoom In/Out) are enabled
  • width: 150; The menu's width
  • items: []; An array of object|string

Methods

contextmenu.clear()

Remove all elements from the menu.

contextmenu.close()

Close the menu programmatically.

contextmenu.extend(arr)

@param {Array} arr

Add items to the menu. This pushes each item in the provided array to the end of the menu.

Example:

const contextmenu = new ContextMenu();
map.addControl(contextmenu);

const add_later = [
    '-', // this is a separator
    {
        text: 'Add a Marker',
        icon: 'img/marker.png',
        callback: marker,
    },
];
contextmenu.extend(add_later);

contextmenu.push(item)

@param {Object|String} item

Insert the provided item at the end of the menu.

contextmenu.shift()

Remove the first item of the menu.

contextmenu.pop()

Remove the last item of the menu.

contextmenu.getDefaultItems()

Get an array of default items.

contextmenu.isOpen()

Whether the menu is open.

contextmenu.updatePosition(pixel)

@param {Array} pixel

Update menu's position.

Events

If you want to disable this plugin under certain circumstances, listen to beforeopen

contextmenu.on('beforeopen', function (evt) {
    const feature = map.forEachFeatureAtPixel(evt.pixel, function (ft, l) {
        return ft;
    });

    if (feature) {
        // open only on features
        contextmenu.enable();
    } else {
        contextmenu.disable();
    }
});

Listen and make some changes when context menu opens

contextmenu.on('open', function (evt) {
    const feature = map.forEachFeatureAtPixel(evt.pixel, function (ft, l) {
        return ft;
    });

    if (feature) {
        // add some other items to the menu
    }
});

Any action when context menu gets closed?

contextmenu.on('close', function (evt) {
    // it's upon you
});