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