<template>
  <div :class="classnames" :style="{ cursor, userSelect }" @mousedown="onMouseDown">
    <slot />
  </div>
</template>

<script>
/*
Resizable split panes for Vue.js. https://yansern.github.io/vue-multipane/
Below is a copy of the license.

MIT License

For vue-multipane component

Copyright (c) 2017 Yan Sern.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 */

const LAYOUT_HORIZONTAL = 'horizontal'
const LAYOUT_VERTICAL = 'vertical'

export default {
  name: 'Multipane',

  props: {
    layout: {
      type: String,
      default: LAYOUT_VERTICAL
    }
  },

  data() {
    return {
      isResizing: false
    }
  },

  computed: {
    classnames() {
      return [
        'multipane',
        'layout-' + this.layout.slice(0, 1),
        this.isResizing ? 'is-resizing' : ''
      ]
    },
    cursor() {
      return this.isResizing ? (this.layout == LAYOUT_VERTICAL ? 'col-resize' : 'row-resize') : ''
    },
    userSelect() {
      return this.isResizing ? 'none' : ''
    }
  },

  methods: {
    onMouseDown({ target: resizer, pageX: initialPageX, pageY: initialPageY }) {
      if (resizer._prevClass && resizer._prevClass.match('multipane-resizer')) {
        let self = this
        let { $el: container, layout } = self

        let pane = resizer.previousElementSibling
        let { offsetWidth: initialPaneWidth, offsetHeight: initialPaneHeight } = pane

        let usePercentage = !!(pane.style.width + '').match('%')

        const { addEventListener, removeEventListener } = window

        const resize = (initialSize, offset = 0) => {
          if (layout == LAYOUT_VERTICAL) {
            let containerWidth = container.clientWidth
            let paneWidth = initialSize + offset

            return (pane.style.width = usePercentage
              ? (paneWidth / containerWidth) * 100 + '%'
              : paneWidth + 'px')
          }

          if (layout == LAYOUT_HORIZONTAL) {
            let containerHeight = container.clientHeight
            let paneHeight = initialSize + offset

            return (pane.style.height = usePercentage
              ? (paneHeight / containerHeight) * 100 + '%'
              : paneHeight + 'px')
          }
        }

        // This adds is-resizing class to container
        self.isResizing = true

        // Resize once to get current computed size
        let size = resize()

        // Trigger paneResizeStart event
        self.$emit('paneResizeStart', pane, resizer, size)

        const onMouseMove = function({ pageX, pageY }) {
          size =
            layout == LAYOUT_VERTICAL
              ? resize(initialPaneWidth, pageX - initialPageX)
              : resize(initialPaneHeight, pageY - initialPageY)

          self.$emit('paneResize', pane, resizer, size)
        }

        const onMouseUp = function() {
          // Run resize one more time to set computed width/height.
          size = layout == LAYOUT_VERTICAL ? resize(pane.clientWidth) : resize(pane.clientHeight)

          // This removes is-resizing class to container
          self.isResizing = false

          removeEventListener('mousemove', onMouseMove)
          removeEventListener('mouseup', onMouseUp)

          self.$emit('paneResizeStop', pane, resizer, size)
        }

        addEventListener('mousemove', onMouseMove)
        addEventListener('mouseup', onMouseUp)
      }
    }
  }
}
</script>

<style lang="sass">
.multipane
  display: flex

  &.layout-h
    flex-direction: column

  &.layout-v
    flex-direction: row

.multipane > div
  position: relative
  z-index: 1

.multipane-resizer
  display: block
  position: relative
  z-index: 2

.layout-h > .multipane-resizer
  width: 100%
  height: 20px
  margin-top: -30px
  top: 10px
  cursor: row-resize

.layout-v > .multipane-resizer
  width: 20px
  height: 100%
  margin-left: -20px
  left: 5px
  cursor: col-resize
</style>
