@osimatic/helpers-js 1.3.8 → 1.4.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/CHANGELOG CHANGED
@@ -75,4 +75,7 @@ Duration.convertToDurationAsCentieme() -> Duration.convertToDurationAsHundredthO
75
75
 
76
76
  1.3.0
77
77
  HTTPRequest -> HTTPClient
78
+ PostalAddress.setAutocomplete(input, onChange(formatted_address)) -> PostalAddress.setAutocomplete(input, onChange(place));
78
79
 
80
+ 1.4.0
81
+ new OpenStreetMap(id) -> new OpenStreetMap(element)
package/index.js CHANGED
@@ -16,7 +16,7 @@ const { DateTime, DatePeriod, TimestampUnix, SqlDate, SqlTime, SqlDateTime } = r
16
16
  const { Duration } = require('./duration');
17
17
  const { File, CSV, Img } = require('./file');
18
18
  const { FormHelper, EditValue } = require('./form_helper');
19
- const { Country, PostalAddress, GeographicCoordinates } = require('./location');
19
+ const { Country, PostalAddress, GeographicCoordinates, Polygon } = require('./location');
20
20
  const { HexColor, RgbColor } = require('./draw');
21
21
  const { SocialNetwork } = require('./social_network');
22
22
  const { sleep, refresh } = require('./util');
@@ -52,7 +52,7 @@ const { WebSocket } = require('./web_socket');
52
52
 
