-
Notifications
You must be signed in to change notification settings - Fork 994
/
Draw.Rectangle.js
98 lines (83 loc) · 2.43 KB
/
Draw.Rectangle.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
/**
* @class L.Draw.Rectangle
* @aka Draw.Rectangle
* @inherits L.Draw.SimpleShape
*/
L.Draw.Rectangle = L.Draw.SimpleShape.extend({
statics: {
TYPE: 'rectangle'
},
options: {
shapeOptions: {
stroke: true,
color: '#3388ff',
weight: 4,
opacity: 0.5,
fill: true,
fillColor: null, //same as color by default
fillOpacity: 0.2,
showArea: true,
clickable: true
},
metric: true // Whether to use the metric measurement system or imperial
},
// @method initialize(): void
initialize: function (map, options) {
// Save the type so super can fire, need to do this as cannot do this.TYPE :(
this.type = L.Draw.Rectangle.TYPE;
this._initialLabelText = L.drawLocal.draw.handlers.rectangle.tooltip.start;
L.Draw.SimpleShape.prototype.initialize.call(this, map, options);
},
// @method disable(): void
disable: function () {
if (!this._enabled) {
return;
}
this._isCurrentlyTwoClickDrawing = false;
L.Draw.SimpleShape.prototype.disable.call(this);
},
_onMouseUp: function (e) {
if (!this._shape && !this._isCurrentlyTwoClickDrawing) {
this._isCurrentlyTwoClickDrawing = true;
return;
}
// Make sure closing click is on map
if (this._isCurrentlyTwoClickDrawing && !_hasAncestor(e.target, 'leaflet-pane')) {
return;
}
L.Draw.SimpleShape.prototype._onMouseUp.call(this);
},
_drawShape: function (latlng) {
if (!this._shape) {
this._shape = new L.Rectangle(new L.LatLngBounds(this._startLatLng, latlng), this.options.shapeOptions);
this._map.addLayer(this._shape);
} else {
this._shape.setBounds(new L.LatLngBounds(this._startLatLng, latlng));
}
},
_fireCreatedEvent: function () {
var rectangle = new L.Rectangle(this._shape.getBounds(), this.options.shapeOptions);
L.Draw.SimpleShape.prototype._fireCreatedEvent.call(this, rectangle);
},
_getTooltipText: function () {
var tooltipText = L.Draw.SimpleShape.prototype._getTooltipText.call(this),
shape = this._shape,
showArea = this.options.showArea,
latLngs, area, subtext;
if (shape) {
latLngs = this._shape._defaultShape ? this._shape._defaultShape() : this._shape.getLatLngs();
area = L.GeometryUtil.geodesicArea(latLngs);
subtext = showArea ? L.GeometryUtil.readableArea(area, this.options.metric) : '';
}
return {
text: tooltipText.text,
subtext: subtext
};
}
});
function _hasAncestor(el, cls) {
while ((el = el.parentElement) && !el.classList.contains(cls)) {
;
}
return el;
}