Fix browse artists pagination normalization
This commit is contained in:
parent
6e438d049d
commit
9fb5abf519
1 changed files with 34 additions and 9 deletions
43
script.js
43
script.js
|
@ -1114,13 +1114,28 @@
|
||||||
|
|
||||||
const listState = createAsyncListState({ table, statusEl: $status, pagination });
|
const listState = createAsyncListState({ table, statusEl: $status, pagination });
|
||||||
|
|
||||||
|
const defaultPageSize = Math.max(1, Number(pagination.pageSize) || 25);
|
||||||
const state = {
|
const state = {
|
||||||
prefix: initialPrefix,
|
prefix: initialPrefix,
|
||||||
filter: initialFilter,
|
filter: initialFilter,
|
||||||
page: 1,
|
page: 1,
|
||||||
pageSize: pagination.pageSize,
|
pageSize: defaultPageSize,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function normalizePaginationState() {
|
||||||
|
let pageSize = Number(state.pageSize);
|
||||||
|
if (!Number.isFinite(pageSize) || pageSize <= 0) pageSize = defaultPageSize;
|
||||||
|
else pageSize = Math.max(1, Math.floor(pageSize));
|
||||||
|
if (state.pageSize !== pageSize) state.pageSize = pageSize;
|
||||||
|
|
||||||
|
let page = Number(state.page);
|
||||||
|
if (!Number.isFinite(page) || page <= 0) page = 1;
|
||||||
|
else page = Math.max(1, Math.floor(page));
|
||||||
|
if (state.page !== page) state.page = page;
|
||||||
|
|
||||||
|
return { page, pageSize };
|
||||||
|
}
|
||||||
|
|
||||||
function escapeLike(str) {
|
function escapeLike(str) {
|
||||||
return String(str).replace(/[\\%_]/g, (m) => `\\${m}`);
|
return String(str).replace(/[\\%_]/g, (m) => `\\${m}`);
|
||||||
}
|
}
|
||||||
|
@ -1195,7 +1210,8 @@
|
||||||
const useUnfilteredQuery = !typedFilter && !state.prefix;
|
const useUnfilteredQuery = !typedFilter && !state.prefix;
|
||||||
listState.showLoading('Loading…');
|
listState.showLoading('Loading…');
|
||||||
|
|
||||||
const offset = (state.page - 1) * state.pageSize;
|
let { page, pageSize } = normalizePaginationState();
|
||||||
|
let offset = (page - 1) * pageSize;
|
||||||
let total = 0;
|
let total = 0;
|
||||||
const rows = [];
|
const rows = [];
|
||||||
let usedFts = false;
|
let usedFts = false;
|
||||||
|
@ -1207,9 +1223,13 @@
|
||||||
ftsCountStmt.free();
|
ftsCountStmt.free();
|
||||||
|
|
||||||
if (total > 0) {
|
if (total > 0) {
|
||||||
if (offset >= total) state.page = Math.max(1, Math.ceil(total / state.pageSize));
|
if (offset >= total) {
|
||||||
|
state.page = Math.max(1, Math.ceil(total / pageSize));
|
||||||
|
({ page, pageSize } = normalizePaginationState());
|
||||||
|
offset = (page - 1) * pageSize;
|
||||||
|
}
|
||||||
const ftsRowsStmt = db.prepare(applyFtsMatch(ftsRowsSql, ftsMatch));
|
const ftsRowsStmt = db.prepare(applyFtsMatch(ftsRowsSql, ftsMatch));
|
||||||
ftsRowsStmt.bind([state.pageSize, (state.page - 1) * state.pageSize]);
|
ftsRowsStmt.bind([pageSize, offset]);
|
||||||
while (ftsRowsStmt.step()) rows.push(ftsRowsStmt.getAsObject());
|
while (ftsRowsStmt.step()) rows.push(ftsRowsStmt.getAsObject());
|
||||||
ftsRowsStmt.free();
|
ftsRowsStmt.free();
|
||||||
usedFts = true;
|
usedFts = true;
|
||||||
|
@ -1228,11 +1248,15 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (offset >= total) state.page = Math.max(1, Math.ceil(total / state.pageSize));
|
if (offset >= total) {
|
||||||
|
state.page = Math.max(1, Math.ceil(total / pageSize));
|
||||||
|
({ page, pageSize } = normalizePaginationState());
|
||||||
|
offset = (page - 1) * pageSize;
|
||||||
|
}
|
||||||
|
|
||||||
const rowsStmt = db.prepare(useUnfilteredQuery ? baseRowsSql : rowsSql);
|
const rowsStmt = db.prepare(useUnfilteredQuery ? baseRowsSql : rowsSql);
|
||||||
if (useUnfilteredQuery) rowsStmt.bind([state.pageSize, (state.page - 1) * state.pageSize]);
|
if (useUnfilteredQuery) rowsStmt.bind([pageSize, offset]);
|
||||||
else rowsStmt.bind([likeTerm, state.pageSize, (state.page - 1) * state.pageSize]);
|
else rowsStmt.bind([likeTerm, pageSize, offset]);
|
||||||
while (rowsStmt.step()) rows.push(rowsStmt.getAsObject());
|
while (rowsStmt.step()) rows.push(rowsStmt.getAsObject());
|
||||||
rowsStmt.free();
|
rowsStmt.free();
|
||||||
}
|
}
|
||||||
|
@ -1248,11 +1272,12 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
({ page, pageSize } = normalizePaginationState());
|
||||||
listState.showRows({
|
listState.showRows({
|
||||||
rows,
|
rows,
|
||||||
total,
|
total,
|
||||||
page: state.page,
|
page,
|
||||||
pageSize: state.pageSize,
|
pageSize,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue