PageSourceSearch

https://markokuosmanen.fi/pages/vantaa/js/leaflet/1.7.1/plugins/le…/leaflet.measurecontrol.js

js markokuosmanen.fi collected 2026-10-09 22:08:21 UTC 4,486 bytes, 173 lines download raw bytes

1(function (factory, window) {
2  // define an AMD module that relies on 'leaflet'
3  if (typeof define === 'function' && define.amd) {
4    define(['leaflet'], function (L) {
5        factory(L, window.toGeoJSON);
6    });
7
8    // define a Common JS module that relies on 'leaflet'
9  } else if (typeof exports === 'object') {
10    module.exports = function (L) {
11      if (L === undefined) {
12        if (typeof window !== 'undefined') {
13          L = require('leaflet');
14        }
15      }
16      factory(L);
17      return L;
18    };
19  } else if (typeof window !== 'undefined' && window.L) {
20    factory(window.L);
21  }
22}(function (L) {
23  L.Polyline.Measure = L.Draw.Polyline.extend({
24    addHooks: function () {
25      L.Draw.Polyline.prototype.addHooks.call(this);
26      if (this._map) {
27        this._markerGroup = new L.LayerGroup();
28        this._map.addLayer(this._markerGroup);
29
30        this._markers = [];
31        this._map.on('click', this._onClick, this);
32        this._startShape();
33      }
34    },
35
36    removeHooks: function () {
37      L.Draw.Polyline.prototype.removeHooks.call(this);
38
39      this._clearHideErrorTimeout();
40
41      // !\ Still useful when control is disabled before any drawing (refactor needed?)
42      this._map
43      .off('pointermove', this._onMouseMove, this)
44      .off('mousemove', this._onMouseMove, this)
45      .off('click', this._onClick, this);
46
47      this._clearGuides();
48      this._container.style.cursor = '';
49
50      this._removeShape();
51    },
52
53    _startShape: function () {
54      this._drawing = true;
55      this._poly = new L.Polyline([], this.options.shapeOptions);
56      // this is added as a placeholder, if leaflet doesn't recieve
57      // this when the tool is turned off all onclick events are removed
58      this._poly._onClick = function () {};
59
60      this._container.style.cursor = 'crosshair';
61
62      this._updateTooltip();
63      this._map
64      .on('pointermove', this._onMouseMove, this)
65      .on('mousemove', this._onMouseMove, this);
66    },
67
68    _finishShape: function () {
69      this._drawing = false;
70
71      this._cleanUpShape();
72      this._clearGuides();
73
74      this._updateTooltip();
75
76      this._map
77      .off('pointermove', this._onMouseMove, this)
78      .off('mousemove', this._onMouseMove, this);
79
80      this._container.style.cursor = '';
81    },
82
83    _removeShape: function () {
84      if (!this._poly) return;
85      this._map.removeLayer(this._poly);
86      delete this._poly;
87      this._markers.splice(0);
88      this._markerGroup.clearLayers();
89    },
90
91    _onClick: function () {
92      if (!this._drawing) {
93        this._removeShape();
94        this._startShape();
95        return;
96      }
97    },
98
99    _getTooltipText: function () {
100      var labelText = L.Draw.Polyline.prototype._getTooltipText.call(this);
101      if (!this._drawing) {
102        labelText.text = '';
103      }
104      return labelText;
105    }
106  });
107
108  L.Control.MeasureControl = L.Control.extend({
109
110    statics: {
111      TITLE: 'Mittaa matkaa'
112    },
113    options: {
114      position: 'topleft',
115      handler: {}
116    },
117
118    toggle: function () {
119      if (this.handler.enabled()) {
120        this.handler.disable.call(this.handler);
121      } else {
122        this.handler.enable.call(this.handler);
123      }
124    },
125
126    onAdd: function (map) {
127      var link = null;
128      var className = 'leaflet-control-draw';
129
130      this._container = L.DomUtil.create('div', 'leaflet-bar');
131
132      this.handler = new L.Polyline.Measure(map, this.options.handler);
133
134      this.handler.on('enabled', function () {
135        this.enabled = true;
136        L.DomUtil.addClass(this._container, 'enabled');
137      }, this);
138
139      this.handler.on('disabled', function () {
140        delete this.enabled;
141        L.DomUtil.removeClass(this._container, 'enabled');
142      }, this);
143
144      link = L.DomUtil.create('a', className + '-measure', this._container);
145      link.href = '#';
146      link.title = L.Control.MeasureControl.TITLE;
147
148      L.DomEvent
149      .addListener(link, 'click', L.DomEvent.stopPropagation)
150      .addListener(link, 'click', L.DomEvent.preventDefault)
151      .addListener(link, 'click', this.toggle, this);
152
153      return this._container;
154    }
155  });
156
157
158  L.Map.mergeOptions({
159    measureControl: false
160  });
161
162
163  L.Map.addInitHook(function () {
164    if (this.options.measureControl) {
165      this.measureControl = L.Control.measureControl().addTo(this);
166    }
167  });
168
169
170  L.Control.measureControl = function (options) {
171    return new L.Control.MeasureControl(options);
172  };
173}, window));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.