Skip to content

Commit

Permalink
[jarun#603] implemented a custom/default list size action
Browse files Browse the repository at this point in the history
  • Loading branch information
LeXofLeviafan committed Dec 1, 2022
1 parent ba9d62b commit 2e5fb19
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
20 changes: 20 additions & 0 deletions bukuserver/static/bukuserver/js/page_size.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
$(document).ready(function() {
const SIZES = [20, 50, 100]; // hardcoded list; see page_size_form in admin/model/layout.html
let pageSize = url => new URL(url || location.host).searchParams.get('page_size');
$(`.actions-nav .dropdown-menu`).each(function () {
let _sizes = $(`li a`, this).map(function () {return pageSize(this.href)}).get();
if (SIZES.every((x, i) => x == _sizes[i]))
$(`li`, this).last().clone().each(function () {
$('a', this).text("custom").attr('href', `#`).on('click', () => {
let page = prompt(`Set custom page size (empty for default)`, pageSize(location) || '');
if (Number(page) || (page == "")) {
let search = new URL(location).searchParams;
(page ? search.set('page_size', page) : search.delete('page_size'));
location.search = search;
} else if (page != null)
alert(`Invalid page size: "${page}"`);
return false;
});
}).appendTo(this);
})
});
19 changes: 11 additions & 8 deletions bukuserver/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def _list_entry(self, context: Any, model: Namespace, name: str) -> Markup:
edit_template = "bukuserver/bookmark_edit.html"
named_filter_urls = True
extra_css = ['/static/bukuserver/css/bookmark.css']
extra_js = ['/static/bukuserver/js/page_size.js']

def __init__(self, *args, **kwargs):
self.bukudb: buku.BukuDb = args[0]
Expand Down Expand Up @@ -241,11 +242,7 @@ def get_list(self, page, sort_field, sort_desc, _, filters, page_size=None):
]
bookmarks = sorted(bookmarks, key=lambda x: x[key_idx], reverse=sort_desc)
count = len(bookmarks)
if page_size and bookmarks:
try:
bookmarks = list(chunks(bookmarks, page_size))[page]
except IndexError:
bookmarks = []
bookmarks = page_of(bookmarks, page_size, page)
data = []
for bookmark in bookmarks:
bm_sns = types.SimpleNamespace(id=None, url=None, title=None, tags=None, description=None)
Expand Down Expand Up @@ -445,6 +442,7 @@ def _name_formatter(self, _, model, name):
column_formatters = {
"name": _name_formatter,
}
extra_js = ['/static/bukuserver/js/page_size.js']

def __init__(self, *args, **kwargs):
self.bukudb = args[0]
Expand Down Expand Up @@ -492,8 +490,7 @@ def get_list(
)
)
count = len(tags)
if page_size and tags:
tags = list(chunks(tags, page_size))[page]
tags = page_of(tags, page_size, page)
data = []
for name, usage_count in tags:
tag_sns = types.SimpleNamespace(name=None, usage_count=None)
Expand Down Expand Up @@ -702,4 +699,10 @@ def index(self):

def chunks(arr, n):
n = max(1, n)
return (arr[i : i + n] for i in range(0, len(arr), n))
return [arr[i : i + n] for i in range(0, len(arr), n)]

def page_of(items, size, idx):
try:
return chunks(items, size)[idx] if size and items else []
except IndexError:
return []

0 comments on commit 2e5fb19

Please sign in to comment.