Skip to content

Commit 42a516a

Browse files
committed
Update sample 8.
1 parent 10f99a6 commit 42a516a

File tree

2 files changed

+115
-24
lines changed

2 files changed

+115
-24
lines changed

tests/samples/sample08.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ <h1>Sample 8 use custom design.</h1>
3434
</div>
3535

3636
<footer>
37+
<a href="https://github.com/Rundiz/rd-pagination.js" target="rdpaginationjs">rd-pagination.js on GitHub</a>
38+
|
3739
Dummy content thanks to <a href="https://www.mockaroo.com/" target="mockaroo">Mockaroo.com</a>.
3840
</footer>
3941
</div><!-- .rdpjs-page-column -->

tests/samples/sample08.js

Lines changed: 113 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ class Sample08 {
1414
#paginationObj;
1515

1616

17+
/**
18+
* @type {number} Current start offset for main contents.
19+
*/
20+
#currentOffset = 0;
21+
22+
1723
/**
1824
* @type {object} Raw data of custom pagination.
1925
*/
@@ -71,7 +77,7 @@ class Sample08 {
7177

7278

7379
/**
74-
* Diaplay pagination for main contents.
80+
* Diaplay custom pagination for main contents.
7581
*
7682
* @returns {void} Return void if not found a placeholder (wrapper) to display custom pagination.
7783
*/
@@ -149,6 +155,38 @@ class Sample08 {
149155
}// #content_displayPaginationPlaceholders
150156

151157

158+
/**
159+
* Pagination tasks.
160+
*
161+
* Display custom pagination & display placeholders in pagination such as start item, end item, etc.
162+
*
163+
* @param {HTMLElement|null} thisTarget The selected element that is active on event such as clicked, changed. Use `null` for not on event.
164+
* @param {number} pageValue Pagination value (or offset) on currently active page.
165+
*/
166+
#content_paginationTasks(thisTarget, pageValue) {
167+
this.#content_displayCustomPagination();
168+
this.#content_displayPaginationPlaceholders(pageValue);
169+
170+
if (thisTarget instanceof HTMLElement) {
171+
let href = '';
172+
if (thisTarget.href) {
173+
href = thisTarget.href;
174+
} else if (thisTarget.options) {
175+
const selected = thisTarget.options[thisTarget.options.selectedIndex];
176+
href = '?start=' + selected.dataset.rdPaginationPageValue;
177+
}
178+
179+
if ('' !== href) {
180+
const data = {
181+
'pageValue': pageValue,
182+
'prevURL': window.location.href,
183+
};
184+
window.history.pushState(data, '', href);
185+
}
186+
}
187+
}// #content_paginationTasks
188+
189+
152190
/**
153191
* Listen change on custom pagination select box.
154192
*/
@@ -163,13 +201,13 @@ class Sample08 {
163201

164202
const selected = thisTarget.options[thisTarget.options.selectedIndex];
165203
const pageValue = parseInt(selected.dataset.rdPaginationPageValue);
204+
this.#currentOffset = pageValue;
166205
// update main contents
167-
this.content_display(pageValue);
206+
this.content_display();
168207
// update option to pagination class and re-display custom pagination again.
169208
this.#paginationObj.updateOptions({page_number_value: pageValue});
170209
this.#customPaginationRawData = this.#paginationObj.getPaginationData();
171-
this.#content_displayCustomPagination();
172-
this.#content_displayPaginationPlaceholders(pageValue);
210+
this.#content_paginationTasks(thisTarget, pageValue);
173211
// re-create primary pagination links.
174212
this.#paginationObj.reCreateLinks(this.#customPaginationRawData);
175213
});
@@ -193,31 +231,70 @@ class Sample08 {
193231
}
194232

195233
const pageValue = parseInt(thisTarget.dataset.rdPaginationPageValue);
196-
// trigger click the same button on primary pagination to update main contents (based on page clicked).
197-
const targetLink = document.querySelector(this.#mainPaginationSelector + ' [data-rd-pagination-page-value="' + pageValue + '"]');
198-
if (targetLink) {
199-
targetLink.click();
200-
}
234+
this.#currentOffset = pageValue;
235+
// update main contents
236+
this.content_display();
201237
// update option to pagination class and re-display custom pagination again.
202238
this.#paginationObj.updateOptions({page_number_value: pageValue});
203239
this.#customPaginationRawData = this.#paginationObj.getPaginationData();
204-
this.#content_displayCustomPagination();
205-
this.#content_displayPaginationPlaceholders(pageValue);
240+
this.#content_paginationTasks(thisTarget, pageValue);
241+
// re-create primary pagination links.
242+
this.#paginationObj.reCreateLinks(this.#customPaginationRawData);
206243
});
207244
}// #listenCustomPaginationClickEvent
208245

