inifinity.js 12.3 KB
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 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 375 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 402 403 404 405 406 407 408 409 410 411 412 413
import {
  style,
  cssVendor
} from '../util/dom'
import { assert } from '../util/debug'
// import { ease } from '../util/ease'

// Number of items to instantiate beyond current view in the scroll direction.
const RUNWAY_ITEMS = 30

// Number of items to instantiate beyond current view in the opposite direction.
const RUNWAY_ITEMS_OPPOSITE = 10

// The animation interval (in ms) for fading in content from tombstones.
const ANIMATION_DURATION_MS = 200

// The number of pixels of default additional length to allow scrolling to.
const DEFAULT_SCROLL_RUNWAY = 2000

export function infiniteMixin(BScroll) {
  BScroll.prototype._initInfinite = function () {
    this.options.probeType = 3
    this.maxScrollY = -DEFAULT_SCROLL_RUNWAY
    this.infiniteScroller = new InfiniteScroller(this, this.options.infinity)
  }
}

function isTombstoneNode(node) {
  if (node && node.classList) {
    return node.classList.contains('tombstone')
  }
}

function InfiniteScroller(scroller, options) {
  this.options = options
  assert(typeof this.options.createTombstone === 'function', 'Infinite scroll need createTombstone Function to create tombstone')

  assert(typeof this.options.fetch === 'function', 'Infinite scroll need fetch Function to fetch new data.')

  assert(typeof this.options.render === 'function', 'Infinite scroll need render Function to render each item.')

  this.firstAttachedItem = 0
  this.lastAttachedItem = 0

  this.anchorScrollTop = 0
  this.anchorItem = {
    index: 0,
    offset: 0
  }
  this.tombstoneHeight = 0
  this.tombstoneWidth = 0
  this.tombstones = []
  this.tombstonesAnimationHandlers = []

  this.items = []
  this.loadedItems = 0
  this.requestInProgress = false
  this.hasMore = true

  this.scroller = scroller
  this.wrapperEl = this.scroller.wrapper
  this.scrollerEl = this.scroller.scroller
  this.scroller.on('scroll', () => {
    this.onScroll()
  })
  this.scroller.on('resize', () => {
    this.onResize()
  })
  this.scroller.on('destroy', () => {
    this.destroy()
  })

  // wait scroll core init
  this._onResizeHandler = setTimeout(() => {
    this.onResize()
  })
}

InfiniteScroller.prototype.destroy = function () {
  // In extreme scene, destroy is triggered before _onResizeHandler
  clearTimeout(this._onResizeHandler)
  this.tombstonesAnimationHandlers.forEach(function (handler) {
    clearTimeout(handler)
  })
  this.tombstonesAnimationHandlers = null
  this.items.forEach((item) => {
    if (item.node) {
      this.scrollerEl.removeChild(item.node)
      item.node = null
    }
  })
  this.scroller.infiniteScroller = null
  this.scroller = null
  this.wrapperEl = null
  this.scrollerEl = null
  this.items = null
  this.tombstones = null
}

InfiniteScroller.prototype.onScroll = function () {
  const scrollTop = -this.scroller.y
  let delta = scrollTop - this.anchorScrollTop
  if (scrollTop === 0) {
    this.anchorItem = {
      index: 0,
      offset: 0
    }
  } else {
    this.anchorItem = this._calculateAnchoredItem(this.anchorItem, delta)
  }

  this.anchorScrollTop = scrollTop
  let lastScreenItem = this._calculateAnchoredItem(this.anchorItem, this.scroller.wrapperHeight)

  let start = this.anchorItem.index
  let end = lastScreenItem.index
  if (delta < 0) {
    start -= RUNWAY_ITEMS
    end += RUNWAY_ITEMS_OPPOSITE
  } else {
    start -= RUNWAY_ITEMS_OPPOSITE
    end += RUNWAY_ITEMS
  }
  this.fill(start, end)
  this.maybeRequestContent()
}

InfiniteScroller.prototype.onResize = function () {
  let tombstone = this.options.createTombstone()
  tombstone.style.position = 'absolute'
  this.scrollerEl.appendChild(tombstone)
  tombstone.style.display = ''
  this.tombstoneHeight = tombstone.offsetHeight
  this.tombstoneWidth = tombstone.offsetWidth
  this.scrollerEl.removeChild(tombstone)

  for (let i = 0; i < this.items.length; i++) {
    this.items[i].height = this.items[i].width = 0
  }

  this.onScroll()
}

InfiniteScroller.prototype.fill = function (start, end) {
  this.firstAttachedItem = Math.max(0, start)
  if (!this.hasMore) {
    end = Math.min(end, this.items.length)
  }
  this.lastAttachedItem = end
  this.attachContent()
}

