-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "wide" option to article TableBlock (#12843)
* Create wide table block and template * Add WideTableBlock to customblocks * Use new WideTableBlock instead of TableBlock * Convert TableBlock blockdata to WideTableBlock data * Linting * Help text correction * Remove unused block import * flake8 logic check pattern * Additional formatting * Apply narrow table width rule for only large and up viewports * Remove ignore file for article_table_block * Update migration file --------- Co-authored-by: Daniel Miranda <manieldiranda@gmail.com>
- Loading branch information
1 parent
4ffdb29
commit c129386
Showing
7 changed files
with
1,383 additions
and
70 deletions.
There are no files selected for viewing
1,285 changes: 1,285 additions & 0 deletions
1,285
network-api/networkapi/wagtailpages/migrations/0162_alter_articlepage_body.py
Large diffs are not rendered by default.
Oops, something went wrong.
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
37 changes: 37 additions & 0 deletions
37
network-api/networkapi/wagtailpages/pagemodels/customblocks/table_block.py
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,37 @@ | ||
from wagtail import blocks | ||
from wagtail.contrib.table_block.blocks import TableBlock | ||
|
||
|
||
class WideTableBlock(blocks.StructBlock): | ||
""" | ||
A custom block that contains a table and an option to render it wider than the page body content. | ||
""" | ||
|
||
table = TableBlock() | ||
|
||
wide = blocks.BooleanBlock( | ||
required=False, | ||
default=True, | ||
help_text="If checked, the table will render wider than the other page body content.", | ||
) | ||
|
||
def get_context(self, value, parent_context=None): | ||
""" | ||
Override get_context to add the table body logic to the template context. | ||
If `first_row_is_table_header` is True, it slices the data to skip the first row. | ||
Otherwise, it returns the full data set. | ||
""" | ||
context = super().get_context(value, parent_context=parent_context) | ||
|
||
# Check if the first row is a header and slice the data accordingly | ||
if value.get("table").get("first_row_is_table_header") is True: | ||
context["table_body"] = value["table"]["data"][1:] # Skip the first row | ||
else: | ||
context["table_body"] = value["table"]["data"] # Return the full data set | ||
|
||
return context | ||
|
||
class Meta: | ||
icon = "table" | ||
label = "Table" | ||
template = "wagtailpages/blocks/wide_table_block.html" |
67 changes: 0 additions & 67 deletions
67
network-api/networkapi/wagtailpages/templates/wagtailpages/blocks/article_table_block.html
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
network-api/networkapi/wagtailpages/templates/wagtailpages/blocks/wide_table_block.html
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,59 @@ | ||
{% extends "./base_streamfield_block.html" %} | ||
{% load wagtailcore_tags table_block_tags %} | ||
|
||
{% block block_content %} | ||
{% with self.table as table %} | ||
<div class="article-table-block table-responsive{% if not self.wide %} large:tw-mx-auto large:tw-max-w-[80%]{% endif %}"> | ||
{% comment %} | ||
Modified table template comes from "table_block/blocks/table.html" | ||
{% endcomment %} | ||
<table class="table"> | ||
{% if table.table_caption %} | ||
<caption>{{ table.table_caption }}</caption> | ||
{% endif %} | ||
{% if table.first_row_is_table_header %} | ||
<thead> | ||
<tr> | ||
{% for column in table.data.0 %} | ||
<th>{{ column }}</th> | ||
{% endfor %} | ||
</tr> | ||
</thead> | ||
{% endif %} | ||
<tbody> | ||
{% for row in table_body %} | ||
{% with forloop.counter0 as row_index %} | ||
<tr> | ||
{% for column in row %} | ||
{% with forloop.counter0 as col_index %} | ||
{% if table.first_col_is_header and forloop.first %} | ||
<th scope="row" class="{% if table.first_col_is_header and forloop.first %}highlighted{% endif %}" {% cell_classname row_index col_index table_header %}> | ||
{% if column.strip %} | ||
{% if html_renderer %} | ||
{{ column.strip|safe|linebreaksbr }} | ||
{% else %} | ||
{{ column.strip|linebreaksbr }} | ||
{% endif %} | ||
{% endif %} | ||
</th> | ||
{% else %} | ||
<td class="{% if table.first_col_is_header and forloop.first %}highlighted{% endif %}" {% cell_classname row_index col_index table_header %}> | ||
{% if column.strip %} | ||
{% if html_renderer %} | ||
{{ column.strip|safe|linebreaksbr }} | ||
{% else %} | ||
{{ column.strip|linebreaksbr }} | ||
{% endif %} | ||
{% endif %} | ||
</td> | ||
{% endif %} | ||
{% endwith %} | ||
{% endfor %} | ||
</tr> | ||
{% endwith %} | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
</div> | ||
{% endwith %} | ||
{% endblock block_content %} |
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