Home Reference Source

src/urlapi/handling/marker.js

import { Debug } from '../../Debug'
import { restoreText } from '../../xssprotection'
import { transform } from 'ol/proj'

/**
 * @type {URLParameter}
 */
export const markerParam = {
  keys: ['marklat', 'marklon', 'markx', 'marky', 'marktext', 'markpop', 'srid'],
  setEvent: 'ready',
  setToMap: (map, query) => {
    const marker = map.get('marker')

    let coords, fromProjection
    if (marker) {
      if (query.isSet('markx') && query.isSet('marky')) {
        coords = [parseFloat(query.getSanitizedVal('markx')), parseFloat(query.getSanitizedVal('marky'))]
        fromProjection = query.isSet('srid') ? query.getSanitizedVal('srid') : map.get('interfaceProjection')
      } else if (query.isSet('marklat') && query.isSet('marklon')) {
        coords = [parseFloat(query.getSanitizedVal('marklon')), parseFloat(query.getSanitizedVal('marklat'))]
        fromProjection = 'EPSG:4326'
      } else {
        coords = map.getView().getCenter()
        fromProjection = map.getView().getProjection()
      }
      marker.setPosition(transform(coords, fromProjection, map.get('mapProjection')))

      if (query.isSet('marklat') || query.isSet('marklon') || query.isSet('markx') || query.isSet('marky')) {
        marker.setActive(true)
      }

      if (query.isSet('marktext')) {
        marker.setActive(true)
        marker.setText(query.getSanitizedVal('marktext'))

        if (query.isTrue('markpop') || !query.isSet('markpop')) {
          marker.setPopupVisible(true)
        } else {
          marker.setPopupVisible(false)
        }
      }
    } else {
      Debug.warn('There is no marker configured for the map, but it was tried to set it via the urlapi.')
    }
  },
  getFromMap: (map, query) => {
    const marker = map.get('marker')

    if (marker && marker.getActive() && !query.isExcluded('marker')) {
      const result = {
        srid: map.get('interfaceProjection')
      }

      const xy = transform(
        marker.getPosition(), map.get('mapProjection'), map.get('interfaceProjection'))
      const text = restoreText(marker.getText())
      const popvis = marker.getPopupVisible()

      result.markx = xy[0].toFixed(5)
      result.marky = xy[1].toFixed(5)

      if (text) {
        result.marktext = text
        result.markpop = popvis // maybe don't set if not needed?
      }

      return result
    }
  }
}