all files / src/nav/ sub-nav.vue

20% Statements 4/20
75% Branches 3/4
27.27% Functions 3/11
20% Lines 4/20
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161                                                                                                                                                                                                                                                                                                                         
<template>
  <div class="w-sub-nav" :class="{active, vertical}" v-click-outside="close">
    <span class="w-sub-nav-label" @click="onClick">
      <slot name="title"></slot>
      <span class="w-sub-nav-icon" :class="{open, vertical}">
        <w-icon name="right"></w-icon>
      </span>
    </span>
    <template v-if="vertical">
      <transition @enter="enter" @leave="leave" @after-leave="afterLeave"
        @after-enter="afterEnter">
        <div class="w-sub-nav-popover" v-show="open" :class="{vertical}">
          <slot></slot>
        </div>
      </transition>
    </template>
    <template v-else>
      <div class="w-sub-nav-popover" v-show="open">
        <slot></slot>
      </div>
    </template>
  </div>
</template>
<script>
import ClickOutside from '../click-outside'
import WIcon from '../icon/icon'
export default {
  directives: {ClickOutside},
  name: 'WheelSubNav',
  components: {
    WIcon
  },
  inject: ['root', 'vertical'],
  props: {
    name: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      open: false
    }
  },
  computed: {
    active() {
      return this.root.namePath.indexOf(this.name) >= 0 ? true : false
    }
  },
  methods: {
    enter (el, done) {
      let {height} = el.getBoundingClientRect()
      el.style.height = 0
      el.getBoundingClientRect() // 浏览器会自动合并height的设置,因此加此一行计算height高度使得上一行代码生效
      el.style.height = `${height}px`
      el.addEventListener('transitionend', () => {
        done()
      })
    },
    afterEnter (el) {
      el.style.height = 'auto'
    },
    leave: function (el, done) {
      let {height} = el.getBoundingClientRect()
      el.style.height = `${height}px`
      el.getBoundingClientRect()
      el.style.height = 0
      el.addEventListener('transitionend', () => {
        done()
      })
    },
    afterLeave: function (el) {
      el.style.height = 'auto'
    },
    onClick() {
      this.open = !this.open
    },
    close() {
      this.open = false
    },
    updateNamePath() {
      this.root.namePath.unshift(this.name)
      this.$parent.updateNamePath && this.$parent.updateNamePath()
    }
  }
}
</script>
<style lang="scss" scoped>
  @import "../../styles/_var.scss";
  .w-sub-nav {
    position: relative;
    &:not(.vertical) {
      &.active {
        &::after {
          content: '';
          position: absolute;
          bottom: 0;
          left: 0;
          border-bottom: 2px solid $blue;
          width: 100%;
        }
      }
    }
    &-label { padding: 10px 20px; display: block; }
    &-icon { display: none; }
    &-popover {
      transition: height 250ms;
      background: white;
      position: absolute;
      top: 100%;
      left: 0;
      border: 1px solid $grey;
      margin-top: 4px;
      white-space: nowrap;
      box-shadow: 0 0 3px fade_out(black, 0.8);
      border-radius: $border-radius;
      font-size: $font-size;
      color: $light-color;
      min-width: 8em;
      &.vertical {
        position: static;
        border-radius: 0;
        border: none;
        box-shadow: none;
        overflow: hidden;
      }
    }
  }
  .w-sub-nav .w-sub-nav {
    &.active {
      &::after {
        display: none;
      }
    }
    .w-sub-nav-popover {
    top: 0;
    left: 100%;
    margin-left: 8px;
    }
    .w-sub-nav-label {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    .w-sub-nav-icon {
      transition: transform 250ms;
      display: inline-flex; margin-left: 1em;
      svg {fill: $light-color;}
      &.vertical {
        transform: rotate(90deg);
        &.open {
          transform: rotate(270deg);
        }
      }
      &.open {
        transform: rotate(180deg);
      }
    }
  }
</style>