mapcomplete/UI/OpeningHours/OpeningHoursRange.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
2 KiB
TypeScript
Raw Normal View History

/**
* A single opening hours range, shown on top of the OH-picker table
*/
import Svg from "../../Svg"
import { Utils } from "../../Utils"
import Combine from "../Base/Combine"
import { OH, OpeningHour } from "./OpeningHours"
2021-06-10 14:05:26 +02:00
import BaseUIElement from "../BaseUIElement"
2021-06-16 14:23:53 +02:00
import { FixedUiElement } from "../Base/FixedUiElement"
2021-06-16 14:23:53 +02:00
export default class OpeningHoursRange extends BaseUIElement {
private _oh: OpeningHour
2020-10-04 01:04:46 +02:00
2021-06-16 14:23:53 +02:00
private readonly _onDelete: () => void
2020-10-04 01:04:46 +02:00
2021-06-16 14:23:53 +02:00
constructor(oh: OpeningHour, onDelete: () => void) {
super()
2020-10-04 01:04:46 +02:00
this._oh = oh
2021-06-16 14:23:53 +02:00
this._onDelete = onDelete
2020-10-04 01:04:46 +02:00
this.SetClass("oh-timerange")
2021-06-16 14:23:53 +02:00
}
2020-10-04 01:04:46 +02:00
2021-06-16 14:23:53 +02:00
InnerConstructElement(): HTMLElement {
const height = this.getHeight()
const oh = this._oh
2021-06-16 16:39:48 +02:00
const startTime = new FixedUiElement(
Utils.TwoDigits(oh.startHour) + ":" + Utils.TwoDigits(oh.startMinutes)
2022-09-08 21:40:48 +02:00
)
2021-06-16 16:39:48 +02:00
const endTime = new FixedUiElement(
Utils.TwoDigits(oh.endHour) + ":" + Utils.TwoDigits(oh.endMinutes)
2021-06-16 14:23:53 +02:00
)
2020-10-04 01:04:46 +02:00
2021-06-16 16:39:48 +02:00
const deleteRange = Svg.delete_icon_ui()
.SetClass("rounded-full w-6 h-6 block bg-black")
2021-06-16 14:23:53 +02:00
.onClick(() => {
this._onDelete()
2022-09-08 21:40:48 +02:00
})
2020-10-08 19:03:00 +02:00
2021-06-16 16:39:48 +02:00
let content: BaseUIElement
2020-10-08 19:03:00 +02:00
if (height > 2) {
content = new Combine([startTime, deleteRange, endTime]).SetClass(
"flex flex-col h-full justify-between"
2022-09-08 21:40:48 +02:00
)
2021-06-16 16:39:48 +02:00
} else {
content = new Combine([deleteRange])
.SetClass("flex flex-col h-full")
.SetStyle("flex-content: center; overflow-x: unset;")
2020-10-08 19:03:00 +02:00
}
2021-06-16 16:39:48 +02:00
const el = new Combine([content]).ConstructElement()
2021-06-16 14:23:53 +02:00
2021-06-16 16:39:48 +02:00
el.style.top = `${(100 * OH.startTime(oh)) / 24}%`
el.style.height = `${(100 * this.getHeight()) / 24}%`
2021-06-16 14:23:53 +02:00
return el
}
2020-10-04 01:04:46 +02:00
private getHeight(): number {
2021-06-16 14:23:53 +02:00
const oh = this._oh
2020-10-04 01:04:46 +02:00
let endhour = oh.endHour
if (oh.endHour == 0 && oh.endMinutes == 0) {
endhour = 24
}
2021-06-10 14:05:26 +02:00
return endhour - oh.startHour + (oh.endMinutes - oh.startMinutes) / 60
2020-10-04 01:04:46 +02:00
}
}