Skip to content

Commit

Permalink
refactor: custom_menu list of dicts instead of list of lists
Browse files Browse the repository at this point in the history
  • Loading branch information
redimp committed Sep 14, 2024
1 parent 1a91c50 commit 3cb2021
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
10 changes: 6 additions & 4 deletions otterwiki/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ def handle_mail_preferences(form):


def handle_sidebar_preferences(form):
custom_menu_js = json.dumps(
[ x
custom_menu_js = json.dumps([
{
"link": x[0],
"title": x[1],
}
for x in list(zip(form.getlist("link"), form.getlist("title")))
if x[0].strip() or x[1].strip()
]
)
])

_update_preference(
"SIDEBAR_CUSTOM_MENU", custom_menu_js
Expand Down
16 changes: 8 additions & 8 deletions otterwiki/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ def __init__(self):
f"Error decoding SIDEBAR_CUSTOM_MENU={app.config.get('SIDEBAR_CUSTOM_MENU','')}: {e}"
)
raw_config = []
# generate both config and menu from raw_config
for entry in raw_config:
if len(entry) != 2: continue # FIXME: print warning
if empty(entry[0]) and empty(entry[1]): continue
self.config.append(entry)
for entry in self.config:
link, title = entry
if not entry.get("title", None) and not entry.get("link", None):
continue
link, title = entry.get("link",""), entry.get("title", "")
self.config.append({"link": link, "title": title})
if empty(link):
if empty(title): continue
self.menu.append([url_for("view", path=title), title])
self.menu.append({"link":url_for("view", path=title), "title": title})
elif self.URI_SIMPLE.match(link):
if empty(title): title = link
self.menu.append([link, title])
self.menu.append({"link": link, "title": title})
else:
if empty(title): title = link
self.menu.append([url_for("view", path=link), title])
self.menu.append({"link": url_for("view", path=link), "title": title})

def query(self):
return self.menu
Expand Down
8 changes: 4 additions & 4 deletions otterwiki/templates/admin/sidebar_preferences.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ <h3 class="card-title">Custom Menu</h3>
<th>&nbsp;</th>
</thead>
<tbody id="custom-page-index">
{% for link,title in custom_menu + [["",""]] %}
{% for entry in custom_menu + [{"link":"","title":""}] %}
<tr>
<td class="pr-10">
<span class="btn btn-square btn-sm btn-handle"><i class="fas fa-arrows-alt"></i></span>
</td>
<td><input type="text" name="link" placeholder="..." class="w-200 form-control" value="{{link}}"/></td>
<td><input type="text" name="title" placeholder="" class="w-200 form-control"/ value="{{title}}"> </td>
<td><input type="text" name="link" placeholder="..." class="w-200 form-control" value="{{entry.link}}"/></td>
<td><input type="text" name="title" placeholder="" class="w-200 form-control"/ value="{{entry.title}}"> </td>
<td class="pl-10">
<span class="btn btn-square btn-sm" onclick="cloneRow(this);"><i class="fas fa-clone"></i></span>
<span class="btn btn-square btn-sm btn-danger" onclick="deleteRow(this);"><i class="far fa-trash-alt"></i></span>
Expand Down Expand Up @@ -135,7 +135,7 @@ <h3 class="card-title">Page Index</h3>
var tbody = document.getElementById('custom-page-index');
var table = tbody.closest("table");
var lastrow = document.getElementById('custom-page-index').lastElementChild;
if (lastrow.querySelector("input[name='title']").value == "")
if (lastrow.querySelector("input[name='link']").value == "")
row = lastrow;
else
row = cloneRow(null, true);
Expand Down
4 changes: 2 additions & 2 deletions otterwiki/templates/snippets/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<div id="custom-menu" class="collapse-content">
<!-- menu [[ -->
<ul class="sidebarmenu parent-sidebar-menu">
{%- for link, title in custom_menu -%}
<li> <a href="{{link}}">{{title}}</a> </li>
{%- for entry in custom_menu -%}
<li> <a href="{{entry.link}}">{{entry.title}}</a> </li>
{%- endfor %}
</ul>
<!-- ]] menu -->
Expand Down
12 changes: 6 additions & 6 deletions tests/test_sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,20 @@ def test_sidebar_custom_menu(create_app, test_client, req_ctx):
links = get_sidebar_menu(test_client)
assert links is None

create_app.config["SIDEBAR_CUSTOM_MENU"] = """[["Home", ""]]"""
assert [['Home', '']] == SidebarMenu().config
create_app.config["SIDEBAR_CUSTOM_MENU"] = """[{"link": "Home", "title": ""}]"""
assert [{'link':'Home', 'title':''}] == SidebarMenu().config
links = get_sidebar_menu(test_client)
assert links
assert ('Home', '/Home') in links

create_app.config["SIDEBAR_CUSTOM_MENU"] = """[["https://example.com", "Example"]]"""
assert [['https://example.com', 'Example']] == SidebarMenu().config
create_app.config["SIDEBAR_CUSTOM_MENU"] = """[{"link":"https://example.com", "title":"Example"}]"""
assert {"title":"Example", "link":"https://example.com"} in SidebarMenu().config
links = get_sidebar_menu(test_client)
assert links
assert ('Example', 'https://example.com') in links

create_app.config["SIDEBAR_CUSTOM_MENU"] = """[["/Example", ""]]"""
assert [['/Example', '']] == SidebarMenu().config
create_app.config["SIDEBAR_CUSTOM_MENU"] = """[{"link": "/Example", "title": ""}]"""
assert [{'link':'/Example', 'title':''}] == SidebarMenu().config
links = get_sidebar_menu(test_client)
assert links
assert ('/Example', '/Example') in links
Expand Down

0 comments on commit 3cb2021

Please sign in to comment.