1
1
import Vue from 'vue/dist/vue.esm'
2
2
3
3
import matestackEventHub from 'js/event-hub'
4
+ import queryParamsHelper from 'js/helpers/query-params-helper'
4
5
5
6
import componentMixin from 'component/component'
6
7
import asyncMixin from 'async/async'
@@ -11,14 +12,15 @@ const componentDef = {
11
12
return {
12
13
currentLimit : null ,
13
14
currentOffset : null ,
14
- currentFilteredCount : null
15
+ currentFilteredCount : null ,
16
+ currentBaseCount : null
15
17
}
16
18
} ,
17
19
methods : {
18
20
next : function ( ) {
19
- if ( this . currentTo ( ) < this . currentFilteredCount ) {
21
+ if ( this . currentTo ( ) < this . currentCount ( ) ) {
20
22
this . currentOffset += this . currentLimit
21
- var url = this . updateQueryParams ( this . componentConfig [ "id" ] + "-offset" , this . currentOffset )
23
+ var url = queryParamsHelper . updateQueryParams ( this . componentConfig [ "id" ] + "-offset" , this . currentOffset )
22
24
window . history . pushState ( { matestackApp : true , url : url } , null , url ) ;
23
25
matestackEventHub . $emit ( this . componentConfig [ "id" ] + "-update" )
24
26
}
@@ -30,72 +32,36 @@ const componentDef = {
30
32
} else {
31
33
this . currentOffset -= this . currentLimit
32
34
}
33
- var url = this . updateQueryParams ( this . componentConfig [ "id" ] + "-offset" , this . currentOffset )
35
+ var url = queryParamsHelper . updateQueryParams ( this . componentConfig [ "id" ] + "-offset" , this . currentOffset )
34
36
window . history . pushState ( { matestackApp : true , url : url } , null , url ) ;
35
37
matestackEventHub . $emit ( this . componentConfig [ "id" ] + "-update" )
36
38
}
37
39
} ,
38
40
currentTo : function ( ) {
39
41
var to = parseInt ( this . currentOffset ) + parseInt ( this . currentLimit )
40
- if ( to > parseInt ( this . currentFilteredCount ) ) {
41
- return this . currentFilteredCount ;
42
+ if ( to > parseInt ( this . currentCount ( ) ) ) {
43
+ return this . currentCount ( ) ;
42
44
} else {
43
45
return to ;
44
46
}
45
47
} ,
48
+ currentCount : function ( ) {
49
+ if ( this . currentFilteredCount != null || this . currentFilteredCount != undefined ) {
50
+ return this . currentFilteredCount ;
51
+ } else {
52
+ return this . currentBaseCount ;
53
+ }
54
+ } ,
46
55
goToPage : function ( page ) {
47
56
this . currentOffset = parseInt ( this . currentLimit ) * ( parseInt ( page ) - 1 )
48
- var url = this . updateQueryParams ( this . componentConfig [ "id" ] + "-offset" , this . currentOffset )
57
+ var url = queryParamsHelper . updateQueryParams ( this . componentConfig [ "id" ] + "-offset" , this . currentOffset )
49
58
window . history . pushState ( { matestackApp : true , url : url } , null , url ) ;
50
59
matestackEventHub . $emit ( this . componentConfig [ "id" ] + "-update" )
51
- } ,
52
- updateQueryParams : function ( key , value , url ) {
53
- if ( ! url ) url = window . location . href ;
54
- var re = new RegExp ( "([?&])" + key + "=.*?(&|#|$)(.*)" , "gi" ) ,
55
- hash ;
56
-
57
- if ( re . test ( url ) ) {
58
- if ( typeof value !== 'undefined' && value !== null )
59
- return url . replace ( re , '$1' + key + "=" + value + '$2$3' ) ;
60
- else {
61
- hash = url . split ( '#' ) ;
62
- url = hash [ 0 ] . replace ( re , '$1$3' ) . replace ( / ( & | \? ) $ / , '' ) ;
63
- if ( typeof hash [ 1 ] !== 'undefined' && hash [ 1 ] !== null )
64
- url += '#' + hash [ 1 ] ;
65
- return url ;
66
- }
67
- }
68
- else {
69
- if ( typeof value !== 'undefined' && value !== null ) {
70
- var separator = url . indexOf ( '?' ) !== - 1 ? '&' : '?' ;
71
- hash = url . split ( '#' ) ;
72
- url = hash [ 0 ] + separator + key + '=' + value ;
73
- if ( typeof hash [ 1 ] !== 'undefined' && hash [ 1 ] !== null )
74
- url += '#' + hash [ 1 ] ;
75
- return url ;
76
- }
77
- else
78
- return url ;
79
- }
80
- } ,
81
- getQueryParam : function ( name , url ) {
82
- if ( ! url ) url = window . location . href ;
83
- name = name . replace ( / [ \[ \] ] / g, '\\$&' ) ;
84
- var regex = new RegExp ( '[?&]' + name + '(=([^&#]*)|&|#|$)' ) ,
85
- results = regex . exec ( url ) ;
86
- if ( ! results ) return null ;
87
- if ( ! results [ 2 ] ) return '' ;
88
- return decodeURIComponent ( results [ 2 ] . replace ( / \+ / g, ' ' ) ) ;
89
- }
90
- } ,
91
- beforeCreate : function ( ) {
92
- if ( this . $options . propsData . componentConfig [ "rerender_on" ] == undefined ) {
93
- this . $options . propsData . componentConfig [ "rerender_on" ] = this . $options . propsData . componentConfig [ "id" ] + "-update"
94
60
}
95
61
} ,
96
62
mounted : function ( ) {
97
- if ( this . getQueryParam ( this . componentConfig [ "id" ] + "-offset" ) != null ) {
98
- this . currentOffset = parseInt ( this . getQueryParam ( this . componentConfig [ "id" ] + "-offset" ) )
63
+ if ( queryParamsHelper . getQueryParam ( this . componentConfig [ "id" ] + "-offset" ) != null ) {
64
+ this . currentOffset = parseInt ( queryParamsHelper . getQueryParam ( this . componentConfig [ "id" ] + "-offset" ) )
99
65
} else {
100
66
if ( this . componentConfig [ "init_offset" ] != undefined ) {
101
67
this . currentOffset = this . componentConfig [ "init_offset" ]
@@ -104,8 +70,8 @@ const componentDef = {
104
70
}
105
71
}
106
72
107
- if ( this . getQueryParam ( this . componentConfig [ "id" ] + "-limit" ) != null ) {
108
- this . currentOffset = parseInt ( this . getQueryParam ( this . componentConfig [ "id" ] + "-limit" ) )
73
+ if ( queryParamsHelper . getQueryParam ( this . componentConfig [ "id" ] + "-limit" ) != null ) {
74
+ this . currentOffset = parseInt ( queryParamsHelper . getQueryParam ( this . componentConfig [ "id" ] + "-limit" ) )
109
75
} else {
110
76
if ( this . componentConfig [ "init_limit" ] != undefined ) {
111
77
this . currentLimit = this . componentConfig [ "init_limit" ]
@@ -116,6 +82,15 @@ const componentDef = {
116
82
117
83
if ( this . componentConfig [ "filtered_count" ] != undefined ) {
118
84
this . currentFilteredCount = this . componentConfig [ "filtered_count" ]
85
+ if ( this . currentOffset >= this . currentFilteredCount ) {
86
+ this . previous ( )
87
+ }
88
+ }
89
+ if ( this . componentConfig [ "base_count" ] != undefined ) {
90
+ this . currentBaseCount = this . componentConfig [ "base_count" ]
91
+ if ( this . currentOffset >= this . currentBaseCount ) {
92
+ this . previous ( )
93
+ }
119
94
}
120
95
}
121
96
}
0 commit comments