Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/container tabs on component view #497

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package nl.avisi.structurizr.site.generatr

import com.structurizr.Workspace
import com.structurizr.model.Container
import com.structurizr.model.Location
import com.structurizr.model.SoftwareSystem
import com.structurizr.view.ViewSet
import nl.avisi.structurizr.site.generatr.site.GeneratorContext

val Workspace.includedSoftwareSystems: List<SoftwareSystem>
get() = model.softwareSystems.filter {
Expand All @@ -14,6 +14,8 @@ val Workspace.includedSoftwareSystems: List<SoftwareSystem>

fun Workspace.hasImageViews(id: String) = views.imageViews.any { it.elementId == id }

fun Workspace.hasComponentDiagrams(container: Container) = views.componentViews.any { it.container == container}

val SoftwareSystem.hasContainers
get() = this.containers.isNotEmpty()

Expand All @@ -23,6 +25,12 @@ val SoftwareSystem.includedProperties
val Container.hasComponents
get() = this.components.isNotEmpty()

fun SoftwareSystem.firstContainerName(generatorContext: GeneratorContext) = containers
.firstOrNull { container ->
generatorContext.workspace.hasComponentDiagrams(container) or
generatorContext.workspace.hasImageViews(container.id) }
?.name?.normalize()

fun SoftwareSystem.hasDecisions() = documentation.decisions.isNotEmpty()

fun SoftwareSystem.hasContainerDecisions() = containers.any { it.hasDecisions() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private class WriterWithElementLinks(
private fun getUrlToElement(element: Element?, page: String? = null): String {
val path = when (element) {
is SoftwareSystem -> "/${element.name?.normalize()}/${page?.let { page } ?: "container"}/".asUrlToDirectory(url)
is Container -> "/${element.parent?.name?.normalize()}/${page?.let { page } ?: "component"}/".asUrlToDirectory(url)
is Container -> "/${element.parent?.name?.normalize()}/${page?.let { page } ?: "component"}/${element.name?.normalize()}".asUrlToDirectory(url)
is Component -> "/${element.parent?.parent?.name?.normalize()}/${page?.let { page } ?: "code"}/".asUrlToDirectory(url)
else -> throw IllegalStateException("Not supported element")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ private fun generateHtmlFiles(context: GeneratorContext, branchDir: File) {
add { writeHtmlFile(branchDir, SoftwareSystemHomePageViewModel(context, it)) }
add { writeHtmlFile(branchDir, SoftwareSystemContextPageViewModel(context, it)) }
add { writeHtmlFile(branchDir, SoftwareSystemContainerPageViewModel(context, it)) }
add { writeHtmlFile(branchDir, SoftwareSystemComponentPageViewModel(context, it)) }
add { writeHtmlFile(branchDir, SoftwareSystemCodePageViewModel(context, it)) }
add { writeHtmlFile(branchDir, SoftwareSystemDynamicPageViewModel(context, it)) }
add { writeHtmlFile(branchDir, SoftwareSystemDeploymentPageViewModel(context, it)) }
Expand Down Expand Up @@ -174,6 +173,13 @@ private fun generateHtmlFiles(context: GeneratorContext, branchDir: File) {
}
}

it.containers
.filter { container ->
context.workspace.views.componentViews.any { containerView -> containerView.container == container } or
context.workspace.views.imageViews.any { imageView -> imageView.elementId in container.id } }
.forEach { container ->
add { writeHtmlFile(branchDir, SoftwareSystemContainerComponentsPageViewModel(context, container)) } }

it.documentation.sections.filter { section -> section.order != 1 }.forEach { section ->
add { writeHtmlFile(branchDir, SoftwareSystemSectionPageViewModel(context, it, section)) }
}
Expand All @@ -198,7 +204,7 @@ private fun writeHtmlFile(exportDir: File, viewModel: PageViewModel) {
is SoftwareSystemContainerDecisionsPageViewModel -> softwareSystemContainerDecisionsPage(viewModel)
is SoftwareSystemContainerSectionPageViewModel -> softwareSystemContainerSectionPage(viewModel)
is SoftwareSystemContainerSectionsPageViewModel -> softwareSystemContainerSectionsPage(viewModel)
is SoftwareSystemComponentPageViewModel -> softwareSystemComponentPage(viewModel)
is SoftwareSystemContainerComponentsPageViewModel -> softwareSystemContainerComponentsPage(viewModel)
is SoftwareSystemCodePageViewModel -> softwareSystemCodePage(viewModel)
is SoftwareSystemDynamicPageViewModel -> softwareSystemDynamicPage(viewModel)
is SoftwareSystemDeploymentPageViewModel -> softwareSystemDeploymentPage(viewModel)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package nl.avisi.structurizr.site.generatr.site.model

data class ContainerTabViewModel(val pageViewModel: SoftwareSystemPageViewModel, val title: String, val url: String) {
val link = LinkViewModel(pageViewModel, title, url)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nl.avisi.structurizr.site.generatr.site.model

import com.structurizr.model.SoftwareSystem
import nl.avisi.structurizr.site.generatr.hasComponentDiagrams
import nl.avisi.structurizr.site.generatr.hasImageViews
import nl.avisi.structurizr.site.generatr.site.GeneratorContext

fun SoftwareSystemPageViewModel.createContainersTabViewModel(
generatorContext: GeneratorContext,
softwareSystem: SoftwareSystem,
) = buildList {
softwareSystem
.containers
.filter { container ->
generatorContext.workspace.hasComponentDiagrams(container) or
generatorContext.workspace.hasImageViews(container.id) }
.map {
ContainerTabViewModel(
this@createContainersTabViewModel,
it.name,
SoftwareSystemContainerComponentsPageViewModel.url(it)
)
}
.forEach { add(it) }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package nl.avisi.structurizr.site.generatr.site.model

data class DecisionTabViewModel(val pageViewModel: SoftwareSystemPageViewModel, val title: String, val url: String) {
val link = LinkViewModel(pageViewModel, title, url, true)
val link = LinkViewModel(pageViewModel, title, url)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ data class LinkViewModel(
private val pageViewModel: PageViewModel,
val title: String,
val href: String,
val exact: Boolean = true
val match: Match = Match.EXACT
) {
val relativeHref get() = href.asUrlToDirectory(pageViewModel.url)
val active get() = if (exact) isHrefOfContainingPage else isChildHrefOfContainingPage
val active get() =
when (match) {
Match.EXACT -> isHrefOfContainingPage
Match.CHILD -> isChildHrefOfContainingPage
Match.SIBLING -> isSiblingHrefOfContainingPage
}

private val isHrefOfContainingPage get() = href == pageViewModel.url
private val isChildHrefOfContainingPage get() = pageViewModel.url == href || pageViewModel.url.startsWith("$href/")
private val isSiblingHrefOfContainingPage get() = pageViewModel.url.trimEnd('/').dropLastWhile { it != '/' } == href.trimEnd('/').dropLastWhile { it != '/' }
}
enum class Match {
EXACT,
CHILD,
SIBLING
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class MenuViewModel(generatorContext: GeneratorContext, private val pageViewMode
yield(createMenuItem("Home", HomePageViewModel.url()))

if (generatorContext.workspace.documentation.decisions.isNotEmpty())
yield(createMenuItem("Decisions", WorkspaceDecisionsPageViewModel.url(), false))
yield(createMenuItem("Decisions", WorkspaceDecisionsPageViewModel.url(), Match.CHILD))

if (generatorContext.workspace.model.softwareSystems.isNotEmpty())
yield(createMenuItem("Software Systems", SoftwareSystemsPageViewModel.url()))
Expand All @@ -21,7 +21,7 @@ class MenuViewModel(generatorContext: GeneratorContext, private val pageViewMode
val softwareSystemItems = pageViewModel.includedSoftwareSystems
.sortedBy { it.name.lowercase() }
.map {
createMenuItem(it.name, SoftwareSystemPageViewModel.url(it, SoftwareSystemPageViewModel.Tab.HOME), false)
createMenuItem(it.name, SoftwareSystemPageViewModel.url(it, SoftwareSystemPageViewModel.Tab.HOME), Match.CHILD)
}

private val groupSeparator = generatorContext.workspace.model.properties["structurizr.groupSeparator"] ?: "/"
Expand All @@ -30,8 +30,8 @@ class MenuViewModel(generatorContext: GeneratorContext, private val pageViewMode
.map { "${it.group ?: ""}$groupSeparator${it.name}" }
.sortedBy { it.lowercase() }

private fun createMenuItem(title: String, href: String, exact: Boolean = true) =
LinkViewModel(pageViewModel, title, href, exact)
private fun createMenuItem(title: String, href: String, match: Match = Match.EXACT) =
LinkViewModel(pageViewModel, title, href, match)

fun softwareSystemNodes(): MenuNodeViewModel {
data class MutableMenuNode(val name: String, val children: MutableList<MutableMenuNode>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package nl.avisi.structurizr.site.generatr.site.model

data class SectionTabViewModel(val pageViewModel: SoftwareSystemPageViewModel, val title: String, val url: String) {
val link = LinkViewModel(pageViewModel, title, url, true)
val link = LinkViewModel(pageViewModel, title, url)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nl.avisi.structurizr.site.generatr.site.model

import com.structurizr.model.Container
import nl.avisi.structurizr.site.generatr.normalize
import nl.avisi.structurizr.site.generatr.site.GeneratorContext

class SoftwareSystemContainerComponentsPageViewModel(generatorContext: GeneratorContext, container: Container) :
SoftwareSystemPageViewModel(generatorContext, container.softwareSystem, Tab.COMPONENT) {
override val url = url(container)
val diagrams = generatorContext.workspace.views.componentViews
.filter { it.container == container }
.sortedBy { it.key }
.map { DiagramViewModel.forView(this, it, generatorContext.svgFactory) }
val images = generatorContext.workspace.views.imageViews
.filter { it.elementId in container.id }
.sortedBy { it.key }
.map { ImageViewViewModel(it) }

val visible = diagrams.isNotEmpty() or images.isNotEmpty()
val containerTabs = createContainersTabViewModel(generatorContext, container.softwareSystem)
companion object {
fun url(container: Container) = "${url(container.softwareSystem, Tab.COMPONENT)}/${container.name.normalize()}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ open class SoftwareSystemPageViewModel(
) : PageViewModel(generatorContext) {
enum class Tab { HOME, SYSTEM_CONTEXT, CONTAINER, COMPONENT, CODE, DYNAMIC, DEPLOYMENT, DEPENDENCIES, DECISIONS, SECTIONS }

inner class TabViewModel(val tab: Tab, exactLink: Boolean = true) {
val link = LinkViewModel(this@SoftwareSystemPageViewModel, title, url(softwareSystem, tab), exactLink)
inner class TabViewModel(val tab: Tab, match: Match = Match.EXACT) {
val link = when (tab) {
Tab.COMPONENT -> LinkViewModel(
this@SoftwareSystemPageViewModel,
title,
"${url(softwareSystem, tab)}/${softwareSystem.firstContainerName(generatorContext)}",
match
)
else -> LinkViewModel(this@SoftwareSystemPageViewModel, title, url(softwareSystem, tab), match)
}

private val title
get() = when (tab) {
Expand Down Expand Up @@ -50,13 +58,13 @@ open class SoftwareSystemPageViewModel(
TabViewModel(Tab.HOME),
TabViewModel(Tab.SYSTEM_CONTEXT),
TabViewModel(Tab.CONTAINER),
TabViewModel(Tab.COMPONENT),
TabViewModel(Tab.COMPONENT, Match.SIBLING),
TabViewModel(Tab.CODE),
TabViewModel(Tab.DYNAMIC),
TabViewModel(Tab.DEPLOYMENT),
TabViewModel(Tab.DEPENDENCIES),
TabViewModel(Tab.DECISIONS, exactLink = false),
TabViewModel(Tab.SECTIONS, exactLink = false)
TabViewModel(Tab.DECISIONS, Match.CHILD),
TabViewModel(Tab.SECTIONS, Match.CHILD)
)

val description: String = softwareSystem.description
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package nl.avisi.structurizr.site.generatr.site.views

import kotlinx.html.HTML
import kotlinx.html.div
import kotlinx.html.li
import kotlinx.html.ul
import nl.avisi.structurizr.site.generatr.site.model.SoftwareSystemContainerComponentsPageViewModel

fun HTML.softwareSystemContainerComponentsPage(viewModel: SoftwareSystemContainerComponentsPageViewModel) {
if (viewModel.visible) {
softwareSystemPage(viewModel) {
div(classes = "tabs") {
ul(classes = "m-0 is-flex-wrap-wrap is flex-shrink-1 is flex-grow-0") {
viewModel.containerTabs
.forEach {
li(classes = if (it.link.active) "is-active" else null) {
link(it.link)
}
}
}
}
viewModel.diagrams.forEach { diagram(it) }
viewModel.images.forEach { image(it) }
}
} else
redirectUpPage()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.isEqualTo
import com.structurizr.Workspace
import com.structurizr.model.Location
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.TestFactory
import kotlin.test.Test
Expand Down Expand Up @@ -142,7 +141,7 @@ class PlantUmlExporterTest {
assertThat(diagram.definition.withoutC4HeaderAndFooter()).isEqualTo(
"""
System_Boundary("System1_boundary", "System 1", ${'$'}tags="") {
Container(System1.Container1, "Container 1", ${'$'}techn="", ${'$'}descr="", ${'$'}tags="", ${'$'}link="../system-1/component/")
Container(System1.Container1, "Container 1", ${'$'}techn="", ${'$'}descr="", ${'$'}tags="", ${'$'}link="../system-1/component/container-1/")
}
""".trimIndent()
)
Expand All @@ -158,7 +157,7 @@ class PlantUmlExporterTest {
assertThat(diagram.definition.withoutStructurizrHeaderAndFooter()).isEqualTo(
"""
rectangle "System 1\n<size:10>[Software System]</size>" <<System1>> {
rectangle "==Container 1\n<size:10>[Container]</size>" <<System1.Container1>> as System1.Container1 [[../system-1/component/]]
rectangle "==Container 1\n<size:10>[Container]</size>" <<System1.Container1>> as System1.Container1 [[../system-1/component/container-1/]]
}
""".trimIndent()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ class LinkViewModelTest : ViewModelTest() {
@ValueSource(strings = ["/some-page", "/some-page/1", "/some-page/subpage/subsubpage"])
fun `non-exact links are active when the page url matches partially`(pageHref: String) {
val pageViewModel = pageViewModel(pageHref)
val viewModel = LinkViewModel(pageViewModel, "Some page", "/some-page", false)
val viewModel = LinkViewModel(pageViewModel, "Some page", "/some-page", Match.CHILD)
assertThat(viewModel.active).isTrue()
}

@ParameterizedTest
@ValueSource(strings = ["/", "/some-other-page/1", "/some-other-page/subpage/subsubpage"])
fun `non-exact links are not active when the page url doesn't match partially`(pageHref: String) {
val pageViewModel = pageViewModel(pageHref)
val viewModel = LinkViewModel(pageViewModel, "Some page", "/some-page", false)
val viewModel = LinkViewModel(pageViewModel, "Some page", "/some-page", Match.CHILD)
assertThat(viewModel.active).isFalse()
}

@Test
fun `non-exact links are only active when page url is a subdirectory of the link`() {
val expectInactivePartialMatch = LinkViewModel(pageViewModel("/page-two"), "Some page", "/page", false)
val expectActivePartialMatch = LinkViewModel(pageViewModel("/page/two"), "Some page", "/page", false)
val expectInactivePartialMatch = LinkViewModel(pageViewModel("/page-two"), "Some page", "/page", Match.CHILD)
val expectActivePartialMatch = LinkViewModel(pageViewModel("/page/two"), "Some page", "/page", Match.CHILD)
assertThat(expectInactivePartialMatch.active).isFalse()
assertThat(expectActivePartialMatch.active).isTrue()
}
Expand All @@ -54,4 +54,28 @@ class LinkViewModelTest : ViewModelTest() {
val viewModel = LinkViewModel(pageViewModel, "Some other page", "/some-other-page")
assertThat(viewModel.relativeHref).isEqualTo("../../some-other-page/")
}

@ParameterizedTest
@ValueSource(strings = ["/some-page/sibling-page-1/", "/some-page/sibling-page-2/"])
fun `sibling links are active when the previous url path matches`(pageHref: String) {
val pageViewModel = pageViewModel(pageHref)
val viewModel = LinkViewModel(pageViewModel, "Some page", "/some-page/sibling-page-3/", Match.SIBLING)
assertThat(viewModel.active).isTrue()
}

@ParameterizedTest
@ValueSource(strings = ["/some-other-page/sibling-page-1/", "/some-other-page/sibling-page-2/"])
fun `sibling links are not active when the previous url path doesn't match`(pageHref: String) {
val pageViewModel = pageViewModel(pageHref)
val viewModel = LinkViewModel(pageViewModel, "Some page", "/some-page/sibling-page-1", Match.SIBLING)
assertThat(viewModel.active).isFalse()
}

@Test
fun `sibling links are only active when previous page url path matches previous href path url`() {
val expectInactiveSiblingMatch = LinkViewModel(pageViewModel("/page/two/"), "Some page", "/some-page/", Match.SIBLING)
val expectActiveSiblingMatch = LinkViewModel(pageViewModel("/page/two"), "Some page", "/page/one", Match.SIBLING)
assertThat(expectInactiveSiblingMatch.active).isFalse()
assertThat(expectActiveSiblingMatch.active).isTrue()
}
}
Loading
Loading