@@ -13,6 +13,19 @@ const getOffsetFromUrl = (url) => {
1313 return parseInt ( offsetMatch [ 1 ] , 10 ) ;
1414} ;
1515
16+ const removeHost = ( url ) => {
17+ if ( ! url ) {
18+ return url ;
19+ }
20+
21+ const hostMatch = url . match ( / ^ h t t p s ? : \/ \/ [ ^ / ] * \/ ( .* ) $ / ) ;
22+ if ( hostMatch == null ) {
23+ return url ;
24+ }
25+
26+ return `${ window . location . origin } /${ hostMatch [ 1 ] } ` ;
27+ } ;
28+
1629const storeOffsetInUrl = ( offset ) => {
1730 let href = window . location . href ;
1831
@@ -36,6 +49,28 @@ const storeOffsetInUrl = (offset) => {
3649 window . location . href = href ;
3750} ;
3851
52+ const getLimitFromUrl = ( url , prevLimit ) => {
53+ try {
54+ const limitMatch = url . match ( / [ ? # ] .* l i m i t = ( \d + ) / ) ;
55+
56+ return parseInt ( limitMatch [ 1 ] , 10 ) ;
57+ } catch ( err ) {
58+ return prevLimit ;
59+ }
60+ } ;
61+
62+ const getSidebarTotal = ( count , limit ) => (
63+ count !== 0 && limit !== 0
64+ ? Math . ceil ( count / limit )
65+ : 0
66+ ) ;
67+
68+ const getSidebarPage = ( offset , limit ) => (
69+ limit !== 0
70+ ? Math . ceil ( offset / limit ) + 1
71+ : 0
72+ ) ;
73+
3974export default {
4075 components : { VueJsonPretty, Preview } ,
4176
@@ -53,6 +88,9 @@ export default {
5388 offset : getOffsetFromUrl ( window . location . href ) ,
5489 picked : 'all' ,
5590 count : 0 ,
91+ prevLimit : 0 ,
92+ paginationPages : 0 ,
93+ paginationPage : 0 ,
5694 isSuperuser : false ,
5795 isMetadataActive : false ,
5896 isAnnotationGuidelineActive : false ,
@@ -97,14 +135,44 @@ export default {
97135 }
98136 } ,
99137
138+ async nextPagination ( ) {
139+ if ( this . next ) {
140+ this . url = this . next ;
141+ await this . search ( ) ;
142+ this . pageNumber = 0 ;
143+ } else {
144+ this . pageNumber = this . docs . length - 1 ;
145+ }
146+ this . resetScrollbar ( ) ;
147+ } ,
148+
149+ async prevPagination ( ) {
150+ if ( this . prev ) {
151+ this . url = this . prev ;
152+ await this . search ( ) ;
153+ this . pageNumber = this . docs . length - this . limit ;
154+ } else {
155+ this . pageNumber = 0 ;
156+ }
157+ this . resetScrollbar ( ) ;
158+ } ,
159+
100160 async search ( ) {
101161 await HTTP . get ( this . url ) . then ( ( response ) => {
102162 this . docs = response . data . results ;
103- this . next = response . data . next ;
104- this . prev = response . data . previous ;
163+ this . next = removeHost ( response . data . next ) ;
164+ this . prev = removeHost ( response . data . previous ) ;
105165 this . count = response . data . count ;
106166 this . annotations = this . docs . map ( doc => doc . annotations ) ;
107167 this . offset = getOffsetFromUrl ( this . url ) ;
168+ this . prevLimit = this . limit ;
169+ if ( this . next || this . prevLimit ) {
170+ this . limit = getLimitFromUrl ( this . next , this . prevLimit ) ;
171+ } else {
172+ this . limit = this . count ;
173+ }
174+ this . paginationPages = getSidebarTotal ( this . count , this . limit ) ;
175+ this . paginationPage = getSidebarPage ( this . offset , this . limit ) ;
108176 } ) ;
109177 } ,
110178
0 commit comments