Skip to content

Commit

Permalink
feat: Make caseless sorting of the sidebar configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
redimp committed Jan 21, 2025
1 parent 48e0592 commit 47aefc0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions otterwiki/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ def handle_sidebar_preferences(form):
_update_preference(
"SIDEBAR_MENUTREE_MODE", form.get("sidebar_menutree_mode", "")
)
for checkbox in [
"sidebar_menutree_ignore_case",
]:
_update_preference(checkbox.upper(), form.get(checkbox, "False"))
# commit changes to the database
db.session.commit()
update_app_config()
Expand Down
2 changes: 2 additions & 0 deletions otterwiki/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
SQLALCHEMY_TRACK_MODIFICATIONS=False,
MINIFY_HTML=True,
SIDEBAR_MENUTREE_MODE="SORTED",
SIDEBAR_MENUTREE_IGNORE_CASE=False,
SIDEBAR_MENUTREE_MAXDEPTH="",
SIDEBAR_CUSTOM_MENU="",
COMMIT_MESSAGE="REQUIRED", # OPTIONAL DIISABLED
Expand Down Expand Up @@ -135,6 +136,7 @@ def update_app_config():
"NOTIFY_ADMINS_ON_REGISTER",
"NOTIFY_USER_ON_APPROVAL",
"RETAIN_PAGE_NAME_CASE",
"SIDEBAR_MENUTREE_IGNORE_CASE",
"GIT_WEB_SERVER",
"HIDE_LOGO",
] or item.name.upper().startswith("SIDEBAR_SHORTCUT_"):
Expand Down
13 changes: 11 additions & 2 deletions otterwiki/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,18 @@ def order_tree(
# convert OrderedDict into list
entries = list(tree.items())
# decide sort_key lambda on mode
sort_key = lambda k: (True, str.lower(k[0]))
if app.config["SIDEBAR_MENUTREE_IGNORE_CASE"]:
sort_key = lambda k: (True, str.lower(k[0]))
else:
sort_key = lambda k: (True, k[0])
if self.mode in ["DIRECTORIES_GROUPED"]:
sort_key = lambda k: (len(k[1]["children"]) == 0, str.lower(k[0]))
if app.config["SIDEBAR_MENUTREE_IGNORE_CASE"]:
sort_key = lambda k: (
len(k[1]["children"]) == 0,
str.lower(k[0]),
)
else:
sort_key = lambda k: (len(k[1]["children"]) == 0, k[0])
# sort entries
filtered_list = sorted(entries, key=sort_key)
# filter entries
Expand Down
12 changes: 11 additions & 1 deletion otterwiki/templates/admin/sidebar_preferences.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,23 @@ <h3 class="card-title">Custom Menu</h3>
{##}
<h3 class="card-title">Page Index</h3>
<div class="form-group">
<label for="sidebar_menutree_mode" class="required">Page Index Mode</label>
<label for="sidebar_menutree_mode">Page Index Mode</label>
<select class="form-control w-md-400" id="sidebar_menutree_mode" name="sidebar_menutree_mode">
{% for mode in [("", "Not displayed"),("SORTED","Directories and pages, sorted"),("DIRECTORIES_GROUPED","Directories and pages, with directories grouped first"),("DIRECTORIES_ONLY","Directories only")] %}
<option value="{{mode[0]}}"{%if config.SIDEBAR_MENUTREE_MODE == mode[0] %} selected="selected"{%endif%}>{{mode[1]}}</option>
{% endfor %}
</select>
</div>
{##}
{%if config.RETAIN_PAGE_NAME_CASE %}
<div class="form-group">
<div class="custom-checkbox">
<input {%if config.SIDEBAR_MENUTREE_IGNORE_CASE %}checked{% endif %} type="checkbox" id="menutree_ignore_case" name="sidebar_menutree_ignore_case" value="True">
<label for="menutree_ignore_case">Sort case insensitive</label>
</div>
</div>
{% endif %}
{##}
<div class="form-group">
<label for="sidebar_menutree_maxdepth">Maximal depth of pages and directories listed.</label>
<input name="sidebar_menutree_maxdepth" type="text" placeholder="unlimited" class="form-control w-md-400" id="sidebar_menutree_maxdepth" value="{{ config.SIDEBAR_MENUTREE_MAXDEPTH or "" }}">
Expand Down

0 comments on commit 47aefc0

Please sign in to comment.