# Code coverage report for src/map/handler/Map.ScrollWheelZoom.js

## Statements: 17.86% (5 / 28) Branches: 0% (0 / 6) Functions: 20% (1 / 5) Lines: 18.52% (5 / 27)

All files » src/map/handler/ » Map.ScrollWheelZoom.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71

    1

    1

    50
    50

    1
     /*
     * L.Handler.ScrollWheelZoom is used by L.Map to enable mouse scroll wheel zoom on the map.
     */

    L.Map.mergeOptions({
    	scrollWheelZoom: true
    });

    L.Map.ScrollWheelZoom = L.Handler.extend({
    	addHooks: function () {
    		L.DomEvent.on(this._map._container, 'mousewheel', this._onWheelScroll, this);
    		this._delta = 0;
    	},

    	removeHooks: function () {
    		L.DomEvent.off(this._map._container, 'mousewheel', this._onWheelScroll);
    	},

    	_onWheelScroll: function (e) {
    		var delta = L.DomEvent.getWheelDelta(e);

    		this._delta += delta;
    		this._lastMousePos = this._map.mouseEventToContainerPoint(e);

    		if (!this._startTime) {
    			this._startTime = +new Date();
    		}

    		var left = Math.max(40 - (+new Date() - this._startTime), 0);

    		clearTimeout(this._timer);
    		this._timer = setTimeout(L.bind(this._performZoom, this), left);

    		L.DomEvent.preventDefault(e);
    		L.DomEvent.stopPropagation(e);
    	},

    	_performZoom: function () {
    		var map = this._map,
    		    delta = this._delta,
    		    zoom = map.getZoom();

    		delta = delta > 0 ? Math.ceil(delta) : Math.round(delta);
    		delta = Math.max(Math.min(delta, 4), -4);
    		delta = map._limitZoom(zoom + delta) - zoom;

    		this._delta = 0;

    		this._startTime = null;

    		if (!delta) { return; }

    		var newZoom = zoom + delta,
    		    newCenter = this._getCenterForScrollWheelZoom(newZoom);

    		map.setView(newCenter, newZoom);
    	},

    	_getCenterForScrollWheelZoom: function (newZoom) {
    		var map = this._map,
    		    scale = map.getZoomScale(newZoom),
    		    viewHalf = map.getSize()._divideBy(2),
    		    centerOffset = this._lastMousePos._subtract(viewHalf)._multiplyBy(1 - 1 / scale),
    		    newCenterPoint = map._getTopLeftPoint()._add(viewHalf)._add(centerOffset);

    		return map.unproject(newCenterPoint);
    	}
    });

    L.Map.addInitHook('addHandler', 'scrollWheelZoom', L.Map.ScrollWheelZoom);