blob: 03c5258b684cbec56e3090d6dde2d1791bf3b0a2 [file] [log] [blame]
Akron644ad9f2021-07-26 16:12:59 +02001/**
2 * The pagination panel
3 *
4 * @author Nils Diewald
5 */
6"use strict";
7
8define([
9 'panel',
10 'buttongroup',
11 'pageInfo'
12], function (panelClass, buttonGroupClass, pageInfoClass) {
13
14 const d = document;
15
16 // Localization values
17 const loc = KorAP.Locale;
18 loc.RANDOM_PAGE = loc.RANDOM_PAGE || 'Random page';
Akron644ad9f2021-07-26 16:12:59 +020019
20 return {
Akron386f1222022-12-21 16:26:11 +010021 type : 'pagination',
22
Akron644ad9f2021-07-26 16:12:59 +020023 create : function () {
24 return Object.create(panelClass)._init(['pagination']).upgradeTo(this)._init();
25 },
26
27 // Initialize panel
28 _init : function () {
29
30 // Override existing button group
31 // This allows actions.add() ... to work for list items in server.js
32 let pagEl = document.getElementById("pagination");
33 if (pagEl === null) {
34 return null;
35 };
36
37 // Do not show if no pages exist
38 if (pagEl.getAttribute('data-total') === '0') {
39 return null;
40 };
41
42 this._actions.panel = undefined;
43 this._actions.clear();
44
45 const bg = buttonGroupClass.adopt(pagEl);
46 bg._omOutside = true;
47 bg._omLeft = true;
48
49 bg.anchor(pagEl.lastElementChild);
50
51 let button = bg.addList("More", {'cls':['button-group-list','button-icon']});
52 this._actions = button.list;
53 this._bg = bg;
54
55 button.setAttribute('data-icon',"\uf0c9");
56
57 // Warning: This is circular
58 this._actions.panel = this;
59
60 this.prepend = true;
61
62 return this;
63 },
64
65 /**
66 * The buttongroup holding the pagination panel.
67 * This differs from action, as action contains the list.
68 */
69 buttonGroup : function () {
70 return this._bg;
71 },
72
73
74 /**
75 * Add random paginator to list
76 */
77 addRandomPage : function () {
78 const pi = pageInfoClass.create();
Akron0ac1b5f2024-09-25 14:03:39 +020079
80 if (pi.total() < 2)
81 return false;
82
Akron644ad9f2021-07-26 16:12:59 +020083 const button = this.actions().add(
84 loc.RANDOM_PAGE,
85 {},
86 function () {
Akron0ac1b5f2024-09-25 14:03:39 +020087 if (pi.total() > 1) {
Akron644ad9f2021-07-26 16:12:59 +020088 const sp = new URLSearchParams(window.location.search);
89 sp.set("p", Math.floor(Math.random() * pi.total()) + 1);
90 window.location.search = sp.toString();
91 };
92 }
Akron0ac1b5f2024-09-25 14:03:39 +020093 );
94
95 return true;
Akron644ad9f2021-07-26 16:12:59 +020096 }
97 }
98});