all files / src/drawer/ drawer.vue

3.57% Statements 1/28
0% Branches 0/22
0% Functions 0/3
4.17% Lines 1/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133                      1×                                                                                                                                                                                                                                                  
<template>
<paper class="mu-drawer" :style="drawerStyle" :class="{'open': open, 'right': right}" :zDepth="zDepth">
  <slot></slot>
</paper>
</template>
 
<script>
import paper from '../paper'
import PopupManager from '../internal/popup/manager'
import {getZIndex} from '../internal/popup/utils'
import {getWidth} from '../utils'
const transitionEvents = ['msTransitionEnd', 'mozTransitionEnd', 'oTransitionEnd', 'webkitTransitionEnd', 'transitionend']
 
export default {
  name: 'mu-drawer',
  props: {
    right: {
      type: Boolean,
      default: false
    },
    open: {
      type: Boolean,
      default: false
    },
    docked: {
      type: Boolean,
      default: true
    },
    width: {
      type: [Number, String]
    },
    zDepth: {
      type: Number,
      default: 2
    }
  },
  data () {
    return {
      overlayZIndex: getZIndex(),
      zIndex: getZIndex()
    }
  },
  computed: {
    drawerStyle () {
      return {
        width: getWidth(this.width),
        'z-index': this.docked ? '' : this.zIndex
      }
    },
    overlay () {
      return !this.docked
    }
  },
  methods: {
    overlayClick () {
      this.$emit('close', 'overlay')
    },
    bindTransition () {
      this.handleTransition = (e) => {
        if (e.propertyName !== 'transform') return
        if (!this.docked) this.$emit(this.open ? 'show' : 'hide')
      }
      transitionEvents.forEach((eventName) => {
        this.$el.addEventListener(eventName, this.handleTransition)
      })
    },
    unBindTransition () {
      if (!this.handleTransition) return
      transitionEvents.forEach((eventName) => {
        this.$el.removeEventListener(eventName, this.handleTransition)
      })
    },
    resetZIndex () {
      this.overlayZIndex = getZIndex()
      this.zIndex = getZIndex()
    }
  },
  watch: {
    open (val) {
      if (val && !this.docked) {
        PopupManager.open(this)
      } else {
        PopupManager.close(this)
      }
    },
    docked (val, oldVal) {
      if (val && !oldVal) {
        PopupManager.close(this)
      }
    }
  },
  mounted () {
    if (this.open && !this.docked) PopupManager.open(this)
    this.bindTransition()
  },
  beforeDestroy () {
    PopupManager.close(this)
    this.unBindTransition()
  },
  components: {
    paper
  }
}
</script>
 
<style lang="less">
@import "../styles/import.less";
.mu-drawer {
  width: 256px;
  position: fixed;
  top: 0;
  bottom: 0;
  .scrollable();
  .no-scrollbar();
  transition-property: transform, visibility;
  transition-duration: 0.45s;
  transform: translate3d(-100%, 0, 0);
  border-radius: 0;
  left: 0;
  visibility: hidden;
  z-index: 200;
  &.right {
    right: 0;
    left: auto;
    transform: translate3d(100%, 0, 0);
  }
  &.open {
    transform: translate3d(0, 0, 0);
    visibility: visible;
  }
}
</style>