index.vue 3.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
<template>
2 3 4
  <uni-navigator
    v-if="hoverClass && hoverClass !== 'none'"
    :class="[hovering ? hoverClass : '']"
fxy060608's avatar
fxy060608 已提交
5
    @touchstart="_hoverTouchStart"
6 7
    @touchend="_hoverTouchEnd"
    @touchcancel="_hoverTouchCancel"
8 9
    @mousedown="_hoverMousedown"
    @mouseup="_hoverMouseup"
10 11
    @click="_onClick"
    v-on="$listeners"
fxy060608's avatar
fxy060608 已提交
12
  >
fxy060608's avatar
fxy060608 已提交
13 14
    <slot />
  </uni-navigator>
15 16 17 18
  <uni-navigator
    v-else
    @click="_onClick"
    v-on="$listeners"
fxy060608's avatar
fxy060608 已提交
19
  >
fxy060608's avatar
fxy060608 已提交
20 21 22 23 24 25 26 27
    <slot />
  </uni-navigator>
</template>
<script>
import {
  hover
} from 'uni-mixins'

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
const OPEN_TYPES = [
  'navigate',
  'redirect',
  'switchTab',
  'reLaunch',
  'navigateBack'
]
const ANIMATION_TYPE_IN = [
  'slide-in-right',
  'slide-in-left',
  'slide-in-top',
  'slide-in-bottom',
  'fade-in',
  'zoom-out',
  'zoom-fade-out',
  'pop-in',
  'none'
]
const ANIMATION_TYPE_OUT = [
  'slide-out-right',
  'slide-out-left',
  'slide-out-top',
  'slide-out-bottom',
  'fade-out',
  'zoom-in',
  'zoom-fade-in',
  'pop-out',
  'none'
]
fxy060608's avatar
fxy060608 已提交
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

export default {
  name: 'Navigator',
  mixins: [hover],
  props: {
    hoverClass: {
      type: String,
      default: 'navigator-hover'
    },
    url: {
      type: String,
      default: ''
    },
    openType: {
      type: String,
      default: 'navigate',
      validator (value) {
        return ~OPEN_TYPES.indexOf(value)
      }
    },
    delta: {
      type: Number,
      default: 1
    },
    hoverStartTime: {
82
      type: [Number, String],
83
      default: 50
fxy060608's avatar
fxy060608 已提交
84 85
    },
    hoverStayTime: {
86
      type: [Number, String],
fxy060608's avatar
fxy060608 已提交
87
      default: 600
88 89 90 91
    },
    exists: {
      type: String,
      default: ''
92 93 94 95 96
    },
    animationType: {
      type: String,
      validator (value) {
        return !value || ~ANIMATION_TYPE_IN.concat(ANIMATION_TYPE_OUT).indexOf(value)
97 98
      },
      default: ''
99 100 101 102
    },
    animationDuration: {
      type: [String, Number],
      default: 300
fxy060608's avatar
fxy060608 已提交
103 104 105 106 107
    }
  },

  methods: {
    _onClick ($event) {
108
      if (this.openType !== 'navigateBack' && !this.url) {
109 110
        console.error(
          '<navigator/> should have url attribute when using navigateTo, redirectTo, reLaunch or switchTab')
fxy060608's avatar
fxy060608 已提交
111 112 113
        return
      }

114 115
      const animationDuration = parseInt(this.animationDuration)

fxy060608's avatar
fxy060608 已提交
116 117 118
      switch (this.openType) {
        case 'navigate':
          uni.navigateTo({
119 120 121
            url: this.url,
            animationType: this.animationType || 'pop-in',
            animationDuration
fxy060608's avatar
fxy060608 已提交
122 123 124 125
          })
          break
        case 'redirect':
          uni.redirectTo({
126 127
            url: this.url,
            exists: this.exists
fxy060608's avatar
fxy060608 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141
          })
          break
        case 'switchTab':
          uni.switchTab({
            url: this.url
          })
          break
        case 'reLaunch':
          uni.reLaunch({
            url: this.url
          })
          break
        case 'navigateBack':
          uni.navigateBack({
142 143 144
            delta: this.delta,
            animationType: this.animationType || 'pop-out',
            animationDuration
fxy060608's avatar
fxy060608 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158
          })
          break
        default:
          break
      }
    }
  }
}
</script>
<style>
  uni-navigator {
    height: auto;
    width: auto;
    display: block;
159
    cursor: pointer;
fxy060608's avatar
fxy060608 已提交
160 161 162 163 164
  }

  uni-navigator[hidden] {
    display: none;
  }
165 166 167 168 169

  .navigator-hover {
    background-color: rgba(0, 0, 0, 0.1);
    opacity: 0.7;
  }
170
</style>