-
Notifications
You must be signed in to change notification settings - Fork 22
Allow an object's __str__() or other method to be used as the card header #5
Comments
I've thought of a way we might actually be able to support dynamic updating with this approach: If we could gather the form values for a specific Inline and ajax post them (along with a ContentType ID, and the name of the desired method/property method/attribute) to a view that attempts to assemble (but not save) an instance and call the desired method on it... That could be reused for pretty much any model. Triggering that process could prove tricky though (listening for changes on any field change would be overkill). I imagine you might also have to swap the csrf_token out for a new each time, but that's no biggie. |
Hi @Adrian-Turjak. I'm afraid all I did was play around so far - trying to familiarise myself with the bits and pieces. I'm normally not the type to raise things and not tackle them myself, but I'm completely unfamiliar with React, so my confidence fizzled out pretty quickly. I would honestly settle for card titles on items not dynamically updating using this approach. I think it would be fine to just have:
I feel the coloured status highlighting would provide enough feedback to users for them to keep track of which item is which - Dynamically updating the title on every change (or perhaps changes to one or more specified fields) would just be a nice touch, but not crucial. Being able to specify different methods for the string would be useful, but not necessary. I suppose it might be useful to look for a I'd also like to suggest that maybe this is the default behaviour if you didn't specify Keen to hear your thoughts :) |
@ababic Sorry for not responding right away! Non-dynamic is more than enough for my use cases, and for menus would be fine. New menus you'd be keeping open anyway and then on reloading/saving the titles would now be needed. Although if we aren't dynamically updating titles, we should have the js alter the title to something like As for how to do the dynamic stuff,
Again, my JS is rusty so I don't know what the best method to do here is, but the approach where you pass in some JS to provide the title for the card isn't a terrible idea. Since for something like the menu objects, when you select a page to link to, the page 'title' is somewhere in the DOM, and if you know where, writing some js to pull that out isn't impossible. Or if the Any of that make sense? :P |
Thanks @Adrian-Turjak! some good points there.
My personal feeling is that the current behaviour of colour-highlighting is enough to provide feedback that something has been edited - if the item has not been saved yet, to me, it would still make sense for it to have a "New menu item" heading. Changing to "Edited menu item" could confuse things if you added new items to an existing menu.
Yes, I believe this is the current behaviour. It's a perfectly good solution for cases when the card header does track a specific field.
This is a potential route, but I'm not really sure how this option works currently, or how I'd go about using it to generate menu item card headers, especially since we'd need to access field values for selected pages in order to report the same things as str() (see https://github.com/rkhleics/wagtailmenus/blob/master/wagtailmenus/models/menuitems.py#L84) That would have to involve some other view to access that data from the Page.
Yes, but it's not always the 'title' field that is used from pages, it can be customised using the To have card headers update dynamically for menu items, you'd need to be able to trigger both when If we can call |
I realise the way I phrased that made little sense! I was mostly thinking for the header from
It appears to just be an eval() which means you can easily throw in some javascript that pulls the data from the formset in the same way that the from field dynamic refresh does it.
Currently the dynamic card header refresh happens on card close, which should work fine. Given how the js is setup for this, you should be able to easily enough throw in an eval as it is that checks the formset for both of those values and outputs a result on card close. The formset is built here, and that will be accessible in the js as a json. The problem is the case where some has used
Adding that could likely be done with just another value in the extra dict added in |
Thank you for clarifying. That may be fine. I guess it's one of those things that you can't judge until you see it working.
Great. That sounds like the way to go then, if @kaedroho feels like this is something they want to support?. If not, I suppose we can implement that side of things within
Thanks. I did try to figure out where to set this last night, but I was way off :) |
Oh, there shouldn't need to be anything special that needs to change here. Looking at it, we can probably just use it as is with the following javascript passed in to (function(){
if (form.extra.hasOwnProperty('link_page')){
page_title = form.extra['link_page']['title'];
if (page_title){
return page_title;
}
} else if (form.fields.hasOwnProperty('link_text')){
link_text = form.fields['link_text'];
if (link_text){
return link_text;
}
}
return "";
})(); Then in python that would look like: header_from_js = """
(function(){
if (form.extra.hasOwnProperty('link_page')){
page_title = form.extra['link_page']['title'];
if (page_title){
return page_title;
}
} else if (form.fields.hasOwnProperty('link_text')){
link_text = form.fields['link_text'];
if (link_text){
return link_text;
}
}
return "";
})();
"""
# .....
panels = (
CondensedInlinePanel(
app_settings.MAIN_MENU_ITEMS_RELATED_NAME, label=_("menu items")
card_header_from_js=" ".join(header_from_js.split()),
),
)
Note the It's the |
Thanks @Adrian-Turjak, Let's take wagtailmenus-specific solutions out of the equation for the time being, as we can work that out later (this board isn't really the place for that anyway). What I want to iron out here specifically, is whether @kaedroho would be interested in supporting the following as default behaviour?
I feel like that would create a better 'out-of-the-box' experience for most use cases (familiar behaviour too - most 'inline' solutions I've seen for the django admin behave this way) |
Thanks @Adrian-Turjak. I should have some time this weekend to try things out. |
Yeah, let's close this :) the rest of the stuff here is wagtailmenus specific, so we'll work that out in that project :) |
For a bit of background: I'm playing with the idea of using this for wagtailmenus. The most meaningful thing to use as a card header for MenuItem instances is a method that returns a page's title (if the item links to a page), or custom menu text value if that is provided.
I would be happy with card headers being blank until the form was saved (or maybe just something generic, like 'New menu item'), and also with the card headers not changing dynamically during editing.
I'd be happy to have a crack at this myself in PR (though, I may need some help on the React side of things) but wanted to first ask if you think this is something you would want to support, and whether you might have any ideas about how best to approach the problem?
Thank you for the great work so far!
The text was updated successfully, but these errors were encountered: