-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update test matrix, docs * Wagtail 6.0 upgrade consideration: Deprecated WidgetWithScript base widget class * Update implementation of ReadonlyUUIDInput * Drop support for Wagtail < 5.2 --------- Co-authored-by: Katherine Domingo <katherine.domingo@torchbox.com>
- Loading branch information
Showing
12 changed files
with
118 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
wagtail_footnotes/static/footnotes/js/read-only-uuid-controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class CustomEditorController extends window.StimulusModule.Controller { | ||
connect() { | ||
setUUID(this.element.id); | ||
} | ||
} | ||
|
||
window.wagtail.app.register('read-only-uuid', CustomEditorController); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,64 @@ | ||
from django.forms import HiddenInput | ||
from wagtail.utils.widgets import WidgetWithScript | ||
from wagtail import VERSION as WAGTAIL_VERSION | ||
|
||
|
||
class ReadonlyUUIDInput(WidgetWithScript, HiddenInput): | ||
""" | ||
This isn't really read-only. It's a hidden input with an an adjacent div | ||
showing the current value; that way we can set the value in JavaScript, but | ||
the user can't easily change it. | ||
""" | ||
if WAGTAIL_VERSION >= (6, 0): | ||
from django.forms import Media | ||
from django.utils.safestring import mark_safe | ||
|
||
def render_html(self, name, value, attrs): | ||
"""Render the HTML (non-JS) portion of the field markup""" | ||
hidden = super(WidgetWithScript, self).render(name, value, attrs) | ||
display_value = value[:6] if value is not None else value | ||
shown = f'<div id="{attrs["id"]}_display-value" style="padding-top: 1.2em;">{display_value}</div>' | ||
return shown + hidden | ||
class ReadonlyUUIDInput(HiddenInput): | ||
""" | ||
This isn't really read-only. It's a hidden input with an an adjacent div | ||
showing the current value; that way we can set the value in JavaScript, but | ||
the user can't easily change it. | ||
""" | ||
|
||
def render_js_init(self, id_, name, value): | ||
return f'setUUID("{id_}");' | ||
def render(self, name, value, attrs=None, renderer=None): | ||
# no point trying to come up with sensible semantics for when 'id' is missing from attrs, | ||
# so let's make sure it fails early in the process | ||
try: | ||
attrs["id"] | ||
except (KeyError, TypeError) as exc: | ||
raise TypeError( | ||
"ReadonlyUUIDInput cannot be rendered without an 'id' attribute" | ||
) from exc | ||
|
||
widget_html = self.render_html(name, value, attrs) | ||
|
||
return mark_safe(widget_html) # noqa: S308 | ||
|
||
def render_html(self, name, value, attrs): | ||
"""Render the HTML (non-JS) portion of the field markup""" | ||
hidden = super().render(name, value, attrs) | ||
display_value = value[:6] if value is not None else value | ||
shown = f'<div id="{attrs["id"]}_display-value" style="padding-top: 1.2em;">{display_value}</div>' | ||
return shown + hidden | ||
|
||
def build_attrs(self, *args, **kwargs): | ||
attrs = super().build_attrs(*args, **kwargs) | ||
attrs["data-controller"] = "read-only-uuid" | ||
return attrs | ||
|
||
@property | ||
def media(self): | ||
return Media(js=["footnotes/js/read-only-uuid-controller.js"]) | ||
|
||
else: | ||
from wagtail.utils.widgets import WidgetWithScript | ||
|
||
class ReadonlyUUIDInput(WidgetWithScript, HiddenInput): | ||
""" | ||
This isn't really read-only. It's a hidden input with an an adjacent div | ||
showing the current value; that way we can set the value in JavaScript, but | ||
the user can't easily change it. | ||
""" | ||
|
||
def render_html(self, name, value, attrs): | ||
"""Render the HTML (non-JS) portion of the field markup""" | ||
hidden = super(WidgetWithScript, self).render(name, value, attrs) | ||
display_value = value[:6] if value is not None else value | ||
shown = f'<div id="{attrs["id"]}_display-value" style="padding-top: 1.2em;">{display_value}</div>' | ||
return shown + hidden | ||
|
||
def render_js_init(self, id_, name, value): | ||
return f'setUUID("{id_}");' |