pagination.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. const CURRENT_CONTINUATION = (new URL(document.location)).searchParams.get("continuation");
  3. const CONT_CACHE_KEY = `continuation_cache_${encodeURIComponent(window.location.pathname)}`;
  4. function get_data(){
  5. return JSON.parse(sessionStorage.getItem(CONT_CACHE_KEY)) || [];
  6. }
  7. function save_data(){
  8. const prev_data = get_data();
  9. prev_data.push(CURRENT_CONTINUATION);
  10. sessionStorage.setItem(CONT_CACHE_KEY, JSON.stringify(prev_data));
  11. }
  12. function button_press(){
  13. let prev_data = get_data();
  14. if (!prev_data.length) return null;
  15. // Sanity check. Nowhere should the current continuation token exist in the cache
  16. // but it can happen when using the browser's back feature. As such we'd need to travel
  17. // back to the point where the current continuation token first appears in order to
  18. // account for the rewind.
  19. const conflict_at = prev_data.indexOf(CURRENT_CONTINUATION);
  20. if (conflict_at != -1) {
  21. prev_data.length = conflict_at;
  22. }
  23. const prev_ctoken = prev_data.pop();
  24. // On the first page, the stored continuation token is null.
  25. if (prev_ctoken === null) {
  26. sessionStorage.removeItem(CONT_CACHE_KEY);
  27. let url = set_continuation();
  28. window.location.href = url;
  29. return;
  30. }
  31. sessionStorage.setItem(CONT_CACHE_KEY, JSON.stringify(prev_data));
  32. let url = set_continuation(prev_ctoken);
  33. window.location.href = url;
  34. };
  35. // Method to set the current page's continuation token
  36. // Removes the continuation parameter when a continuation token is not given
  37. function set_continuation(prev_ctoken = null){
  38. let url = window.location.href.split('?')[0];
  39. let params = window.location.href.split('?')[1];
  40. let url_params = new URLSearchParams(params);
  41. if (prev_ctoken) {
  42. url_params.set("continuation", prev_ctoken);
  43. } else {
  44. url_params.delete('continuation');
  45. };
  46. if(Array.from(url_params).length > 0){
  47. return `${url}?${url_params.toString()}`;
  48. } else {
  49. return url;
  50. }
  51. }
  52. addEventListener('DOMContentLoaded', function(){
  53. const pagination_data = JSON.parse(document.getElementById('pagination-data').textContent);
  54. const next_page_containers = document.getElementsByClassName("page-next-container");
  55. for (let container of next_page_containers){
  56. const next_page_button = container.getElementsByClassName("pure-button")
  57. // exists?
  58. if (next_page_button.length > 0){
  59. next_page_button[0].addEventListener("click", save_data);
  60. }
  61. }
  62. // Only add previous page buttons when not on the first page
  63. if (CURRENT_CONTINUATION) {
  64. const prev_page_containers = document.getElementsByClassName("page-prev-container")
  65. for (let container of prev_page_containers) {
  66. if (pagination_data.is_rtl) {
  67. container.innerHTML = `<button class="pure-button pure-button-secondary">${pagination_data.prev_page}&nbsp;&nbsp;<i class="icon ion-ios-arrow-forward"></i></button>`
  68. } else {
  69. container.innerHTML = `<button class="pure-button pure-button-secondary"><i class="icon ion-ios-arrow-back"></i>&nbsp;&nbsp;${pagination_data.prev_page}</button>`
  70. }
  71. container.getElementsByClassName("pure-button")[0].addEventListener("click", button_press);
  72. }
  73. }
  74. });