picker.vue 10.8 KB
Newer Older
M
init  
miaodian 已提交
1 2 3
<template>
  <transition name="cube-picker-fade">
    <cube-popup
4 5 6
        type="picker"
        :mask="true"
        :center="false"
D
dolymood 已提交
7
        :z-index="zIndex"
8 9 10
        v-show="isVisible"
        @touchmove.prevent
        @mask-click="cancel">
M
init  
miaodian 已提交
11
      <transition name="cube-picker-move">
12
        <div class="cube-picker-panel cube-safe-area-pb" v-show="isVisible" @click.stop>
M
init  
miaodian 已提交
13 14 15 16 17 18 19 20 21
          <div class="cube-picker-choose border-bottom-1px">
            <span data-action="cancel" @click="cancel">{{cancelTxt}}</span>
            <span data-action="confirm" @click="confirm">{{confirmTxt}}</span>
            <h1>{{title}}</h1>
          </div>
          <div class="cube-picker-content">
            <i class="border-bottom-1px"></i>
            <i class="border-top-1px"></i>
            <div class="cube-picker-wheel-wrapper" ref="wheelWrapper">
A
update  
AmyFoxFN 已提交
22
              <div v-for="(data,index) in pickerData" :key="index">
M
init  
miaodian 已提交
23
                <ul class="wheel-scroll">
A
update  
AmyFoxFN 已提交
24
                  <li v-for="(item,index) in data" class="wheel-item" :key="index">{{item[textKey]}}</li>
M
init  
miaodian 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
                </ul>
              </div>
            </div>
          </div>
          <div class="cube-picker-footer"></div>
        </div>
      </transition>
    </cube-popup>
  </transition>
</template>

<script type="text/ecmascript-6">
  import BScroll from 'better-scroll'
  import CubePopup from '../popup/popup.vue'
  import apiMixin from '../../common/mixins/api'

  const COMPONENT_NAME = 'cube-picker'

  const EVENT_SELECT = 'select'
  const EVENT_VALUE_CHANGE = 'value-change'
  const EVENT_CANCEL = 'cancel'
  const EVENT_CHANGE = 'change'

A
Amy 已提交
48 49 50 51 52
  const DEFAULT_KEYS = {
    value: 'value',
    text: 'text'
  }

M
init  
miaodian 已提交
53 54 55 56 57 58 59 60 61 62
  export default {
    name: COMPONENT_NAME,
    mixins: [apiMixin],
    props: {
      data: {
        type: Array,
        default() {
          return []
        }
      },
A
Amy 已提交
63 64 65 66 67 68
      selectedIndex: {
        type: Array,
        default() {
          return []
        }
      },
M
init  
miaodian 已提交
69 70 71 72 73 74 75 76 77 78 79
      title: {
        type: String
      },
      cancelTxt: {
        type: String,
        default: '取消'
      },
      confirmTxt: {
        type: String,
        default: '确定'
      },
A
Amy 已提交
80 81 82
      swipeTime: {
        type: Number,
        default: 2500
A
Amy 已提交
83 84 85 86 87 88
      },
      alias: {
        type: Object,
        default() {
          return {}
        }
D
dolymood 已提交
89 90 91
      },
      zIndex: {
        type: Number
M
init  
miaodian 已提交
92 93 94 95 96 97 98 99
      }
    },
    data() {
      return {
        pickerData: this.data.slice(),
        pickerSelectedIndex: this.selectedIndex
      }
    },
A
Amy 已提交
100 101 102 103 104 105 106 107
    computed: {
      valueKey() {
        return this.alias.value || DEFAULT_KEYS.value
      },
      textKey() {
        return this.alias.text || DEFAULT_KEYS.text
      }
    },
M
init  
miaodian 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    created() {
      this.pickerSelectedVal = []
      if (!this.pickerSelectedIndex.length) {
        this.pickerSelectedIndex = []
        for (let i = 0; i < this.pickerData.length; i++) {
          this.pickerSelectedIndex[i] = 0
        }
      }
    },
    methods: {
      confirm() {
        if (!this._canConfirm()) {
          return
        }
        this.hide()

        let changed = false
125
        let pickerSelectedText = []
126

F
Fyerl 已提交
127
        const dataLength = this.pickerData.length
128
        const selectedValLength = this.pickerSelectedVal.length
129

130 131 132 133 134
        if (selectedValLength !== dataLength) {
          if (selectedValLength > dataLength) {
            this.pickerSelectedVal.splice(dataLength)
            this.pickerSelectedIndex.splice(dataLength)
          }
135 136 137
          changed = true
        }

F
Fyerl 已提交
138
        for (let i = 0; i < dataLength; i++) {
M
init  
miaodian 已提交
139 140 141 142
          let index = this.wheels[i].getSelectedIndex()
          this.pickerSelectedIndex[i] = index

          let value = null
143
          let text = ''
M
init  
miaodian 已提交
144
          if (this.pickerData[i].length) {
A
Amy 已提交
145 146
            value = this.pickerData[i][index][this.valueKey]
            text = this.pickerData[i][index][this.textKey]
M
init  
miaodian 已提交
147 148 149 150 151
          }
          if (this.pickerSelectedVal[i] !== value) {
            changed = true
          }
          this.pickerSelectedVal[i] = value
152
          pickerSelectedText[i] = text
M
init  
miaodian 已提交
153 154
        }

155
        this.$emit(EVENT_SELECT, this.pickerSelectedVal, this.pickerSelectedIndex, pickerSelectedText)
M
init  
miaodian 已提交
156 157

        if (changed) {
158
          this.$emit(EVENT_VALUE_CHANGE, this.pickerSelectedVal, this.pickerSelectedIndex, pickerSelectedText)
M
init  
miaodian 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172
        }
      },
      cancel() {
        this.hide()
        this.$emit(EVENT_CANCEL)
      },
      show() {
        if (this.isVisible) {
          return
        }

        this.isVisible = true
        if (!this.wheels || this.dirty) {
          this.$nextTick(() => {
173
            this.wheels = this.wheels || []
M
init  
miaodian 已提交
174 175
            let wheelWrapper = this.$refs.wheelWrapper
            for (let i = 0; i < this.pickerData.length; i++) {
176
              this._createWheel(wheelWrapper, i).enable()
177
              this.wheels[i].wheelTo(this.pickerSelectedIndex[i])
M
init  
miaodian 已提交
178
            }
179
            this.dirty && this._destroyExtraWheels()
M
init  
miaodian 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
            this.dirty = false
          })
        } else {
          for (let i = 0; i < this.pickerData.length; i++) {
            this.wheels[i].enable()
            this.wheels[i].wheelTo(this.pickerSelectedIndex[i])
          }
        }
      },
      hide() {
        if (!this.isVisible) {
          return
        }
        this.isVisible = false

        for (let i = 0; i < this.pickerData.length; i++) {
          this.wheels[i].disable()
        }
      },
      setData(data, selectedIndex) {
D
sync  
dolymood 已提交
200
        this.pickerSelectedIndex = selectedIndex ? [...selectedIndex] : []
M
init  
miaodian 已提交
201
        this.pickerData = data.slice()
A
Amy 已提交
202 203
        if (this.isVisible) {
          this.$nextTick(() => {
204 205 206 207
            const wheelWrapper = this.$refs.wheelWrapper
            this.pickerData.forEach((item, i) => {
              this._createWheel(wheelWrapper, i)
              this.wheels[i].wheelTo(this.pickerSelectedIndex[i])
A
Amy 已提交
208
            })
209
            this._destroyExtraWheels()
A
Amy 已提交
210 211 212 213
          })
        } else {
          this.dirty = true
        }
M
init  
miaodian 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
      },
      refill(datas) {
        let ret = []
        if (!datas.length) {
          return ret
        }
        datas.forEach((data, index) => {
          ret[index] = this.refillColumn(index, data)
        })
        return ret
      },
      refillColumn(index, data) {
        const wheelWrapper = this.$refs.wheelWrapper
        let scroll = wheelWrapper.children[index].querySelector('.wheel-scroll')
        let wheel = this.wheels ? this.wheels[index] : false
        let dist = 0
        if (scroll && wheel) {
          let oldData = this.pickerData[index]
          this.$set(this.pickerData, index, data)
          let selectedIndex = wheel.getSelectedIndex()
          if (oldData.length) {
A
Amy 已提交
235
            let oldValue = oldData[selectedIndex][this.valueKey]
M
init  
miaodian 已提交
236
            for (let i = 0; i < data.length; i++) {
A
Amy 已提交
237
              if (data[i][this.valueKey] === oldValue) {
M
init  
miaodian 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
                dist = i
                break
              }
            }
          }
          this.pickerSelectedIndex[index] = dist
          this.$nextTick(() => {
            // recreate wheel so that the wrapperHeight will be correct.
            wheel = this._createWheel(wheelWrapper, index)
            wheel.wheelTo(dist)
          })
        }
        return dist
      },
      scrollTo(index, dist) {
        const wheel = this.wheels[index]
        this.pickerSelectedIndex[index] = dist
        wheel.wheelTo(dist)
      },
      refresh() {
258
        this.$nextTick(() => {
M
init  
miaodian 已提交
259 260 261
          this.wheels.forEach((wheel) => {
            wheel.refresh()
          })
262
        })
M
init  
miaodian 已提交
263 264
      },
      _createWheel(wheelWrapper, i) {
265 266 267 268
        if (!this.wheels[i]) {
          const wheel = this.wheels[i] = new BScroll(wheelWrapper.children[i], {
            wheel: {
              selectedIndex: this.pickerSelectedIndex[i] || 0
D
dolymood 已提交
269
            },
A
Amy 已提交
270
            swipeTime: this.swipeTime,
D
dolymood 已提交
271
            observeDOM: false
272 273 274 275 276 277 278 279
          })
          wheel.on('scrollEnd', () => {
            this.$emit(EVENT_CHANGE, i, wheel.getSelectedIndex())
          })
        } else {
          this.wheels[i].refresh()
        }
        return this.wheels[i]
M
init  
miaodian 已提交
280
      },
281 282 283 284 285 286 287 288 289
      _destroyExtraWheels() {
        const dataLength = this.pickerData.length
        if (this.wheels.length > dataLength) {
          const extraWheels = this.wheels.splice(dataLength)
          extraWheels.forEach((wheel) => {
            wheel.destroy()
          })
        }
      },
M
init  
miaodian 已提交
290 291 292 293 294 295 296
      _canConfirm() {
        return this.wheels.every((wheel) => {
          return !wheel.isInTransition
        })
      }
    },
    watch: {
297 298 299 300 301
      data(newVal) {
        this.setData(newVal, this.selectedIndex)
      },
      selectedIndex(newVal) {
        this.setData(this.data, newVal)
M
init  
miaodian 已提交
302 303 304 305 306 307 308 309 310
      }
    },
    components: {
      CubePopup
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
D
dolymood 已提交
311 312
  @require "../../common/stylus/mixin.styl"
  @require "../../common/stylus/variable.styl"
M
init  
miaodian 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

  $picker-lr-padding = 16px

  .cube-picker-fade-enter, .cube-picker-fade-leave-active
    opacity: 0

  .cube-picker-fade-enter-active, .cube-picker-fade-leave-active
    transition: all .3s ease-in-out

  .cube-picker-panel
    height: 273px
    text-align: center
    font-size: $fontsize-medium
    background: $picker-bgc

  .cube-picker-move-enter, .cube-picker-move-leave-active
    transform: translate3d(0, 273px, 0)

  .cube-picker-move-enter-active, .cube-picker-move-leave-active
    transition: all .3s ease-in-out

  .cube-picker-choose
    position: relative
    height: 60px
    > span
      position: absolute
      top: 6px
      padding: 16px $picker-lr-padding
      font-size: $fontsize-medium
    > [data-action="confirm"]
      right: 0
      color: $picker-confirm-btn-color
      &:active
        color: $picker-confirm-btn-active-color
    > [data-action="cancel"]
      left: 0
      color: $picker-cancel-btn-color
      &:active
        color: $picker-cancel-btn-active-color
    > h1
      margin: 0
      line-height: 60px
      text-align: center
      font-weight: normal
      font-size: $fontsize-large-x
      color: $picker-title-color

  .cube-picker-content
    position: relative
    top: 20px
    > i
      position: absolute
      z-index: 10
      left: 0
      width: 100%
      height: 68px
      pointer-events: none
      transform: translateZ(0)
    > .border-bottom-1px
      top: 0
      background: linear-gradient(to top, rgba(255, 255, 255, 0.4), rgba(255, 255, 255, 0.8))
    > .border-top-1px
D
dolymood 已提交
375
      bottom: 0
M
init  
miaodian 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
      background: linear-gradient(to bottom, rgba(255, 255, 255, 0.4), rgba(255, 255, 255, 0.8))

  .cube-picker-wheel-wrapper
    display: flex
    padding: 0 $picker-lr-padding
    > div
      flex-fix()
      height: 173px
      overflow: hidden
      font-size: $fontsize-large-xx

  .wheel-scroll
    padding: 0
    margin-top: 68px
    line-height: 36px
    list-style: none
    > li
      list-style: none
      height: 36px
      overflow: hidden
      white-space: nowrap
      color: $picker-item-color

  .cube-picker-footer
    height: 20px
</style>