File size: 12,691 Bytes
96dd062
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
---
import { Icon } from "astro-icon/components";
import I18nKey from "@/i18n/i18nKey";
import { i18n } from "@/i18n/translation";

interface Props {
	totalItems: number;
	itemsPerPage: number;
	currentPage: number;
	sectionId: string;
}

const { totalItems, itemsPerPage, currentPage, sectionId } = Astro.props;
const totalPages = Math.ceil(totalItems / itemsPerPage);

// 生成智能分页页码数组
function generatePageNumbers(current: number, total: number) {
	const delta = 2; // 当前页左右显示的页码数量
	const range: number[] = [];
	const rangeWithDots: (number | string)[] = [];

	// 如果总页数小于等于7,显示所有页码
	if (total <= 7) {
		for (let i = 1; i <= total; i++) {
			range.push(i);
		}
		return range;
	}

	// 计算显示范围
	const left = Math.max(2, current - delta);
	const right = Math.min(total - 1, current + delta);

	// 始终显示第一页
	rangeWithDots.push(1);

	// 如果左边界大于2,添加省略号
	if (left > 2) {
		rangeWithDots.push("...");
	}

	// 添加中间页码
	for (let i = left; i <= right; i++) {
		rangeWithDots.push(i);
	}

	// 如果右边界小于最后一页-1,添加省略号
	if (right < total - 1) {
		rangeWithDots.push("...");
	}

	// 始终显示最后一页(如果总页数大于1)
	if (total > 1) {
		rangeWithDots.push(total);
	}

	return rangeWithDots;
}

const pageNumbers = generatePageNumbers(currentPage, totalPages);
---

{totalPages > 1 && (
  <div class="responsive-pagination flex justify-center items-center mt-8" data-pagination-section={sectionId}>
    <!-- 移动端简化版分页 -->
    <div class="mobile-pagination items-center gap-3">
      <button
        type="button"
        class="btn-card overflow-hidden rounded-lg text-(--primary) w-11 h-11 disabled:opacity-50 disabled:cursor-not-allowed"
        data-page="prev"
        data-section={sectionId}
        disabled={currentPage === 1}
        aria-label={i18n(I18nKey.bangumiPrevPage)}
      >
        <Icon name="material-symbols:chevron-left-rounded" class="text-[1.75rem]" />
      </button>

      <!-- 移动端页码信息 -->
      <div class="bg-(--card-bg) flex items-center rounded-lg px-4 h-11 gap-1.5">
        <span class="mobile-current-page text-base font-bold text-(--primary)">{currentPage}</span>
        <span class="text-sm text-neutral-500 dark:text-neutral-500">/</span>
        <span class="mobile-total-pages text-base font-bold text-neutral-700 dark:text-neutral-300">{totalPages}</span>
      </div>

      <button
        type="button"
        class="btn-card overflow-hidden rounded-lg text-(--primary) w-11 h-11 disabled:opacity-50 disabled:cursor-not-allowed"
        data-page="next"
        data-section={sectionId}
        disabled={currentPage === totalPages}
        aria-label={i18n(I18nKey.bangumiNextPage)}
      >
        <Icon name="material-symbols:chevron-right-rounded" class="text-[1.75rem]" />
      </button>
    </div>

    <!-- 桌面端完整版分页 -->
    <div class="desktop-pagination items-center gap-3">
      <button
        type="button"
        class="btn-card overflow-hidden rounded-lg text-(--primary) w-11 h-11 disabled:opacity-50 disabled:cursor-not-allowed"
        data-page="prev"
        data-section={sectionId}
        disabled={currentPage === 1}
        aria-label={i18n(I18nKey.bangumiPrevPage)}
      >
        <Icon name="material-symbols:chevron-left-rounded" class="text-[1.75rem]" />
      </button>

      <div class="bg-(--card-bg) flex flex-row rounded-lg items-center text-neutral-700 dark:text-neutral-300 font-bold" data-page-numbers={sectionId}>
        {pageNumbers.map((pageItem) => (
          pageItem === '...' ? (
            <Icon name="material-symbols:more-horiz" class="mx-1" />
          ) : (
            <button
              type="button"
              class:list={[
                "rounded-lg overflow-hidden w-11 h-11 flex items-center justify-center font-bold",
                {
                  "bg-(--primary) text-white dark:text-black/70": pageItem === currentPage,
                  "btn-card active:scale-[0.85]": pageItem !== currentPage
                }
              ]}
              data-page={pageItem}
              data-section={sectionId}
              aria-label={`${pageItem}`}
              aria-current={pageItem === currentPage ? "page" : undefined}
            >
              {pageItem}
            </button>
          )
        ))}
      </div>

      <button
        type="button"
        class="btn-card overflow-hidden rounded-lg text-(--primary) w-11 h-11 disabled:opacity-50 disabled:cursor-not-allowed"
        data-page="next"
        data-section={sectionId}
        disabled={currentPage === totalPages}
        aria-label={i18n(I18nKey.bangumiNextPage)}
      >
        <Icon name="material-symbols:chevron-right-rounded" class="text-[1.75rem]" />
      </button>
    </div>
  </div>
)}

