Skip to content

Commit 5a75024

Browse files
committed
feat(model-server): add delete branch button in RepositoryOverview
1 parent b2aae13 commit 5a75024

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

model-server/src/main/kotlin/org/modelix/model/server/handlers/ui/RepositoryOverview.kt

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import io.ktor.server.routing.routing
99
import kotlinx.html.FlowContent
1010
import kotlinx.html.FlowOrInteractiveOrPhrasingContent
1111
import kotlinx.html.a
12+
import kotlinx.html.button
1213
import kotlinx.html.form
1314
import kotlinx.html.h1
1415
import kotlinx.html.i
1516
import kotlinx.html.onClick
1617
import kotlinx.html.p
1718
import kotlinx.html.postButton
19+
import kotlinx.html.script
1820
import kotlinx.html.span
1921
import kotlinx.html.table
2022
import kotlinx.html.tbody
@@ -23,6 +25,8 @@ import kotlinx.html.th
2325
import kotlinx.html.thead
2426
import kotlinx.html.title
2527
import kotlinx.html.tr
28+
import kotlinx.html.unsafe
29+
import org.modelix.model.lazy.RepositoryId
2630
import org.modelix.model.server.handlers.IRepositoriesManager
2731
import org.modelix.model.server.templates.PageWithMenuBar
2832

@@ -32,7 +36,21 @@ class RepositoryOverview(private val repoManager: IRepositoriesManager) {
3236
application.routing {
3337
get("/repos") {
3438
call.respondHtmlTemplate(PageWithMenuBar("repos/", "..")) {
35-
headContent { title("Repositories") }
39+
headContent {
40+
title("Repositories")
41+
script(type = "text/javascript") {
42+
unsafe {
43+
+"""
44+
function removeBranch(repository, branch) {
45+
if (confirm('Are you sure you want to delete the branch' + branch +' of repository' +repository + '?')) {
46+
fetch('../v2/repositories/' + repository + '/branches/' + branch, { method: 'DELETE'})
47+
.then( _ => location.reload())
48+
}
49+
}
50+
""".trimIndent()
51+
}
52+
}
53+
}
3654
bodyContent { buildMainPage() }
3755
}
3856
}
@@ -89,10 +107,13 @@ class RepositoryOverview(private val repoManager: IRepositoriesManager) {
89107
}
90108
}
91109
td {
92-
buildHistoryLink(branch.repositoryId.id, branch.branchName)
110+
buildHistoryLink(repository.id, branch.branchName)
93111
}
94112
td {
95-
buildExploreLatestLink(branch.repositoryId.id, branch.branchName)
113+
buildExploreLatestLink(repository.id, branch.branchName)
114+
}
115+
td {
116+
buildDeleteBranchButton(repository.id, branch.branchName)
96117
}
97118
}
98119
}
@@ -104,19 +125,19 @@ class RepositoryOverview(private val repoManager: IRepositoriesManager) {
104125
}
105126
}
106127

107-
fun FlowOrInteractiveOrPhrasingContent.buildHistoryLink(repositoryId: String, branchName: String) {
128+
internal fun FlowOrInteractiveOrPhrasingContent.buildHistoryLink(repositoryId: String, branchName: String) {
108129
a("../history/${repositoryId.encodeURLPathPart()}/${branchName.encodeURLPathPart()}/") {
109130
+"Show History"
110131
}
111132
}
112133

113-
fun FlowOrInteractiveOrPhrasingContent.buildExploreLatestLink(repositoryId: String, branchName: String) {
134+
internal fun FlowOrInteractiveOrPhrasingContent.buildExploreLatestLink(repositoryId: String, branchName: String) {
114135
a("../content/repositories/${repositoryId.encodeURLPathPart()}/branches/${branchName.encodeURLPathPart()}/latest/") {
115136
+"Explore Latest Version"
116137
}
117138
}
118139

119-
fun FlowContent.buildDeleteRepositoryForm(repositoryId: String) {
140+
internal fun FlowContent.buildDeleteRepositoryForm(repositoryId: String) {
120141
form {
121142
postButton {
122143
name = "delete"
@@ -126,3 +147,11 @@ fun FlowContent.buildDeleteRepositoryForm(repositoryId: String) {
126147
}
127148
}
128149
}
150+
151+
internal fun FlowContent.buildDeleteBranchButton(repositoryId: String, branchName: String) {
152+
if (branchName == RepositoryId.DEFAULT_BRANCH) return
153+
button {
154+
onClick = "return removeBranch('${repositoryId.encodeURLPathPart()}', '${branchName.encodeURLPathPart()}')"
155+
+"Delete Branch"
156+
}
157+
}

0 commit comments

Comments
 (0)