You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
4.3 KiB
129 lines
4.3 KiB
package nova.monadic_sfx.ui.components.todo
|
|
|
|
import monix.bio.Task
|
|
import monix.execution.cancelables.CompositeCancelable
|
|
import monix.{eval => me}
|
|
import nova.monadic_sfx.util.controls.FontIcon
|
|
import nova.monadic_sfx.util.controls.IconLiteral
|
|
import nova.monadic_sfx.util.controls.JFXButton
|
|
import nova.monadic_sfx.util.controls.JFXListView
|
|
import nova.monadic_sfx.util.controls.JFXTextField
|
|
import nova.monadic_sfx.util.controls.MenuItem
|
|
import nova.monadic_sfx.implicits._
|
|
import nova.monadic_sfx.util.reactive.store._
|
|
import org.gerweck.scalafx.util._
|
|
import scalafx.Includes._
|
|
import scalafx.beans.property.ObjectProperty
|
|
import scalafx.beans.property.StringProperty
|
|
import scalafx.geometry.Insets
|
|
import scalafx.scene.Parent
|
|
import scalafx.scene.control.ContextMenu
|
|
import scalafx.scene.control.ListCell
|
|
import scalafx.scene.control.SelectionMode
|
|
import scalafx.scene.layout.HBox
|
|
import scalafx.scene.text.Text
|
|
|
|
object TodoListView {
|
|
def apply(
|
|
store: Store[TodoListStore.Action, TodoListStore.State]
|
|
): Task[Parent] =
|
|
Task.deferAction(implicit s =>
|
|
Task {
|
|
val cc = CompositeCancelable()
|
|
val todos = store.map { case (_, state) => state.todos }
|
|
val _selectedItems = ObjectProperty(Seq.empty[Todo])
|
|
|
|
new HBox {
|
|
padding = Insets(5)
|
|
val _content = StringProperty("")
|
|
children = Seq(
|
|
new JFXTextField {
|
|
text ==> _content
|
|
},
|
|
new JFXListView[Todo] {
|
|
def selectedItems = selectionModel().selectedItems.view
|
|
|
|
selectionModel().selectionMode = SelectionMode.Multiple
|
|
selectionModel().selectedItems.observableSeqValue ==> _selectedItems
|
|
|
|
cc += items <-- todos
|
|
|
|
val emptyCell = ObjectProperty(new HBox)
|
|
cellFactory = _ =>
|
|
new ListCell[Todo] {
|
|
val _text = StringProperty("")
|
|
val _graphic = ObjectProperty(
|
|
new HBox {
|
|
children = Seq(
|
|
new FontIcon {
|
|
iconSize = 10
|
|
iconLiteral = IconLiteral.Gmi10k
|
|
},
|
|
new Text {
|
|
text <== _text
|
|
}
|
|
)
|
|
}
|
|
)
|
|
|
|
item.asOption.map(
|
|
_.fold("")(todo => s"${todo.id} - ${todo.content}")
|
|
) ==> _text
|
|
|
|
graphic <== item.asOption.flatMap(
|
|
_.fold(emptyCell)(_ => _graphic)
|
|
)
|
|
|
|
}
|
|
|
|
contextMenu = new ContextMenu {
|
|
items ++= Seq(
|
|
new MenuItem {
|
|
text = "Add"
|
|
// obsAction.useLazyEval(TodoListStore.Add("blah3")) --> store
|
|
},
|
|
new MenuItem {
|
|
text = "Delete"
|
|
obsAction.useIterableEval(_ =>
|
|
selectedItems
|
|
.map(todo => TodoListStore.Delete(todo.id))
|
|
.toList
|
|
) --> store
|
|
|
|
// obsAction.split(
|
|
// _.useLazyEval(me.Task(TodoListStore.Delete(0))) --> store,
|
|
// _.useLazyEval(me.Task(TodoListStore.Delete(0))) --> store,
|
|
// _.useLazyEval(me.Task(TodoListStore.Delete(0))) --> store
|
|
// )
|
|
},
|
|
new MenuItem {
|
|
text = "Edit"
|
|
}
|
|
)
|
|
}
|
|
},
|
|
new JFXButton {
|
|
text = "Add"
|
|
// disable <== _selectedItems.map(_.length > 0)
|
|
obsAction
|
|
.useLazyEval(me.Task(TodoListStore.Add(_content()))) --> store
|
|
},
|
|
new JFXButton {
|
|
text = "Edit"
|
|
disable <== _selectedItems.map(_.length > 1)
|
|
obsAction.useLazyEval(
|
|
me.Task(
|
|
TodoListStore.Edit(
|
|
_selectedItems
|
|
.map(_.headOption.map(_.id).getOrElse(-1))
|
|
.value,
|
|
_content()
|
|
)
|
|
)
|
|
) --> store
|
|
}
|
|
)
|
|
}
|
|
}
|
|
)
|
|
}
|