UNPKG

@visactor/vrender-components

Version:

components library for dp visualization

117 lines (108 loc) 6.09 kB
var __rest = this && this.__rest || function(s, e) { var t = {}; for (var p in s) Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0 && (t[p] = s[p]); if (null != s && "function" == typeof Object.getOwnPropertySymbols) { var i = 0; for (p = Object.getOwnPropertySymbols(s); i < p.length; i++) e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]) && (t[p[i]] = s[p[i]]); } return t; }; import { isValidNumber, merge } from "@visactor/vutils"; import { AbstractComponent } from "../core/base"; import { bitmapTool } from "./overlap"; import { LabelBase as PointLabel } from "./base"; import { getLabelComponent } from "./data-label-register"; import { appendExitReleaseCallback, runExitReleaseCallbacks } from "../animation/exit-release"; export class DataLabel extends AbstractComponent { constructor(attributes, options) { const {dataLabels: dataLabels} = attributes, restAttributes = __rest(attributes, [ "dataLabels" ]); super((null == options ? void 0 : options.skipDefault) ? attributes : Object.assign({ dataLabels: dataLabels }, merge({}, DataLabel.defaultAttributes, restAttributes))), this.name = "data-label"; } render() { var _a; if (this._exitReleaseState) return; const {dataLabels: dataLabels, size: size} = this.attribute; if (!dataLabels || 0 === dataLabels.length) return; const {width: width = 0, height: height = 0, padding: padding} = size || {}; if (!width || !height || !isValidNumber(height * width)) return; this._componentMap || (this._componentMap = new Map); const tool = bitmapTool(width, height, padding), bitmap = tool.bitmap(), currentComponentMap = new Map, prevComponentMap = this._componentMap; for (let i = 0; i < dataLabels.length; i++) { const dataLabel = dataLabels[i], labelComponent = getLabelComponent(dataLabel.type) || PointLabel; if (labelComponent) { const {baseMarkGroupName: baseMarkGroupName, type: type} = dataLabel, id = null !== (_a = dataLabel.id) && void 0 !== _a ? _a : `${baseMarkGroupName}-${type}-${i}`; "arc" === dataLabel.type && (dataLabel.width = size.width, dataLabel.height = size.height); let component = this._componentMap.get(id); component ? (component.setBitmapTool(tool), component.setBitmap(bitmap), component.setAttributes(dataLabel), currentComponentMap.set(id, component)) : (component = new labelComponent(dataLabel), component.setBitmap(bitmap), component.setBitmapTool(tool), this.add(component), currentComponentMap.set(id, component)); } } prevComponentMap.forEach(((cp, key) => { currentComponentMap.get(key) || this.removeChild(cp); })), this._componentMap = currentComponentMap; } _finalizeExitRelease() { var _a, _b, _c; const state = this._exitReleaseState; if (null == state ? void 0 : state.finalized) return; state && (state.finalized = !0); const parent = this.parent, removeFromParent = !!(null == state ? void 0 : state.removeFromParent), callbacks = null !== (_a = null == state ? void 0 : state.onComplete) && void 0 !== _a ? _a : []; this._exitReleaseState = void 0, null === (_b = this._componentMap) || void 0 === _b || _b.clear(), this.removeAllChild(!0), super.release(!0), removeFromParent && (null === (_c = null != parent ? parent : this.parent) || void 0 === _c || _c.removeChild(this)), runExitReleaseCallbacks(callbacks); } releaseWithExitAnimation(options = {}) { var _a; if ("released" === this.releaseStatus) return !1; if (this._exitReleaseState && !this._exitReleaseState.finalized) return this._exitReleaseState.removeFromParent = this._exitReleaseState.removeFromParent || !!options.removeFromParent, appendExitReleaseCallback(this._exitReleaseState, options.onComplete), !0; if (!this.stage || !(null === (_a = this._componentMap) || void 0 === _a ? void 0 : _a.size)) return !1; const state = { pendingCount: 0, finalized: !1, removeFromParent: !!options.removeFromParent, onComplete: options.onComplete ? [ options.onComplete ] : [] }, exitingComponents = [], fallbackComponents = []; let initializing = !0; const finish = () => { state.finalized || (state.pendingCount -= 1, state.pendingCount <= 0 && !initializing && this._finalizeExitRelease()); }; return this._exitReleaseState = state, this._componentMap.forEach((component => { state.pendingCount += 1; component.releaseWithExitAnimation({ removeFromParent: !1, onComplete: finish }) ? exitingComponents.push(component) : (state.pendingCount -= 1, fallbackComponents.push(component)); })), exitingComponents.length ? (fallbackComponents.forEach((component => { component.release(!0), this.removeChild(component); })), this.setAttribute("childrenPickable", !1), this.releaseStatus = "willRelease", initializing = !1, state.pendingCount <= 0 && this._finalizeExitRelease(), !0) : (this._exitReleaseState = void 0, !1); } release(all) { var _a; this._exitReleaseState ? this._finalizeExitRelease() : (all && this.removeAllChild(!0), super.release(all), null === (_a = this._componentMap) || void 0 === _a || _a.clear()); } setLocation(point) { this.translateTo(point.x, point.y); } disableAnimation() { this._componentMap.forEach((component => { component.disableAnimation(); })); } enableAnimation() { this._componentMap.forEach((component => { component.enableAnimation(); })); } } DataLabel.defaultAttributes = { pickable: !1 }; //# sourceMappingURL=dataLabel.js.map