From 3dbebea530945992a7bdabb90ddbfcc4c0027199 Mon Sep 17 00:00:00 2001 From: jonataswalker Date: Mon, 23 May 2016 14:56:34 -0300 Subject: [PATCH 1/2] Add event --- src/js/constants.js | 4 ++++ src/js/internal.js | 12 +++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/js/constants.js b/src/js/constants.js index d0e7a59..99b7adb 100644 --- a/src/js/constants.js +++ b/src/js/constants.js @@ -2,6 +2,10 @@ import * as vars from '../../config/vars.json'; import utils from './utils'; export const eventType = { + /** + * Triggered before context menu is openned. + */ + BEFOREOPEN: 'beforeopen', /** * Triggered when context menu is openned. */ diff --git a/src/js/internal.js b/src/js/internal.js index 1d75b56..7eea533 100644 --- a/src/js/internal.js +++ b/src/js/internal.js @@ -110,7 +110,7 @@ export class Internal { this.Base.dispatchEvent({ type: constants.eventType.OPEN, pixel: pixel, - coordinate: coordinate, + coordinate: coordinate }); } @@ -130,15 +130,21 @@ export class Internal { map = this.map, canvas = map.getTargetElement(), menu = function(evt) { + this_.coordinate_clicked = map.getEventCoordinate(evt); + this_.pixel_clicked = map.getEventPixel(evt); + this_.Base.dispatchEvent({ + type: constants.eventType.BEFOREOPEN, + pixel: this_.pixel_clicked, + coordinate: this_.coordinate_clicked + }); + if (this_.Base.disabled) { return; } evt.stopPropagation(); evt.preventDefault(); - this_.coordinate_clicked = map.getEventCoordinate(evt); - this_.pixel_clicked = map.getEventPixel(evt); this_.openMenu(this_.pixel_clicked, this_.coordinate_clicked); //one-time fire From 582025d19c5e55f195f5b7060eb98923e85790bc Mon Sep 17 00:00:00 2001 From: jonataswalker Date: Mon, 23 May 2016 15:02:39 -0300 Subject: [PATCH 2/2] Add beforeopen event --- README.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a47f77f..939d2f9 100644 --- a/README.md +++ b/README.md @@ -159,13 +159,30 @@ Get an array of default items. ## Events -#### Listen and make some changes before context menu opens +#### If you want to disable this plugin under certain circumstances, listen to `beforeopen` + +```javascript +contextmenu.on('beforeopen', function(evt){ + var feature = map.forEachFeatureAtPixel(evt.pixel, function(ft, l){ + return ft; + }); + + if (feature) { // open only on features + contextmenu.disable(); + } else { + contextmenu.enable(); + } +}); +``` + +#### Listen and make some changes when context menu opens ```javascript contextmenu.on('open', function(evt){ var feature = map.forEachFeatureAtPixel(evt.pixel, function(ft, l){ return ft; }); + if (feature) { // add some other items to the menu }