diff --git a/otterwiki/preferences.py b/otterwiki/preferences.py index f36a049..d098719 100644 --- a/otterwiki/preferences.py +++ b/otterwiki/preferences.py @@ -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 diff --git a/otterwiki/sidebar.py b/otterwiki/sidebar.py index 2812d55..af08284 100644 --- a/otterwiki/sidebar.py +++ b/otterwiki/sidebar.py @@ -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 diff --git a/otterwiki/templates/admin/sidebar_preferences.html b/otterwiki/templates/admin/sidebar_preferences.html index f793601..a43b4bd 100644 --- a/otterwiki/templates/admin/sidebar_preferences.html +++ b/otterwiki/templates/admin/sidebar_preferences.html @@ -38,13 +38,13 @@

Custom Menu

  -{% for link,title in custom_menu + [["",""]] %} +{% for entry in custom_menu + [{"link":"","title":""}] %} - - + + @@ -135,7 +135,7 @@

Page Index

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); diff --git a/otterwiki/templates/snippets/menu.html b/otterwiki/templates/snippets/menu.html index 676a764..ed06c62 100644 --- a/otterwiki/templates/snippets/menu.html +++ b/otterwiki/templates/snippets/menu.html @@ -10,8 +10,8 @@
diff --git a/tests/test_sidebar.py b/tests/test_sidebar.py index faef5b3..f2d9a65 100644 --- a/tests/test_sidebar.py +++ b/tests/test_sidebar.py @@ -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