2023-09-30 21:31:48 +05:30
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const CURRENT_CONTINUATION = (new URL(document.location)).searchParams.get("continuation");
|
|
|
|
const CONT_CACHE_KEY = `continuation_cache_${encodeURIComponent(window.location.pathname)}`;
|
|
|
|
|
|
|
|
function get_data(){
|
|
|
|
return JSON.parse(sessionStorage.getItem(CONT_CACHE_KEY)) || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function save_data(){
|
|
|
|
const prev_data = get_data();
|
|
|
|
prev_data.push(CURRENT_CONTINUATION);
|
|
|
|
|
|
|
|
sessionStorage.setItem(CONT_CACHE_KEY, JSON.stringify(prev_data));
|
|
|
|
}
|
|
|
|
|
|
|
|
function button_press(){
|
|
|
|
let prev_data = get_data();
|
|
|
|
if (!prev_data.length) return null;
|
|
|
|
|
|
|
|
// Sanity check. Nowhere should the current continuation token exist in the cache
|
|
|
|
// but it can happen when using the browser's back feature. As such we'd need to travel
|
|
|
|
// back to the point where the current continuation token first appears in order to
|
|
|
|
// account for the rewind.
|
|
|
|
const conflict_at = prev_data.indexOf(CURRENT_CONTINUATION);
|
|
|
|
if (conflict_at != -1) {
|
|
|
|
prev_data.length = conflict_at;
|
|
|
|
}
|
|
|
|
|
|
|
|
const prev_ctoken = prev_data.pop();
|
|
|
|
|
|
|
|
// On the first page, the stored continuation token is null.
|
|
|
|
if (prev_ctoken === null) {
|
|
|
|
sessionStorage.removeItem(CONT_CACHE_KEY);
|
2023-10-26 14:00:12 +05:30
|
|
|
let url = window.location.href.split('?')[0];
|
|
|
|
let params = window.location.href.split('?')[1];
|
|
|
|
let url_params = new URLSearchParams(params);
|
|
|
|
url_params.delete('continuation');
|
|
|
|
window.location.href = `${url}?${url_params.toString()}`;
|
2023-09-30 21:31:48 +05:30
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionStorage.setItem(CONT_CACHE_KEY, JSON.stringify(prev_data));
|
2023-10-26 14:00:12 +05:30
|
|
|
let url = window.location.href.split('?')[0];
|
|
|
|
let params = window.location.href.split('?')[1];
|
|
|
|
let url_params = new URLSearchParams(params);
|
|
|
|
url_params.set("continuation", prev_ctoken);
|
2023-09-30 21:31:48 +05:30
|
|
|
|
2023-10-26 14:00:12 +05:30
|
|
|
window.location.href = `${url}?${url_params.toString()}`;
|
2023-09-30 21:31:48 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
addEventListener('DOMContentLoaded', function(){
|
2023-10-20 11:30:23 +05:30
|
|
|
const pagination_data = JSON.parse(document.getElementById('pagination-data').textContent);
|
2023-09-30 21:31:48 +05:30
|
|
|
const next_page_containers = document.getElementsByClassName("page-next-container");
|
|
|
|
|
|
|
|
for (let container of next_page_containers){
|
|
|
|
const next_page_button = container.getElementsByClassName("pure-button")
|
|
|
|
|
|
|
|
// exists?
|
|
|
|
if (next_page_button.length > 0){
|
|
|
|
next_page_button[0].addEventListener("click", save_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only add previous page buttons when not on the first page
|
|
|
|
if (CURRENT_CONTINUATION) {
|
|
|
|
const prev_page_containers = document.getElementsByClassName("page-prev-container")
|
|
|
|
|
|
|
|
for (let container of prev_page_containers) {
|
2023-11-08 15:18:32 +05:30
|
|
|
if (pagination_data.is_rtl) {
|
2023-09-30 21:31:48 +05:30
|
|
|
container.innerHTML = `<button class="pure-button pure-button-secondary">${pagination_data.prev_page} <i class="icon ion-ios-arrow-forward"></i></button>`
|
|
|
|
} else {
|
|
|
|
container.innerHTML = `<button class="pure-button pure-button-secondary"><i class="icon ion-ios-arrow-back"></i> ${pagination_data.prev_page}</button>`
|
|
|
|
}
|
|
|
|
container.getElementsByClassName("pure-button")[0].addEventListener("click", button_press);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|