index.vue 4.85 KB
Newer Older
wangdanlei's avatar
wangdanlei committed
1 2 3
<template>
  <div :style="{ cursor, userSelect}" class="vue-splitter-container clearfix" @mouseup="onMouseUp" @mousemove="onMouseMove">

wangdanlei's avatar
wangdanlei committed
4
    <pane class="splitter-pane splitter-paneL" :split="split" :style="panelStyle.left">
wangdanlei's avatar
wangdanlei committed
5 6 7
      <slot name="paneL" />
    </pane>

wangdanlei's avatar
wangdanlei committed
8 9 10 11 12 13 14 15 16 17 18 19 20
    <resizer
      v-show="isShowResizer"
      :class-name="className"
      :is-folder="isFolder"
      :fold-direction="foldDirection"
      :is-fold="isFold"
      :style="panelStyle.resizer"
      :split="split"
      :is-default-fold-left="isDefaultFoldLeft"
      @fold="fold"
      @onMouseDown="onMouseDown"
    />
    <pane class="splitter-pane splitter-paneR" :split="split" :style="panelStyle.right">
wangdanlei's avatar
wangdanlei committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
      <slot name="paneR" />
    </pane>
    <div v-if="active" class="vue-splitter-container-mask" />
  </div>
</template>

<script>
import Resizer from './resizer.vue'
import Pane from './pane.vue'

export default {
  name: 'SplitPane',
  components: { Resizer, Pane },
  props: {
    minPercent: {
      type: Number,
      default: 10
    },
wangdanlei's avatar
wangdanlei committed
39 40 41 42
    foldMechanism: {
      type: String,
      default: () => 'left'
    },
wangdanlei's avatar
wangdanlei committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    defaultPercent: {
      type: Number,
      default: 50
    },
    split: {
      validator(value) {
        return ['vertical', 'horizontal'].indexOf(value) >= 0
      },
      required: true
    },
    className: {
      type: String,
      default: ''
    },
    resizerWidth: {
      type: String,
      default: '8px'
    },
    isFolder: {
      type: Boolean,
      default: true
    },
wangdanlei's avatar
wangdanlei committed
65 66 67 68
    defaultIsFold: {
      type: Boolean,
      default: true
    },
wangdanlei's avatar
wangdanlei committed
69 70 71
    isShowResizer: {
      type: Boolean,
      default: true
wangdanlei's avatar
wangdanlei committed
72 73 74 75 76 77 78 79
    },
    foldDirection: {
      type: String,
      default: 'left' // true: 左收起, false:右收起
    },
    isDefaultFoldLeft: {
      type: Boolean,
      default: false // 是否默认收起左
wangdanlei's avatar
wangdanlei committed
80 81 82 83
    }
  },
  data() {
    return {
wangdanlei's avatar
wangdanlei committed
84
      baseWidth: '0px',
wangdanlei's avatar
wangdanlei committed
85 86 87 88 89 90
      active: false,
      hasMoved: false,
      height: null,
      percent: this.defaultPercent,
      type: this.split === 'vertical' ? 'width' : 'height',
      resizeType: this.split === 'vertical' ? 'left' : 'top',
wangdanlei's avatar
wangdanlei committed
91 92
      isFold: false
      // defaultIsFold: true
wangdanlei's avatar
wangdanlei committed
93 94 95 96 97 98 99 100
    }
  },
  computed: {
    userSelect() {
      return this.active ? 'none' : ''
    },
    cursor() {
      return this.active ? (this.split === 'vertical' ? 'col-resize' : 'row-resize') : ''
wangdanlei's avatar
wangdanlei committed
101 102 103 104 105 106 107 108 109
    },
    panelStyle() {
      const toLeft = `calc(${this.percent}% - ${this.baseWidth})`
      const toRight = `calc(${100 - this.percent}% - ${this.resizerWidth})`
      return {
        left: { [this.type]: this.foldDirection === 'left' ? toLeft : toRight },
        right: { [this.type]: this.foldDirection === 'left' ? toRight : toLeft },
        resizer: { [this.resizeType]: this.foldDirection === 'left' ? toLeft : toRight, [this.type]: this.resizerWidth }
      }
wangdanlei's avatar
wangdanlei committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    }
  },
  watch: {
    defaultPercent(newValue, oldValue) {
      this.percent = newValue
    }
  },
  methods: {
    onClick() {
      if (!this.hasMoved) {
        this.percent = 50
        this.$emit('resize', this.percent)
      }
    },
    onMouseDown() {
      this.active = true
      this.hasMoved = false
    },
    onMouseUp() {
      this.active = false
    },
    onMouseMove(e) {
      if (e.buttons === 0 || e.which === 0) {
        this.active = false
      }

      if (this.active) {
        let offset = 0
wangdanlei's avatar
wangdanlei committed
138 139
        const target = e.currentTarget
        const boundingRect = target.getBoundingClientRect()
wangdanlei's avatar
wangdanlei committed
140
        if (this.split === 'vertical') {
wangdanlei's avatar
wangdanlei committed
141
          offset = this.foldDirection === 'left' ? boundingRect.left : boundingRect.right
wangdanlei's avatar
wangdanlei committed
142
        } else {
wangdanlei's avatar
wangdanlei committed
143
          offset = this.foldDirection === 'left' ? boundingRect.top : boundingRect.bottom
wangdanlei's avatar
wangdanlei committed
144 145 146 147
        }

        const currentPage = this.split === 'vertical' ? e.pageX : e.pageY
        const targetOffset = this.split === 'vertical' ? e.currentTarget.offsetWidth : e.currentTarget.offsetHeight
wangdanlei's avatar
wangdanlei committed
148
        const percent = Math.floor(((Math.abs(currentPage - offset)) / targetOffset) * 10000) / 100
wangdanlei's avatar
wangdanlei committed
149 150 151 152 153 154 155 156
        if (percent > this.minPercent && percent < 100 - this.minPercent) {
          this.percent = percent
        }
        this.$emit('resize', this.percent)
        this.hasMoved = true
      }
    },
    fold(isFold) {
wangdanlei's avatar
wangdanlei committed
157 158 159 160 161
      // this.defaultIsFold = !isFold
      if (!isFold) {
        if (this.foldMechanism === 'right') {
          this.baseWidth = '0px'
        }
wangdanlei's avatar
wangdanlei committed
162 163
        this.percent = this.defaultPercent
      } else {
wangdanlei's avatar
wangdanlei committed
164 165 166 167 168 169
        if (this.foldMechanism === 'right') {
          this.percent = 100
          this.baseWidth = '8px'
        } else {
          this.percent = 0
        }
wangdanlei's avatar
wangdanlei committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
      }
    }
  }
}
</script>

<style scoped>
.clearfix:after {
  visibility: hidden;
  display: block;
  font-size: 0;
  content: " ";
  clear: both;
  height: 0;
}

.vue-splitter-container {
  height: 100%;
  position: relative;
}

.vue-splitter-container-mask {
  z-index: 9999;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}
</style>