mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-27 10:32:58 +02:00
Server: Fixed pagination
This commit is contained in:
parent
f48697572d
commit
81aba8b8b0
@ -35,11 +35,12 @@ export default class FileController extends BaseController {
|
||||
const parent: File = await fileModel.load(parentTemp.id);
|
||||
const paginatedFiles = await fileModel.childrens(parent.id, pagination);
|
||||
const pageCount = Math.ceil((await fileModel.childrenCount(parent.id)) / pagination.limit);
|
||||
|
||||
const parentBaseUrl = await fileModel.fileUrl(parent.id);
|
||||
const paginationLinks = createPaginationLinks(pagination.page, pageCount, setQueryParameters(parentBaseUrl, { ...baseUrlQuery, 'page': 'PAGE_NUMBER' }));
|
||||
|
||||
async function fileToViewItem(file: File): Promise<any> {
|
||||
const filePath = await fileModel.itemFullPath(file);
|
||||
async function fileToViewItem(file: File, fileFullPaths: Record<string, string>): Promise<any> {
|
||||
const filePath = fileFullPaths[file.id];
|
||||
|
||||
let url = `${baseUrl()}/files/${filePath}`;
|
||||
if (!file.is_directory) {
|
||||
@ -60,17 +61,19 @@ export default class FileController extends BaseController {
|
||||
|
||||
const files: any[] = [];
|
||||
|
||||
const fileFullPaths = await fileModel.itemFullPaths(paginatedFiles.items);
|
||||
|
||||
if (parent.id !== root.id) {
|
||||
const p = await fileModel.load(parent.parent_id);
|
||||
files.push({
|
||||
...await fileToViewItem(p),
|
||||
...await fileToViewItem(p, await fileModel.itemFullPaths([p])),
|
||||
icon: 'fas fa-arrow-left',
|
||||
name: '..',
|
||||
});
|
||||
}
|
||||
|
||||
for (const file of paginatedFiles.items) {
|
||||
files.push(await fileToViewItem(file));
|
||||
files.push(await fileToViewItem(file, fileFullPaths));
|
||||
}
|
||||
|
||||
const view: View = defaultView('files', owner);
|
||||
@ -80,6 +83,8 @@ export default class FileController extends BaseController {
|
||||
view.content.parentId = parent.id;
|
||||
view.cssFiles = ['index/files'];
|
||||
view.partials.push('pagination');
|
||||
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
|
@ -62,6 +62,36 @@ export default class FileModel extends BaseModel {
|
||||
return null; // Not a special dir
|
||||
}
|
||||
|
||||
public async itemFullPaths(items: File[]): Promise<Record<string, string>> {
|
||||
const output: Record<string, string> = {};
|
||||
|
||||
const itemCache: Record<string, File> = {};
|
||||
|
||||
await this.withTransaction(async () => {
|
||||
for (const item of items) {
|
||||
const segments: string[] = [];
|
||||
let current: File = item;
|
||||
|
||||
while (current) {
|
||||
if (current.is_root) break;
|
||||
segments.splice(0, 0, current.name);
|
||||
|
||||
if (current.parent_id) {
|
||||
const id = current.parent_id;
|
||||
current = itemCache[id] ? itemCache[id] : await this.load(id);
|
||||
itemCache[id] = current;
|
||||
} else {
|
||||
current = null;
|
||||
}
|
||||
}
|
||||
|
||||
output[item.id] = segments.length ? (`root:/${segments.join('/')}:`) : 'root';
|
||||
}
|
||||
});
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
public async itemFullPath(item: File): Promise<string> {
|
||||
const segments: string[] = [];
|
||||
while (item) {
|
||||
@ -348,8 +378,8 @@ export default class FileModel extends BaseModel {
|
||||
public async childrenCount(id: string): Promise<number> {
|
||||
const parent = await this.load(id);
|
||||
await this.checkCanReadPermissions(parent);
|
||||
const r = await this.db(this.tableName).where('parent_id', id).count('id', { as: 'total' });
|
||||
return Number(r);
|
||||
const r: any = await this.db(this.tableName).where('parent_id', id).count('id', { as: 'total' }).first();
|
||||
return r.total;
|
||||
}
|
||||
|
||||
public async childrens(id: string, pagination: Pagination): Promise<PaginatedFiles> {
|
||||
|
@ -26,7 +26,7 @@ export interface PaginatedResults {
|
||||
cursor?: string;
|
||||
}
|
||||
|
||||
export const pageMaxSize = 1000;
|
||||
export const pageMaxSize = 100;
|
||||
const defaultOrderField_ = 'updated_time';
|
||||
const defaultOrderDir_ = PaginationOrderDir.DESC;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user