Code coverage report for src/map/handler/Map.Keyboard.js

Statements: 58.7% (27 / 46)      Branches: 20% (2 / 10)      Functions: 45.45% (5 / 11)      Lines: 58.7% (27 / 46)     

All files » src/map/handler/ » Map.Keyboard.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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141        1           1                       50   50 50       50     50 50     50         50                                         1 1                             50       50 50   50 50   50 50   50 50         50       50 150   50 150                                                                   1  
/*
 * L.Map.Keyboard is handling keyboard interaction with the map, enabled by default.
 */
 
L.Map.mergeOptions({
	keyboard: true,
	keyboardPanOffset: 80,
	keyboardZoomOffset: 1
});
 
L.Map.Keyboard = L.Handler.extend({
 
	keyCodes: {
		left:    [37],
		right:   [39],
		down:    [40],
		up:      [38],
		zoomIn:  [187, 107, 61],
		zoomOut: [189, 109, 173]
	},
 
	initialize: function (map) {
		this._map = map;
 
		this._setPanOffset(map.options.keyboardPanOffset);
		this._setZoomOffset(map.options.keyboardZoomOffset);
	},
 
	addHooks: function () {
		var container = this._map._container;
 
		// make the container focusable by tabbing
		Eif (container.tabIndex === -1) {
			container.tabIndex = "0";
		}
 
		L.DomEvent
		    .on(container, 'focus', this._onFocus, this)
		    .on(container, 'blur', this._onBlur, this)
		    .on(container, 'mousedown', this._onMouseDown, this);
 
		this._map
		    .on('focus', this._addHooks, this)
		    .on('blur', this._removeHooks, this);
	},
 
	removeHooks: function () {
		this._removeHooks();
 
		var container = this._map._container;
 
		L.DomEvent
		    .off(container, 'focus', this._onFocus, this)
		    .off(container, 'blur', this._onBlur, this)
		    .off(container, 'mousedown', this._onMouseDown, this);
 
		this._map
		    .off('focus', this._addHooks, this)
		    .off('blur', this._removeHooks, this);
	},
 
	_onMouseDown: function () {
		Eif (!this._focused) {
			this._map._container.focus();
		}
	},
 
	_onFocus: function () {
		this._focused = true;
		this._map.fire('focus');
	},
 
	_onBlur: function () {
		this._focused = false;
		this._map.fire('blur');
	},
 
	_setPanOffset: function (pan) {
		var keys = this._panKeys = {},
		    codes = this.keyCodes,
		    i, len;
 
		for (i = 0, len = codes.left.length; i < len; i++) {
			keys[codes.left[i]] = [-1 * pan, 0];
		}
		for (i = 0, len = codes.right.length; i < len; i++) {
			keys[codes.right[i]] = [pan, 0];
		}
		for (i = 0, len = codes.down.length; i < len; i++) {
			keys[codes.down[i]] = [0, pan];
		}
		for (i = 0, len = codes.up.length; i < len; i++) {
			keys[codes.up[i]] = [0, -1 * pan];
		}
	},
 
	_setZoomOffset: function (zoom) {
		var keys = this._zoomKeys = {},
		    codes = this.keyCodes,
		    i, len;
 
		for (i = 0, len = codes.zoomIn.length; i < len; i++) {
			keys[codes.zoomIn[i]] = zoom;
		}
		for (i = 0, len = codes.zoomOut.length; i < len; i++) {
			keys[codes.zoomOut[i]] = -zoom;
		}
	},
 
	_addHooks: function () {
		L.DomEvent.on(document, 'keydown', this._onKeyDown, this);
	},
 
	_removeHooks: function () {
		L.DomEvent.off(document, 'keydown', this._onKeyDown, this);
	},
 
	_onKeyDown: function (e) {
		var key = e.keyCode,
		    map = this._map;
 
		if (this._panKeys.hasOwnProperty(key)) {
			map.panBy(this._panKeys[key]);
 
			if (map.options.maxBounds) {
				map.panInsideBounds(map.options.maxBounds);
			}
 
		} else if (this._zoomKeys.hasOwnProperty(key)) {
			map.setZoom(map.getZoom() + this._zoomKeys[key]);
 
		} else {
			return;
		}
 
		L.DomEvent.stop(e);
	}
});
 
L.Map.addInitHook('addHandler', 'keyboard', L.Map.Keyboard);