1
1
# OpenLayers Custom Context Menu
2
2
3
3
<p align =" center " >
4
- <a href =" https://travis-ci.org/jonataswalker/ol-contextmenu " >
5
- <img src="https://travis-ci.org/jonataswalker/ol-contextmenu.svg?branch=master" alt="build status">
6
- </a >
7
- <a href =" https://www.npmjs.com/package/ol-contextmenu " >
8
- <img src="https://img.shields.io/npm/v/ol-contextmenu.svg"
9
- alt="npm version">
10
- </a >
11
- <a href =" https://github.com/jonataswalker/ol-contextmenu/blob/master/LICENSE " >
12
- <img src="https://img.shields.io/npm/l/ol-contextmenu.svg"
13
- alt="license">
14
- </a >
15
- <a href =" https://david-dm.org/jonataswalker/ol-contextmenu " >
16
- <img src="https://david-dm.org/jonataswalker/ol-contextmenu/status.svg"
17
- alt="dependency status">
18
- </a >
19
- <a href =" https://david-dm.org/jonataswalker/ol-contextmenu " >
20
- <img src="https://david-dm.org/jonataswalker/ol-contextmenu/dev-status.svg" alt="devDependency status">
21
- </a >
4
+ <a href="https://github.com/jonataswalker/ol-contextmenu/actions/workflows/test.yml">
5
+ <img src="https://github.com/jonataswalker/ol-contextmenu/actions/workflows/test.yml/badge.svg?branch=master" alt="Build Status">
6
+ </a>
7
+ <a href="https://www.npmjs.com/package/ol-contextmenu">
8
+ <img src="https://img.shields.io/npm/v/ol-contextmenu.svg" alt="npm version">
9
+ </a>
10
+ <a href="https://img.shields.io/npm/dm/ol-contextmenu">
11
+ <img alt="npm" src="https://img.shields.io/npm/dm/ol-contextmenu">
12
+ </a>
13
+ <a href="https://github.com/jonataswalker/ol-contextmenu/blob/master/LICENSE">
14
+ <img src="https://img.shields.io/npm/l/ol-contextmenu.svg" alt="license">
15
+ </a>
22
16
</p >
23
17
24
18
A ` contextmenu ` extension for [ OpenLayers] ( http://openlayers.org/ ) . ** Requires** OpenLayers ** v3.11.0** or higher.
25
19
26
20
![ contextmenu anim] ( https://raw.githubusercontent.com/jonataswalker/ol-contextmenu/screenshot/images/anim.gif )
27
21
28
22
## Demo
23
+
29
24
You can see [ here a demo] ( http://rawgit.com/jonataswalker/ol-contextmenu/master/examples/contextmenu.html ) or [ JSFiddle] ( https://jsfiddle.net/jonataswalker/ooxs1w5d/ ) .
30
25
31
26
## How to use it?
27
+
32
28
##### NPM
29
+
33
30
` npm install ol-contextmenu `
34
31
35
32
##### CDN Hosted - [ jsDelivr] ( https://www.jsdelivr.com/package/npm/ol-contextmenu )
33
+
36
34
Load CSS and Javascript:
35
+
37
36
``` HTML
38
- <link href =" https://cdn.jsdelivr.net/npm/ol-contextmenu@latest/dist/ol-contextmenu.min. css" rel =" stylesheet" >
37
+ <link href =" https://cdn.jsdelivr.net/npm/ol-contextmenu@latest/dist/ol-contextmenu.css" rel =" stylesheet" >
39
38
<script src =" https://cdn.jsdelivr.net/npm/ol-contextmenu" ></script >
40
39
```
41
40
42
41
##### CDN Hosted - UNPKG
42
+
43
43
Load CSS and Javascript:
44
+
44
45
``` HTML
45
- <link href =" https://unpkg.com/ol-contextmenu/dist/ol-contextmenu.min. css" rel =" stylesheet" >
46
+ <link href =" https://unpkg.com/ol-contextmenu/dist/ol-contextmenu.css" rel =" stylesheet" >
46
47
<script src =" https://unpkg.com/ol-contextmenu" ></script >
47
48
```
49
+
48
50
##### Self hosted
51
+
49
52
Download [ latest release] ( https://github.com/jonataswalker/ol-contextmenu/releases/latest ) and (obviously) load CSS and Javascript.
50
53
51
54
##### Instantiate with some options and add the Control
55
+
52
56
``` javascript
53
57
var contextmenu = new ContextMenu ({
54
- width: 170 ,
55
- defaultItems: true , // defaultItems are (for now) Zoom In/Zoom Out
56
- items: [
57
- {
58
- text: ' Center map here' ,
59
- classname: ' some-style-class' , // add some CSS rules
60
- callback: center // `center` is your callback function
61
- },
62
- {
63
- text: ' Add a Marker' ,
64
- classname: ' some-style-class' , // you can add this icon with a CSS class
65
- // instead of `icon` property (see next line)
66
- icon: ' img/marker.png' , // this can be relative or absolute
67
- callback: marker
68
- },
69
- ' -' // this is a separator
70
- ]
58
+ width: 170 ,
59
+ defaultItems: true , // defaultItems are (for now) Zoom In/Zoom Out
60
+ items: [
61
+ {
62
+ text: ' Center map here' ,
63
+ classname: ' some-style-class' , // add some CSS rules
64
+ callback: center, // `center` is your callback function
65
+ },
66
+ {
67
+ text: ' Add a Marker' ,
68
+ classname: ' some-style-class' , // you can add this icon with a CSS class
69
+ // instead of `icon` property (see next line)
70
+ icon: ' img/marker.png' , // this can be relative or absolute
71
+ callback: marker,
72
+ },
73
+ ' -' , // this is a separator
74
+ ],
71
75
});
72
76
map .addControl (contextmenu);
73
77
```
@@ -78,55 +82,57 @@ If you provide `items {Array}` a submenu will be created as a child of the curre
78
82
79
83
``` javascript
80
84
var all_items = [
81
- {
82
- text: ' Some Actions' ,
83
- items: [ // <== this is a submenu
84
- {
85
- text: ' Action 1' ,
86
- callback: action
87
- },
88
- {
89
- text: ' Other action' ,
90
- callback: action2
91
- }
92
- ]
93
- },
94
- {
95
- text: ' Add a Marker' ,
96
- icon: ' img/marker.png' ,
97
- callback: marker
98
- },
99
- ' -' // this is a separator
85
+ {
86
+ text: ' Some Actions' ,
87
+ items: [
88
+ // <== this is a submenu
89
+ {
90
+ text: ' Action 1' ,
91
+ callback: action,
92
+ },
93
+ {
94
+ text: ' Other action' ,
95
+ callback: action2,
96
+ },
97
+ ],
98
+ },
99
+ {
100
+ text: ' Add a Marker' ,
101
+ icon: ' img/marker.png' ,
102
+ callback: marker,
103
+ },
104
+ ' -' , // this is a separator
100
105
];
101
106
```
102
107
103
108
##### Would you like to propagate custom data to the callback handler?
109
+
104
110
``` javascript
105
111
var removeMarker = function (obj ) {
106
- vectorLayer .getSource ().removeFeature (obj .data .marker );
112
+ vectorLayer .getSource ().removeFeature (obj .data .marker );
107
113
};
108
114
var removeMarkerItem = {
109
- text: ' Remove this Marker' ,
110
- icon: ' img/marker.png' ,
111
- callback: removeMarker
115
+ text: ' Remove this Marker' ,
116
+ icon: ' img/marker.png' ,
117
+ callback: removeMarker,
112
118
};
113
119
114
120
var restore = false ;
115
121
contextmenu .on (' open' , function (evt ) {
116
- var feature = map .forEachFeatureAtPixel (evt .pixel , function (ft , l ) {
117
- return ft;
118
- });
119
- if (feature) {
120
- contextmenu .clear ();
121
- removeMarkerItem .data = { marker: feature };
122
- contextmenu .push (removeMarkerItem);
123
- restore = true ;
124
- } else if (restore) {
125
- contextmenu .clear ();
126
- contextmenu .extend (contextmenu_items);
127
- contextmenu .extend (contextmenu .getDefaultItems ());
128
- restore = false ;
129
- }
122
+ var feature = map .forEachFeatureAtPixel (evt .pixel , function (ft , l ) {
123
+ return ft;
124
+ });
125
+ if (feature) {
126
+ contextmenu .clear ();
127
+ removeMarkerItem .data = { marker: feature };
128
+ contextmenu .push (removeMarkerItem);
129
+ restore = true ;
130
+ } else if (restore) {
131
+ contextmenu .clear ();
132
+ contextmenu .extend (contextmenu_items);
133
+ contextmenu .extend (contextmenu .getDefaultItems ());
134
+ restore = false ;
135
+ }
130
136
});
131
137
```
132
138
@@ -137,10 +143,11 @@ contextmenu.on('open', function (evt) {
137
143
#### ` new ContextMenu(options) `
138
144
139
145
###### ` options ` is an object with the following possible properties:
140
- * ` eventType ` : ` contextmenu ` ; The listening event type (You could use ` 'click' ` , ` 'dblclick' ` )
141
- * ` defaultItems ` : ` true ` ; Whether the default items (which are: Zoom In/Out) are enabled
142
- * ` width ` : ` 150 ` ; The menu's width
143
- * ` items ` : ` [] ` ; An array of object|string
146
+
147
+ - ` eventType ` : ` contextmenu ` ; The listening event type (You could use ` 'click' ` , ` 'dblclick' ` )
148
+ - ` defaultItems ` : ` true ` ; Whether the default items (which are: Zoom In/Out) are enabled
149
+ - ` width ` : ` 150 ` ; The menu's width
150
+ - ` items ` : ` [] ` ; An array of object|string
144
151
145
152
## Methods
146
153
@@ -159,17 +166,18 @@ Close the menu programmatically.
159
166
Add items to the menu. This pushes each item in the provided array to the end of the menu.
160
167
161
168
Example:
169
+
162
170
``` js
163
171
var contextmenu = new ContextMenu ();
164
172
map .addControl (contextmenu);
165
173
166
174
var add_later = [
167
- ' -' , // this is a separator
168
- {
169
- text: ' Add a Marker' ,
170
- icon: ' img/marker.png' ,
171
- callback: marker
172
- }
175
+ ' -' , // this is a separator
176
+ {
177
+ text: ' Add a Marker' ,
178
+ icon: ' img/marker.png' ,
179
+ callback: marker,
180
+ },
173
181
];
174
182
contextmenu .extend (add_later);
175
183
```
@@ -202,43 +210,43 @@ Whether the menu is open.
202
210
203
211
Update menu's position.
204
212
205
-
206
213
## Events
207
214
208
215
#### If you want to disable this plugin under certain circumstances, listen to ` beforeopen `
209
216
210
217
``` javascript
211
218
contextmenu .on (' beforeopen' , function (evt ) {
212
- var feature = map .forEachFeatureAtPixel (evt .pixel , function (ft , l ) {
213
- return ft;
214
- });
215
-
216
- if (feature) { // open only on features
217
- contextmenu .enable ();
218
- } else {
219
- contextmenu .disable ();
220
- }
219
+ var feature = map .forEachFeatureAtPixel (evt .pixel , function (ft , l ) {
220
+ return ft;
221
+ });
222
+
223
+ if (feature) {
224
+ // open only on features
225
+ contextmenu .enable ();
226
+ } else {
227
+ contextmenu .disable ();
228
+ }
221
229
});
222
230
```
223
231
224
232
#### Listen and make some changes when context menu opens
225
233
226
234
``` javascript
227
- contextmenu .on (' open' , function (evt ){
228
- var feature = map .forEachFeatureAtPixel (evt .pixel , function (ft , l ) {
229
- return ft;
230
- });
231
-
232
- if (feature) {
233
- // add some other items to the menu
234
- }
235
+ contextmenu .on (' open' , function (evt ) {
236
+ var feature = map .forEachFeatureAtPixel (evt .pixel , function (ft , l ) {
237
+ return ft;
238
+ });
239
+
240
+ if (feature) {
241
+ // add some other items to the menu
242
+ }
235
243
});
236
244
```
237
245
238
246
#### Any action when context menu gets closed?
239
247
240
248
``` javascript
241
249
contextmenu .on (' close' , function (evt ) {
242
- // it's upon you
250
+ // it's upon you
243
251
});
244
252
```
0 commit comments