@raruto/leaflet-elevation 2.3.0 → 2.3.1

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/README.md CHANGED
@@ -209,7 +209,7 @@ cd ./leaflet-elevation
209
209
  npm i # install dependencies
210
210
  npm run dev # start dev server at: http://localhost:8080
211
211
  npm run build # generate "dist" files (once)
212
- npm run test # test all ".*spec.js" files (once)
212
+ npm run test # test all "*.spec.js" files (once)
213
213
  ```
214
214
 
215
215
  After that you can start developing inside the `src` and `test` folders (eg. open "http://localhost:8080/test" in your browser to preview changes). Check also [CONTRIBUTING.md](.github/CONTRIBUTING.md) file for some information about it.
@@ -259,8 +259,34 @@ var map = L.map('map', {
259
259
  ```
260
260
  </details>
261
261
 
262
+ <details> <summary>3. How can I import this library as ES module?</summary>
263
+
264
+ Usually, when working with a js bundler like [Vite](https://vitest.dev/) or [Webpack](https://webpack.js.org/), you need to provide to this library the full path to some dynamically imported files from the [`srcFolder`](./src/):
265
+
266
+ ```js
267
+ import './your-custom-style.css';
268
+ import 'leaflet/dist/leaflet.css';
269
+ import L from 'leaflet';
270
+ import '@raruto/leaflet-elevation/src/index.js';
271
+ import '@raruto/leaflet-elevation/src/index.css';
272
+
273
+ const map = L.map('map', {
274
+ center: [35.681, 139.767],
275
+ zoom: 11,
276
+ });
277
+
278
+ const controlElevation = L.control.elevation({
279
+ srcFolder: 'http://unpkg.com/@raruto/leaflet-elevation/src/' // CHANGE ME: with your own http server custom "src" folder
280
+ }).addTo(map);
281
+
282
+ // Load track from url (allowed data types: "*.geojson", "*.gpx", "*.tcx")
283
+ controlElevation.load("https://raruto.github.io/leaflet-elevation/examples/via-emilia.gpx");
284
+ ```
285
+
286
+ </details>
287
+
262
288
  <details>
263
- <summary>3. Some real world projects based on this plugin?</summary><br>
289
+ <summary>4. Some real world projects based on this plugin?</summary><br>
264
290
 
265
291
  - https://parcours.scasb.org/
266
292
  - https://velocat.ru/velo/phpBB3/map.php
@@ -21,6 +21,9 @@
21
21
  const HOUR = MIN * 60;
22
22
  const DAY = HOUR * 24;
23
23
 
24
+ function resolveURL(src, baseUrl) {
25
+ return (new URL(src, (src.startsWith('../') || src.startsWith('./')) ? baseUrl : undefined)).toString()
26
+ }
24
27
  /**
25
28
  * Convert a time (millis) to a human readable duration string (%Dd %H:%M'%S")
26
29
  */
@@ -188,6 +191,7 @@
188
191
  randomId: randomId,
189
192
  removeClass: removeClass,
190
193
  replaceClass: replaceClass,
194
+ resolveURL: resolveURL,
191
195
  round: round,
192
196
  saveFile: saveFile,
193
197
  select: select,
@@ -283,6 +287,9 @@
283
287
  interpolation: "curveLinear",
284
288
  yAxisMax: undefined,
285
289
  yAxisMin: undefined,
290
+
291
+ // Prevent CORS issues for relative locations (dynamic import)
292
+ srcFolder: ((document.currentScript && document.currentScript.src) || (({ url: (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('leaflet-elevation.js', document.baseURI).href)) }) && (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('leaflet-elevation.js', document.baseURI).href)))).split("/").slice(0,-1).join("/") + '/',
286
293
  };
287
294
 
288
295
  // "leaflet-i18n" fallback
@@ -290,9 +297,6 @@
290
297
  L._ = L.i18n = (string, data) => string;
291
298
  }
292
299
 
293
- // Prevent CORS issues for relative locations (dynamic import)
294
- const baseURL = ((document.currentScript && document.currentScript.src) || (({ url: (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('leaflet-elevation.js', document.baseURI).href)) }) && (typeof document === 'undefined' && typeof location === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : typeof document === 'undefined' ? location.href : (document.currentScript && document.currentScript.src || new URL('leaflet-elevation.js', document.baseURI).href)))).split("/").slice(0,-1).join("/") + '/';
295
-
296
300
  const Elevation = L.Control.Elevation = L.Control.extend({
297
301
 
298
302
  includes: L.Evented ? L.Evented.prototype : L.Mixin.Events,
@@ -311,7 +315,7 @@
311
315
  __LMARKER: '../src/components/marker.js',
312
316
  __LSUMMARY: '../src/components/summary.js',
313
317
  __modulesFolder: '../src/handlers/',
314
- __btnIcon: baseURL + '../images/elevation.svg',
318
+ __btnIcon: '../images/elevation.svg',
315
319
 
316
320
  /*
317
321
  * Add data to the diagram either from GPX or GeoJSON and update the axis domain and data
@@ -509,8 +513,7 @@
509
513
  case this.__LEDGESCALE: condition = typeof L.Control.EdgeScale !== 'function'; break;
510
514
  case this.__LHOTLINE: condition = typeof L.Hotline !== 'function'; break;
511
515
  }
512
- src = (src.startsWith('../') || src.startsWith('./') ? baseURL : '') + src;
513
- return condition !== false ? import(src) : Promise.resolve();
516
+ return condition !== false ? import(resolveURL(src, this.options.srcFolder)) : Promise.resolve();
514
517
  },
515
518
 
516
519
  /**
@@ -1000,7 +1003,7 @@
1000
1003
  on(link, 'click', L.DomEvent.stop);
1001
1004
  on(link, 'click', this._toggle, this);
1002
1005
  on(link, 'focus', this._toggle, this);
1003
- fetch(this.__btnIcon).then(r => r.ok && r.text().then(img => link.innerHTML = img));
1006
+ fetch(resolveURL(this.__btnIcon, this.options.srcFolder)).then(r => r.ok && r.text().then(img => link.innerHTML = img));
1004
1007
  }
1005
1008
  },
1006
1009