UNPKG

cione-comp-lib

Version:

`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`

163 lines (162 loc) 5.03 kB
import { parseSVG, makeViewBoxTransform } from "../../../../zrender/lib/tool/parseSVG.mjs"; import Group from "../../../../zrender/lib/graphic/Group.mjs"; import Rect from "../../../../zrender/lib/graphic/shape/Rect.mjs"; import { createHashMap, assert, each } from "../../../../zrender/lib/core/util.mjs"; import BoundingRect from "../../../../zrender/lib/core/BoundingRect.mjs"; import { parseXML } from "../../../../zrender/lib/tool/parseXML.mjs"; import { GeoSVGRegion } from "./Region.mjs"; var REGION_AVAILABLE_SVG_TAG_MAP = createHashMap([ "rect", "circle", "line", "ellipse", "polygon", "polyline", "path", "text", "tspan", "g" ]); var GeoSVGResource = function() { function GeoSVGResource2(mapName, svg) { this.type = "geoSVG"; this._usedGraphicMap = createHashMap(); this._freedGraphics = []; this._mapName = mapName; this._parsedXML = parseXML(svg); } GeoSVGResource2.prototype.load = function() { var firstGraphic = this._firstGraphic; if (!firstGraphic) { firstGraphic = this._firstGraphic = this._buildGraphic(this._parsedXML); this._freedGraphics.push(firstGraphic); this._boundingRect = this._firstGraphic.boundingRect.clone(); var _a = createRegions(firstGraphic.named), regions = _a.regions, regionsMap = _a.regionsMap; this._regions = regions; this._regionsMap = regionsMap; } return { boundingRect: this._boundingRect, regions: this._regions, regionsMap: this._regionsMap }; }; GeoSVGResource2.prototype._buildGraphic = function(svgXML) { var result; var rootFromParse; try { result = svgXML && parseSVG(svgXML, { ignoreViewBox: true, ignoreRootClip: true }) || {}; rootFromParse = result.root; assert(rootFromParse != null); } catch (e) { throw new Error("Invalid svg format\n" + e.message); } var root = new Group(); root.add(rootFromParse); root.isGeoSVGGraphicRoot = true; var svgWidth = result.width; var svgHeight = result.height; var viewBoxRect = result.viewBoxRect; var boundingRect = this._boundingRect; if (!boundingRect) { var bRectX = void 0; var bRectY = void 0; var bRectWidth = void 0; var bRectHeight = void 0; if (svgWidth != null) { bRectX = 0; bRectWidth = svgWidth; } else if (viewBoxRect) { bRectX = viewBoxRect.x; bRectWidth = viewBoxRect.width; } if (svgHeight != null) { bRectY = 0; bRectHeight = svgHeight; } else if (viewBoxRect) { bRectY = viewBoxRect.y; bRectHeight = viewBoxRect.height; } if (bRectX == null || bRectY == null) { var calculatedBoundingRect = rootFromParse.getBoundingRect(); if (bRectX == null) { bRectX = calculatedBoundingRect.x; bRectWidth = calculatedBoundingRect.width; } if (bRectY == null) { bRectY = calculatedBoundingRect.y; bRectHeight = calculatedBoundingRect.height; } } boundingRect = this._boundingRect = new BoundingRect(bRectX, bRectY, bRectWidth, bRectHeight); } if (viewBoxRect) { var viewBoxTransform = makeViewBoxTransform(viewBoxRect, boundingRect); rootFromParse.scaleX = rootFromParse.scaleY = viewBoxTransform.scale; rootFromParse.x = viewBoxTransform.x; rootFromParse.y = viewBoxTransform.y; } root.setClipPath(new Rect({ shape: boundingRect.plain() })); var named = []; each(result.named, function(namedItem) { if (REGION_AVAILABLE_SVG_TAG_MAP.get(namedItem.svgNodeTagLower) != null) { named.push(namedItem); setSilent(namedItem.el); } }); return { root, boundingRect, named }; }; GeoSVGResource2.prototype.useGraphic = function(hostKey) { var usedRootMap = this._usedGraphicMap; var svgGraphic = usedRootMap.get(hostKey); if (svgGraphic) { return svgGraphic; } svgGraphic = this._freedGraphics.pop() || this._buildGraphic(this._parsedXML); usedRootMap.set(hostKey, svgGraphic); return svgGraphic; }; GeoSVGResource2.prototype.freeGraphic = function(hostKey) { var usedRootMap = this._usedGraphicMap; var svgGraphic = usedRootMap.get(hostKey); if (svgGraphic) { usedRootMap.removeKey(hostKey); this._freedGraphics.push(svgGraphic); } }; return GeoSVGResource2; }(); function setSilent(el) { el.silent = false; if (el.isGroup) { el.traverse(function(child) { child.silent = false; }); } } function createRegions(named) { var regions = []; var regionsMap = createHashMap(); each(named, function(namedItem) { if (namedItem.namedFrom != null) { return; } var region = new GeoSVGRegion(namedItem.name, namedItem.el); regions.push(region); regionsMap.set(namedItem.name, region); }); return { regions, regionsMap }; } export { GeoSVGResource };