Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add beforeopen event #31

Merged
merged 2 commits into from
May 23, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions src/js/constants.js
Original file line number Diff line number Diff line change
@@ -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.
*/
12 changes: 9 additions & 3 deletions src/js/internal.js
Original file line number Diff line number Diff line change
@@ -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