Add auto constraint check

This commit is contained in:
Lars Westermann 2019-06-11 15:57:39 +02:00
parent e7bdaabca8
commit 7d3a279ff1
Signed by: lars.westermann
GPG key ID: 9D417FA5BB9D5E1D
4 changed files with 76 additions and 26 deletions

View file

@ -49,6 +49,26 @@ class Calendar(calendar: HTMLElement) : View(calendar) {
Orientation.ROOM_TO_TIME
}
private var autoCheck: Boolean = false
fun autoCheck() {
if (autoCheck) checkConstraints()
}
private fun checkConstraints() {
launch {
val errors = ScheduleRepository.checkConstraints()
for ((s, l) in errors.map) {
for (entry in body.calendarEntries) {
if (entry.scheduleId == s) {
entry.setError(l)
}
}
}
}
}
init {
scroll += calendarTable
@ -56,18 +76,29 @@ class Calendar(calendar: HTMLElement) : View(calendar) {
CalendarEdit(this, calendar.querySelector(".calendar-edit") as HTMLElement)
}
(document.getElementById("calendar-check-constraints") as? HTMLElement)?.let { wrap(it) }
?.onClick?.addListener {
launch {
val errors = ScheduleRepository.checkConstraints()
val checkConstraintsBtn =
(document.getElementById("calendar-check-constraints") as? HTMLElement)?.let { wrap(it) }
if (checkConstraintsBtn != null) {
checkConstraintsBtn.onClick.addListener {
checkConstraints()
}
}
for ((s, l) in errors.map) {
val autoCheckConstraintsBtn =
(document.getElementById("calendar-auto-check-constraints") as? HTMLElement)?.let { wrap(it) }
if (autoCheckConstraintsBtn != null) {
autoCheckConstraintsBtn.onClick.addListener {
if (autoCheck) {
for (entry in body.calendarEntries) {
if (entry.scheduleId == s) {
entry.setError(l)
}
entry.setError(emptyList())
}
autoCheck = false
} else {
autoCheck = true
autoCheck()
}
autoCheckConstraintsBtn.classList["btn-primary"] = autoCheck
}
}

View file

@ -243,6 +243,8 @@ class CalendarEntry(private val calendar: CalendarBody, view: HTMLElement) : Vie
if (cell != null && cell.html != html.parentElement) {
cell += this
}
calendar.calendar.autoCheck()
}
fun load(workGroup: WorkGroup) {