|
|
| 193行目: |
193行目: |
| } | | } |
|
| |
|
| meta.content = desc; | | meta.content = desc;//# sourceURL=foobar.js |
| // 現在のURL取得
| |
| let url = window.location.href;
| |
| // ページネーション
| |
| const fullurl = new URL(location.href);
| |
| const page = parseInt(fullurl.searchParams.get('page') || '1', 10);
| |
| | |
| // 最終ページ制御
| |
| const countMeta = document.getElementById('count');
| |
| if (!countMeta) return;
| |
| | |
| const total = parseInt(countMeta.dataset.total, 10);
| |
| const limit = parseInt(countMeta.dataset.limit, 10);
| |
| | |
| if (isNaN(total) || isNaN(limit) || limit <= 0) return;
| |
| | |
| const lastPage = Math.ceil(total / limit);
| |
| | |
| if (lastPage <= 1) {
| |
| const pagination = document.querySelector('.pagination');
| |
| if (pagination) pagination.style.display = 'none';
| |
| return;
| |
| }
| |
| | |
| if (page > lastPage) {
| |
| fullurl.searchParams.set('page', lastPage);
| |
| location.replace(fullurl.toString());
| |
| }
| |
| | |
| if (page < 1) {
| |
| fullurl.searchParams.set('page', 1);
| |
| location.replace(fullurl.toString());
| |
| }
| |
| | |
| function makeLink(targetPage) {
| |
| fullurl.searchParams.set('page', targetPage);
| |
| return fullurl.toString();
| |
| }
| |
| | |
| // 各ボタン
| |
| const first = document.querySelector('[data-role="first-page"]');
| |
| const prev = document.querySelector('[data-role="prev-page"]');
| |
| const next = document.querySelector('[data-role="next-page"]');
| |
| const last = document.querySelector('[data-role="last-page"]');
| |
| | |
| if (page <= 1) {
| |
| first.classList.add('disabled');
| |
| prev.classList.add('disabled');
| |
| } else {
| |
| first.href = makeLink(1);
| |
| prev.href = makeLink(page - 1);
| |
| }
| |
| | |
| if (page >= lastPage) {
| |
| next.classList.add('disabled');
| |
| last.classList.add('disabled');
| |
| } else {
| |
| next.href = makeLink(page + 1);
| |
| last.href = makeLink(lastPage);
| |
| }
| |
| | |
| // 数字ボタン
| |
| const pageNumbers = document.querySelector('[data-role="page-numbers"]');
| |
| if (!pageNumbers) return;
| |
| | |
| const start = Math.max(1, page - 2);
| |
| const end = Math.min(lastPage, page + 2);
| |
| | |
| for (let i = start; i <= end; i++) {
| |
| const link = document.createElement('a');
| |
| link.textContent = i;
| |
| | |
| const newUrl = new URL(url);
| |
| newUrl.searchParams.set('page', i);
| |
| link.href = newUrl.toString();
| |
| | |
| if (i === page) {
| |
| link.classList.add('current');
| |
| }
| |
| | |
| pageNumbers.appendChild(link);
| |
| }
| |
| });//# sourceURL=foobar.js
| |
| </script> | | </script> |
| </html> | | </html> |