nakarte

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

index.js (3569B)


      1 import L from 'leaflet';
      2 import './style.css';
      3 import {fixVectorMarkerWorldJump} from './fixWorldCopyJump';
      4 
      5 // https://github.com/Leaflet/Leaflet/issues/3575
      6 function fixPanAnimationBug() {
      7     if (!L.Browser.chrome) {
      8         return;
      9     }
     10     if (L.PosAnimation.prototype.__panAnimationFixed) {
     11         return;
     12     }
     13 
     14     const originalStep = L.PosAnimation.prototype._step;
     15     L.PosAnimation.prototype._step = function() {
     16         return originalStep.call(this, true);
     17     };
     18     L.PosAnimation.prototype.__panAnimationFixed = true;
     19 }
     20 
     21 function fixTouchDetection() {
     22     L.Browser.touch &= ((navigator.pointerEnabled && !L.Browser.ie) || navigator.maxTouchPoints);
     23 }
     24 
     25 function fixMapKeypressEvent() {
     26     const originalHandleDOMEvent = L.Map.prototype._handleDOMEvent;
     27     L.Map.prototype._handleDOMEvent = function(e) {
     28         if (e.type === 'keypress' && e.keyCode === 13) {
     29             this._fireDOMEvent(e, e.type);
     30         } else {
     31             originalHandleDOMEvent.call(this, e);
     32         }
     33     };
     34 }
     35 
     36 function fixVectorDrawWhileAnimation() {
     37     if (L.Renderer.__animationFixed) {
     38         return;
     39     }
     40 
     41     const resetInterval = 300;
     42 
     43     const originalGetEvents = L.Renderer.prototype.getEvents;
     44 
     45     function onZoom() {
     46         const now = Date.now();
     47         if (!this._lastReset || (now - this._lastReset > resetInterval)) {
     48             this._reset();
     49             this._lastReset = now;
     50         } else {
     51             L.Renderer.prototype._onZoom.call(this);
     52         }
     53     }
     54 
     55     function onMove() {
     56         const now = Date.now();
     57         if (!this._lastReset || (now - this._lastReset > resetInterval)) {
     58             this._reset();
     59             this._lastReset = now;
     60         }
     61     }
     62 
     63     function getEvents() {
     64         const result = originalGetEvents.call(this);
     65         result.move = onMove;
     66         result.zoom = onZoom;
     67         return result;
     68     }
     69 
     70     L.Renderer.prototype.getEvents = getEvents;
     71     L.Renderer.__animationFixed = true;
     72 }
     73 
     74 function allowControlHorizontalStacking() {
     75     const original_addTo = L.Control.prototype.addTo;
     76     L.Control.prototype.addTo = function(map) {
     77         const result = original_addTo.call(this, map);
     78         if (this.options.stackHorizontally) {
     79             L.DomUtil.addClass(this._container, 'leaflet-control-horizontal-stack');
     80         }
     81         return result;
     82     };
     83 }
     84 
     85 function addTooltipDelay() {
     86     const origOpenTooltip = L.Layer.prototype._openTooltip;
     87     L.Layer.prototype._openTooltip = function(e) {
     88         if (this._tooltip.options.delay) {
     89             const that = this;
     90             this._pendingTooltip = setTimeout(() => origOpenTooltip.call(that, e), this._tooltip.options.delay);
     91         } else {
     92             origOpenTooltip.call(this, e);
     93         }
     94     };
     95 
     96     const origCloseTooltip = L.Layer.prototype.closeTooltip;
     97     L.Layer.prototype.closeTooltip = function() {
     98         clearInterval(this._pendingTooltip);
     99         origCloseTooltip.call(this);
    100     };
    101 }
    102 
    103 // Should become obsolete when https://github.com/Leaflet/Leaflet/issues/4696 is done
    104 function fixDoubleZoomOnMouseWheel() {
    105     const origGetWheelDelta = L.DomEvent.getWheelDelta;
    106     L.DomEvent.getWheelDelta = function(e) {
    107         const delta = origGetWheelDelta(e);
    108         return Math.sign(delta) * Math.min(Math.abs(delta), 60);
    109     };
    110 }
    111 
    112 function fixAll() {
    113     fixPanAnimationBug();
    114     fixTouchDetection();
    115     fixMapKeypressEvent();
    116     fixVectorDrawWhileAnimation();
    117     fixVectorMarkerWorldJump();
    118     allowControlHorizontalStacking();
    119     addTooltipDelay();
    120     fixDoubleZoomOnMouseWheel();
    121 }
    122 
    123 export {fixAll};