nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

hash-state.js (2287B)


      1 import L from 'leaflet';
      2 import '~/lib/leaflet.hashState/leaflet.hashState';
      3 
      4 L.Control.JNX.include(L.Mixin.HashState);
      5 L.Control.JNX.include({
      6         stateChangeEvents: ['settingschange'],
      7 
      8         serializeState: function() {
      9             let state;
     10             if (this._areaSelector) {
     11                 const bounds = this._areaSelector.getBounds();
     12                 state = [
     13                     bounds.getSouth().toFixed(5),
     14                     bounds.getWest().toFixed(5),
     15                     bounds.getNorth().toFixed(5),
     16                     bounds.getEast().toFixed(5),
     17                     this.zoomLevel() ?? '',
     18                     this.fixZoom() ? '1' : '0',
     19                 ];
     20             }
     21             return state;
     22         },
     23 
     24         unserializeState: function(values) {
     25             function validateFloat(value) {
     26                 value = parseFloat(value);
     27                 if (isNaN(value)) {
     28                     throw new Error('INVALID VALUE');
     29                 }
     30                 return value;
     31             }
     32 
     33             function validateFloatRange(value, min, max) {
     34                 value = validateFloat(value);
     35                 if (value < min || value > max) {
     36                     throw new Error('INVALID VALUE');
     37                 }
     38                 return value;
     39             }
     40 
     41             if (values && values.length >= 4) {
     42                 let south, west, north, east;
     43                 try {
     44                         south = validateFloatRange(values[0], -86, 86);
     45                         west = validateFloat(values[1]);
     46                         north = validateFloatRange(values[2], -86, 86);
     47                         east = validateFloat(values[3]);
     48                 } catch (e) {
     49                     if (e.message === 'INVALID VALUE') {
     50                         return false;
     51                     }
     52                     throw e;
     53                 }
     54                 this.setAreaSelector([[south, west], [north, east]]);
     55 
     56                 let zoomLevel = parseInt(values[4], 10);
     57                 if (!this.zoomChoices()?.[zoomLevel]) {
     58                     zoomLevel = null;
     59                 }
     60                 this.zoomLevel(zoomLevel);
     61 
     62                 this.fixZoom(values[5] === '1');
     63 
     64                 return true;
     65             }
     66             return false;
     67         }
     68     }
     69 );