11<template >
22<div >
33 <div class =" nav" >
4- <label class =" title" >文言片語</label >
5- <input class =" search" placeholder =" Search snippets..." v-model =" searchText" />
6- <span @click =" search" class =" iconify" data-icon =" mdi:search" data-inline =" false" ></span >
7- <span @click =" newSnippet" class =" iconify" data-icon =" mdi:plus" data-inline =" false" ></span >
4+ <div class =" content" >
5+ <label class =" title" >文言片語</label >
6+ <input class =" search" placeholder =" Search snippets..." v-model =" searchText" />
7+ <span @click =" search" class =" iconify" data-icon =" mdi:search" data-inline =" false" ></span >
8+ <span @click =" newSnippet" class =" iconify" data-icon =" mdi:plus" data-inline =" false" ></span >
9+ </div >
810 </div >
911 <div class =" showcase" >
1012 <snippet-preview
2123 </div >
2224 </div >
2325 <spinner v-show =" loading" />
26+ <div class =" end-of-pages" v-if =' endOfPages' >you reached the end :)</div >
2427</div >
2528</template >
2629
@@ -39,18 +42,28 @@ export default {
3942 },
4043 data () {
4144 return {
42- loading: true ,
45+ loading: false ,
4346 searchText: ' ' ,
44- snippets: {} ,
47+ snippets: [] ,
4548 page: 0 ,
46- totalPage : - 1 ,
49+ totalPages : 9999 ,
4750 editing: null
4851 }
4952 },
53+ computed: {
54+ endOfPages (){
55+ return this .page >= this .totalPages
56+ }
57+ },
5058 methods: {
51- async fetch () {
59+ async fetchNext () {
60+ if (this .endOfPages || this .loading )
61+ return
5262 this .loading = true
53- this .snippets = await API .getAll ()
63+ const { snippets , totalPages , page } = await API .getPage (this .page + 1 )
64+ this .snippets .push (... snippets)
65+ this .totalPages = totalPages
66+ this .page = page
5467 this .loading = false
5568 },
5669 async search () {
@@ -63,15 +76,24 @@ export default {
6376 this .$set (this .snippets , idx, snippet)
6477 }
6578 },
66- mounted (){
67- this .fetch ()
79+ mounted () {
80+ this .page = 0
81+ this .fetchNext ()
82+
83+ window .onscroll = () => {
84+ let bottomOfWindow = Math .max (window .pageYOffset , document .documentElement .scrollTop , document .body .scrollTop ) + window .innerHeight === document .documentElement .offsetHeight
85+
86+ if (bottomOfWindow)
87+ this .fetchNext ()
88+ }
6889 }
6990}
7091 </script >
7192
7293<style lang="stylus">
94+ $max-width = 85rem
95+
7396.nav
74- padding : 5px
7597 background : #f1 f1 f1
7698 border-bottom : 1px solid gainsboro
7799 position : fixed
@@ -81,8 +103,13 @@ export default {
81103 z-index : 1
82104 white-space : nowrap
83105
84- *
85- vertical-align : middle
106+ .content
107+ max-width $max-width
108+ margin 0 auto
109+ padding 5px
110+
111+ *
112+ vertical-align : middle
86113
87114 .iconify
88115 font-size : 1.5em
@@ -101,10 +128,11 @@ export default {
101128 font-size : 1.1em
102129
103130.showcase
104- margin-top : 3.6rem
105131 padding : 1rem
106132 display : grid
107133 grid-template-columns : repeat (auto - fill , minmax (18rem , 1 fr ))
134+ max-width $max-width
135+ margin 3.6rem auto 1rem auto
108136
109137.modal
110138 z-index 2
@@ -121,4 +149,10 @@ export default {
121149 top 5rem
122150 bottom 5rem
123151 right 5rem
152+
153+ .end-of-pages
154+ padding 2rem
155+ opacity 0.3
156+ text-align center
157+ font-style italic
124158 </style >
0 commit comments