{"version":3,"file":"setText.mjs","sources":["../../../../../../packages/sdk/plugins/utils/setText.ts"],"sourcesContent":["import L from 'leaflet'\n\nconst __onAdd = L.Polyline.prototype.onAdd,\n  __onRemove = L.Polyline.prototype.onRemove,\n  __updatePath = (L.Polyline.prototype as any)._updatePath,\n  __bringToFront = L.Polyline.prototype.bringToFront\n\nconst PolylineTextPath = {\n  onAdd(map: L.Map) {\n    __onAdd.call(this, map)\n    this._textRedraw()\n  },\n\n  onRemove(map: any) {\n    map = map || this._map\n    if (map && this._textNode && map._renderer._container)\n      map._renderer._container.removeChild(this._textNode)\n    __onRemove.call(this, map)\n  },\n\n  bringToFront() {\n    __bringToFront.call(this)\n    this._textRedraw()\n  },\n\n  _updatePath() {\n    __updatePath.call(this)\n    this._textRedraw()\n  },\n\n  _textRedraw() {\n    const text = this._text,\n      options = this._textOptions\n    if (text) {\n      this.setText(null).setText(text, options)\n    }\n  },\n\n  setText(\n    text: string | undefined,\n    options: {\n      repeat: any\n      attributes: { [x: string]: string }\n      offset: any\n      below: any\n      center: any\n      orientation: number\n    }\n  ) {\n    this._text = text\n    this._textOptions = options\n\n    /* If not in SVG mode or Polyline not added to map yet return */\n    /* setText will be called by onAdd, using value stored in this._text */\n    if (!L.Browser.svg || typeof this._map === 'undefined') {\n      return this\n    }\n\n    const defaults = {\n      repeat: false,\n      fillColor: 'black',\n      attributes: {},\n      below: false,\n    }\n    options = L.Util.extend(defaults, options)\n\n    /* If empty text, hide */\n    if (!text) {\n      if (this._textNode && this._textNode.parentNode) {\n        this._map._renderer._container.removeChild(this._textNode)\n\n        /* delete the node, so it will not be removed a 2nd time if the layer is later removed from the map */\n        delete this._textNode\n      }\n      return this\n    }\n\n    text = text.replace(/ /g, '\\u00A0') // Non breakable spaces\n    const id = `pathdef-${L.Util.stamp(this)}`\n    const svg = this._map._renderer._container\n    this._path.setAttribute('id', id)\n\n    if (options.repeat) {\n      /* Compute single pattern length */\n      const pattern = L.SVG.create('text')\n      for (const attr in options.attributes)\n        pattern.setAttribute(attr, options.attributes[attr])\n      pattern.appendChild(document.createTextNode(text))\n      svg.appendChild(pattern)\n      const alength = pattern.getComputedTextLength()\n      svg.removeChild(pattern)\n\n      /* Create string as long as path */\n      text = Array.from({\n        length: Math.ceil(\n          Number.isNaN(this._path.getTotalLength() / alength)\n            ? 0\n            : this._path.getTotalLength() / alength\n        ),\n      }).join(text)\n    }\n\n    /* Put it along the path using textPath */\n    const textNode = L.SVG.create('text') as any,\n      textPath = L.SVG.create('textPath')\n\n    const dy = options.offset || this._path.getAttribute('stroke-width')\n\n    textPath.setAttributeNS(\n      'http://www.w3.org/1999/xlink',\n      'xlink:href',\n      `#${id}`\n    )\n    textNode.setAttribute('dy', dy)\n    for (const attr in options.attributes)\n      textNode.setAttribute(attr, options.attributes[attr])\n    textPath.appendChild(document.createTextNode(text))\n    textNode.appendChild(textPath)\n    this._textNode = textNode\n\n    if (options.below) {\n      svg.insertBefore(textNode, svg.firstChild)\n    } else {\n      svg.appendChild(textNode)\n    }\n\n    /* Center text according to the path's bounding box */\n    if (options.center) {\n      const textLength = textNode.getComputedTextLength() as any\n      const pathLength = this._path.getTotalLength() as any\n      /* Set the position for the left side of the textNode */\n      textNode.setAttribute('dx', `${pathLength / 2 - textLength / 2}`)\n    }\n\n    /* Change label rotation (if required) */\n    if (options.orientation) {\n      let rotateAngle = 0\n      switch (options.orientation as any) {\n        case 'flip':\n          rotateAngle = 180\n          break\n        case 'perpendicular':\n          rotateAngle = 90\n          break\n        default:\n          rotateAngle = options.orientation\n      }\n\n      const rotatecenterX = textNode.getBBox().x + textNode.getBBox().width / 2\n      const rotatecenterY = textNode.getBBox().y + textNode.getBBox().height / 2\n      textNode.setAttribute(\n        'transform',\n        `rotate(${rotateAngle} ${rotatecenterX} ${rotatecenterY})`\n      )\n    }\n\n    /* Initialize mouse events for the additional nodes */\n    if (this.options.interactive) {\n      if (L.Browser.svg || !L.Browser.vml) {\n        textPath.setAttribute('class', 'leaflet-interactive')\n      }\n\n      const events = [\n        'click',\n        'dblclick',\n        'mousedown',\n        'mouseover',\n        'mouseout',\n        'mousemove',\n        'contextmenu',\n      ]\n      for (const event_ of events) {\n        L.DomEvent.on(textNode, event_, this.fire, this)\n      }\n    }\n\n    return this\n  },\n}\n\nL.Polyline.include(PolylineTextPath)\n\nL.LayerGroup.include({\n  setText(text: any, options: any) {\n    for (const layer in this._layers) {\n      if (typeof this._layers[layer].setText === 'function') {\n        this._layers[layer].setText(text, options)\n      }\n    }\n    return this\n  },\n})\n\nexport const polyline = (latlng: any, options: any) => {\n  return L.polyline(latlng, options)\n}\n"],"names":["u","i","Polyline","prototype","onAdd","_","onRemove","f","_updatePath","p","bringToFront","x","t","call","this","_textRedraw","_map","_textNode","_renderer","_container","removeChild","_text","e","_textOptions","setText","Browser","svg","Util","extend","repeat","fillColor","attributes","below","parentNode","replace","d","stamp","s","_path","setAttribute","r","SVG","create","l","appendChild","document","createTextNode","o","getComputedTextLength","Array","from","length","Math","ceil","Number","isNaN","getTotalLength","join","n","h","c","offset","getAttribute","setAttributeNS","insertBefore","firstChild","center","orientation","getBBox","width","y","height","options","interactive","vml","DomEvent","on","fire","include","LayerGroup","a","_layers","polyline"],"mappings":"uBAAuB,MAAMA,EAAEC,EAAEC,SAASC,UAAUC,MAAMC,EAAEJ,EAAEC,SAASC,UAAUG,SAASC,EAAEN,EAAEC,SAASC,UAAUK,YAAYC,EAAER,EAAEC,SAASC,UAAUO,aAAaC,EAAE,CAAC,KAAAP,CAAMQ,GAAGZ,EAAEa,KAAKC,KAAKF,GAAGE,KAAKC,aAAa,EAAE,QAAAT,CAASM,IAAGA,EAAEA,GAAGE,KAAKE,OAAQF,KAAKG,WAAWL,EAAEM,UAAUC,YAAYP,EAAEM,UAAUC,WAAWC,YAAYN,KAAKG,WAAWZ,EAAEQ,KAAKC,KAAKF,EAAE,EAAE,YAAAF,GAAeD,EAAEI,KAAKC,MAAMA,KAAKC,aAAa,EAAE,WAAAP,GAAcD,EAAEM,KAAKC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