Skip to content

Commit

Permalink
Align table headers width
Browse files Browse the repository at this point in the history
For consistency across table, most visible on the dependencies
page which contains two tables.
  • Loading branch information
jenspav committed Jul 2, 2024
1 parent 32f81b3 commit 304dcca
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import nl.avisi.structurizr.site.generatr.site.formatDate

fun PageViewModel.createDecisionsTableViewModel(decisions: Collection<Decision>, hrefFactory: (Decision) -> String) =
TableViewModel.create {
headerRow(headerCell("ID"), headerCell("Date"), headerCell("Status"), headerCell("Title"))
headerRow(headerCellWithIndex("ID"), headerCell("Date"), headerCell("Status"), headerCell("Title"))
decisions
.sortedBy { it.id.toInt() }
.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.structurizr.util.Url

fun createPropertiesTableViewModel(properties: Map<String, String>) =
TableViewModel.create {
headerRow(headerCell("Name"), headerCell("Value"))
headerRow(headerCellWithName("Name"), headerCell("Value"))
properties
.toSortedMap()
.forEach { (name, value) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import nl.avisi.structurizr.site.generatr.hasSections

fun PageViewModel.createSectionsTableViewModel(sections: Collection<Section>, dropFirst: Boolean = true, hrefFactory: (Section) -> String) =
TableViewModel.create {
headerRow(headerCell("#"), headerCell("Title"))
headerRow(headerCellWithIndex("#"), headerCell("Title"))
sections
.sortedBy { it.order }
.drop(if (dropFirst) 1 else 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class SoftwareSystemDependenciesPageViewModel(

private fun TableViewModel.TableViewInitializerContext.header() {
headerRow(
headerCell("System"),
headerCell("Description"),
headerCellWithName("System"),
headerCellWithDescription("Description"),
headerCell("Technology")
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SoftwareSystemsPageViewModel(generatorContext: GeneratorContext) : PageVie
override val pageSubTitle = "Software Systems"

val softwareSystemsTable: TableViewModel = TableViewModel.create {
headerRow(headerCell("Name"), headerCell("Description"))
headerRow(headerCellWithName("Name"), headerCell("Description"))

generatorContext.workspace.model.softwareSystems
.sortedBy { it.name.lowercase() }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package nl.avisi.structurizr.site.generatr.site.model

enum class CellWidth {
UNSPECIFIED,
ONE_TENTH,
ONE_FOURTH,
TWO_FOURTH,
}

data class TableViewModel(val headerRows: List<RowViewModel>, val bodyRows: List<RowViewModel>) {
sealed interface CellViewModel {
val isHeader: Boolean
Expand All @@ -10,12 +17,12 @@ data class TableViewModel(val headerRows: List<RowViewModel>, val bodyRows: List
override val isHeader: Boolean,
val greyText: Boolean = false,
val boldText: Boolean = false,
val oneTenthWidth: Boolean = false
val width: CellWidth = CellWidth.UNSPECIFIED
) : CellViewModel {
override fun toString() = if (isHeader)
"headerCell($title, greyText=$greyText)"
else
"cell($title, greyText=$greyText, boldText=$boldText, oneTenthWidth=$oneTenthWidth)"
"cell($title, greyText=$greyText, boldText=$boldText, width=${width.name})"
}

data class LinkCellViewModel(val link: LinkViewModel, override val isHeader: Boolean) : CellViewModel {
Expand Down Expand Up @@ -47,16 +54,21 @@ data class TableViewModel(val headerRows: List<RowViewModel>, val bodyRows: List
}

fun headerCell(title: String, greyText: Boolean = false) = TextCellViewModel(title, true, greyText)
fun headerCellWithIndex(title: String): TextCellViewModel =
TextCellViewModel(title, isHeader = true, width = CellWidth.ONE_TENTH)
fun headerCellWithName(title: String): TextCellViewModel =
TextCellViewModel(title, isHeader = true, width = CellWidth.ONE_FOURTH)
fun headerCellWithDescription(title: String): TextCellViewModel =
TextCellViewModel(title, isHeader = true, width = CellWidth.TWO_FOURTH)

fun headerCellWithLink(pageViewModel: PageViewModel, title: String, href: String) =
LinkCellViewModel(LinkViewModel(pageViewModel, title, href), true)

fun cell(title: String): TextCellViewModel = TextCellViewModel(title, false)
fun cellWithIndex(title: String): TextCellViewModel =
TextCellViewModel(title, false, greyText = false, boldText = true, oneTenthWidth = true)

TextCellViewModel(title, false, greyText = false, boldText = true)
fun cellWithLink(pageViewModel: PageViewModel, title: String, href: String) =
LinkCellViewModel(LinkViewModel(pageViewModel, title, href), false)

fun cellWithExternalLink(title: String, href: String) =
ExternalLinkCellViewModel(ExternalLinkViewModel(title, href), false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nl.avisi.structurizr.site.generatr.site.views

import kotlinx.html.*
import nl.avisi.structurizr.site.generatr.site.model.TableViewModel
import nl.avisi.structurizr.site.generatr.site.model.CellWidth

fun FlowContent.table(viewModel: TableViewModel) {
table (classes = "table is-fullwidth") {
Expand Down Expand Up @@ -36,19 +37,23 @@ private fun TBODY.row(viewModel: TableViewModel.RowViewModel) {

private fun TR.cell(viewModel: TableViewModel.CellViewModel) {
when (viewModel) {
is TableViewModel.TextCellViewModel ->
is TableViewModel.TextCellViewModel -> {
val classes = when (viewModel.width) {
CellWidth.UNSPECIFIED -> null
CellWidth.ONE_TENTH -> "is-one-tenth"
CellWidth.ONE_FOURTH -> "is-one-fourth"
CellWidth.TWO_FOURTH -> "is-two-fourth"
}

if (viewModel.isHeader && viewModel.greyText)
th { span(classes = "has-text-grey") { +viewModel.title } }
th(classes = classes) { span(classes = "has-text-grey") { +viewModel.title } }
else if (viewModel.isHeader)
th { +viewModel.title }
else if (viewModel.boldText && viewModel.oneTenthWidth)
td(classes = "is-one-tenth") { span(classes = "has-text-weight-bold") { +viewModel.title } }
th(classes = classes) { +viewModel.title }
else if (viewModel.boldText)
td { span(classes = "has-text-weight-bold") { +viewModel.title } }
else if (viewModel.oneTenthWidth)
td(classes = "is-one-tenth") { +viewModel.title }
td(classes = classes) { span(classes = "has-text-weight-bold") { +viewModel.title } }
else
td { +viewModel.title }
td(classes = classes) { +viewModel.title }
}
is TableViewModel.LinkCellViewModel ->
if (viewModel.isHeader)
th { link(viewModel.link) }
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
width: 10%;
}

.is-one-fourth {
width: 25%;
}

.is-two-fourth {
width: 50%;
}

svg a:hover {
opacity: 90%;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ class DecisionsTableViewModelTest : ViewModelTest() {
}

private fun TableViewModel.TableViewInitializerContext.decisionsTableHeaderRow() {
headerRow(headerCell("ID"), headerCell("Date"), headerCell("Status"), headerCell("Title"))
headerRow(headerCellWithIndex("ID"), headerCell("Date"), headerCell("Status"), headerCell("Title"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ class PropertiesTableViewModelTest : ViewModelTest() {
}

private fun TableViewModel.TableViewInitializerContext.propertiesTableHeaderRow() {
headerRow(headerCell("Name"), headerCell("Value"))
headerRow(headerCellWithName("Name"), headerCell("Value"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ class SoftwareSystemDependenciesPageViewModelTest : ViewModelTest() {

private fun TableViewModel.TableViewInitializerContext.dependenciesTableHeader() {
headerRow(
headerCell("System"),
headerCell("Description"),
headerCellWithName("System"),
headerCellWithDescription("Description"),
headerCell("Technology"),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SoftwareSystemsPageViewModelTest : ViewModelTest() {

assertThat(viewModel.softwareSystemsTable).isEqualTo(
TableViewModel.create {
headerRow(headerCell("Name"), headerCell("Description"))
headerRow(headerCellWithName("Name"), headerCell("Description"))
bodyRow(
headerCellWithLink(
viewModel, system1.name, SoftwareSystemPageViewModel.url(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nl.avisi.structurizr.site.generatr.site.model

import assertk.assertThat
import assertk.assertions.containsAll
import assertk.assertions.containsAtLeast
import kotlin.test.Test

Expand All @@ -28,6 +27,51 @@ class TableViewModelTest : ViewModelTest() {
)
}

@Test
fun `header row with index`() {
val viewModel = TableViewModel.create {
headerRow(headerCellWithIndex("1"))
}

assertThat(viewModel.headerRows).containsAtLeast(
TableViewModel.RowViewModel(
listOf(
TableViewModel.TextCellViewModel("1", isHeader = true, width = CellWidth.ONE_TENTH)
)
)
)
}

@Test
fun `header row with name`() {
val viewModel = TableViewModel.create {
headerRow(headerCellWithName("Name"))
}

assertThat(viewModel.headerRows).containsAtLeast(
TableViewModel.RowViewModel(
listOf(
TableViewModel.TextCellViewModel("Name", isHeader = true, width = CellWidth.ONE_FOURTH)
)
)
)
}

@Test
fun `header row with description`() {
val viewModel = TableViewModel.create {
headerRow(headerCellWithDescription("Description"))
}

assertThat(viewModel.headerRows).containsAtLeast(
TableViewModel.RowViewModel(
listOf(
TableViewModel.TextCellViewModel("Description", isHeader = true, width = CellWidth.TWO_FOURTH)
)
)
)
}

@Test
fun `header cell with grey text`() {
val viewModel = TableViewModel.create {
Expand Down Expand Up @@ -127,8 +171,7 @@ class TableViewModelTest : ViewModelTest() {
"1",
isHeader = false,
greyText = false,
boldText = true,
oneTenthWidth = true
boldText = true
)
)
)
Expand Down

0 comments on commit 304dcca

Please sign in to comment.