四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.9 KiB

  1. export default {
  2. methods: {
  3. //判断是否显示头部
  4. getFlipHeaderShow (item) {
  5. return ['text', 'slot'].includes(item.type) ? typeof item.headerShow == 'boolean' ? item.headerShow : this.headerShow : false
  6. },
  7. //判断是否显示底部
  8. getFlipFooterShow (item) {
  9. return ['text', 'slot'].includes(item.type) ? typeof item.footerShow == 'boolean' ? item.footerShow : this.footerShow : false
  10. },
  11. //渲染页面
  12. flipRender ({chapter, current, start = 0}) {
  13. if ( chapter ) {//如果传入章节内容
  14. const index = this.chapters.findIndex(c => c.index == chapter.index)//是否已经包含相同章节
  15. if (index > -1) this.chapters[index] = chapter//如果包含则更新
  16. else this.chapters.push(chapter)//否则添加新章节
  17. }
  18. current = parseInt(current || 0)//强制转换类型为int
  19. start = parseInt(start)//强制转换类型为int
  20. this.pages = [{index: current, type: 'loading'}]//显示loading
  21. this.current = 0//重置current
  22. this.$refs.flip.refresh()
  23. const cgs = this.chapters.filter(c => c.index == current || c.index == current - 1 || c.index == current + 1)//筛选出符合条件的三个章节内容
  24. let index = 0, arr = []
  25. const computedId = this.getComputedId()
  26. this.computeds.push(computedId)
  27. const computedIndex = this.computeds.indexOf(computedId)
  28. this.$nextTick(function () {
  29. setTimeout(() => {
  30. const handle = () => {
  31. this.$refs.computed[computedIndex] && this.$refs.computed[computedIndex].start({
  32. chapter: cgs[index],
  33. success: pages => {
  34. arr = arr.concat(pages)
  35. if ( index < cgs.length - 1 ) {
  36. index++
  37. this.$nextTick(function () { handle() })
  38. } else {
  39. this.computeds.splice(computedIndex, 1)
  40. this.pages = this.handlePages(arr)
  41. const pageIndex = this.pages.findIndex(p => start >= p.start && start < p.end && p.index == current )//定位章节
  42. if ( pageIndex == -1 ) this.current = this.pages.findIndex(p => start >= p.end && p.index == current )//定位章节
  43. else this.current = pageIndex
  44. this.$nextTick(function () {
  45. this.$refs.flip.refresh()
  46. this.handleChange({current: this.current, detail: this.pages[this.current] })
  47. this.autoplaySync = this.autoplay
  48. })
  49. }
  50. }
  51. })
  52. }
  53. handle()
  54. }, 100)
  55. })
  56. },
  57. //渲染下一章节
  58. handleFlipLoadRender (status, chapter, callback, isPrev) {
  59. if (status == 'success') {//获取内容成功
  60. const nowIndex = isPrev ? this.pages[0].index : this.pages[this.pages.length-1].index
  61. const chapterIndex = this.chapters.findIndex(c => c.index == chapter.index)//是否已经包含相同章节
  62. if (chapterIndex > -1) this.chapters[chapterIndex] = chapter//如果包含则更新
  63. else this.chapters.push(chapter)//否则添加新章节
  64. const computedId = this.getComputedId()
  65. this.computeds.push(computedId)
  66. const computedIndex = this.computeds.indexOf(computedId)
  67. this.$nextTick(function () {
  68. setTimeout(() => {
  69. this.$refs.computed[computedIndex] && this.$refs.computed[computedIndex].start({
  70. chapter: chapter,
  71. success: p => {
  72. this.computeds.splice(computedIndex, 1)
  73. const pages = isPrev ? p.concat(this.pages) : this.pages.concat(p)//添加新计算的章节内容
  74. this.pages = pages//渲染章节内容
  75. if ( isPrev ) {//如果是加载上一章节需要重新定位
  76. this.current = pages.findIndex(page => page.index == nowIndex)//定位页面
  77. this.$refs.flip.refresh()//刷新翻页组件
  78. }
  79. this.chapterLoading = false//关闭章节加载等待
  80. callback && callback(status)//关闭加载动画
  81. this.handleChange({current: this.current, detail: this.pages[this.current]})//触发change事件
  82. }
  83. })
  84. }, 100)
  85. })
  86. } else {
  87. this.chapterLoading = false//关闭章节加载等待
  88. callback && callback(status)//关闭加载动画
  89. }
  90. }
  91. }
  92. }