{"version":3,"file":"WeatherLabel.mjs","sources":["../../../../../../../packages/sdk/plugins/weather/Weather/WeatherLabel.ts"],"sourcesContent":["import L from 'leaflet'\nimport type { MyMap } from '@map-sdk/sdk/ShipxyAPISDK'\n\ninterface WeatherLabelOptions extends L.LayerOptions {\n  size?: number\n  direction?: number\n  stroke?: boolean\n  color?: string\n  weight?: number\n  text?: string\n  textOptions?: {\n    repeat?: boolean\n    fillColor?: string\n    attributes?: Record<string, string>\n    below?: boolean\n    dataAnchor?: string\n    offset?: number\n    center?: boolean\n  }\n}\n\nclass WeatherLabel extends L.Path {\n  private _latlng: L.LatLng\n  private _size: number\n  private _direction: number\n  private _windSymbol: number[][]\n  private _text!: string\n  private _textOptions: WeatherLabelOptions['textOptions']\n  private _textNode: SVGTextElement | undefined\n\n  constructor(\n    latlng: L.LatLngLiteral | L.LatLng,\n    options?: WeatherLabelOptions\n  ) {\n    super(options)\n\n    if (latlng === undefined) {\n      throw new Error('Please give a valid lat/lon-position')\n    }\n\n    L.setOptions(this, options || {})\n\n    // this._leaflet_id = this._id\n    this._latlng = L.latLng(latlng)\n    this._size = options?.size || 16\n    this._direction = ((options?.direction || 0) * Math.PI) / 180\n    this._windSymbol = [\n      [0.5, 0, -0.5, 0],\n      [-0.3, 0.2, -0.5, 0, -0.3, -0.2],\n    ]\n    this._windSymbol.push(\n      [0.5, 0, 0.5, -0.2 * 2],\n      [0.25, 0, 0.25, -0.2 * 2],\n      [0, 0, 0, -0.2 * 1]\n    )\n\n    this.setStroke(options?.stroke || true)\n    this.setColor(options?.color || '#ff0000')\n    this.setWeight(options?.weight || 1)\n  }\n\n  setColor(color: string): this {\n    this.setStyle({ color })\n    return this.redraw()\n  }\n\n  setFillOpacity(fillOpacity: number): this {\n    this.setStyle({ fillOpacity })\n    return this.redraw()\n  }\n\n  setOpacity(opacity: number): this {\n    this.setStyle({ opacity })\n    return this.redraw()\n  }\n\n  setWeight(weight: number): this {\n    this.setStyle({ weight })\n    return this.redraw()\n  }\n\n  setFill(fill: boolean): this {\n    this.setStyle({ fill })\n    return this.redraw()\n  }\n\n  setStroke(stroke: boolean): this {\n    this.setStyle({ stroke })\n    return this.redraw()\n  }\n\n  setFillColor(fillColor: string): this {\n    this.setStyle({ fillColor })\n    return this\n  }\n\n  // eslint-disable-next-line @typescript-eslint/no-empty-function\n  protected _project(): void {}\n\n  protected _update(): void {\n    this._setPath()\n  }\n\n  protected _setPath(): void {\n    const _ = this as any\n    _._path.setAttribute('d', this.getPathString())\n  }\n\n  setLatLng(latlng: L.LatLngLiteral | L.LatLng): this {\n    const oldLatLng = this._latlng\n    this._latlng = L.latLng(latlng)\n    this.fire('move', { oldLatLng, latlng })\n    return this.redraw()\n  }\n\n  setDirection(direction: number): this {\n    this._direction = direction\n    return this.redraw()\n  }\n\n  private _textRedraw() {\n    const text = this._text\n    const textOptions = this._textOptions\n    if (text) {\n      this.setText('', {}).setText(text, textOptions)\n    }\n  }\n\n  override onAdd(map: MyMap): this {\n    this._textRedraw()\n    return super.onAdd(map)\n  }\n\n  setText(text: string, options?: WeatherLabelOptions['textOptions']): this {\n    this._text = text\n    this._textOptions = options || {}\n\n    if (!L.Browser.svg || this._map === undefined) {\n      return this\n    }\n\n    options = L.Util.extend(\n      {\n        repeat: false,\n        fillColor: 'black',\n        attributes: {},\n        below: false,\n        'data-anchor': null,\n        offset: undefined,\n        center: false,\n      },\n      options\n    )\n\n    if (!text) {\n      if (this._textNode && this._textNode.parentNode) {\n        ;(this._map as any)._renderer.removeChild(this._textNode)\n        this._textNode = undefined\n      }\n      return this\n    }\n\n    text = text.replace(/ /g, '\\u00A0')\n\n    const pathDefId = `pathdef-${L.Util.stamp(this)}`\n    const rendererContainer = (this._map as any)._renderer\n      ._container as SVGSVGElement\n    const svgText = L.SVG.create('text') as SVGTextElement\n    const textPath = L.SVG.create('textPath') as SVGTextPathElement\n    const strokeWidth =\n      options.offset ||\n      Number.parseFloat((this as any)._path.getAttribute('stroke-width'))\n\n    textPath.setAttributeNS(\n      'http://www.w3.org/1999/xlink',\n      'xlink:href',\n      `#${pathDefId}`\n    )\n    svgText.setAttribute('dy', strokeWidth.toString())\n\n    for (const attrName in options.attributes) {\n      svgText.setAttribute(attrName, options.attributes[attrName])\n    }\n\n    if (options as any['data-anchor']) {\n      svgText.dataset.anchor = options as any['data-anchor']\n    }\n\n    textPath.appendChild(document.createTextNode(text))\n    svgText.appendChild(textPath)\n    this._textNode = svgText\n\n    if (options.below) {\n      rendererContainer.insertBefore(svgText, rendererContainer.firstChild)\n    } else {\n      rendererContainer.appendChild(svgText)\n    }\n\n    if (options.center) {\n      const boxWidth = svgText.getBBox().width\n      svgText.setAttribute('dx', (16 - boxWidth / 2).toString())\n    }\n\n    return this\n  }\n\n  // private _getLatSize(): number {\n  //   return this._getLatSizeOf(this._size)\n  // }\n\n  // private _getLngSize(): number {\n  //   return this._getLngSizeOf(this._size)\n  // }\n\n  // private _getLatSizeOf(size: number): number {\n  //   return (size / 40075017) * 360\n  // }\n\n  // private _getLngSizeOf(size: number): number {\n  //   return ((size / 40075017) * 360) / Math.cos((Math.PI / 180) * this._latlng.lat)\n  // }\n\n  getLatLng(): L.LatLng {\n    return this._latlng\n  }\n\n  private _rotate(point: number[], angle: number): [number, number] {\n    const [x, y] = point\n    const sin = Math.sin(angle)\n    const cos = Math.cos(angle)\n    return [x * cos - y * sin, x * sin + y * cos]\n  }\n\n  private _rotateAllPoints(points: number[][], angle: number): number[][] {\n    return points.map((p) =>\n      this._rotate(\n        p.map((v) => v * this._size),\n        angle\n      )\n    )\n  }\n\n  private _transformAllPointsToView(points: number[]): number[] {\n    const layerPoint = this._map.latLngToLayerPoint(this._latlng)\n    return points.map((value, index) => {\n      if (index % 2 === 0) {\n        return layerPoint.x + value\n      } else {\n        return layerPoint.y - points[index - 1]\n      }\n    })\n  }\n\n  private _createPathFromPoints(points: number[]): string {\n    let pathString = ''\n    for (let i = 0; i < points.length; i += 2) {\n      const x = points[i]\n      const y = points[i + 1]\n      if (!pathString) {\n        pathString = `M ${x} ${y} `\n      } else {\n        pathString += `L ${x} ${y} `\n      }\n    }\n    return `${pathString} Z`\n  }\n\n  private _getViewAngleFromModel(modelAngle: number): number {\n    return Math.PI / 2 - modelAngle\n  }\n\n  // private _resizeAndMovePoint(\n  //   point: [number, number],\n  //   scale: [number, number],\n  //   offset: [number, number]\n  // ): [number, number] {\n  //   return [point[0] * scale[0] + offset[0], point[1] * scale[1] + offset[1]]\n  // }\n\n  private _createWindPathString(): string {\n    let pathString = ''\n    const viewAngle = this._getViewAngleFromModel(this._direction)\n    for (let i = 0; i < this._windSymbol.length; ++i) {\n      const transformedPoints = this._transformAllPointsToView(\n        this._rotateAllPoints(this._windSymbol[i] as any, viewAngle) as any\n      )\n      if (i === 0) {\n        pathString = this._createPathFromPoints(transformedPoints)\n      } else {\n        pathString += ` ${this._createPathFromPoints(transformedPoints)}`\n      }\n    }\n    return pathString\n  }\n\n  getPathString(): string {\n    return this._createWindPathString()\n  }\n\n  static create(\n    latlng: L.LatLngLiteral | L.LatLng,\n    options?: WeatherLabelOptions\n  ): WeatherLabel {\n    return new WeatherLabel(latlng, options)\n  }\n}\n\nconst weatherLabel = (\n  latlng: L.LatLngLiteral | L.LatLng,\n  options?: WeatherLabelOptions\n) => WeatherLabel.create(latlng, options)\n\nexport { WeatherLabel, weatherLabel }\n"],"names":["l","s","Path","constructor","t","e","super","Error","setOptions","this","_latlng","latLng","_size","size","_direction","direction","Math","PI","_windSymbol","push","setStroke","stroke","setColor","color","setWeight","weight","setStyle","redraw","setFillOpacity","fillOpacity","setOpacity","opacity","setFill","fill","setFillColor","fillColor","_project","_update","_setPath","_path","setAttribute","getPathString","setLatLng","fire","oldLatLng","latlng","setDirection","_textRedraw","_text","_textOptions","setText","onAdd","Browser","svg","_map","Util","extend","repeat","attributes","below","offset","center","_textNode","parentNode","_renderer","removeChild","replace","r","stamp","i","_container","a","SVG","create","n","u","Number","parseFloat","getAttribute","setAttributeNS","toString","h","dataset","anchor","appendChild","document","createTextNode","insertBefore","firstChild","getBBox","width","getLatLng","_rotate","sin","cos","_rotateAllPoints","map","_transformAllPointsToView","latLngToLayerPoint","x","y","_createPathFromPoints","length","_getViewAngleFromModel","_createWindPathString","c","d","o"],"mappings":"uBAAuB,MAAMA,UAAUC,EAAEC,KAAK,WAAAC,CAAYC,EAAEC,GAAY,GAATC,MAAMD,QAAU,IAAJD,EAAW,MAAM,IAAIG,MAAM,wCAAwCN,EAAEO,WAAWC,KAAKJ,GAAG,CAAE,GAAEI,KAAKC,QAAQT,EAAEU,OAAOP,GAAGK,KAAKG,OAAU,MAAHP,OAAQ,EAAOA,EAAEQ,OAAO,GAAGJ,KAAKK,aAAgB,MAAHT,OAAQ,EAAOA,EAAEU,YAAY,GAAGC,KAAKC,GAAG,IAAIR,KAAKS,YAAY,CAAC,CAAC,GAAG,GAAG,GAAG,GAAG,EAAE,GAAG,IAAI,GAAG,GAAG,IAAI,KAAKT,KAAKS,YAAYC,KAAK,CAAC,GAAG,EAAE,IAAG,IAAO,CAAC,IAAI,EAAE,KAAI,IAAO,CAAC,EAAE,EAAE,GAAE,KAAQV,KAAKW,WAAc,MAAHf,OAAQ,EAAOA,EAAEgB,UAAS,GAAIZ,KAAKa,UAAa,MAAHjB,OAAQ,EAAOA,EAAEkB,QAAQ,WAAWd,KAAKe,WAAc,MAAHnB,OAAQ,EAAOA,EAAEoB,SAAS,EAAE,CAAC,QAAAH,CAASlB,GAAG,OAAOK,KAAKiB,SAAS,CAACH,MAAMnB,IAAIK,KAAKkB,QAAQ,CAAC,cAAAC,CAAexB,GAAG,OAAOK,KAAKiB,SAAS,CAACG,YAAYzB,IAAIK,KAAKkB,QAAQ,CAAC,UAAAG,CAAW1B,GAAG,OAAOK,KAAKiB,SAAS,CAACK,QAAQ3B,IAAIK,KAAKkB,QAAQ,CAAC,SAAAH,CAAUpB,GAAG,OAAOK,KAAKiB,SAAS,CAACD,OAAOrB,IAAIK,KAAKkB,QAAQ,CAAC,OAAAK,CAAQ5B,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