Skip to content

Commit

Permalink
Merge pull request #31 from jonataswalker/add-beforeopen-event
Browse files Browse the repository at this point in the history
Add `beforeopen` event
  • Loading branch information
jonataswalker committed May 23, 2016
2 parents e476aa9 + 582025d commit 5abad41
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions src/js/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
12 changes: 9 additions & 3 deletions src/js/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class Internal {
this.Base.dispatchEvent({
type: constants.eventType.OPEN,
pixel: pixel,
coordinate: coordinate,
coordinate: coordinate
});
}

Expand All @@ -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
Expand Down

0 comments on commit 5abad41

Please sign in to comment.