<script is:inline define:vars={{ itemsPerPage, sectionId }}>
  function initPagination() {
    let currentPage = 1;

    const pagination = document.querySelector(`[data-pagination-section="${sectionId}"]`);
    if (!pagination) return;

    // 更新移动端页码显示
    function updateMobileDisplay() {
      const mobileCurrentPage = pagination.querySelector('.mobile-current-page');
      if (mobileCurrentPage) {
        mobileCurrentPage.textContent = currentPage.toString();
      }
    }

    const pageButtons = pagination.querySelectorAll('[data-page]');

    pageButtons.forEach(button => {
      button.addEventListener('click', function() {
        const page = this.dataset.page;

        if (page === 'prev') {
          currentPage = Math.max(1, currentPage - 1);
        } else if (page === 'next') {
          const items = document.querySelectorAll(`[data-item-section="${sectionId}"]:not(.hidden)`);
          const totalPages = Math.ceil(items.length / itemsPerPage);
          currentPage = Math.min(totalPages, currentPage + 1);
        } else {
          currentPage = parseInt(page);
        }

        updatePage();
        updateMobileDisplay();
      });
    });

    // Listen for filter updates
    if (pagination) {
      pagination.addEventListener('updatePagination', function(_event) {
        currentPage = 1;
        updatePage();
        updateMobileDisplay();
      });
    }

    function updatePage() {
      const items = document.querySelectorAll(`[data-item-section="${sectionId}"]:not(.hidden)`);
      const totalPages = Math.ceil(items.length / itemsPerPage);

      // Hide all items first
      for (const item of items) {
        item.style.display = 'none';
      }

      // Show items for current page
      const startIndex = (currentPage - 1) * itemsPerPage;
      const endIndex = startIndex + itemsPerPage;
      for (let i = startIndex; i < endIndex && i < items.length; i++) {
        items[i].style.display = 'block';
      }

      // Update pagination buttons
      updatePaginationButtons(totalPages);
    }

    // 生成智能分页页码数组的JavaScript版本
    function generatePageNumbers(current, total) {
      const delta = 2; // 当前页左右显示的页码数量
      const rangeWithDots = [];

      // 如果总页数小于等于7,显示所有页码
      if (total <= 7) {
        for (let i = 1; i <= total; i++) {
          rangeWithDots.push(i);
        }
        return rangeWithDots;
      }

      // 计算显示范围
      const left = Math.max(2, current - delta);
      const right = Math.min(total - 1, current + delta);

      // 始终显示第一页
      rangeWithDots.push(1);

      // 如果左边界大于2,添加省略号
      if (left > 2) {
        rangeWithDots.push('...');
      }

      // 添加中间页码
      for (let i = left; i <= right; i++) {
        rangeWithDots.push(i);
      }

      // 如果右边界小于最后一页-1,添加省略号
      if (right < total - 1) {
        rangeWithDots.push('...');
      }

      // 始终显示最后一页(如果总页数大于1)
      if (total > 1) {
        rangeWithDots.push(total);
      }

      return rangeWithDots;
    }

    function updatePaginationButtons(totalPages) {
      // 更新所有的上一页和下一页按钮(移动端和桌面端)
      const prevButtons = pagination.querySelectorAll('[data-page="prev"]');
      const nextButtons = pagination.querySelectorAll('[data-page="next"]');
      const pageNumbersContainer = pagination.querySelector(`[data-page-numbers="${sectionId}"]`);

      prevButtons.forEach(btn => {
        btn.disabled = currentPage === 1;
      });

      nextButtons.forEach(btn => {
        btn.disabled = currentPage === totalPages;
      });

      if (pageNumbersContainer) {
        pageNumbersContainer.innerHTML = '';
        const pageNumbers = generatePageNumbers(currentPage, totalPages);

        pageNumbers.forEach(pageItem => {
          if (pageItem === '...') {
            // 创建省略号图标
            const iconContainer = document.createElement('span');
            iconContainer.innerHTML = '<svg class="mx-1" width="24" height="24" viewBox="0 0 24 24"><path fill="currentColor" d="M6 10c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm12 0c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm-6 0c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z"/></svg>';
            pageNumbersContainer.appendChild(iconContainer.firstChild);
          } else {
            const button = document.createElement('button');
            button.className = pageItem === currentPage
              ? 'rounded-lg overflow-hidden w-11 h-11 flex items-center justify-center font-bold bg-(--primary) text-white dark:text-black/70'
              : 'rounded-lg overflow-hidden w-11 h-11 flex items-center justify-center font-bold btn-card active:scale-[0.85]';
            button.dataset.page = pageItem.toString();
            button.dataset.section = sectionId;
            button.textContent = pageItem.toString();
            button.addEventListener('click', function() {
              currentPage = pageItem;
              updatePage();
              updateMobileDisplay();
            });
            pageNumbersContainer.appendChild(button);
          }
        });
      }
    }

    // Initial page setup
    updatePage();
  }

  // Initialize when DOM is ready
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initPagination);
  } else {
    initPagination();
  }
