Refactor DetailsController and fix detekt issues
This commit is contained in:
parent
a4e5b9145f
commit
40222f67ed
@ -28,7 +28,7 @@ class DictionaryEntryCellFactory(private val resources: ResourceBundle, private
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class EntryCell(private val resources: ResourceBundle, private val config: Config) :
|
private class EntryCell(private val resources: ResourceBundle, private val config: Config) :
|
||||||
ListCell<DictionaryEntryFx?>() {
|
ListCell<DictionaryEntryFx?>() {
|
||||||
private val labelHeadword = Label().apply {
|
private val labelHeadword = Label().apply {
|
||||||
styleClass.add("headword")
|
styleClass.add("headword")
|
@ -50,7 +50,6 @@ private class SentenceCell(private val config: Config) : ListCell<SentenceFx?>()
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
graphic = root
|
graphic = root
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import javafx.scene.control.TabPane
|
|||||||
import javafx.scene.input.ContextMenuEvent
|
import javafx.scene.input.ContextMenuEvent
|
||||||
import javafx.scene.layout.FlowPane
|
import javafx.scene.layout.FlowPane
|
||||||
import javafx.scene.web.WebView
|
import javafx.scene.web.WebView
|
||||||
|
import kotlinx.html.DIV
|
||||||
import kotlinx.html.body
|
import kotlinx.html.body
|
||||||
import kotlinx.html.div
|
import kotlinx.html.div
|
||||||
import kotlinx.html.h1
|
import kotlinx.html.h1
|
||||||
@ -29,7 +30,7 @@ import kotlinx.html.span
|
|||||||
import kotlinx.html.stream.createHTML
|
import kotlinx.html.stream.createHTML
|
||||||
import java.util.ResourceBundle
|
import java.util.ResourceBundle
|
||||||
|
|
||||||
@Suppress("UnusedPrivateMember")
|
@Suppress("UnusedPrivateMember", "TooManyFunctions")
|
||||||
class DetailsController(private val model: Model, private val config: Config) {
|
class DetailsController(private val model: Model, private val config: Config) {
|
||||||
@FXML
|
@FXML
|
||||||
private lateinit var resources: ResourceBundle
|
private lateinit var resources: ResourceBundle
|
||||||
@ -79,7 +80,6 @@ class DetailsController(private val model: Model, private val config: Config) {
|
|||||||
@FXML
|
@FXML
|
||||||
private lateinit var labelNoSentencesFound: Label
|
private lateinit var labelNoSentencesFound: Label
|
||||||
|
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private fun initialize() {
|
private fun initialize() {
|
||||||
initializeLabelHeadword()
|
initializeLabelHeadword()
|
||||||
@ -227,30 +227,88 @@ class DetailsController(private val model: Model, private val config: Config) {
|
|||||||
model.selectedEntry.addListener { _, _, newEntry ->
|
model.selectedEntry.addListener { _, _, newEntry ->
|
||||||
if (newEntry == null) return@addListener
|
if (newEntry == null) return@addListener
|
||||||
|
|
||||||
webViewDefinition.engine.loadContent(
|
webViewDefinition.engine.loadContent(createDefinitionHtml(newEntry))
|
||||||
createHTML().html {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private fun headerOnContextMenuRequested(contextMenuEvent: ContextMenuEvent) {
|
||||||
|
if (model.selectedEntry.value == null) return
|
||||||
|
|
||||||
|
createContextMenuForEntry(model.selectedEntry.value, resources).show(
|
||||||
|
flowPaneHeader.scene.window,
|
||||||
|
contextMenuEvent.screenX,
|
||||||
|
contextMenuEvent.screenY
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("ReturnCount")
|
||||||
|
private fun lazyUpdateTabContent(selectedTabId: String?) {
|
||||||
|
when (selectedTabId) {
|
||||||
|
"tabWords" -> {
|
||||||
|
if (model.wordsContaining.isNotEmpty()) return
|
||||||
|
|
||||||
|
model.findWords()
|
||||||
|
}
|
||||||
|
|
||||||
|
"tabCharacters" -> {
|
||||||
|
if (model.characters.isNotEmpty()) return
|
||||||
|
|
||||||
|
model.findCharacters()
|
||||||
|
}
|
||||||
|
|
||||||
|
"tabSentences" -> {
|
||||||
|
if (model.sentences.isNotEmpty()) return
|
||||||
|
|
||||||
|
model.findSentences()
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createDefinitionHtml(entry: DictionaryEntryFx) = createHTML().html {
|
||||||
body {
|
body {
|
||||||
if (newEntry.cedictDefinitions.isNotEmpty()) {
|
if (entry.cedictDefinitions.isNotEmpty()) {
|
||||||
div(classes = "cedict-definition") {
|
div(classes = "cedict-definition") {
|
||||||
h1 {
|
h1 {
|
||||||
+"CC-CEDICT"
|
+"CC-CEDICT"
|
||||||
}
|
}
|
||||||
ol {
|
cedictDefinition(entry)
|
||||||
for (definition in newEntry.cedictDefinitions) {
|
|
||||||
li {
|
|
||||||
+definition.joinToString(separator = "; ")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
if (entry.crossStraitsDefinitions.isNotEmpty()) {
|
||||||
}
|
|
||||||
if (newEntry.crossStraitsDefinitions.isNotEmpty()) {
|
|
||||||
div(classes = "cross-straits-definition") {
|
div(classes = "cross-straits-definition") {
|
||||||
h1 {
|
h1 {
|
||||||
+"Cross-Straits"
|
+"Cross-Straits"
|
||||||
}
|
}
|
||||||
ol {
|
crossStraitsDefinition(entry)
|
||||||
newEntry.crossStraitsDefinitions.forEach { definition ->
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entry.moedictDefinitions.isNotEmpty()) {
|
||||||
|
div(classes = "moe-definition") {
|
||||||
|
h1 {
|
||||||
|
+"MOE"
|
||||||
|
}
|
||||||
|
moeDefinition(entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun DIV.cedictDefinition(entry: DictionaryEntryFx) = ol {
|
||||||
|
for (definition in entry.cedictDefinitions) {
|
||||||
|
li {
|
||||||
|
+definition.joinToString(separator = "; ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun DIV.crossStraitsDefinition(entry: DictionaryEntryFx) = ol {
|
||||||
|
entry.crossStraitsDefinitions.forEach { definition ->
|
||||||
li {
|
li {
|
||||||
span(classes = "definition") {
|
span(classes = "definition") {
|
||||||
+definition.definition
|
+definition.definition
|
||||||
@ -266,17 +324,10 @@ class DetailsController(private val model: Model, private val config: Config) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
private fun DIV.moeDefinition(entry: DictionaryEntryFx) =
|
||||||
if (newEntry.moedictDefinitions.isNotEmpty()) {
|
entry.moedictDefinitions.groupBy { it.type ?: "" }.entries.forEach { (type, definitions) ->
|
||||||
div(classes = "moe-definition") {
|
|
||||||
h1 {
|
|
||||||
+"MOE"
|
|
||||||
}
|
|
||||||
newEntry.moedictDefinitions.groupBy {
|
|
||||||
it.type ?: ""
|
|
||||||
}.entries.forEach { (type, definitions) ->
|
|
||||||
if (type != "") {
|
if (type != "") {
|
||||||
span(classes = "type") {
|
span(classes = "type") {
|
||||||
+type
|
+type
|
||||||
@ -317,46 +368,3 @@ class DetailsController(private val model: Model, private val config: Config) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@FXML
|
|
||||||
private fun headerOnContextMenuRequested(contextMenuEvent: ContextMenuEvent) {
|
|
||||||
if (model.selectedEntry.value == null) return
|
|
||||||
|
|
||||||
createContextMenuForEntry(model.selectedEntry.value, resources).show(
|
|
||||||
flowPaneHeader.scene.window,
|
|
||||||
contextMenuEvent.screenX,
|
|
||||||
contextMenuEvent.screenY
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun lazyUpdateTabContent(selectedTabId: String?) {
|
|
||||||
when (selectedTabId) {
|
|
||||||
"tabWords" -> {
|
|
||||||
if (model.wordsContaining.isNotEmpty()) return
|
|
||||||
|
|
||||||
model.findWords()
|
|
||||||
}
|
|
||||||
|
|
||||||
"tabCharacters" -> {
|
|
||||||
if (model.characters.isNotEmpty()) return
|
|
||||||
|
|
||||||
model.findCharacters()
|
|
||||||
}
|
|
||||||
|
|
||||||
"tabSentences" -> {
|
|
||||||
if (model.sentences.isNotEmpty()) return
|
|
||||||
|
|
||||||
model.findSentences()
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user