InfiniteScroller.prototype.maybeRequestContent = function () {
  if (this.requestInProgress || !this.hasMore) {
    return
  }
  let itemsNeeded = this.lastAttachedItem - this.loadedItems
  if (itemsNeeded <= 0) {
    return
  }
  this.requestInProgress = true
  this.options.fetch(itemsNeeded).then((items) => {
    this.requestInProgress = false
    if (items) {
      this.addContent(items)
    } else {
      this.hasMore = false
      let tombstoneLen = this._removeTombstones()
      let curPos = 0
      if (this.anchorItem.index <= this.items.length) {
        curPos = this._fixScrollPosition()
        this._setupAnimations({}, curPos)
        this.scroller.resetPosition(this.scroller.options.bounceTime)
      } else {
        this.anchorItem.index -= tombstoneLen
        curPos = this._fixScrollPosition()
        this._setupAnimations({}, curPos)
        this.scroller.stop()
        this.scroller.resetPosition()
        this.onScroll()
      }
    }
  })
}

InfiniteScroller.prototype.addContent = function (items) {
  for (let i = 0; i < items.length; i++) {
    if (this.items.length <= this.loadedItems) {
      this._addItem()
    }
    this.items[this.loadedItems++].data = items[i]
  }
  this.attachContent()
  this.maybeRequestContent()
}

InfiniteScroller.prototype.attachContent = function () {
  let unusedNodes = this._collectUnusedNodes()
  let tombstoneAnimations = this._createDOMNodes(unusedNodes)
  this._cleanupUnusedNodes(unusedNodes)
  this._cacheNodeSize()
  let curPos = this._fixScrollPosition()
  this._setupAnimations(tombstoneAnimations, curPos)
}

InfiniteScroller.prototype.resetMore = function () {
  this.hasMore = true
}

InfiniteScroller.prototype._removeTombstones = function () {
  let markIndex
  let tombstoneLen = 0
  let itemLen = this.items.length
  for (let i = 0; i < itemLen; i++) {
    const currentNode = this.items[i].node
    const currentData = this.items[i].data
    if ((!currentNode || isTombstoneNode(currentNode)) && !currentData) {
      // 0 should be excluded
      if (markIndex === void 0) {
        markIndex = i
      }
      if (currentNode) {
        this.scrollerEl.removeChild(currentNode)
      }
    }
  }
  tombstoneLen = itemLen - markIndex
  this.items.splice(markIndex)
  this.lastAttachedItem = Math.min(this.lastAttachedItem, this.items.length)
  return tombstoneLen
}

InfiniteScroller.prototype._collectUnusedNodes = function () {
  let unusedNodes = []
  for (let i = 0; i < this.items.length; i++) {
    // Skip the items which should be visible.
    if (i === this.firstAttachedItem) {
      i = this.lastAttachedItem - 1
      continue
    }
    const currentNode = this.items[i].node
    if (currentNode) {
      if (isTombstoneNode(currentNode)) {
        // Cache tombstones for reuse
        this.tombstones.push(currentNode)
        this.tombstones[this.tombstones.length - 1].style.display = 'none'
      } else {
        unusedNodes.push(currentNode)
      }
    }
    this.items[i].node = null
  }
  return unusedNodes
}

InfiniteScroller.prototype._createDOMNodes = function (unusedNodes) {
  let tombstoneAnimations = {}
  for (let i = this.firstAttachedItem; i < this.lastAttachedItem; i++) {
    while (this.items.length <= i) {
      this._addItem()
    }
    const currentNode = this.items[i].node
    const currentData = this.items[i].data
    if (currentNode) {
      if (isTombstoneNode(currentNode) && currentData) {
        currentNode.style.zIndex = 1
        tombstoneAnimations[i] = [currentNode, this.items[i].top - this.anchorScrollTop]
        this.items[i].node = null
      } else {
        continue
      }
    }
    let node = currentData ? this.options.render(currentData, unusedNodes.pop()) : this._getTombStone()
    node.style.position = 'absolute'
    this.items[i].top = -1
    this.scrollerEl.appendChild(node)
    this.items[i].node = node
  }
  return tombstoneAnimations
}

InfiniteScroller.prototype._cleanupUnusedNodes = function (unusedNodes) {
  while (unusedNodes.length) {
    this.scrollerEl.removeChild(unusedNodes.pop())
  }
}

