kopia lustrzana https://github.com/wagtail/wagtail
Add attrs to base MenuItem class and default it to empty dict
rodzic
45f0c73ff4
commit
34a79d36ad
|
@ -18,7 +18,7 @@ export const LinkMenuItem: React.FunctionComponent<
|
|||
}
|
||||
|
||||
// For compatibility purposes – do not capture clicks for links with a target.
|
||||
if (item.attrs && item.attrs.target) {
|
||||
if (item.attrs.target) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -72,24 +72,24 @@ export class LinkMenuItemDefinition implements MenuItemDefinition {
|
|||
name: string;
|
||||
label: string;
|
||||
url: string;
|
||||
attrs: { [key: string]: any };
|
||||
iconName: string | null;
|
||||
classNames?: string;
|
||||
attrs: { [key: string]: any } | null;
|
||||
|
||||
constructor({
|
||||
name,
|
||||
label,
|
||||
url,
|
||||
attrs,
|
||||
icon_name: iconName = null,
|
||||
classnames = undefined,
|
||||
attrs = null,
|
||||
}) {
|
||||
this.name = name;
|
||||
this.label = label;
|
||||
this.url = url;
|
||||
this.attrs = attrs;
|
||||
this.iconName = iconName;
|
||||
this.classNames = classnames;
|
||||
this.attrs = attrs;
|
||||
}
|
||||
|
||||
render({ path, slim, state, dispatch, navigate }) {
|
||||
|
|
|
@ -11,6 +11,7 @@ export interface MenuItemRenderContext {
|
|||
export interface MenuItemDefinition {
|
||||
name: string;
|
||||
label: string;
|
||||
attrs: { [key: string]: any };
|
||||
iconName: string | null;
|
||||
classNames?: string;
|
||||
render(context: MenuItemRenderContext): React.ReactFragment;
|
||||
|
|
|
@ -116,10 +116,17 @@ export class PageExplorerMenuItemDefinition extends LinkMenuItemDefinition {
|
|||
startPageId: number;
|
||||
|
||||
constructor(
|
||||
{ name, label, url, icon_name: iconName = null, classnames = undefined },
|
||||
{
|
||||
name,
|
||||
label,
|
||||
url,
|
||||
attrs,
|
||||
icon_name: iconName = null,
|
||||
classnames = undefined,
|
||||
},
|
||||
startPageId: number,
|
||||
) {
|
||||
super({ name, label, url, icon_name: iconName, classnames });
|
||||
super({ name, label, url, attrs, icon_name: iconName, classnames });
|
||||
this.startPageId = startPageId;
|
||||
}
|
||||
|
||||
|
|
|
@ -68,6 +68,7 @@ export const SubMenuItem: React.FunctionComponent<SubMenuItemProps> = ({
|
|||
<li className={className}>
|
||||
<Tippy disabled={isOpen || !slim} content={item.label} placement="right">
|
||||
<button
|
||||
{...item.attrs}
|
||||
onClick={onClick}
|
||||
className={`sidebar-menu-item__link ${item.classNames}`}
|
||||
aria-haspopup="menu"
|
||||
|
@ -112,6 +113,7 @@ export class SubMenuItemDefinition implements MenuItemDefinition {
|
|||
name: string;
|
||||
label: string;
|
||||
menuItems: MenuItemDefinition[];
|
||||
attrs: { [key: string]: any };
|
||||
iconName: string | null;
|
||||
classNames?: string;
|
||||
footerText: string;
|
||||
|
@ -120,6 +122,7 @@ export class SubMenuItemDefinition implements MenuItemDefinition {
|
|||
{
|
||||
name,
|
||||
label,
|
||||
attrs,
|
||||
icon_name: iconName = null,
|
||||
classnames = undefined,
|
||||
footer_text: footerText = '',
|
||||
|
@ -129,6 +132,7 @@ export class SubMenuItemDefinition implements MenuItemDefinition {
|
|||
this.name = name;
|
||||
this.label = label;
|
||||
this.menuItems = menuItems;
|
||||
this.attrs = attrs;
|
||||
this.iconName = iconName;
|
||||
this.classNames = classnames;
|
||||
this.footerText = footerText;
|
||||
|
|
|
@ -17,7 +17,7 @@ class MenuItem(metaclass=MediaDefiningClass):
|
|||
self.classnames = classnames
|
||||
self.icon_name = icon_name
|
||||
self.name = name or cautious_slugify(str(label))
|
||||
self.attrs = attrs
|
||||
self.attrs = attrs or {}
|
||||
self.order = order
|
||||
|
||||
def is_shown(self, request):
|
||||
|
@ -124,6 +124,7 @@ class SubmenuMenuItem(MenuItem):
|
|||
self.menu.render_component(request),
|
||||
icon_name=self.icon_name,
|
||||
classnames=self.classnames,
|
||||
attrs=self.attrs,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -37,13 +37,13 @@ class TestHome(TestCase, WagtailTestUtils):
|
|||
# check that custom menu items (including classname / icon_name) are pulled in
|
||||
self.assertContains(
|
||||
response,
|
||||
'{"name": "kittens", "label": "Kittens!", "icon_name": "kitten", "classnames": "kitten--test", "url": "http://www.tomroyal.com/teaandkittens/", "attrs": {"data-is-custom": "true"}}',
|
||||
'{"name": "kittens", "label": "Kittens!", "icon_name": "kitten", "classnames": "kitten--test", "attrs": {"data-is-custom": "true"}, "url": "http://www.tomroyal.com/teaandkittens/"}',
|
||||
)
|
||||
|
||||
# Check that the explorer menu item is here, with the right start page.
|
||||
self.assertContains(
|
||||
response,
|
||||
'[{"name": "explorer", "label": "Pages", "icon_name": "folder-open-inverse", "classnames": "", "url": "/admin/pages/", "attrs": null}, 1]',
|
||||
'[{"name": "explorer", "label": "Pages", "icon_name": "folder-open-inverse", "classnames": "", "attrs": {}, "url": "/admin/pages/"}, 1]',
|
||||
)
|
||||
|
||||
# There should be a link to the friend admin in on the home page.
|
||||
|
@ -56,7 +56,7 @@ class TestHome(TestCase, WagtailTestUtils):
|
|||
response = self.client.get(reverse("wagtailadmin_home") + "?hide-kittens=true")
|
||||
self.assertNotContains(
|
||||
response,
|
||||
'{"name": "kittens", "label": "Kittens!", "icon_name": "kitten", "classnames": "kitten--test", "url": "http://www.tomroyal.com/teaandkittens/", "attrs": {"data-is-custom": "true"}}',
|
||||
'{"name": "kittens", "label": "Kittens!", "icon_name": "kitten", "classnames": "kitten--test", "attrs": {"data-is-custom": "true"}, "url": "http://www.tomroyal.com/teaandkittens/"}',
|
||||
)
|
||||
|
||||
def test_dashboard_panels(self):
|
||||
|
|
|
@ -30,7 +30,7 @@ class TestAdaptLinkMenuItem(TestCase):
|
|||
"label": "Link",
|
||||
"name": "link",
|
||||
"url": "/link/",
|
||||
"attrs": None,
|
||||
"attrs": {},
|
||||
}
|
||||
],
|
||||
},
|
||||
|
@ -90,6 +90,7 @@ class TestAdaptSubMenuItem(TestCase):
|
|||
"icon_name": "",
|
||||
"classnames": "",
|
||||
"footer_text": "Footer text",
|
||||
"attrs": {},
|
||||
},
|
||||
[
|
||||
{
|
||||
|
@ -101,7 +102,7 @@ class TestAdaptSubMenuItem(TestCase):
|
|||
"icon_name": "link-icon",
|
||||
"classnames": "",
|
||||
"url": "/link/",
|
||||
"attrs": None,
|
||||
"attrs": {},
|
||||
}
|
||||
],
|
||||
}
|
||||
|
@ -132,6 +133,7 @@ class TestAdaptSubMenuItem(TestCase):
|
|||
"icon_name": "",
|
||||
"classnames": "",
|
||||
"footer_text": "",
|
||||
"attrs": {},
|
||||
},
|
||||
[
|
||||
{
|
||||
|
@ -143,7 +145,7 @@ class TestAdaptSubMenuItem(TestCase):
|
|||
"icon_name": "link-icon",
|
||||
"classnames": "",
|
||||
"url": "/link/",
|
||||
"attrs": None,
|
||||
"attrs": {},
|
||||
}
|
||||
],
|
||||
}
|
||||
|
@ -163,7 +165,7 @@ class TestAdaptPageExplorerMenuItem(TestCase):
|
|||
"_type": "wagtail.sidebar.PageExplorerMenuItem",
|
||||
"_args": [
|
||||
{
|
||||
"attrs": None,
|
||||
"attrs": {},
|
||||
"classnames": "",
|
||||
"icon_name": "",
|
||||
"label": "Pages",
|
||||
|
@ -217,7 +219,7 @@ class TestAdaptMainMenuModule(DjangoTestCase, WagtailTestUtils):
|
|||
"icon_name": "",
|
||||
"classnames": "",
|
||||
"url": "/pages/",
|
||||
"attrs": None,
|
||||
"attrs": {},
|
||||
}
|
||||
],
|
||||
}
|
||||
|
@ -232,7 +234,7 @@ class TestAdaptMainMenuModule(DjangoTestCase, WagtailTestUtils):
|
|||
"icon_name": "user",
|
||||
"classnames": "",
|
||||
"url": reverse("wagtailadmin_account"),
|
||||
"attrs": None,
|
||||
"attrs": {},
|
||||
}
|
||||
],
|
||||
},
|
||||
|
@ -245,7 +247,7 @@ class TestAdaptMainMenuModule(DjangoTestCase, WagtailTestUtils):
|
|||
"icon_name": "logout",
|
||||
"classnames": "",
|
||||
"url": reverse("wagtailadmin_logout"),
|
||||
"attrs": None,
|
||||
"attrs": {},
|
||||
}
|
||||
],
|
||||
},
|
||||
|
|
|
@ -23,12 +23,18 @@ class BaseSidebarAdapter(Adapter):
|
|||
|
||||
class MenuItem:
|
||||
def __init__(
|
||||
self, name: str, label: str, icon_name: str = "", classnames: str = ""
|
||||
self,
|
||||
name: str,
|
||||
label: str,
|
||||
icon_name: str = "",
|
||||
classnames: str = "",
|
||||
attrs: Mapping[str, Any] = None,
|
||||
):
|
||||
self.name = name
|
||||
self.label = label
|
||||
self.icon_name = icon_name
|
||||
self.classnames = classnames
|
||||
self.attrs = attrs or {}
|
||||
|
||||
def js_args(self):
|
||||
return [
|
||||
|
@ -37,6 +43,7 @@ class MenuItem:
|
|||
"label": self.label,
|
||||
"icon_name": self.icon_name,
|
||||
"classnames": self.classnames,
|
||||
"attrs": self.attrs,
|
||||
}
|
||||
]
|
||||
|
||||
|
@ -52,14 +59,18 @@ class LinkMenuItem(MenuItem):
|
|||
classnames: str = "",
|
||||
attrs: Mapping[str, Any] = None,
|
||||
):
|
||||
super().__init__(name, label, icon_name=icon_name, classnames=classnames)
|
||||
super().__init__(
|
||||
name,
|
||||
label,
|
||||
icon_name=icon_name,
|
||||
classnames=classnames,
|
||||
attrs=attrs,
|
||||
)
|
||||
self.url = url
|
||||
self.attrs = attrs
|
||||
|
||||
def js_args(self):
|
||||
args = super().js_args()
|
||||
args[0]["url"] = self.url
|
||||
args[0]["attrs"] = self.attrs
|
||||
return args
|
||||
|
||||
def __eq__(self, other):
|
||||
|
@ -84,8 +95,15 @@ class SubMenuItem(MenuItem):
|
|||
icon_name: str = "",
|
||||
classnames: str = "",
|
||||
footer_text: str = "",
|
||||
attrs: Mapping[str, Any] = None,
|
||||
):
|
||||
super().__init__(name, label, icon_name=icon_name, classnames=classnames)
|
||||
super().__init__(
|
||||
name,
|
||||
label,
|
||||
icon_name=icon_name,
|
||||
classnames=classnames,
|
||||
attrs=attrs,
|
||||
)
|
||||
self.menu_items = menu_items
|
||||
self.footer_text = footer_text
|
||||
|
||||
|
@ -104,6 +122,7 @@ class SubMenuItem(MenuItem):
|
|||
and self.icon_name == other.icon_name
|
||||
and self.classnames == other.classnames
|
||||
and self.footer_text == other.footer_text
|
||||
and self.attrs == other.attrs
|
||||
)
|
||||
|
||||
|
||||
|
@ -117,8 +136,16 @@ class PageExplorerMenuItem(LinkMenuItem):
|
|||
start_page_id: int,
|
||||
icon_name: str = "",
|
||||
classnames: str = "",
|
||||
attrs: Mapping[str, Any] = None,
|
||||
):
|
||||
super().__init__(name, label, url, icon_name=icon_name, classnames=classnames)
|
||||
super().__init__(
|
||||
name,
|
||||
label,
|
||||
url,
|
||||
icon_name=icon_name,
|
||||
classnames=classnames,
|
||||
attrs=attrs,
|
||||
)
|
||||
self.start_page_id = start_page_id
|
||||
|
||||
def js_args(self):
|
||||
|
@ -135,6 +162,7 @@ class PageExplorerMenuItem(LinkMenuItem):
|
|||
and self.start_page_id == other.start_page_id
|
||||
and self.icon_name == other.icon_name
|
||||
and self.classnames == other.classnames
|
||||
and self.attrs == other.attrs
|
||||
)
|
||||
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue