ol-contextmenu 4.1.0 → 5.0.1-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ol-contextmenu.css +1 -109
- package/dist/ol-contextmenu.js +313 -6
- package/dist/ol-contextmenu.umd.cjs +1 -0
- package/package.json +64 -53
- package/dist/ol-contextmenu-debug.js +0 -821
- package/dist/ol-contextmenu.min.css +0 -7
|
@@ -1,821 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
/*!
|
|
3
|
-
* ol-contextmenu - v4.1.0
|
|
4
|
-
* https://github.com/jonataswalker/ol-contextmenu
|
|
5
|
-
* Built: Sat Aug 15 2020 10:43:26 GMT-0300 (Brasilia Standard Time)
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
(function (global, factory) {
|
|
9
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('ol/control/Control')) :
|
|
10
|
-
typeof define === 'function' && define.amd ? define(['ol/control/Control'], factory) :
|
|
11
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.ContextMenu = factory(global.ol.control.Control));
|
|
12
|
-
}(this, (function (Control) { 'use strict';
|
|
13
|
-
|
|
14
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
15
|
-
|
|
16
|
-
var Control__default = /*#__PURE__*/_interopDefaultLegacy(Control);
|
|
17
|
-
|
|
18
|
-
var namespace = 'ol-ctx-menu';
|
|
19
|
-
|
|
20
|
-
var cssVars = {
|
|
21
|
-
namespace: namespace,
|
|
22
|
-
container: (namespace + "-container"),
|
|
23
|
-
separator: (namespace + "-separator"),
|
|
24
|
-
submenu: (namespace + "-submenu"),
|
|
25
|
-
hidden: (namespace + "-hidden"),
|
|
26
|
-
icon: (namespace + "-icon"),
|
|
27
|
-
zoomIn: (namespace + "-zoom-in"),
|
|
28
|
-
zoomOut: (namespace + "-zoom-out"),
|
|
29
|
-
unselectable: 'ol-unselectable',
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
var CSS_VARS = cssVars;
|
|
33
|
-
|
|
34
|
-
var EVENT_TYPE = {
|
|
35
|
-
/**
|
|
36
|
-
* Triggered before context menu is open.
|
|
37
|
-
*/
|
|
38
|
-
BEFOREOPEN: 'beforeopen',
|
|
39
|
-
/**
|
|
40
|
-
* Triggered when context menu is open.
|
|
41
|
-
*/
|
|
42
|
-
OPEN: 'open',
|
|
43
|
-
/**
|
|
44
|
-
* Triggered when context menu is closed.
|
|
45
|
-
*/
|
|
46
|
-
CLOSE: 'close',
|
|
47
|
-
/**
|
|
48
|
-
* Internal. Triggered when a menu entry is added.
|
|
49
|
-
*/
|
|
50
|
-
ADD_MENU_ENTRY: 'add-menu-entry',
|
|
51
|
-
/**
|
|
52
|
-
* Internal.
|
|
53
|
-
*/
|
|
54
|
-
CONTEXTMENU: 'contextmenu',
|
|
55
|
-
/**
|
|
56
|
-
* Internal.
|
|
57
|
-
*/
|
|
58
|
-
HOVER: 'mouseover',
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
var DEFAULT_OPTIONS = {
|
|
62
|
-
width: 150,
|
|
63
|
-
scrollAt: 4,
|
|
64
|
-
eventType: EVENT_TYPE.CONTEXTMENU,
|
|
65
|
-
defaultItems: true,
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
var DEFAULT_ITEMS = [
|
|
69
|
-
{
|
|
70
|
-
text: 'Zoom In',
|
|
71
|
-
classname: ((cssVars.zoomIn) + " " + (cssVars.icon)),
|
|
72
|
-
callback: function (obj, map) {
|
|
73
|
-
var view = map.getView();
|
|
74
|
-
view.animate({
|
|
75
|
-
zoom: +view.getZoom() + 1,
|
|
76
|
-
duration: 700,
|
|
77
|
-
center: obj.coordinate,
|
|
78
|
-
});
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
{
|
|
82
|
-
text: 'Zoom Out',
|
|
83
|
-
classname: ((cssVars.zoomOut) + " " + (cssVars.icon)),
|
|
84
|
-
callback: function (obj, map) {
|
|
85
|
-
var view = map.getView();
|
|
86
|
-
view.animate({
|
|
87
|
-
zoom: +view.getZoom() - 1,
|
|
88
|
-
duration: 700,
|
|
89
|
-
center: obj.coordinate,
|
|
90
|
-
});
|
|
91
|
-
},
|
|
92
|
-
} ];
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Overwrites obj1's values with obj2's and adds
|
|
96
|
-
* obj2's if non existent in obj1
|
|
97
|
-
* @returns obj3 a new object based on obj1 and obj2
|
|
98
|
-
*/
|
|
99
|
-
function mergeOptions(obj1, obj2) {
|
|
100
|
-
var obj3 = {};
|
|
101
|
-
for (var attr1 in obj1) { obj3[attr1] = obj1[attr1]; }
|
|
102
|
-
for (var attr2 in obj2) { obj3[attr2] = obj2[attr2]; }
|
|
103
|
-
return obj3;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function assert(condition, message) {
|
|
107
|
-
if ( message === void 0 ) message = 'Assertion failed';
|
|
108
|
-
|
|
109
|
-
if (!condition) {
|
|
110
|
-
if (typeof Error !== 'undefined') { throw new Error(message); }
|
|
111
|
-
throw message; // Fallback
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Does str contain test?
|
|
117
|
-
* @param {String} str_test
|
|
118
|
-
* @param {String} str
|
|
119
|
-
* @returns Boolean
|
|
120
|
-
*/
|
|
121
|
-
function contains(str_test, str) {
|
|
122
|
-
return !!~str.indexOf(str_test);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function getUniqueId() {
|
|
126
|
-
return '_' + Math.random().toString(36).substr(2, 9);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function isDefAndNotNull(val) {
|
|
130
|
-
// Note that undefined == null.
|
|
131
|
-
return val != null; // eslint-disable-line no-eq-null
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function isNumeric(str) {
|
|
135
|
-
return /^\d+$/.test(str);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* @param {Element|Array<Element>} element DOM node or array of nodes.
|
|
140
|
-
* @param {String|Array<String>} classname Class or array of classes.
|
|
141
|
-
* For example: 'class1 class2' or ['class1', 'class2']
|
|
142
|
-
* @param {Number|undefined} timeout Timeout to remove a class.
|
|
143
|
-
*/
|
|
144
|
-
function addClass(element, classname, timeout) {
|
|
145
|
-
if (Array.isArray(element)) {
|
|
146
|
-
element.forEach(function (each) { return addClass(each, classname); });
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
var array = Array.isArray(classname) ? classname : classname.split(/\s+/);
|
|
151
|
-
var i = array.length;
|
|
152
|
-
|
|
153
|
-
while (i--) {
|
|
154
|
-
if (!hasClass(element, array[i])) {
|
|
155
|
-
_addClass(element, array[i], timeout);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* @param {Element|Array<Element>} element DOM node or array of nodes.
|
|
162
|
-
* @param {String|Array<String>} classname Class or array of classes.
|
|
163
|
-
* For example: 'class1 class2' or ['class1', 'class2']
|
|
164
|
-
* @param {Number|undefined} timeout Timeout to add a class.
|
|
165
|
-
*/
|
|
166
|
-
function removeClass(element, classname, timeout) {
|
|
167
|
-
if (Array.isArray(element)) {
|
|
168
|
-
element.forEach(function (each) { return removeClass(each, classname, timeout); });
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
var array = Array.isArray(classname) ? classname : classname.split(/\s+/);
|
|
173
|
-
var i = array.length;
|
|
174
|
-
|
|
175
|
-
while (i--) {
|
|
176
|
-
if (hasClass(element, array[i])) {
|
|
177
|
-
_removeClass(element, array[i], timeout);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* @param {Element} element DOM node.
|
|
184
|
-
* @param {String} classname Classname.
|
|
185
|
-
* @return {Boolean}
|
|
186
|
-
*/
|
|
187
|
-
function hasClass(element, c) {
|
|
188
|
-
// use native if available
|
|
189
|
-
return element.classList
|
|
190
|
-
? element.classList.contains(c)
|
|
191
|
-
: classRegex(c).test(element.className);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Abstraction to querySelectorAll for increased
|
|
196
|
-
* performance and greater usability
|
|
197
|
-
* @param {String} selector
|
|
198
|
-
* @param {Element} context (optional)
|
|
199
|
-
* @param {Boolean} find_all (optional)
|
|
200
|
-
* @return (find_all) {Element} : {Array}
|
|
201
|
-
*/
|
|
202
|
-
function find(selector, context, find_all) {
|
|
203
|
-
if ( context === void 0 ) context = window.document;
|
|
204
|
-
|
|
205
|
-
var simpleRe = /^(#?[\w-]+|\.[\w-.]+)$/,
|
|
206
|
-
periodRe = /\./g,
|
|
207
|
-
slice = Array.prototype.slice,
|
|
208
|
-
matches = [];
|
|
209
|
-
|
|
210
|
-
// Redirect call to the more performant function
|
|
211
|
-
// if it's a simple selector and return an array
|
|
212
|
-
// for easier usage
|
|
213
|
-
if (simpleRe.test(selector)) {
|
|
214
|
-
switch (selector[0]) {
|
|
215
|
-
case '#':
|
|
216
|
-
matches = [$(selector.substr(1))];
|
|
217
|
-
break;
|
|
218
|
-
case '.':
|
|
219
|
-
matches = slice.call(
|
|
220
|
-
context.getElementsByClassName(
|
|
221
|
-
selector.substr(1).replace(periodRe, ' ')
|
|
222
|
-
)
|
|
223
|
-
);
|
|
224
|
-
break;
|
|
225
|
-
default:
|
|
226
|
-
matches = slice.call(context.getElementsByTagName(selector));
|
|
227
|
-
}
|
|
228
|
-
} else {
|
|
229
|
-
// If not a simple selector, query the DOM as usual
|
|
230
|
-
// and return an array for easier usage
|
|
231
|
-
matches = slice.call(context.querySelectorAll(selector));
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return find_all ? matches : matches[0];
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function $(id) {
|
|
238
|
-
id = id[0] === '#' ? id.substr(1, id.length) : id;
|
|
239
|
-
return document.getElementById(id);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
function offset(element) {
|
|
243
|
-
var rect = element.getBoundingClientRect();
|
|
244
|
-
var docEl = document.documentElement;
|
|
245
|
-
return {
|
|
246
|
-
left: rect.left + window.pageXOffset - docEl.clientLeft,
|
|
247
|
-
top: rect.top + window.pageYOffset - docEl.clientTop,
|
|
248
|
-
width: element.offsetWidth,
|
|
249
|
-
height: element.offsetHeight,
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function getViewportSize() {
|
|
254
|
-
return {
|
|
255
|
-
w: window.innerWidth || document.documentElement.clientWidth,
|
|
256
|
-
h: window.innerHeight || document.documentElement.clientHeight,
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
function createFragment(html) {
|
|
261
|
-
var frag = document.createDocumentFragment(),
|
|
262
|
-
temp = document.createElement('div');
|
|
263
|
-
temp.innerHTML = html;
|
|
264
|
-
while (temp.firstChild) {
|
|
265
|
-
frag.appendChild(temp.firstChild);
|
|
266
|
-
}
|
|
267
|
-
return frag;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
function classRegex(classname) {
|
|
271
|
-
return new RegExp(("(^|\\s+) " + classname + " (\\s+|$)"));
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
function _addClass(el, klass, timeout) {
|
|
275
|
-
// use native if available
|
|
276
|
-
if (el.classList) {
|
|
277
|
-
el.classList.add(klass);
|
|
278
|
-
} else {
|
|
279
|
-
el.className = (el.className + ' ' + klass).trim();
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
if (timeout && isNumeric(timeout)) {
|
|
283
|
-
window.setTimeout(function () { return _removeClass(el, klass); }, timeout);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
function _removeClass(el, klass, timeout) {
|
|
288
|
-
if (el.classList) {
|
|
289
|
-
el.classList.remove(klass);
|
|
290
|
-
} else {
|
|
291
|
-
el.className = el.className.replace(classRegex(klass), ' ').trim();
|
|
292
|
-
}
|
|
293
|
-
if (timeout && isNumeric(timeout)) {
|
|
294
|
-
window.setTimeout(function () { return _addClass(el, klass); }, timeout);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* @class Internal
|
|
300
|
-
*/
|
|
301
|
-
var Internal = function Internal(base) {
|
|
302
|
-
/**
|
|
303
|
-
* @type {ol.control.Control}
|
|
304
|
-
*/
|
|
305
|
-
this.Base = base;
|
|
306
|
-
/**
|
|
307
|
-
* @type {ol.Map}
|
|
308
|
-
*/
|
|
309
|
-
this.map = undefined;
|
|
310
|
-
/**
|
|
311
|
-
* @type {Element}
|
|
312
|
-
*/
|
|
313
|
-
this.viewport = undefined;
|
|
314
|
-
/**
|
|
315
|
-
* @type {ol.Coordinate}
|
|
316
|
-
*/
|
|
317
|
-
this.coordinateClicked = undefined;
|
|
318
|
-
/**
|
|
319
|
-
* @type {ol.Pixel}
|
|
320
|
-
*/
|
|
321
|
-
this.pixelClicked = undefined;
|
|
322
|
-
/**
|
|
323
|
-
* @type {Number}
|
|
324
|
-
*/
|
|
325
|
-
this.lineHeight = 0;
|
|
326
|
-
/**
|
|
327
|
-
* @type {Object}
|
|
328
|
-
*/
|
|
329
|
-
this.items = {};
|
|
330
|
-
/**
|
|
331
|
-
* @type {Boolean}
|
|
332
|
-
*/
|
|
333
|
-
this.opened = false;
|
|
334
|
-
/**
|
|
335
|
-
* @type {Object}
|
|
336
|
-
*/
|
|
337
|
-
this.submenu = {
|
|
338
|
-
left: base.options.width - 15 + 'px',
|
|
339
|
-
lastLeft: '', // string + px
|
|
340
|
-
};
|
|
341
|
-
/**
|
|
342
|
-
* @type {Function}
|
|
343
|
-
*/
|
|
344
|
-
this.eventHandler = this.handleEvent.bind(this);
|
|
345
|
-
/**
|
|
346
|
-
* @type {Function}
|
|
347
|
-
*/
|
|
348
|
-
this.eventMapMoveHandler = this.handleMapMoveEvent.bind(this);
|
|
349
|
-
return this;
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
Internal.prototype.init = function init (map) {
|
|
353
|
-
this.map = map;
|
|
354
|
-
this.viewport = map.getViewport();
|
|
355
|
-
this.setListeners();
|
|
356
|
-
this.Base.Html.createMenu();
|
|
357
|
-
|
|
358
|
-
this.lineHeight =
|
|
359
|
-
this.getItemsLength() > 0
|
|
360
|
-
? this.Base.container.offsetHeight / this.getItemsLength()
|
|
361
|
-
: this.Base.Html.cloneAndGetLineHeight();
|
|
362
|
-
};
|
|
363
|
-
|
|
364
|
-
Internal.prototype.getItemsLength = function getItemsLength () {
|
|
365
|
-
var this$1 = this;
|
|
366
|
-
|
|
367
|
-
var count = 0;
|
|
368
|
-
Object.keys(this.items).forEach(function (k) {
|
|
369
|
-
if (this$1.items[k].submenu || this$1.items[k].separator) { return; }
|
|
370
|
-
count++;
|
|
371
|
-
});
|
|
372
|
-
return count;
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
Internal.prototype.getPixelClicked = function getPixelClicked () {
|
|
376
|
-
return this.pixelClicked;
|
|
377
|
-
};
|
|
378
|
-
|
|
379
|
-
Internal.prototype.getCoordinateClicked = function getCoordinateClicked () {
|
|
380
|
-
return this.coordinateClicked;
|
|
381
|
-
};
|
|
382
|
-
|
|
383
|
-
Internal.prototype.positionContainer = function positionContainer (pixel) {
|
|
384
|
-
var this$1 = this;
|
|
385
|
-
|
|
386
|
-
var container = this.Base.container;
|
|
387
|
-
var mapSize = this.map.getSize();
|
|
388
|
-
// how much (width) space left over
|
|
389
|
-
var space_left_h = mapSize[1] - pixel[1];
|
|
390
|
-
// how much (height) space left over
|
|
391
|
-
var space_left_w = mapSize[0] - pixel[0];
|
|
392
|
-
|
|
393
|
-
var menuSize = {
|
|
394
|
-
w: container.offsetWidth,
|
|
395
|
-
// a cheap way to recalculate container height
|
|
396
|
-
// since offsetHeight is like cached
|
|
397
|
-
h: Math.round(this.lineHeight * this.getItemsLength()),
|
|
398
|
-
};
|
|
399
|
-
// submenus
|
|
400
|
-
var subs = find(("li." + (CSS_VARS.submenu) + ">div"), container, true);
|
|
401
|
-
|
|
402
|
-
if (space_left_w >= menuSize.w) {
|
|
403
|
-
container.style.right = 'auto';
|
|
404
|
-
container.style.left = (pixel[0] + 5) + "px";
|
|
405
|
-
} else {
|
|
406
|
-
container.style.left = 'auto';
|
|
407
|
-
container.style.right = '15px';
|
|
408
|
-
}
|
|
409
|
-
// set top or bottom
|
|
410
|
-
if (space_left_h >= menuSize.h) {
|
|
411
|
-
container.style.bottom = 'auto';
|
|
412
|
-
container.style.top = (pixel[1] - 10) + "px";
|
|
413
|
-
} else {
|
|
414
|
-
container.style.top = 'auto';
|
|
415
|
-
container.style.bottom = 0;
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
removeClass(container, CSS_VARS.hidden);
|
|
419
|
-
|
|
420
|
-
if (subs.length) {
|
|
421
|
-
if (space_left_w < menuSize.w * 2) {
|
|
422
|
-
// no space (at right) for submenu
|
|
423
|
-
// position them at left
|
|
424
|
-
this.submenu.lastLeft = "-" + (menuSize.w) + "px";
|
|
425
|
-
} else {
|
|
426
|
-
this.submenu.lastLeft = this.submenu.left;
|
|
427
|
-
}
|
|
428
|
-
subs.forEach(function (sub) {
|
|
429
|
-
// is there enough space for submenu height?
|
|
430
|
-
var viewport = getViewportSize();
|
|
431
|
-
var sub_offset = offset(sub);
|
|
432
|
-
var sub_height = sub_offset.height;
|
|
433
|
-
var sub_top = space_left_h - sub_height;
|
|
434
|
-
|
|
435
|
-
if (sub_top < 0) {
|
|
436
|
-
sub_top = sub_height - (viewport.h - sub_offset.top);
|
|
437
|
-
sub.style.top = "-" + sub_top + "px";
|
|
438
|
-
}
|
|
439
|
-
sub.style.left = this$1.submenu.lastLeft;
|
|
440
|
-
});
|
|
441
|
-
}
|
|
442
|
-
};
|
|
443
|
-
|
|
444
|
-
Internal.prototype.openMenu = function openMenu (pixel, coordinate) {
|
|
445
|
-
this.Base.dispatchEvent({
|
|
446
|
-
type: EVENT_TYPE.OPEN,
|
|
447
|
-
pixel: pixel,
|
|
448
|
-
coordinate: coordinate,
|
|
449
|
-
});
|
|
450
|
-
this.opened = true;
|
|
451
|
-
this.positionContainer(pixel);
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
Internal.prototype.closeMenu = function closeMenu () {
|
|
455
|
-
this.opened = false;
|
|
456
|
-
addClass(this.Base.container, CSS_VARS.hidden);
|
|
457
|
-
this.Base.dispatchEvent({
|
|
458
|
-
type: EVENT_TYPE.CLOSE,
|
|
459
|
-
});
|
|
460
|
-
};
|
|
461
|
-
|
|
462
|
-
Internal.prototype.setListeners = function setListeners () {
|
|
463
|
-
this.viewport.addEventListener(
|
|
464
|
-
this.Base.options.eventType,
|
|
465
|
-
this.eventHandler,
|
|
466
|
-
false
|
|
467
|
-
);
|
|
468
|
-
|
|
469
|
-
this.map.on('movestart', this.eventMapMoveHandler);
|
|
470
|
-
};
|
|
471
|
-
|
|
472
|
-
Internal.prototype.removeListeners = function removeListeners () {
|
|
473
|
-
this.viewport.removeEventListener(
|
|
474
|
-
this.Base.options.eventType,
|
|
475
|
-
this.eventHandler,
|
|
476
|
-
false
|
|
477
|
-
);
|
|
478
|
-
|
|
479
|
-
this.map.un('movestart', this.eventMapMoveHandler);
|
|
480
|
-
};
|
|
481
|
-
|
|
482
|
-
Internal.prototype.handleEvent = function handleEvent (evt) {
|
|
483
|
-
var this_ = this;
|
|
484
|
-
|
|
485
|
-
this.coordinateClicked = this.map.getEventCoordinate(evt);
|
|
486
|
-
this.pixelClicked = this.map.getEventPixel(evt);
|
|
487
|
-
|
|
488
|
-
this.Base.dispatchEvent({
|
|
489
|
-
type: EVENT_TYPE.BEFOREOPEN,
|
|
490
|
-
pixel: this.pixelClicked,
|
|
491
|
-
coordinate: this.coordinateClicked,
|
|
492
|
-
});
|
|
493
|
-
|
|
494
|
-
if (this.Base.disabled) { return; }
|
|
495
|
-
|
|
496
|
-
if (this.Base.options.eventType === EVENT_TYPE.CONTEXTMENU) {
|
|
497
|
-
// don't be intrusive with other event types
|
|
498
|
-
evt.stopPropagation();
|
|
499
|
-
evt.preventDefault();
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
this.openMenu(this.pixelClicked, this.coordinateClicked);
|
|
503
|
-
|
|
504
|
-
//one-time fire
|
|
505
|
-
evt.target.addEventListener(
|
|
506
|
-
'pointerdown',
|
|
507
|
-
{
|
|
508
|
-
handleEvent: function (e) {
|
|
509
|
-
if (this_.opened) {
|
|
510
|
-
this_.closeMenu();
|
|
511
|
-
e.stopPropagation();
|
|
512
|
-
evt.target.removeEventListener(e.type, this, false);
|
|
513
|
-
}
|
|
514
|
-
},
|
|
515
|
-
},
|
|
516
|
-
false
|
|
517
|
-
);
|
|
518
|
-
};
|
|
519
|
-
|
|
520
|
-
Internal.prototype.handleMapMoveEvent = function handleMapMoveEvent (evt) {
|
|
521
|
-
this.closeMenu();
|
|
522
|
-
};
|
|
523
|
-
|
|
524
|
-
Internal.prototype.setItemListener = function setItemListener (li, index) {
|
|
525
|
-
var this_ = this;
|
|
526
|
-
if (li && typeof this.items[index].callback === 'function') {
|
|
527
|
-
(function (callback) {
|
|
528
|
-
li.addEventListener(
|
|
529
|
-
'click',
|
|
530
|
-
function (evt) {
|
|
531
|
-
evt.preventDefault();
|
|
532
|
-
var obj = {
|
|
533
|
-
coordinate: this_.getCoordinateClicked(),
|
|
534
|
-
data: this_.items[index].data || null,
|
|
535
|
-
};
|
|
536
|
-
this_.closeMenu();
|
|
537
|
-
callback(obj, this_.map);
|
|
538
|
-
},
|
|
539
|
-
false
|
|
540
|
-
);
|
|
541
|
-
})(this.items[index].callback);
|
|
542
|
-
}
|
|
543
|
-
};
|
|
544
|
-
|
|
545
|
-
/**
|
|
546
|
-
* @class Html
|
|
547
|
-
*/
|
|
548
|
-
var Html = function Html(base) {
|
|
549
|
-
this.Base = base;
|
|
550
|
-
this.Base.container = this.container = this.createContainer(true);
|
|
551
|
-
return this;
|
|
552
|
-
};
|
|
553
|
-
|
|
554
|
-
Html.prototype.createContainer = function createContainer (hidden) {
|
|
555
|
-
var container = document.createElement('div');
|
|
556
|
-
var ul = document.createElement('ul');
|
|
557
|
-
var klasses = [CSS_VARS.container, CSS_VARS.unselectable];
|
|
558
|
-
|
|
559
|
-
hidden && klasses.push(CSS_VARS.hidden);
|
|
560
|
-
container.className = klasses.join(' ');
|
|
561
|
-
container.style.width = parseInt(this.Base.options.width, 10) + 'px';
|
|
562
|
-
container.appendChild(ul);
|
|
563
|
-
return container;
|
|
564
|
-
};
|
|
565
|
-
|
|
566
|
-
Html.prototype.createMenu = function createMenu () {
|
|
567
|
-
var items = [];
|
|
568
|
-
|
|
569
|
-
if ('items' in this.Base.options) {
|
|
570
|
-
items = this.Base.options.defaultItems
|
|
571
|
-
? this.Base.options.items.concat(DEFAULT_ITEMS)
|
|
572
|
-
: this.Base.options.items;
|
|
573
|
-
} else if (this.Base.options.defaultItems) {
|
|
574
|
-
items = DEFAULT_ITEMS;
|
|
575
|
-
}
|
|
576
|
-
// no item
|
|
577
|
-
if (items.length === 0) { return false; }
|
|
578
|
-
// create entries
|
|
579
|
-
items.forEach(this.addMenuEntry, this);
|
|
580
|
-
};
|
|
581
|
-
|
|
582
|
-
Html.prototype.addMenuEntry = function addMenuEntry (item) {
|
|
583
|
-
var this$1 = this;
|
|
584
|
-
|
|
585
|
-
if (item.items && Array.isArray(item.items)) {
|
|
586
|
-
// submenu - only a second level
|
|
587
|
-
item.classname = item.classname || '';
|
|
588
|
-
if (!contains(CSS_VARS.submenu, item.classname)) {
|
|
589
|
-
item.classname = item.classname.length
|
|
590
|
-
? ' ' + CSS_VARS.submenu
|
|
591
|
-
: CSS_VARS.submenu;
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
var li = this.generateHtmlAndPublish(this.container, item);
|
|
595
|
-
var sub = this.createContainer();
|
|
596
|
-
sub.style.left =
|
|
597
|
-
this.Base.Internal.submenu.lastLeft || this.Base.Internal.submenu.left;
|
|
598
|
-
li.appendChild(sub);
|
|
599
|
-
|
|
600
|
-
item.items.forEach(function (each) {
|
|
601
|
-
this$1.generateHtmlAndPublish(sub, each, true);
|
|
602
|
-
});
|
|
603
|
-
} else {
|
|
604
|
-
this.generateHtmlAndPublish(this.container, item);
|
|
605
|
-
}
|
|
606
|
-
};
|
|
607
|
-
|
|
608
|
-
Html.prototype.generateHtmlAndPublish = function generateHtmlAndPublish (parent, item, submenu) {
|
|
609
|
-
var index = getUniqueId();
|
|
610
|
-
var html,
|
|
611
|
-
frag,
|
|
612
|
-
element,
|
|
613
|
-
separator = false;
|
|
614
|
-
|
|
615
|
-
// separator
|
|
616
|
-
if (typeof item === 'string' && item.trim() === '-') {
|
|
617
|
-
html = "<li id=\"" + index + "\" class=\"" + (CSS_VARS.separator) + "\"><hr></li>";
|
|
618
|
-
frag = createFragment(html);
|
|
619
|
-
// http://stackoverflow.com/a/13347298/4640499
|
|
620
|
-
element = [].slice.call(frag.childNodes, 0)[0];
|
|
621
|
-
parent.firstChild.appendChild(frag);
|
|
622
|
-
// to exclude from lineHeight calculation
|
|
623
|
-
separator = true;
|
|
624
|
-
} else {
|
|
625
|
-
item.classname = item.classname || '';
|
|
626
|
-
html = "<span>" + (item.text) + "</span>";
|
|
627
|
-
frag = createFragment(html);
|
|
628
|
-
element = document.createElement('li');
|
|
629
|
-
|
|
630
|
-
if (item.icon) {
|
|
631
|
-
if (item.classname === '') {
|
|
632
|
-
item.classname = CSS_VARS.icon;
|
|
633
|
-
} else if (item.classname.indexOf(CSS_VARS.icon) === -1) {
|
|
634
|
-
item.classname += " " + (CSS_VARS.icon);
|
|
635
|
-
}
|
|
636
|
-
element.setAttribute('style', ("background-image:url(" + (item.icon) + ")"));
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
element.id = index;
|
|
640
|
-
element.className = item.classname;
|
|
641
|
-
element.appendChild(frag);
|
|
642
|
-
parent.firstChild.appendChild(element);
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
this.Base.Internal.items[index] = {
|
|
646
|
-
id: index,
|
|
647
|
-
submenu: submenu || 0,
|
|
648
|
-
separator: separator,
|
|
649
|
-
callback: item.callback,
|
|
650
|
-
data: item.data || null,
|
|
651
|
-
};
|
|
652
|
-
this.Base.Internal.setItemListener(element, index);
|
|
653
|
-
return element;
|
|
654
|
-
};
|
|
655
|
-
|
|
656
|
-
Html.prototype.removeMenuEntry = function removeMenuEntry (index) {
|
|
657
|
-
var element = find('#' + index, this.container.firstChild);
|
|
658
|
-
element && this.container.firstChild.removeChild(element);
|
|
659
|
-
delete this.Base.Internal.items[index];
|
|
660
|
-
};
|
|
661
|
-
|
|
662
|
-
Html.prototype.cloneAndGetLineHeight = function cloneAndGetLineHeight () {
|
|
663
|
-
// for some reason I have to calculate with 2 items
|
|
664
|
-
var cloned = this.container.cloneNode();
|
|
665
|
-
var frag = createFragment('<span>Foo</span>');
|
|
666
|
-
var frag2 = createFragment('<span>Foo</span>');
|
|
667
|
-
var element = document.createElement('li');
|
|
668
|
-
var element2 = document.createElement('li');
|
|
669
|
-
|
|
670
|
-
element.appendChild(frag);
|
|
671
|
-
element2.appendChild(frag2);
|
|
672
|
-
cloned.appendChild(element);
|
|
673
|
-
cloned.appendChild(element2);
|
|
674
|
-
|
|
675
|
-
this.container.parentNode.appendChild(cloned);
|
|
676
|
-
var height = cloned.offsetHeight / 2;
|
|
677
|
-
this.container.parentNode.removeChild(cloned);
|
|
678
|
-
return height;
|
|
679
|
-
};
|
|
680
|
-
|
|
681
|
-
/**
|
|
682
|
-
* @class Base
|
|
683
|
-
* @extends {ol.control.Control}
|
|
684
|
-
*/
|
|
685
|
-
var Base = /*@__PURE__*/(function (Control) {
|
|
686
|
-
function Base(opt_options) {
|
|
687
|
-
if ( opt_options === void 0 ) opt_options = {};
|
|
688
|
-
|
|
689
|
-
assert(
|
|
690
|
-
typeof opt_options == 'object',
|
|
691
|
-
'@param `opt_options` should be object type!'
|
|
692
|
-
);
|
|
693
|
-
|
|
694
|
-
this.options = mergeOptions(DEFAULT_OPTIONS, opt_options);
|
|
695
|
-
this.disabled = false;
|
|
696
|
-
|
|
697
|
-
this.Internal = new Internal(this);
|
|
698
|
-
this.Html = new Html(this);
|
|
699
|
-
|
|
700
|
-
Control.call(this, { element: this.container });
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
if ( Control ) Base.__proto__ = Control;
|
|
704
|
-
Base.prototype = Object.create( Control && Control.prototype );
|
|
705
|
-
Base.prototype.constructor = Base;
|
|
706
|
-
|
|
707
|
-
/**
|
|
708
|
-
* Remove all elements from the menu.
|
|
709
|
-
*/
|
|
710
|
-
Base.prototype.clear = function clear () {
|
|
711
|
-
Object.keys(this.Internal.items).forEach(
|
|
712
|
-
this.Html.removeMenuEntry,
|
|
713
|
-
this.Html
|
|
714
|
-
);
|
|
715
|
-
};
|
|
716
|
-
|
|
717
|
-
/**
|
|
718
|
-
* Close the menu programmatically.
|
|
719
|
-
*/
|
|
720
|
-
Base.prototype.close = function close () {
|
|
721
|
-
this.Internal.closeMenu();
|
|
722
|
-
};
|
|
723
|
-
|
|
724
|
-
/**
|
|
725
|
-
* Enable menu
|
|
726
|
-
*/
|
|
727
|
-
Base.prototype.enable = function enable () {
|
|
728
|
-
this.disabled = false;
|
|
729
|
-
};
|
|
730
|
-
|
|
731
|
-
/**
|
|
732
|
-
* Disable menu
|
|
733
|
-
*/
|
|
734
|
-
Base.prototype.disable = function disable () {
|
|
735
|
-
this.disabled = true;
|
|
736
|
-
};
|
|
737
|
-
|
|
738
|
-
/**
|
|
739
|
-
* @return {Array} Returns default items
|
|
740
|
-
*/
|
|
741
|
-
Base.prototype.getDefaultItems = function getDefaultItems () {
|
|
742
|
-
return DEFAULT_ITEMS;
|
|
743
|
-
};
|
|
744
|
-
|
|
745
|
-
/**
|
|
746
|
-
* @return {Number} Returns how many items
|
|
747
|
-
*/
|
|
748
|
-
Base.prototype.countItems = function countItems () {
|
|
749
|
-
return Object.keys(this.Internal.items).length;
|
|
750
|
-
};
|
|
751
|
-
|
|
752
|
-
/**
|
|
753
|
-
* Add items to the menu. This pushes each item in the provided array
|
|
754
|
-
* to the end of the menu.
|
|
755
|
-
* @param {Array} arr Array.
|
|
756
|
-
*/
|
|
757
|
-
Base.prototype.extend = function extend (arr) {
|
|
758
|
-
assert(Array.isArray(arr), '@param `arr` should be an Array.');
|
|
759
|
-
arr.forEach(this.push, this);
|
|
760
|
-
};
|
|
761
|
-
|
|
762
|
-
Base.prototype.isOpen = function isOpen () {
|
|
763
|
-
return this.Internal.opened;
|
|
764
|
-
};
|
|
765
|
-
|
|
766
|
-
/**
|
|
767
|
-
* Update the menu's position.
|
|
768
|
-
*/
|
|
769
|
-
Base.prototype.updatePosition = function updatePosition (pixel) {
|
|
770
|
-
assert(Array.isArray(pixel), '@param `pixel` should be an Array.');
|
|
771
|
-
|
|
772
|
-
if (this.isOpen()) {
|
|
773
|
-
this.Internal.positionContainer(pixel);
|
|
774
|
-
}
|
|
775
|
-
};
|
|
776
|
-
|
|
777
|
-
/**
|
|
778
|
-
* Remove the last item of the menu.
|
|
779
|
-
*/
|
|
780
|
-
Base.prototype.pop = function pop () {
|
|
781
|
-
var keys = Object.keys(this.Internal.items);
|
|
782
|
-
this.Html.removeMenuEntry(keys[keys.length - 1]);
|
|
783
|
-
};
|
|
784
|
-
|
|
785
|
-
/**
|
|
786
|
-
* Insert the provided item at the end of the menu.
|
|
787
|
-
* @param {Object|String} item Item.
|
|
788
|
-
*/
|
|
789
|
-
Base.prototype.push = function push (item) {
|
|
790
|
-
assert(isDefAndNotNull(item), '@param `item` must be informed.');
|
|
791
|
-
this.Html.addMenuEntry(item);
|
|
792
|
-
};
|
|
793
|
-
|
|
794
|
-
/**
|
|
795
|
-
* Remove the first item of the menu.
|
|
796
|
-
*/
|
|
797
|
-
Base.prototype.shift = function shift () {
|
|
798
|
-
this.Html.removeMenuEntry(Object.keys(this.Internal.items)[0]);
|
|
799
|
-
};
|
|
800
|
-
|
|
801
|
-
/**
|
|
802
|
-
* Not supposed to be used on app.
|
|
803
|
-
*/
|
|
804
|
-
Base.prototype.setMap = function setMap (map) {
|
|
805
|
-
Control.prototype.setMap.call(this, map);
|
|
806
|
-
|
|
807
|
-
if (map) {
|
|
808
|
-
// let's start since now we have the map
|
|
809
|
-
this.Internal.init(map, this);
|
|
810
|
-
} else {
|
|
811
|
-
// I'm removed from the map - remove listeners
|
|
812
|
-
this.Internal.removeListeners();
|
|
813
|
-
}
|
|
814
|
-
};
|
|
815
|
-
|
|
816
|
-
return Base;
|
|
817
|
-
}(Control__default['default']));
|
|
818
|
-
|
|
819
|
-
return Base;
|
|
820
|
-
|
|
821
|
-
})));
|