8
8
@click =" active - 1 > 0 ? setActivePage(active - 1) : false" >
9
9
<component
10
10
:is =" numberElement"
11
- :to =" getLinkPath(active - 1 > 0 ? active - 1 : 1)"
11
+ :to =" !!useLinkElement ? getLinkPath(active - 1 > 0 ? active - 1 : 1) : false "
12
12
:aria-disabled =" active <= 1"
13
13
:tabindex =" active <= 1 ? -1 : 0"
14
14
aria-label =" Previous page"
28
28
@click =" setActivePage(1)" >
29
29
<component
30
30
:is =" numberElement"
31
- :to =" useLinkElement ? getLinkPath(1) : false"
31
+ :to =" !! useLinkElement ? getLinkPath(1) : false"
32
32
:tabindex =" active === 1 ? -1 : 0"
33
33
aria-label =" page 1"
34
34
class =" base-pagination__number-inner"
46
46
@click =" setActivePage(n)" >
47
47
<component
48
48
:is =" numberElement"
49
- :to =" getLinkPath(n)"
49
+ :to =" !!useLinkElement ? getLinkPath(n) : false "
50
50
:tabindex =" active === n ? -1 : 0"
51
51
:aria-label =" `Page ${n}`"
52
52
class =" base-pagination__number-inner"
63
63
@click =" setActivePage(total)" >
64
64
<component
65
65
:is =" numberElement"
66
- :to =" getLinkPath(total)"
66
+ :to =" !!useLinkElement ? getLinkPath(total) : false "
67
67
:tabindex =" active === total ? -1 : 0"
68
68
:aria-label =" `Page ${total}`"
69
69
class =" base-pagination__number-inner"
82
82
@click =" setActivePage(n)" >
83
83
<component
84
84
:is =" numberElement"
85
- :to =" useLinkElement ? getLinkPath(n) : false"
85
+ :to =" !! useLinkElement ? getLinkPath(n) : false"
86
86
:tabindex =" active === n ? -1 : 0"
87
87
:aria-label =" `Page ${n}`"
88
88
class =" base-pagination__number-inner"
99
99
@click =" active + 1 <= total ? setActivePage(active + 1) : false" >
100
100
<component
101
101
:is =" numberElement"
102
- :to =" getLinkPath(active + 1 <= total ? active + 1 : total)"
102
+ :to =" !!useLinkElement ? getLinkPath(active + 1 <= total ? active + 1 : total) : false "
103
103
:aria-disabled =" active >= total"
104
104
:tabindex =" active >= total ? -1 : 0"
105
105
aria-label =" Next Page"
@@ -147,24 +147,65 @@ export default {
147
147
default: 1 ,
148
148
},
149
149
/**
150
- * specify if pagination elements should be links
151
- * (this needs vue-router)
150
+ * specify if pagination elements should be links - specify a vue link element or
151
+ * set the variable false if element should not be a link
152
+ * (this needs vue-router)<br>
153
+ * currently only vue components (like 'router-link' or 'nuxt-link') are supported!
152
154
*/
153
155
useLinkElement: {
154
- type: Boolean ,
155
- default: true ,
156
+ type: [String , Boolean ],
157
+ default: false ,
158
+ validator : val => (typeof val === ' boolean' && ! val) || (typeof val === ' string' && val),
159
+ },
160
+ /**
161
+ * if pagination elements are link-elements the default href is set as
162
+ * '[currentRoute]?page=[currentPage]'. Adding properties here gives the possibility to
163
+ * add additional parameters BEFORE the page parameter:<br>
164
+ * '[currentRoute]?[customParam1]=[customValue1]
165
+ * &[customParam2]=[customValue2]&page=[currentPage]'
166
+ */
167
+ additionalLinkQueryParams: {
168
+ type: Object ,
169
+ default : () => ({}),
156
170
},
157
171
},
158
172
data () {
159
173
return {
174
+ /**
175
+ * currently active page number
176
+ * @type {number}
177
+ */
160
178
active: this .current ,
179
+ /**
180
+ * number the displayed pages between '...' should start with
181
+ * (only relevant if not all numbers can be displayed)
182
+ * @type {?number}
183
+ */
161
184
start: null ,
185
+ /**
186
+ * number the displayed pages between the '...' should end with
187
+ * (only relevant if not all numbers can be displayed)
188
+ * @type {?number}
189
+ */
162
190
end: null ,
191
+ /**
192
+ * total numbers to be displayed before the '...' depending on the width of the
193
+ * pagination element (only relevant if not all numbers can be displayed)
194
+ * @type {number}
195
+ */
163
196
subsetNumber: 7 ,
197
+ /**
198
+ * max numbers to be displayed
199
+ * @type {number}
200
+ */
164
201
maxNumbers: 10 ,
165
202
};
166
203
},
167
204
computed: {
205
+ /**
206
+ * calculate the actual subset page numbers to be displayed
207
+ * @returns {number[]}
208
+ */
168
209
subset () {
169
210
// check if subset number would exceed total number of items and start
170
211
// array from total - subsetNumber
@@ -173,12 +214,21 @@ export default {
173
214
return Array .from ({ length: this .subsetNumber },
174
215
(v , k ) => k + subsetStart);
175
216
},
217
+ /**
218
+ * check if element should be displayed as a link element, otherwise make it a <span>
219
+ * @returns {String|Boolean|string}
220
+ */
176
221
numberElement () {
177
- return this .$route && this .useLinkElement ? ' router-link ' : ' span' ;
222
+ return this .useLinkElement ? this .useLinkElement : ' span' ;
178
223
},
179
224
},
180
225
watch: {
226
+ /**
227
+ * if active number changes inform parent
228
+ * @param {number} val - the new page number active
229
+ */
181
230
active (val ) {
231
+ // check if new number is different from prop value
182
232
if (this .current !== val) {
183
233
/**
184
234
* triggered on page select
@@ -188,23 +238,37 @@ export default {
188
238
*/
189
239
this .$emit (' set-page' , val);
190
240
}
241
+ // adjust the start and end value accordingly (if not all numbers can be displayed)
191
242
this .setStartEnd ();
192
243
},
244
+ /**
245
+ * check if parent prop changes
246
+ * @param {number} val - the page number provided by the parent component
247
+ */
193
248
current (val ) {
194
249
this .active = val;
195
250
},
196
251
},
197
252
mounted () {
253
+ // initialize the start and end variable in case not all numbers can be displayed
198
254
this .setStartEnd ();
255
+ // add an resize event listener
199
256
window .addEventListener (' resize' , this .setStartEnd );
200
257
},
201
258
destroyed () {
259
+ // remove the resize event listener
202
260
window .removeEventListener (' resize' , this .setStartEnd );
203
261
},
204
262
methods: {
263
+ /**
264
+ * depending on with of the parent element of the pagination calculate
265
+ * how many page numbers can be displayed
266
+ */
205
267
setStartEnd () {
268
+ // get parent width
206
269
const parentWidth = this .$parent .$el .clientWidth
207
270
|| this .$parent .clientWidth || window .innerWidth ;
271
+ // set the subset and the max number accordingly
208
272
if (parentWidth < 390 ) {
209
273
this .subsetNumber = 1 ;
210
274
this .maxNumbers = 5 ;
@@ -214,19 +278,31 @@ export default {
214
278
} else if (parentWidth < 710 ) {
215
279
this .subsetNumber = 5 ;
216
280
}
281
+ // calc start and end number from the subset number
217
282
this .start = this .active - this .subsetNumber / 2 > 0
218
283
? this .active - Math .floor (this .subsetNumber / 2 ) : 1 ;
219
284
this .end = this .active + this .subsetNumber / 2 < this .total
220
285
? this .active + Math .floor (this .subsetNumber / 2 ) : this .total ;
221
286
},
287
+ /**
288
+ * function to set a new page number active
289
+ * @param {number} page - the new page number
290
+ */
222
291
setActivePage (page ) {
292
+ // set internal variable to new page number
223
293
this .active = page;
224
294
},
225
- getLinkPath () {
226
- // check if router in project and set link path accordingly if yes
227
- // TODO: think about adding pagination query to route
228
- return this .$route && this .useLinkElement
229
- ? { path: this .$route .fullPath } : ' ' ;
295
+ /**
296
+ * get the correct link in case element is a link element
297
+ * @param {number} page - the page number of the element in question
298
+ * @returns {{path: string, query: Object}|string}
299
+ */
300
+ getLinkPath (page ) {
301
+ // check if router in project and link element is used and set link path accordingly if yes
302
+ if (!! this .useLinkElement && this .$route ) {
303
+ return ({ path: this .$route .fullPath , query: { ... this .additionalLinkQueryParams , page } });
304
+ }
305
+ return ' ' ;
230
306
},
231
307
},
232
308
};
0 commit comments