53
53
  module.exports = {
54
54
  Array, Object, Number, String,
55
- HTTPClient, Cookie, UrlAndQueryString, IBAN, BankCard, AudioMedia, VideoMedia, UserMedia, PersonName, Email, TelephoneNumber, DateTime, DatePeriod, TimestampUnix, SqlDate, SqlTime, SqlDateTime, Duration, File, CSV, Img, FormHelper, Country, PostalAddress, GeographicCoordinates, HexColor, RgbColor, SocialNetwork, NumberFormatter, Password,
55
+ HTTPClient, Cookie, UrlAndQueryString, IBAN, BankCard, AudioMedia, VideoMedia, UserMedia, PersonName, Email, TelephoneNumber, DateTime, DatePeriod, TimestampUnix, SqlDate, SqlTime, SqlDateTime, Duration, File, CSV, Img, FormHelper, Country, PostalAddress, GeographicCoordinates, Polygon, HexColor, RgbColor, SocialNetwork, NumberFormatter, Password,
56
56
  Browser, DataTable, Pagination, Navigation, DetailsSubArray, SelectAll, MultipleActionInTable, MultipleActionInDivList, MultiFilesInput, EditValue, FormDate, InputPeriod, ShoppingCart, FlashMessage, CountDown, ImportFromCsv, JwtToken, JwtSession, ApiTokenSession, ListBox, WebRTC, WebSocket, EventBus,
57
57
  sleep, refresh, chr, ord, trim, empty,
58
58
  Chartjs, GoogleCharts, GoogleRecaptcha, GoogleMap, OpenStreetMap
package/location.js CHANGED
@@ -296,20 +296,24 @@ class Country {
296
296
 
297
297
  class PostalAddress {
298
298
  static setAutocomplete(input, onPlaceChanged) {
299
- let autocomplete = new google.maps.places.Autocomplete(
299
+ const autocomplete = new google.maps.places.Autocomplete(
300
300
  input[0],
301
301
  {types: ['geocode']}
302
302
  );
303
- //console.log(autocomplete);
304
303
 
305
304
  // When the user selects an address from the dropdown, populate the address fields in the form.
305
+
306
+ if (typeof onPlaceChanged != 'function') {
307
+ return;
308
+ }
309
+
306
310
  autocomplete.addListener('place_changed', function() {
307
311
  let place = autocomplete.getPlace();
308
312
  input.val('');
309
- if (typeof onPlaceChanged == 'function') {
310
- onPlaceChanged(place['formatted_address']);
311
- }
313
+ onPlaceChanged(place);
312
314
  });
315
+
316
+ return autocomplete;
313
317
  }
314
318
 
315
319
  static format(addressData, separator='<br/>') {
@@ -423,20 +427,104 @@ class PostalAddress {
423
427
  }
424
428
  }
425
429
 
430
+ class Polygon {
431
+ static polygonSummary(geoJsonPolygon) {
432
+ const rings = geoJsonPolygon.coordinates || [];
433
+ const ring0 = rings[0] || [];
434
+ const n = ring0.length ? ring0.length - (JSON.stringify(ring0[0])===JSON.stringify(ring0[ring0.length-1]) ? 1 : 0) : 0;
435
+ if (ring0.length === 0) {
436
+ return {label:'Polygon (vide)', title:''};
437
+ }
438
+
439
+ const [firstLon, firstLat] = ring0[0];
440
+
441
+ const maxShow = Math.min(5, ring0.length);
442
+ const concise = ring0.slice(0, maxShow).map(([lon,lat]) => GeographicCoordinates.format(lat, lon)).join(' · ');
443
+ const more = ring0.length > maxShow ? ` … (+${ring0.length - maxShow} sommets)` : '';
444
+
445
+ return {
446
+ label: `Polygon • sommets: ${n} • départ: ${GeographicCoordinates.format(firstLat, firstLon)}`,
447
+ title: `Sommets: ${concise}${more}`
448
+ };
449
+ }
450
+
451
+ static convertToGeoJson(latlngs) {
452
+ let rings = [];
453
+ if (Array.isArray(latlngs) && Array.isArray(latlngs[0])) {
454
+ for (const ring of latlngs) {
455
+ rings.push(ring.map(ll => [Number(ll.lng), Number(ll.lat)]));
456
+ }
457
+ }
458
+ else {
459
+ rings = [latlngs.map(ll => [Number(ll.lng), Number(ll.lat)])];
460
+ }
461
+ rings = rings.map(r => {
462
+ if (r.length >= 3) {
463
+ const [lon0,lat0] = r[0];
464
+ const [lonL,latL] = r[r.length-1];
465
+ if (lon0 !== lonL || lat0 !== latL) r.push([lon0,lat0]);
466
+ }
467
+ return r;
468
+ });
469
+ return { type: 'Polygon', coordinates: rings };
470
+ }
471
+ }
472
+
426
473
  class GeographicCoordinates {
427
474
  static check(str) {
428
475
  return /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/.test(str);
429
476
  }
430
477
 
431
- static parse(str) {
478
+ static parse(str, asString=false) {
432
479
  if (null === str || '' === str || 'NaN,NaN' === str) {
433
480
  return null;
434
481
  }
435
482
 
436
- return str.split(',').map(str => str.trim()).join(',');
483
+ str = (str||'').replace(';', ',').trim().replace(/\s+/g, '');
484
+ const parts = str.split(',');
485
+ if (parts.length !== 2) {
486
+ return null;
487
+ }
488
+
489
+ const lat = parseFloat(parts[0]);
490
+ const lon = parseFloat(parts[1]);
491
+ if (!isFinite(lat) || !isFinite(lon)) {
492
+ return null;
493
+ }
494
+
495
+ return asString ? str.join(',') : [lat, lon];
437
496
  }
438
497
 
498
+ static toFixed(latOrLong, fractionDigit=6) {
499
+ return Number(latOrLong).toFixed(fractionDigit);
500
+ }
501
+
502
+ static format(lat, long, fractionDigit=6) {
503
+ return GeographicCoordinates.toFixed(lat, fractionDigit)+','+GeographicCoordinates.toFixed(long, fractionDigit);
504
+ }
505
+
506
+ static formatPoint(geoJsonPoint, fractionDigit=6) {
507
+ const [long, lat] = geoJsonPoint.coordinates || [];
508
+ if (typeof long == 'undefined' || typeof lat == 'undefined') {
509
+ return '';
510
+ }
511
+ return GeographicCoordinates.format(lat, long, fractionDigit);
512
+ }
513
+
514
+ static convertToGeoJson(lat, long) {
515
+ return { type: 'Point', coordinates: [Number(long), Number(lat)] };
516
+ }
517
+
518
+ static haversine(lat1, long1, lat2, long2) {
519
+ const R = 6371000;
520
+ const toRad = d => d * Math.PI / 180;
521
+ const dLat = toRad(lat2 - lat1);
522
+ const dLong = toRad(long2 - long1);
523
+ const d1 = toRad(lat1), d2 = toRad(lat2);
524
+ const a = Math.sin(dLat/2)**2 + Math.cos(d1)*Math.cos(d2)*Math.sin(dLong/2)**2;
525
+ return 2 * R * Math.asin(Math.min(1, Math.sqrt(a)));
526
+ }
439
527
 
440
528
  }
441
529
 
442
- module.exports = { Country, PostalAddress, GeographicCoordinates };
530
+ module.exports = { Country, PostalAddress, GeographicCoordinates, Polygon };
@@ -5,7 +5,7 @@
5
5
  */
6
6
  class OpenStreetMap {
7
7
 
8
- constructor(mapId, options) {
8
+ constructor(mapContainer, options) {
9
9
  /*let [lat, lng] = button.data('coordinates').split(',');
10
10
  let map = L.map('modal_map_canvas2').setView([lat, lng], 17);
11
11
 
@@ -20,18 +20,17 @@ class OpenStreetMap {
20
20
 
21
21
  this.markers = [];
22
22
  this.locations = [];
23
- this.mapId = mapId;
24
23
 
25
- if (!$('#'+mapId).length) {
24
+ if (!mapContainer.length) {
26
25
  return;
27
26
  }
28
27
 
29
- let container = L.DomUtil.get(mapId);
28
+ const container = L.DomUtil.get(mapContainer[0]);
30
29
  if (container != null) {
31
30
  container._leaflet_id = null;
32
31
  }
33
32
 
34
- this.map = L.map(mapId, options || {});
33
+ this.map = L.map(mapContainer[0], options || {});
35
34
 
36
35
  L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
37
36
  attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.3.8",
3
+ "version": "1.4.1",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"