Package detail

ol-contextmenu

jonataswalker29.8kMIT5.5.0

Custom Context Menu for Openlayers

readme

OpenLayers Custom Context Menu

Build Status npm version npm license

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
});

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

5.5.0

  • up deps #287
  • feat: OpenLayers 10 support #286
  • feat: OpenLayers 10 support #285

5.4.1

8 March 2024

5.4.0

8 March 2024

  • npm publish #279
  • feat: OpenLayers 9 support #278
  • feat: add support for OpenLayers 9 #277
  • chore: bump all minor/patch dependencies 6a3b45e
  • chore: update to vite@v5 0750f93
  • chore: update to husky@v9 db4cb44

5.3.0

22 September 2023

  • sync lock #274
  • Bump 5.3.0 #273
  • Avoid bundling ol imports into the final build #272
  • Update to OpenLayers 8.1.0 #271
  • add node 20 to list of approved engines. #269
  • Added ./* to package exports field #264
  • wip: update deps & better types export 22a1869
  • Update OpenLayers to 8.1.0 662c3ce
  • fix: tests b349562

5.2.1

31 March 2023

  • fix: single emitter instance #262
  • Remove browser field from package.json #260
  • remove field a2d2f1a
  • bump version 0e13324

5.1.5

11 March 2023

5.1.2

13 March 2023

  • Remove browser field from package.json #260
  • bump 5.1.5 #259
  • fix: container positioning #258
  • fix: remove listeners on window unload #257
  • Do not prevent open event #256
  • Added style attribute to exports #255
  • fix: submenu offset #247
  • update deps #243
  • fix: let event happens de51558
  • remove field a2d2f1a
  • fix: tests 87eabdd

5.1.1

12 November 2022

  • Fixed two issues and re-added 'open' event #238
  • Wait for BEFOREOPEN to complete before openmenu() #231
  • Fixed event types and moved emitter c602f66
  • Improve ContextMenuEvent to extend MapBrowserEvent 8d7d1ff

5.1.0

24 September 2022

  • feat: add webpack example #230
  • Fix submenu remains clickable while hovering #222
  • bump new version c520a09

5.0.3

22 September 2022

  • fix: add IIFE version & update fiddle #228
  • Create FUNDING.yml ed222ec
  • Update test.yml 606bb5e

5.0.0

20 September 2022

4.1.0

15 August 2020

  • Release new version #195
  • Update dependencies #190
  • Close context menu on map move #192
  • Internal: Use correct this_ when checking for open context menu #180
  • Release dba60c0
  • Remove listener 86804cc
  • Internal: Use corrent this_ when checking for open context menu dce5c15

4.0.1

27 April 2020

  • Update Deps and publish #183
  • Stopped click events propagating to map #178
  • Changed click event to pointerdown, stopped propagation 33fe865
  • Stopped click events propogating to map b5b5b58
  • Context Menu: Only listen for pointerdown events when menu is open a19b9dd

4.0.0

20 November 2019

  • Release v4.0.0 #173
  • Update Examples and some editor configs #172
  • Ensure newly created menu is hidden. #164
  • Update Deps #171
  • Update version in lock file. 2fa876c

3.3.2

27 June 2019

  • Update dependencies #167
  • setItemListener fires only first time #166
  • dist bfa23fc
  • Delete ol-contextmenu-debug.js 540197c
  • Delete ol-contextmenu.css 814cee2

3.3.1

27 February 2019

  • Release v3.3.1 #163
  • Fix of the #151 issue, close context menu #156
  • Created a flag to block double click on context menu items #153
  • Change Travis dist #162
  • updated example code #160
  • Update deps and change build process #161
  • Update deps #152
  • Remove xvfb 8f1dce9
  • Run xvfb 2fa2924

3.3.0

28 July 2018

3.2.0

5 April 2018

  • Release v3.2.0 #137
  • Remove openlayers and add ol #136
  • Update deps #135

3.1.0

18 November 2017

  • Release v3.1.0 #125
  • Bring back jsDelivr #124
  • Add countItems method #123

3.0.0

18 November 2017

2.5.0

24 February 2017

  • Release v2.5.0 #113
  • Fix examples #111
  • Use map viewport instead of map element #110
  • Change deprecated ol.animation #109
  • Update dependencies #108

2.4.1

27 January 2017

  • Fix bad english isOpened #104
  • Update dependencies #103
  • Change build process #98
  • Fix Travis/Yarn #93
  • rollup@0.36.4 breaks build 🚨 #86
  • Add Yarn to Travis CI 44805e3
  • Fix bad english isOpened ee580c7
  • Add badges 82e918b

2.4.0

17 November 2016

  • Release v2.4.0 #85
  • eslint@3.10.1 breaks build 🚨 #82
  • Added updatePosition #81
  • Try Travis trusty-image #77
  • Fix class instatiation 8c3c4f9
  • Updated source 64db4df
  • Removed erroneous update to built file. fa527b6

2.3.0

6 October 2016

  • Release v2.3.0 #76
  • Fix submenu position #75
  • Add isOpened method #74
  • Add listening event type #73
  • Update dependencies and satisfy ESlint #72
  • eslint-config-jwalker@1.11.0 breaks build 🚨 #69
  • Update rollup to version 0.36.0 🚀 #63
  • Update rollup-plugin-buble to version 0.14.0 🚀 #62
  • Update rollup to version 0.35.0 🚀 #61
  • Tougher eslint rules #60
  • Update all dependencies 🌴 #58
  • Make ESlint happy 0713219
  • Add new target and help a829faa
  • Add timestamp to build && lint e587888

2.2.4

1 September 2016

  • Fix removing ol.control.Control #57

2.2.3

17 August 2016

  • Release v2.2.3 #53
  • Remove internal pub/sub #52

2.2.2

29 July 2016

  • Release v2.2.2 #46
  • Update dependencies and example #45
  • Add some more tests #41
  • Remove unused files 18e76e4
  • Update example cf89bc6
  • Update dependencies 27fdadb

2.2.1

30 June 2016

  • Fix menu placement with no item #40

2.2.0

22 June 2016

  • Release v2.2.0 #37
  • Add API close method #36
  • Add some more tests #34
  • Add API close method adb2f1f
  • Remove unwanted dir 332ae64

2.1.0

23 May 2016

  • Release v2.1.0 #32
  • Add beforeopen event #31
  • Add enable/disable methods #30
  • Add beforeopen event 582025d
  • Add event 3dbebea

2.0.1

23 May 2016

  • Release v2.0.1 #29
  • Show how to use item CSS class #28
  • Fix duplicate icon class #26
  • Update docs #27

2.0.0

19 May 2016

  • Release v2.0.0 #24
  • Add node_modules to Travis cache #23
  • Update Makefile #22
  • Add tests #21
  • Add build badge b9317b8

1.2.0

31 March 2016

1.1.0

15 February 2016

  • Release v1.1.0 #12
  • Changed to show recent events added #11
  • Add Travis CI #10
  • Add 'getDefaultItems' method #9
  • Added open and close events #8
  • Propagating custom data to the callback handler #7
  • Removed duplicate line #6
  • Add version header in JS files #5
  • Declare variables as simple ones #4
  • New (hopefully the last one) build process #3
  • Fixes CSS classes functions #2
  • Adjust example 4584485
  • Added and events fb3433b
  • javascript fixed according to comments 4f86d95

1.0.1

30 October 2015

1.0.0

29 October 2015

v0.1

27 August 2015