209246

247+
/**
248+
* Listen pop state and display contents based on URL.
249+
*/
250+
#listenPopState() {
251+
window.addEventListener('popstate', (event) => {
252+
if (typeof(event.state?.pageValue) === 'number') {
253+
const pageValue = parseInt(event.state.pageValue);
254+
this.#currentOffset = pageValue;
255+
// update main contents
256+
this.content_display();
257+
// update option to pagination class and re-display custom pagination again.
258+
this.#paginationObj.updateOptions({page_number_value: pageValue});
259+
this.#customPaginationRawData = this.#paginationObj.getPaginationData();
260+
this.#content_paginationTasks(null, pageValue);
261+
// re-create primary pagination links.
262+
this.#paginationObj.reCreateLinks(this.#customPaginationRawData);
263+
}
264+
});
265+
}// #listenPopState
266+
267+
268+
/**
269+
* Set initial history state.
270+
*/
271+
#setInitialHistoryState() {
272+
this.content_prepareOffset();
273+
const data = {
274+
'pageValue': this.#currentOffset,
275+
'pageURL': window.location.href,
276+
};
277+
window.history.replaceState(data, '', window.location.href);
278+
}// #setInitialHistoryState
279+
280+
281+
/**
282+
* Sample 8 JS class constructor.
283+
*/
284+
constructor() {
285+
this.#listenPopState();
286+
this.#setInitialHistoryState();
287+
}// constructor
288+
289+
210290
/**
211291
* Display main contents.
212292
*
213293
* @async
214-
* @param {number} start Start offset number.
215294
* @returns
216295
*/
217-
async content_display(start = 0) {
218-
if (typeof(start) !== 'number') {
219-
start = 0;
220-
}
296+
async content_display() {
297+
const start = this.#currentOffset;
221298

222299
const data = this.#dummyData.slice(start, (parseFloat(start) + this.#itemsPerPage));
223300

@@ -264,6 +341,18 @@ class Sample08 {
264341
}// content_prepareData
265342

266343

344+
/**
345+
* Prepare current offset.
346+
*/
347+
content_prepareOffset() {
348+
const params = new URL(document.location.toString()).searchParams;
349+
const start = params.get('start');
350+
if (start) {
351+
this.#currentOffset = parseInt(start);
352+
}
353+
}// content_prepareOffset
354+
355+
267356
/**
268357
* Setup main contents.
269358
*
@@ -276,13 +365,12 @@ class Sample08 {
276365
content_setup(thisTarget, event, options) {
277366
event.preventDefault();
278367
const pageValue = parseInt(thisTarget.dataset.rdPaginationPageValue);
279-
sample08Obj.content_display(pageValue);
368+
sample08Obj.#currentOffset = pageValue;
369+
sample08Obj.content_display();
280370

281371
// get raw data to custom pagination and display them again.
282372
sample08Obj.#customPaginationRawData = sample08Obj.#paginationObj.getPaginationData();
283-
sample08Obj.#content_displayCustomPagination();
284-
285-
sample08Obj.#content_displayPaginationPlaceholders(pageValue);
373+
sample08Obj.#content_paginationTasks(thisTarget, pageValue);
286374
}// content_setup
287375

288376

@@ -292,7 +380,7 @@ class Sample08 {
292380
content_setupPagination() {
293381
this.#paginationObj = new RdPagination(this.#mainPaginationSelector, {
294382
base_url: '?start=%PAGENUMBER%',
295-
page_number_value: 0,
383+
page_number_value: this.#currentOffset,
296384
total_records: this.#dummyData.length,
297385
items_per_page: this.#itemsPerPage,
298386

@@ -361,9 +449,7 @@ class Sample08 {
361449
this.#customPaginationRawData = this.#paginationObj.getPaginationData();
362450
this.#listenCustomPaginationClickEvent();
363451
this.#listenCustomPaginationChangeEvent();
364-
this.#content_displayCustomPagination();
365-
366-
this.#content_displayPaginationPlaceholders();
452+
this.#content_paginationTasks(null, this.#currentOffset);
367453
}// content_setupPagination
368454

369455

@@ -459,6 +545,9 @@ class Sample08 {
459545
}// Sample08
460546

461547

548+
/**
549+
* @type {Sample08} The class Sample08
550+
*/
462551
let sample08Obj;
463552
// ====================================
464553
document.addEventListener('DOMContentLoaded', (event) => {

0 commit comments

Comments
 (0)