</script>

<style>
  .responsive-pagination {
    /* 确保在所有设备上都能正确显示 */
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* 移动端样式 (< 768px) */
  .mobile-pagination {
    display: flex;
    padding: 0 1rem;
  }

  .desktop-pagination {
    display: none;
  }

  /* 桌面端样式 (>= 768px) */
  @media (min-width: 768px) {
    .mobile-pagination {
      display: none;
    }

    .desktop-pagination {
      display: flex;
    }
  }

  /* 小屏手机优化 */
  @media (max-width: 640px) {
    .mobile-pagination {
      padding: 0 0.5rem;
    }
    .mobile-pagination button {
      padding: 0.25rem;
    }
  }

  /* 超小屏优化 */
  @media (max-width: 480px) {
    .mobile-pagination {
      padding: 0 0.25rem;
    }
    .mobile-pagination .space-x-1 > * + * {
      margin-left: 0.125rem;
    }
  }

  /* 平滑过渡效果 */
  .responsive-pagination button {
    transition: all 0.2s ease-in-out;
  }

  /* 高对比度模式支持 */
  @media (prefers-contrast: high) {
    .responsive-pagination button {
      border: 1px solid currentColor;
    }
  }

  /* 减少动画偏好支持 */
  @media (prefers-reduced-motion: reduce) {
    .responsive-pagination button {
      transition: none;
    }
  }

  /* 触摸设备优化 */
  @media (hover: none) and (pointer: coarse) {
    .responsive-pagination button {
      min-height: 44px; /* iOS建议的最小触摸目标 */
      min-width: 44px;
    }

    /* 移动端触摸优化 */
    .mobile-pagination button {
      min-height: 40px;
      min-width: 40px;
    }
  }

  /* 横屏手机优化 */
  @media (max-width: 768px) and (orientation: landscape) {
    .mobile-pagination {
      padding: 0 0.5rem;
    }

    .mobile-pagination button {
      padding: 0.25rem;
    }
  }
</style>