Convert TableBlock widget to Django-1.11-style template rendering

This commit is contained in:
Matt Westcott 2018-08-09 11:54:47 +01:00 committed by Matt Westcott
parent 3213feeb8f
commit a1e31e9ab7
3 changed files with 14 additions and 19 deletions

View file

@ -6,7 +6,6 @@ from django.utils import translation
from django.utils.functional import cached_property
from wagtail.core.blocks import FieldBlock
from wagtail.utils.widgets import WidgetWithScript
DEFAULT_TABLE_OPTIONS = {
'minSpareRows': 0,
@ -35,22 +34,17 @@ DEFAULT_TABLE_OPTIONS = {
}
class TableInput(WidgetWithScript, forms.HiddenInput):
class TableInput(forms.HiddenInput):
template_name = "table_block/widgets/table.html"
def __init__(self, table_options=None, attrs=None):
self.table_options = table_options
super().__init__(attrs=attrs)
def render(self, name, value, attrs=None):
original_field_html = super().render(name, value, attrs)
return render_to_string("table_block/widgets/table.html", {
'original_field_html': original_field_html,
'attrs': attrs,
'value': value,
})
def render_js_init(self, id_, name, value):
return "initTable({0}, {1});".format(json.dumps(id_), json.dumps(self.table_options))
def get_context(self, name, value, attrs=None):
context = super().get_context(name, value, attrs)
context['widget']['table_options_json'] = json.dumps(self.table_options)
return context
class TableBlock(FieldBlock):

View file

@ -1,23 +1,24 @@
{% load i18n %}
<div class="field boolean_field widget-checkbox_input">
<label for="{{ attrs.id }}-handsontable-header">{% trans 'Row header' %}</label>
<label for="{{ widget.attrs.id }}-handsontable-header">{% trans 'Row header' %}</label>
<div class="field-content">
<div class="input">
<input type="checkbox" id="{{ attrs.id }}-handsontable-header" name="handsontable-header"/>
<input type="checkbox" id="{{ widget.attrs.id }}-handsontable-header" name="handsontable-header"/>
</div>
<p class="help">{% trans 'Display the first row as a header.' %}</p>
</div>
</div>
<br/>
<div class="field boolean_field widget-checkbox_input">
<label for="{{ attrs.id }}-handsontable-col-header">{% trans 'Column header' %}</label>
<label for="{{ widget.attrs.id }}-handsontable-col-header">{% trans 'Column header' %}</label>
<div class="field-content">
<div class="input">
<input type="checkbox" id="{{ attrs.id }}-handsontable-col-header" name="handsontable-col-header"/>
<input type="checkbox" id="{{ widget.attrs.id }}-handsontable-col-header" name="handsontable-col-header"/>
</div>
<p class="help">{% trans 'Display the first column as a header.' %}</p>
</div>
</div>
<br/>
<div id="{{ attrs.id }}-handsontable-container"></div>
{{ original_field_html }}
<div id="{{ widget.attrs.id }}-handsontable-container"></div>
{% include 'django/forms/widgets/hidden.html' %}
<script>initTable("{{ widget.attrs.id|escapejs }}", {{ widget.table_options_json|safe }});</script>

View file

@ -354,6 +354,6 @@ class TestTableBlockPageEdit(TestCase, WagtailTestUtils):
self.assertContains(response, 'Battlestar')
self.assertContains(response, 'Galactica')
# check init
self.assertContains(response, 'initTable("table-0-value"')
self.assertContains(response, 'initTable("table\\u002D0\\u002Dvalue"')
self.assertContains(response, 'minSpareRows')
self.assertContains(response, 'startRows')