InfiniteScroller.prototype._cacheNodeSize = function () {
  for (let i = this.firstAttachedItem; i < this.lastAttachedItem; i++) {
    const item = this.items[i]
    // Only cache the height if we have the real contents, not a placeholder.
    if (item.data && !item.height) {
      const isTombstone = isTombstoneNode(item.node)
      item.height = isTombstone ? this.tombstoneHeight : item.node.offsetHeight
      item.width = isTombstone ? this.tombstoneWidth : item.node.offsetWidth
    }
  }
}

InfiniteScroller.prototype._fixScrollPosition = function () {
  this.anchorScrollTop = 0
  for (let i = 0; i < this.anchorItem.index; i++) {
    this.anchorScrollTop += this.items[i].height || this.tombstoneHeight
  }
  this.anchorScrollTop += this.anchorItem.offset

  // Position all nodes.
  let curPos = this.anchorScrollTop - this.anchorItem.offset
  let i = this.anchorItem.index
  while (i > this.firstAttachedItem) {
    curPos -= this.items[i - 1].height || this.tombstoneHeight
    i--
  }

  return curPos
}

InfiniteScroller.prototype._setupAnimations = function (tombstoneAnimations, curPos) {
  for (let i in tombstoneAnimations) {
    const animation = tombstoneAnimations[i]
    this.items[i].node.style[style.transform] = `translateY(${this.anchorScrollTop + animation[1]}px) scale(${this.tombstoneWidth / this.items[i].width}, ${this.tombstoneHeight / this.items[i].height})`
    // Call offsetTop on the nodes to be animated to force them to apply current transforms.
    /* eslint-disable no-unused-expressions */
    this.items[i].node.offsetTop
    animation[0].offsetTop
    this.items[i].node.style[style.transition] = `${cssVendor}transform ${ANIMATION_DURATION_MS}ms`
  }

  for (let i = this.firstAttachedItem; i < this.lastAttachedItem; i++) {
    const animation = tombstoneAnimations[i]
    if (animation) {
      const tombstoneNode = animation[0]
      tombstoneNode.style[style.transition] = `${cssVendor}transform ${ANIMATION_DURATION_MS}ms, opacity ${ANIMATION_DURATION_MS}ms`
      tombstoneNode.style[style.transform] = `translateY(${curPos}px) scale(${this.items[i].width / this.tombstoneWidth}, ${this.items[i].height / this.tombstoneHeight})`
      tombstoneNode.style.opacity = 0
    }
    if (curPos !== this.items[i].top) {
      if (!animation) {
        this.items[i].node.style[style.transition] = ''
      }
      this.items[i].node.style[style.transform] = `translateY(${curPos}px)`
    }
    this.items[i].top = curPos
    curPos += this.items[i].height || this.tombstoneHeight
  }

  this.scroller.maxScrollY = -(curPos - this.scroller.wrapperHeight + (this.hasMore ? DEFAULT_SCROLL_RUNWAY : 0))

  const tombstoneAnimationsHandler = setTimeout(() => {
    for (let i in tombstoneAnimations) {
      const animation = tombstoneAnimations[i]
      animation[0].style.display = 'none'
      // Tombstone can be recycled now.
      this.tombstones.push(animation[0])
    }
  }, ANIMATION_DURATION_MS)

  this.tombstonesAnimationHandlers.push(tombstoneAnimationsHandler)
}

InfiniteScroller.prototype._getTombStone = function () {
  let tombstone = this.tombstones.pop()
  if (tombstone) {
    tombstone.style.display = ''
    tombstone.style.opacity = 1
    tombstone.style[style.transform] = ''
    tombstone.style[style.transition] = ''
    return tombstone
  }
  return this.options.createTombstone()
}

InfiniteScroller.prototype._addItem = function () {
  this.items.push({
    data: null,
    node: null,
    height: 0,
    width: 0,
    top: 0
  })
}

InfiniteScroller.prototype._calculateAnchoredItem = function (initialAnchor, delta) {
  if (delta === 0) {
    return initialAnchor
  }
  let i = initialAnchor.index
  let tombstones = 0

  delta += initialAnchor.offset
  if (delta < 0) {
    while (delta < 0 && i > 0 && this.items[i - 1].height) {
      delta += this.items[i - 1].height
      i--
    }
    tombstones = Math.max(-i, Math.ceil(Math.min(delta, 0) / this.tombstoneHeight))
  } else {
    while (delta > 0 && i < this.items.length && this.items[i].height && this.items[i].height < delta) {
      delta -= this.items[i].height
      i++
    }
    if (i >= this.items.length || !this.items[i].height) {
      tombstones = Math.floor(Math.max(delta, 0) / this.tombstoneHeight)
    }
  }
  i += tombstones
  delta -= tombstones * this.tombstoneHeight

  return {
    index: i,
    offset: delta
  }
}