Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 1 | /** |
| 2 | * The pagination panel |
| 3 | * |
| 4 | * @author Nils Diewald |
| 5 | */ |
| 6 | "use strict"; |
| 7 | |
| 8 | define([ |
| 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'; |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 19 | |
| 20 | return { |
Akron | 386f122 | 2022-12-21 16:26:11 +0100 | [diff] [blame] | 21 | type : 'pagination', |
| 22 | |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 23 | 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(); |
Akron | 0ac1b5f | 2024-09-25 14:03:39 +0200 | [diff] [blame^] | 79 | |
| 80 | if (pi.total() < 2) |
| 81 | return false; |
| 82 | |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 83 | const button = this.actions().add( |
| 84 | loc.RANDOM_PAGE, |
| 85 | {}, |
| 86 | function () { |
Akron | 0ac1b5f | 2024-09-25 14:03:39 +0200 | [diff] [blame^] | 87 | if (pi.total() > 1) { |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 88 | 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 | } |
Akron | 0ac1b5f | 2024-09-25 14:03:39 +0200 | [diff] [blame^] | 93 | ); |
| 94 | |
| 95 | return true; |
Akron | 644ad9f | 2021-07-26 16:12:59 +0200 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | }); |