@@ -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,13 +88,23 @@ 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 ,
5997 } ;
6098 } ,
6199
62100 methods : {
101+ resetScrollbar ( ) {
102+ const textbox = this . $refs . textbox ;
103+ if ( textbox ) {
104+ textbox . scrollTop = 0 ;
105+ }
106+ } ,
107+
63108 async nextPage ( ) {
64109 this . pageNumber += 1 ;
65110 if ( this . pageNumber === this . docs . length ) {
@@ -70,6 +115,8 @@ export default {
70115 } else {
71116 this . pageNumber = this . docs . length - 1 ;
72117 }
118+ } else {
119+ this . resetScrollbar ( ) ;
73120 }
74121 } ,
75122
@@ -83,17 +130,49 @@ export default {
83130 } else {
84131 this . pageNumber = 0 ;
85132 }
133+ } else {
134+ this . resetScrollbar ( ) ;
135+ }
136+ } ,
137+
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 ;
86145 }
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 ( ) ;
87158 } ,
88159
89160 async search ( ) {
90161 await HTTP . get ( this . url ) . then ( ( response ) => {
91162 this . docs = response . data . results ;
92- this . next = response . data . next ;
93- this . prev = response . data . previous ;
163+ this . next = removeHost ( response . data . next ) ;
164+ this . prev = removeHost ( response . data . previous ) ;
94165 this . count = response . data . count ;
95166 this . annotations = this . docs . map ( doc => doc . annotations ) ;
96167 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 ) ;
97176 } ) ;
98177 } ,
99178
0 commit comments