Source: control/ControlBarZoom.js

/** control zoom composé de controlbar afin de "rentrer" dans ControlBar */

import Button from 'ol-ext/control/Button'
import Bar from 'ol-ext/control/Bar'
import { easeOut } from 'ol/easing'
import { getButtonHtml } from './ControlBarUtils'

/** Zoom avant ou arrière */
const zoomByDelta_ = function (delta, duration) {
  const map = this.getMap()
  const view = map.getView()
  if (!view) {
    // the map does not have a view, so we can't act
    // upon it
    return
  }
  const currentZoom = view.getZoom()
  if (currentZoom !== undefined) {
    const newZoom = view.getConstrainedZoom(currentZoom + delta)
    if (duration > 0) {
      if (view.getAnimating()) {
        view.cancelAnimations()
      }
      view.animate({
        zoom: newZoom,
        duration,
        easing: easeOut,
      })
    } else {
      view.setZoom(newZoom)
    }
  }
}

const isPromise = function (obj) {
  return !!obj.then && typeof obj.then === 'function'
}
/** Zoom sur une étendue renvoyée par une fonction fournie a l'initialisation */
const zoomToExtent_ = function (extent_) {
  const map = this.getMap()
  const view = map.getView()

  if (typeof extent_ === 'function') {
    const extent = extent_()

    if (isPromise(extent)) {
      extent.then((e) => {
        if (!e) {
          return
        }

        return view.fit(e)
      })
    } else {
      view.fit(extent)
    }
  } else {
    view.fit(!extent_ ? view.getProjection().getExtent() : extent_)
  }
}

const ControlBarZoom = function (options) {
  options = options || {}
  const duration_ = options.duration !== undefined ? options.duration : 250
  const extent_ = options.extent

  const className = options.className !== undefined ? options.className : ''
  const delta = options.delta !== undefined ? options.delta : 1

  const zoomInLabel = getButtonHtml(options.zoomInLabel, null, null, '+')
  const zoomOutLabel = getButtonHtml(options.zoomOutLabel, null, null, '\u2212')
  const zoomExtentLabel = getButtonHtml(options.zoomExtentLabel, null, null, '<i class="kmapv-icon kmapv-icon-zoom-out"></i>')

  const zoomInTipLabel = options.zoomInTipLabel !== undefined
    ? options.zoomInTipLabel
    : 'Zoom avant'
  const zoomOutTipLabel = options.zoomOutTipLabel !== undefined
    ? options.zoomOutTipLabel
    : 'Zoom arrière'
  const zoomExtentTipLabel = options.zoomExtentTipLabel !== undefined
    ? options.zoomExtentTipLabel
    : 'Zoom vers l\'étendue'

  const controls = [
    new Button({
      html: zoomInLabel,
      title: zoomInTipLabel,
      handleClick: function () {
        zoomByDelta_.call(this, delta, duration_)
      },
    }),
    new Button({
      html: zoomOutLabel,
      title: zoomOutTipLabel,
      handleClick: function (b) {
        zoomByDelta_.call(this, -delta, duration_)
      },
    }),
  ]
  if (extent_) {
    controls.splice(1, 0, new Button({
      html: zoomExtentLabel,
      title: zoomExtentTipLabel,
      handleClick: function () {
        zoomToExtent_.call(this, extent_)
      },
    }))
  }
  const zoombar = new Bar({
    className,
    group: true,
    controls,
  })

  return zoombar
}

export default ControlBarZoom