-
Notifications
You must be signed in to change notification settings - Fork 425
/
L.PM.Toolbar.js
604 lines (546 loc) · 17.7 KB
/
L.PM.Toolbar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import PMButton from './L.Controls';
import { getTranslation } from '../helpers';
L.Control.PMButton = PMButton;
const Toolbar = L.Class.extend({
options: {
drawMarker: true,
drawRectangle: true,
drawPolyline: true,
drawPolygon: true,
drawCircle: true,
drawCircleMarker: true,
editMode: true,
dragMode: true,
cutPolygon: true,
removalMode: true,
snappingOption: true,
drawControls: true,
editControls: true,
optionsControls: true,
customControls: true,
oneBlock: false,
position: 'topleft',
},
customButtons: [],
initialize(map) {
this.init(map);
},
reinit() {
const addControls = this.isVisible;
this.removeControls();
this._defineButtons();
if (addControls) {
this.addControls();
}
},
init(map) {
this.map = map;
this.buttons = {};
this.isVisible = false;
this.drawContainer = L.DomUtil.create(
'div',
'leaflet-pm-toolbar leaflet-pm-draw leaflet-bar leaflet-control'
);
this.editContainer = L.DomUtil.create(
'div',
'leaflet-pm-toolbar leaflet-pm-edit leaflet-bar leaflet-control'
);
this.optionsContainer = L.DomUtil.create(
'div',
'leaflet-pm-toolbar leaflet-pm-options leaflet-bar leaflet-control'
);
this.customContainer = L.DomUtil.create(
'div',
'leaflet-pm-toolbar leaflet-pm-custom leaflet-bar leaflet-control'
);
this._defineButtons();
},
getButtons() {
return this.buttons;
},
addControls(options = this.options) {
// adds all buttons to the map specified inside options
// make button renaming backwards compatible
if (typeof options.editPolygon !== 'undefined') {
options.editMode = options.editPolygon;
}
if (typeof options.deleteLayer !== 'undefined') {
options.removalMode = options.deleteLayer;
}
// first set the options
L.Util.setOptions(this, options);
this.applyIconStyle();
// now show the specified buttons
this._showHideButtons();
this.isVisible = true;
},
applyIconStyle() {
const buttons = this.getButtons();
const iconClasses = {
geomanIcons: {
drawMarker: 'control-icon leaflet-pm-icon-marker',
drawPolyline: 'control-icon leaflet-pm-icon-polyline',
drawRectangle: 'control-icon leaflet-pm-icon-rectangle',
drawPolygon: 'control-icon leaflet-pm-icon-polygon',
drawCircle: 'control-icon leaflet-pm-icon-circle',
drawCircleMarker: 'control-icon leaflet-pm-icon-circle-marker',
editMode: 'control-icon leaflet-pm-icon-edit',
dragMode: 'control-icon leaflet-pm-icon-drag',
cutPolygon: 'control-icon leaflet-pm-icon-cut',
removalMode: 'control-icon leaflet-pm-icon-delete',
},
};
for (const name in buttons) {
const button = buttons[name];
L.Util.setOptions(button, {
className: iconClasses.geomanIcons[name],
});
}
},
removeControls() {
// grab all buttons to loop through
const buttons = this.getButtons();
// remove all buttons
for (const btn in buttons) {
buttons[btn].remove();
}
this.isVisible = false;
},
toggleControls(options = this.options) {
if (this.isVisible) {
this.removeControls();
} else {
this.addControls(options);
}
},
_addButton(name, button) {
this.buttons[name] = button;
this.options[name] = this.options[name] || false;
return this.buttons[name];
},
triggerClickOnToggledButtons(exceptThisButton) {
// this function is used when - e.g. drawing mode is enabled and a possible
// other active mode (like removal tool) is already active.
// we can't have two active modes because of possible event conflicts
// so, we trigger a click on all currently active (toggled) buttons
// the options toolbar should not be disabled during the different modes
// TODO: probably need to abstract this a bit so different options are automatically
// disabled for different modes, like pinning for circles
const exceptOptionButtons = ['snappingOption']
for (const name in this.buttons) {
if (
!exceptOptionButtons.includes(name) &&
this.buttons[name] !== exceptThisButton &&
this.buttons[name].toggled()
) {
this.buttons[name]._triggerClick();
}
}
},
toggleButton(name, status, disableOthers = true) {
// does not fire the events/functionality of the button
// this just changes the state and is used if a functionality (like Draw)
// is enabled manually via script
// backwards compatibility with button rename
if (name === 'editPolygon') {
name = 'editMode';
}
if (name === 'deleteLayer') {
name = 'removalMode';
}
// as some mode got enabled, we still have to trigger the click on the other buttons
// to disable their mode
if (disableOthers) {
this.triggerClickOnToggledButtons(this.buttons[name]);
}
if (!this.buttons[name]) {
return false;
}
// now toggle the state of the button
return this.buttons[name].toggle(status);
},
_defineButtons() {
// some buttons are still in their respective classes, like L.PM.Draw.Polygon
const drawMarkerButton = {
className: 'control-icon leaflet-pm-icon-marker',
title: getTranslation('buttonTitles.drawMarkerButton'),
jsClass: 'Marker',
onClick: () => { },
afterClick: (e, ctx) => {
// toggle drawing mode
this.map.pm.Draw[ctx.button._button.jsClass].toggle();
},
doToggle: true,
toggleStatus: false,
disableOtherButtons: true,
position: this.options.position,
actions: ['cancel'],
};
const drawPolyButton = {
title: getTranslation('buttonTitles.drawPolyButton'),
className: 'control-icon leaflet-pm-icon-polygon',
jsClass: 'Polygon',
onClick: () => { },
afterClick: (e, ctx) => {
// toggle drawing mode
this.map.pm.Draw[ctx.button._button.jsClass].toggle();
},
doToggle: true,
toggleStatus: false,
disableOtherButtons: true,
position: this.options.position,
actions: ['finish', 'removeLastVertex', 'cancel'],
};
const drawLineButton = {
className: 'control-icon leaflet-pm-icon-polyline',
title: getTranslation('buttonTitles.drawLineButton'),
jsClass: 'Line',
onClick: () => { },
afterClick: (e, ctx) => {
// toggle drawing mode
this.map.pm.Draw[ctx.button._button.jsClass].toggle();
},
doToggle: true,
toggleStatus: false,
disableOtherButtons: true,
position: this.options.position,
actions: ['finish', 'removeLastVertex', 'cancel'],
};
const drawCircleButton = {
title: getTranslation('buttonTitles.drawCircleButton'),
className: 'control-icon leaflet-pm-icon-circle',
jsClass: 'Circle',
onClick: () => { },
afterClick: (e, ctx) => {
// toggle drawing mode
this.map.pm.Draw[ctx.button._button.jsClass].toggle();
},
doToggle: true,
toggleStatus: false,
disableOtherButtons: true,
position: this.options.position,
actions: ['cancel'],
};
const drawCircleMarkerButton = {
title: getTranslation('buttonTitles.drawCircleMarkerButton'),
className: 'control-icon leaflet-pm-icon-circle-marker',
jsClass: 'CircleMarker',
onClick: () => { },
afterClick: (e, ctx) => {
// toggle drawing mode
this.map.pm.Draw[ctx.button._button.jsClass].toggle();
},
doToggle: true,
toggleStatus: false,
disableOtherButtons: true,
position: this.options.position,
actions: ['cancel'],
};
const drawRectButton = {
title: getTranslation('buttonTitles.drawRectButton'),
className: 'control-icon leaflet-pm-icon-rectangle',
jsClass: 'Rectangle',
onClick: () => { },
afterClick: (e, ctx) => {
// toggle drawing mode
this.map.pm.Draw[ctx.button._button.jsClass].toggle();
},
doToggle: true,
toggleStatus: false,
disableOtherButtons: true,
position: this.options.position,
actions: ['cancel'],
};
const editButton = {
title: getTranslation('buttonTitles.editButton'),
className: 'control-icon leaflet-pm-icon-edit',
onClick: () => { },
afterClick: () => {
this.map.pm.toggleGlobalEditMode();
},
doToggle: true,
toggleStatus: false,
disableOtherButtons: true,
position: this.options.position,
tool: 'edit',
actions: ['finishMode'],
};
const dragButton = {
title: getTranslation('buttonTitles.dragButton'),
className: 'control-icon leaflet-pm-icon-drag',
onClick: () => { },
afterClick: () => {
this.map.pm.toggleGlobalDragMode();
},
doToggle: true,
toggleStatus: false,
disableOtherButtons: true,
position: this.options.position,
tool: 'edit',
actions: ['finishMode'],
};
const cutButton = {
title: getTranslation('buttonTitles.cutButton'),
className: 'control-icon leaflet-pm-icon-cut',
jsClass: 'Cut',
onClick: () => { },
afterClick: (e, ctx) => {
// enable polygon drawing mode without snap
this.map.pm.Draw[ctx.button._button.jsClass].toggle({
snappable: true,
cursorMarker: true,
allowSelfIntersection: false,
});
},
doToggle: true,
toggleStatus: false,
disableOtherButtons: true,
position: this.options.position,
tool: 'edit',
actions: ['finish', 'removeLastVertex', 'cancel'],
};
const deleteButton = {
title: getTranslation('buttonTitles.deleteButton'),
className: 'control-icon leaflet-pm-icon-delete',
onClick: () => { },
afterClick: () => {
this.map.pm.toggleGlobalRemovalMode();
},
doToggle: true,
toggleStatus: false,
disableOtherButtons: true,
position: this.options.position,
tool: 'edit',
actions: ['finishMode'],
};
this._addButton('drawMarker', new L.Control.PMButton(drawMarkerButton));
this._addButton('drawPolyline', new L.Control.PMButton(drawLineButton));
this._addButton('drawRectangle', new L.Control.PMButton(drawRectButton));
this._addButton('drawPolygon', new L.Control.PMButton(drawPolyButton));
this._addButton('drawCircle', new L.Control.PMButton(drawCircleButton));
this._addButton('drawCircleMarker', new L.Control.PMButton(drawCircleMarkerButton));
this._addButton('editMode', new L.Control.PMButton(editButton));
this._addButton('dragMode', new L.Control.PMButton(dragButton));
this._addButton('cutPolygon', new L.Control.PMButton(cutButton));
this._addButton('removalMode', new L.Control.PMButton(deleteButton));
},
_showHideButtons() {
// remove all buttons, that's because the Toolbar can be added again with
// different options so it's basically a reset and add again
this.removeControls();
const buttons = this.getButtons();
let ignoreBtns = [];
if (this.options.drawControls === false) {
ignoreBtns = ignoreBtns.concat(Object.keys(buttons).filter(btn => !buttons[btn]._button.tool));
}
if (this.options.editControls === false) {
ignoreBtns = ignoreBtns.concat(Object.keys(buttons).filter(btn => buttons[btn]._button.tool === 'edit'));
}
if (this.options.optionsControls === false) {
ignoreBtns = ignoreBtns.concat(Object.keys(buttons).filter(btn => buttons[btn]._button.tool === 'options'));
}
if (this.options.customControls === false) {
ignoreBtns = ignoreBtns.concat(Object.keys(buttons).filter(btn => buttons[btn]._button.tool === 'custom'));
}
for (const btn in buttons) {
if (this.options[btn] && ignoreBtns.indexOf(btn) === -1) {
// if options say the button should be visible, add it to the map
buttons[btn].setPosition(this.options.position);
buttons[btn].addTo(this.map);
}
}
},
copyDrawControl(copyInstance, options) {
if (!options) {
throw new TypeError(
"Button has no name"
);
} else if (typeof options !== 'object') { // if only the name is passed and no options object
options = { name: options };
}
const shapeMapping = {
"Marker": "drawMarker",
"Circle": "drawCircle",
"Polygon": "drawPolygon",
"Rectangle": "drawRectangle",
"Polyline": "drawPolyline",
"Line": "drawPolyline",
"CircleMarker": "drawCircleMarker",
"Edit": "editMode",
"Drag": "dragMode",
"Cut": "cutPolygon",
"Removal": "removalMode"
};
const instance = shapeMapping[copyInstance] ? shapeMapping[copyInstance] : copyInstance;
if (!options.name) {
throw new TypeError(
"Button has no name"
);
}
if (this.buttons[options.name]) {
throw new TypeError(
"Button with this name already exists"
)
}
const drawInstance = this.map.pm.Draw.createNewDrawInstance(options.name, instance);
const btn = this.buttons[instance]._button;
options = Object.assign({}, btn, options);
const control = this.createCustomControl(options);
return { drawInstance, control };
},
createCustomControl(options) {
if (!options.name) {
throw new TypeError(
"Button has no name"
);
}
if (this.buttons[options.name]) {
throw new TypeError(
"Button with this name already exists"
)
}
if (!options.onClick) {
options.onClick = () => { };
}
if (!options.afterClick) {
options.afterClick = () => { };
}
if (options.toggle !== false) {
options.toggle = true;
}
if (options.block) {
options.block = options.block.toLowerCase();
}
if (!options.block || options.block === "draw") {
options.block = "";
}
if (options.className.indexOf('control-icon') === -1) {
options.className = `control-icon ${options.className}`;
}
const _options = {
tool: options.block,
className: options.className,
title: options.title || '',
jsClass: options.name,
onClick: options.onClick,
afterClick: options.afterClick,
doToggle: options.toggle,
toggleStatus: false,
disableOtherButtons: true,
cssToggle: options.toggle,
position: this.options.position,
actions: options.actions || [],
};
if (this.options[options.name] !== false) {
this.options[options.name] = true;
}
const control = this._addButton(options.name, new L.Control.PMButton(_options));
this.changeControlOrder();
return control;
},
changeControlOrder(order = []) {
const shapeMapping = {
"Marker": "drawMarker",
"Circle": "drawCircle",
"Polygon": "drawPolygon",
"Rectangle": "drawRectangle",
"Polyline": "drawPolyline",
"Line": "drawPolyline",
"CircleMarker": "drawCircleMarker",
"Edit": "editMode",
"Drag": "dragMode",
"Cut": "cutPolygon",
"Removal": "removalMode"
};
const _order = [];
order.forEach((shape) => {
if (shapeMapping[shape]) {
_order.push(shapeMapping[shape]);
} else {
_order.push(shape);
}
});
const buttons = this.getButtons();
// This steps are needed to create a new Object which contains the buttons in the correct sorted order.
const newbtnorder = {};
_order.forEach((control) => {
if (buttons[control]) {
newbtnorder[control] = buttons[control];
}
});
const drawBtns = Object.keys(buttons).filter(btn => !buttons[btn]._button.tool);
drawBtns.forEach((btn) => {
if (_order.indexOf(btn) === -1) {
newbtnorder[btn] = buttons[btn];
}
});
const editBtns = Object.keys(buttons).filter(btn => buttons[btn]._button.tool === "edit");
editBtns.forEach((btn) => {
if (_order.indexOf(btn) === -1) {
newbtnorder[btn] = buttons[btn];
}
});
const optionsBtns = Object.keys(buttons).filter(btn => buttons[btn]._button.tool === "options");
optionsBtns.forEach((btn) => {
if (_order.indexOf(btn) === -1) {
newbtnorder[btn] = buttons[btn];
}
});
const customBtns = Object.keys(buttons).filter(btn => buttons[btn]._button.tool === "custom");
customBtns.forEach((btn) => {
if (_order.indexOf(btn) === -1) {
newbtnorder[btn] = buttons[btn];
}
});
// To add all other buttons they are not in a container from above (maybe added manually by a developer)
Object.keys(buttons).forEach((btn) => {
if (_order.indexOf(btn) === -1) {
newbtnorder[btn] = buttons[btn];
}
});
this.map.pm.Toolbar.buttons = newbtnorder;
this._showHideButtons();
},
getControlOrder() {
const buttons = this.getButtons();
const order = [];
for (const btn in buttons) {
order.push(btn);
}
return order;
},
changeActionsOfControl(name, actions) {
const shapeMapping = {
"Marker": "drawMarker",
"Circle": "drawCircle",
"Polygon": "drawPolygon",
"Rectangle": "drawRectangle",
"Polyline": "drawPolyline",
"Line": "drawPolyline",
"CircleMarker": "drawCircleMarker",
"Edit": "editMode",
"Drag": "dragMode",
"Cut": "cutPolygon",
"Removal": "removalMode"
};
const btnName = shapeMapping[name] ? shapeMapping[name] : name;
if (!btnName) {
throw new TypeError(
"No name passed"
);
}
if (!actions) {
throw new TypeError(
"No actions passed"
);
}
if (!this.buttons[btnName]) {
throw new TypeError(
"Button with this name not exists"
)
}
this.buttons[btnName]._button.actions = actions;
this.changeControlOrder();
}
});
export default Toolbar;