Code coverage report for src/layer/vector/Path.SVG.js

Statements: 63.64% (56 / 88)      Branches: 44.83% (26 / 58)      Functions: 56.25% (9 / 16)      Lines: 65.12% (56 / 86)     

All files » src/layer/vector/ » Path.SVG.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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220        1   1   1                                                             18       6 6 6       6   6 6       6 6 6   6 5   6     6     6       7 7 7 7 7     7         7 6 6   1         14 14   1   14         6 6 6     6   6   6 36                                                                     1   6 6 6   6               6     6 6                                       6             6   6                 6       6 6 6 6   6          
/*
 * Extends L.Path with SVG-specific rendering code.
 */
 
L.Path.SVG_NS = 'http://www.w3.org/2000/svg';
 
L.Browser.svg = !!(document.createElementNS && document.createElementNS(L.Path.SVG_NS, 'svg').createSVGRect);
 
L.Path = L.Path.extend({
	statics: {
		SVG: L.Browser.svg
	},
 
	bringToFront: function () {
		var root = this._map._pathRoot,
		    path = this._container;
 
		if (path && root.lastChild !== path) {
			root.appendChild(path);
		}
		return this;
	},
 
	bringToBack: function () {
		var root = this._map._pathRoot,
		    path = this._container,
		    first = root.firstChild;
 
		if (path && first !== path) {
			root.insertBefore(path, first);
		}
		return this;
	},
 
	getPathString: function () {
		// form path string here
	},
 
	_createElement: function (name) {
		return document.createElementNS(L.Path.SVG_NS, name);
	},
 
	_initElements: function () {
		this._map._initPathRoot();
		this._initPath();
		this._initStyle();
	},
 
	_initPath: function () {
		this._container = this._createElement('g');
 
		this._path = this._createElement('path');
		this._container.appendChild(this._path);
	},
 
	_initStyle: function () {
		Eif (this.options.stroke) {
			this._path.setAttribute('stroke-linejoin', 'round');
			this._path.setAttribute('stroke-linecap', 'round');
		}
		if (this.options.fill) {
			this._path.setAttribute('fill-rule', 'evenodd');
		}
		Iif (this.options.pointerEvents) {
			this._path.setAttribute('pointer-events', this.options.pointerEvents);
		}
		Iif (!this.options.clickable && !this.options.pointerEvents) {
			this._path.setAttribute('pointer-events', 'none');
		}
		this._updateStyle();
	},
 
	_updateStyle: function () {
		Eif (this.options.stroke) {
			this._path.setAttribute('stroke', this.options.color);
			this._path.setAttribute('stroke-opacity', this.options.opacity);
			this._path.setAttribute('stroke-width', this.options.weight);
			Iif (this.options.dashArray) {
				this._path.setAttribute('stroke-dasharray', this.options.dashArray);
			} else {
				this._path.removeAttribute('stroke-dasharray');
			}
		} else {
			this._path.setAttribute('stroke', 'none');
		}
		if (this.options.fill) {
			this._path.setAttribute('fill', this.options.fillColor || this.options.color);
			this._path.setAttribute('fill-opacity', this.options.fillOpacity);
		} else {
			this._path.setAttribute('fill', 'none');
		}
	},
 
	_updatePath: function () {
		var str = this.getPathString();
		if (!str) {
			// fix webkit empty string parsing bug
			str = 'M0 0';
		}
		this._path.setAttribute('d', str);
	},
 
	// TODO remove duplication with L.Map
	_initEvents: function () {
		Eif (this.options.clickable) {
			Eif (L.Browser.svg || !L.Browser.vml) {
				this._path.setAttribute('class', 'leaflet-clickable');
			}
 
			L.DomEvent.on(this._container, 'click', this._onMouseClick, this);
 
			var events = ['dblclick', 'mousedown', 'mouseover',
			              'mouseout', 'mousemove', 'contextmenu'];
			for (var i = 0; i < events.length; i++) {
				L.DomEvent.on(this._container, events[i], this._fireMouseEvent, this);
			}
		}
	},
 
	_onMouseClick: function (e) {
		if (this._map.dragging && this._map.dragging.moved()) { return; }
 
		this._fireMouseEvent(e);
	},
 
	_fireMouseEvent: function (e) {
		if (!this.hasEventListeners(e.type)) { return; }
 
		var map = this._map,
		    containerPoint = map.mouseEventToContainerPoint(e),
		    layerPoint = map.containerPointToLayerPoint(containerPoint),
		    latlng = map.layerPointToLatLng(layerPoint);
 
		this.fire(e.type, {
			latlng: latlng,
			layerPoint: layerPoint,
			containerPoint: containerPoint,
			originalEvent: e
		});
 
		if (e.type === 'contextmenu') {
			L.DomEvent.preventDefault(e);
		}
		if (e.type !== 'mousemove') {
			L.DomEvent.stopPropagation(e);
		}
	}
});
 
L.Map.include({
	_initPathRoot: function () {
		Eif (!this._pathRoot) {
			this._pathRoot = L.Path.prototype._createElement('svg');
			this._panes.overlayPane.appendChild(this._pathRoot);
 
			Iif (this.options.zoomAnimation && L.Browser.any3d) {
				this._pathRoot.setAttribute('class', ' leaflet-zoom-animated');
 
				this.on({
					'zoomanim': this._animatePathZoom,
					'zoomend': this._endPathZoom
				});
			} else {
				this._pathRoot.setAttribute('class', ' leaflet-zoom-hide');
			}
 
			this.on('moveend', this._updateSvgViewport);
			this._updateSvgViewport();
		}
	},
 
	_animatePathZoom: function (e) {
		var scale = this.getZoomScale(e.zoom),
		    offset = this._getCenterOffset(e.center)._multiplyBy(-scale)._add(this._pathViewport.min);
 
		this._pathRoot.style[L.DomUtil.TRANSFORM] =
		        L.DomUtil.getTranslateString(offset) + ' scale(' + scale + ') ';
 
		this._pathZooming = true;
	},
 
	_endPathZoom: function () {
		this._pathZooming = false;
	},
 
	_updateSvgViewport: function () {
 
		Iif (this._pathZooming) {
			// Do not update SVGs while a zoom animation is going on otherwise the animation will break.
			// When the zoom animation ends we will be updated again anyway
			// This fixes the case where you do a momentum move and zoom while the move is still ongoing.
			return;
		}
 
		this._updatePathViewport();
 
		var vp = this._pathViewport,
		    min = vp.min,
		    max = vp.max,
		    width = max.x - min.x,
		    height = max.y - min.y,
		    root = this._pathRoot,
		    pane = this._panes.overlayPane;
 
		// Hack to make flicker on drag end on mobile webkit less irritating
		Iif (L.Browser.mobileWebkit) {
			pane.removeChild(root);
		}
 
		L.DomUtil.setPosition(root, min);
		root.setAttribute('width', width);
		root.setAttribute('height', height);
		root.setAttribute('viewBox', [min.x, min.y, width, height].join(' '));
 
		Iif (L.Browser.mobileWebkit) {
			pane.appendChild(root);
		}
	}
});