blob: dd3331c367758d8189badec45c694da9e4faa4ab [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';
19
20
21 return {
Akron386f1222022-12-21 16:26:11 +010022 type : 'pagination',
23
Akron644ad9f2021-07-26 16:12:59 +020024 create : function () {
25 return Object.create(panelClass)._init(['pagination']).upgradeTo(this)._init();
26 },
27
28 // Initialize panel
29 _init : function () {
30
31 // Override existing button group
32 // This allows actions.add() ... to work for list items in server.js
33 let pagEl = document.getElementById("pagination");
34 if (pagEl === null) {
35 return null;
36 };
37
38 // Do not show if no pages exist
39 if (pagEl.getAttribute('data-total') === '0') {
40 return null;
41 };
42
43 this._actions.panel = undefined;
44 this._actions.clear();
45
46 const bg = buttonGroupClass.adopt(pagEl);
47 bg._omOutside = true;
48 bg._omLeft = true;
49
50 bg.anchor(pagEl.lastElementChild);
51
52 let button = bg.addList("More", {'cls':['button-group-list','button-icon']});
53 this._actions = button.list;
54 this._bg = bg;
55
56 button.setAttribute('data-icon',"\uf0c9");
57
58 // Warning: This is circular
59 this._actions.panel = this;
60
61 this.prepend = true;
62
63 return this;
64 },
65
66 /**
67 * The buttongroup holding the pagination panel.
68 * This differs from action, as action contains the list.
69 */
70 buttonGroup : function () {
71 return this._bg;
72 },
73
74
75 /**
76 * Add random paginator to list
77 */
78 addRandomPage : function () {
79 const pi = pageInfoClass.create();
80
81 const button = this.actions().add(
82 loc.RANDOM_PAGE,
83 {},
84 function () {
85 if (pi.total() > 0) {
86 const sp = new URLSearchParams(window.location.search);
87 sp.set("p", Math.floor(Math.random() * pi.total()) + 1);
88 window.location.search = sp.toString();
89 };
90 }
91 )
92 }
93 }
94});