Opened 11 years ago
Closed 11 years ago
#19314 closed New feature (duplicate)
JavaScript for Widget in added inline
Reported by: | Per Rosengren | Owned by: | Per Rosengren |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Normal | Keywords: | admin forms |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
A django.forms.widgets.Widget can add javascript in its render function. This can include state variables related to the widget.
This problem applies to widgets in inline forms. When an extra inline is added (using javascript in admin), a special hidden empty inline is cloned, setup and showed. The widget javascript was applied to the hidden inline, so any state is connected to that.
What is needed is to not run the javascript for the empty inline's widget, and to trigger the javascript to run on any added (cloned) inline forms' widget. I suggest we modify django/contrib/admin/static/admin/js/inlines.js to trigger a custom event that contains the added DOM element. The widget code can then bind its activation function to that event, and apply it to the widget HTML within the event's element.
I can do the patch if you agree this is the way to solve it.
This is the widget javascript I currently use:
jQuery(document).ready(function ($) { $('.form-row:not(.empty-form) #id_{{ name }}').wymeditor({ basePath: '{% static "js/wymeditor/" %}', updateSelector: 'input[type=submit],', updateEvent: 'click', lang: '{{ language }}', skin: 'default', logoHtml: '' }); });
Change History (5)
comment:1 Changed 11 years ago by
Owner: | changed from nobody to Per Rosengren |
---|---|
Status: | new → assigned |
comment:2 Changed 11 years ago by
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 Changed 11 years ago by
Has patch: | set |
---|
comment:4 Changed 11 years ago by
This is a similar approach as #15760 which also contains an event for form deletion.
comment:5 Changed 11 years ago by
Resolution: | → duplicate |
---|---|
Status: | assigned → closed |
Yes, this is exactly #15760.
Patch: https://github.com/django/django/pull/542
My Widget uses this JS, which makes use of this patch:
For the code above to work, the widget's render function adds
{'class': name}
to the attrs of the parent TextArea widget.