all files / src/scroll/ wheel.js

97.67% Statements 42/43
68.97% Branches 20/29
100% Functions 6/6
97.67% Lines 42/43
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            17× 17× 17×   17× 17×   17× 17×   17×       36× 36× 36× 36× 36×         36× 36× 19×   17×     36× 17× 17× 34× 17×   17×         36×       36×           17× 17× 17× 17× 34× 17× 17×          
export function wheelMixin (BScroll) {
  BScroll.prototype.wheelTo = function (index = 0) {
    Eif (this.options.wheel) {
      const y = -index * this.itemHeight
      this.scrollTo(0, y)
    }
  }
 
  BScroll.prototype.getSelectedIndex = function () {
    return this.options.wheel && this.selectedIndex
  }
 
  BScroll.prototype._initWheel = function () {
    const wheel = this.options.wheel
    Eif (!wheel.wheelWrapperClass) {
      wheel.wheelWrapperClass = 'wheel-scroll'
    }
    Eif (!wheel.wheelItemClass) {
      wheel.wheelItemClass = 'wheel-item'
    }
    Eif (!wheel.wheelDisabledItemClass) {
      wheel.wheelDisabledItemClass = 'wheel-disabled-item'
    }
    if (wheel.selectedIndex === undefined) {
      wheel.selectedIndex = 0
    }
  }
 
  BScroll.prototype._findNearestValidWheel = function (y) {
    y = y > 0 ? 0 : y < this.maxScrollY ? this.maxScrollY : y
    const wheel = this.options.wheel
    let currentIndex = Math.abs(Math.round(-y / this.itemHeight))
    const cacheIndex = currentIndex
    const items = this.items
    // Impersonation web native select
    // first, check whether there is a enable item whose index is smaller than currentIndex
    // then, check whether there is a enable item whose index is bigger than currentIndex
    // otherwise, there are all disabled items, just keep currentIndex unchange
    while (currentIndex >= 0) {
      if (items[currentIndex].className.indexOf(wheel.wheelDisabledItemClass) === -1) {
        break
      }
      currentIndex--
    }
 
    if (currentIndex < 0) {
      currentIndex = cacheIndex
      while (currentIndex <= items.length - 1) {
        if (items[currentIndex].className.indexOf(wheel.wheelDisabledItemClass) === -1) {
          break
        }
        currentIndex++
      }
    }
 
    // keep it unchange when all the items are disabled
    Iif (currentIndex === items.length) {
      currentIndex = cacheIndex
    }
    // when all the items are disabled, this.selectedIndex should always be -1
    return {
      index: this.wheelItemsAllDisabled ? -1 : currentIndex,
      y: -currentIndex * this.itemHeight
    }
  }
 
  BScroll.prototype._checkWheelAllDisabled = function () {
    const wheel = this.options.wheel
    const items = this.items
    this.wheelItemsAllDisabled = true
    for (let i = 0; i < items.length; i++) {
      if (items[i].className.indexOf(wheel.wheelDisabledItemClass) === -1) {
        this.wheelItemsAllDisabled = false
        break
      }
    }
  }
}