2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TimeChangedEvent = "time-changed"
|
|
|
|
TimePickerMin = "time-picker-min"
|
|
|
|
TimePickerMax = "time-picker-max"
|
|
|
|
TimePickerStep = "time-picker-step"
|
|
|
|
TimePickerValue = "time-picker-value"
|
2022-05-03 18:34:26 +03:00
|
|
|
timeFormat = "15:04:05"
|
2021-09-07 17:36:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// TimePicker - TimePicker view
|
|
|
|
type TimePicker interface {
|
|
|
|
View
|
|
|
|
}
|
|
|
|
|
|
|
|
type timePickerData struct {
|
|
|
|
viewData
|
|
|
|
timeChangedListeners []func(TimePicker, time.Time)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTimePicker create new TimePicker object and return it
|
|
|
|
func NewTimePicker(session Session, params Params) TimePicker {
|
|
|
|
view := new(timePickerData)
|
2022-09-01 11:04:50 +03:00
|
|
|
view.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
setInitParams(view, params)
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTimePicker(session Session) View {
|
|
|
|
return NewTimePicker(session, nil)
|
|
|
|
}
|
|
|
|
|
2022-09-01 11:04:50 +03:00
|
|
|
func (picker *timePickerData) init(session Session) {
|
|
|
|
picker.viewData.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
picker.tag = "TimePicker"
|
|
|
|
picker.timeChangedListeners = []func(TimePicker, time.Time){}
|
|
|
|
}
|
|
|
|
|
2022-05-22 12:54:02 +03:00
|
|
|
func (picker *timePickerData) String() string {
|
|
|
|
return getViewString(picker)
|
|
|
|
}
|
|
|
|
|
2022-01-15 01:20:04 +03:00
|
|
|
func (picker *timePickerData) Focusable() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
func (picker *timePickerData) normalizeTag(tag string) string {
|
|
|
|
tag = strings.ToLower(tag)
|
|
|
|
switch tag {
|
|
|
|
case Type, Min, Max, Step, Value:
|
|
|
|
return "time-picker-" + tag
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *timePickerData) Remove(tag string) {
|
|
|
|
picker.remove(picker.normalizeTag(tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *timePickerData) remove(tag string) {
|
|
|
|
switch tag {
|
|
|
|
case TimeChangedEvent:
|
|
|
|
if len(picker.timeChangedListeners) > 0 {
|
|
|
|
picker.timeChangedListeners = []func(TimePicker, time.Time){}
|
2021-11-20 11:15:28 +03:00
|
|
|
picker.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
2021-11-20 11:15:28 +03:00
|
|
|
return
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case TimePickerMin:
|
|
|
|
delete(picker.properties, TimePickerMin)
|
2021-11-20 11:15:28 +03:00
|
|
|
if picker.created {
|
2022-10-30 17:22:33 +03:00
|
|
|
picker.session.removeProperty(picker.htmlID(), Min)
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case TimePickerMax:
|
|
|
|
delete(picker.properties, TimePickerMax)
|
2021-11-20 11:15:28 +03:00
|
|
|
if picker.created {
|
2022-10-30 17:22:33 +03:00
|
|
|
picker.session.removeProperty(picker.htmlID(), Max)
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case TimePickerStep:
|
2022-05-03 18:34:26 +03:00
|
|
|
delete(picker.properties, TimePickerStep)
|
2021-11-20 11:15:28 +03:00
|
|
|
if picker.created {
|
2022-10-30 17:22:33 +03:00
|
|
|
picker.session.removeProperty(picker.htmlID(), Step)
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case TimePickerValue:
|
2021-11-20 11:15:28 +03:00
|
|
|
if _, ok := picker.properties[TimePickerValue]; ok {
|
|
|
|
delete(picker.properties, TimePickerValue)
|
2022-08-31 22:17:46 +03:00
|
|
|
time := GetTimePickerValue(picker)
|
2021-11-20 11:15:28 +03:00
|
|
|
if picker.created {
|
2022-11-02 20:10:19 +03:00
|
|
|
picker.session.callFunc("setInputValue", picker.htmlID(), time.Format(timeFormat))
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
|
|
|
for _, listener := range picker.timeChangedListeners {
|
|
|
|
listener(picker, time)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
picker.viewData.remove(tag)
|
2021-11-20 11:15:28 +03:00
|
|
|
return
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
2021-11-20 11:15:28 +03:00
|
|
|
picker.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (picker *timePickerData) Set(tag string, value any) bool {
|
2021-09-07 17:36:50 +03:00
|
|
|
return picker.set(picker.normalizeTag(tag), value)
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (picker *timePickerData) set(tag string, value any) bool {
|
2021-09-07 17:36:50 +03:00
|
|
|
if value == nil {
|
|
|
|
picker.remove(tag)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeValue := func(tag string) (time.Time, bool) {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case time.Time:
|
|
|
|
picker.properties[tag] = value
|
|
|
|
return value, true
|
|
|
|
|
|
|
|
case string:
|
|
|
|
if text, ok := picker.Session().resolveConstants(value); ok {
|
2022-06-09 13:08:39 +03:00
|
|
|
lowText := strings.ToLower(text)
|
|
|
|
pm := strings.HasSuffix(lowText, "pm") || strings.HasSuffix(lowText, "am")
|
|
|
|
|
|
|
|
var format string
|
|
|
|
switch len(strings.Split(text, ":")) {
|
|
|
|
case 2:
|
|
|
|
if pm {
|
|
|
|
format = "3:04 PM"
|
|
|
|
} else {
|
|
|
|
format = "15:04"
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
if pm {
|
|
|
|
format = "03:04:05 PM"
|
|
|
|
} else {
|
|
|
|
format = "15:04:05"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if time, err := time.Parse(format, text); err == nil {
|
2021-09-07 17:36:50 +03:00
|
|
|
picker.properties[tag] = value
|
|
|
|
return time, true
|
2022-06-09 13:08:39 +03:00
|
|
|
} else {
|
|
|
|
ErrorLog(err.Error())
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
2022-06-09 13:08:39 +03:00
|
|
|
return time.Now(), false
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notCompatibleType(tag, value)
|
|
|
|
return time.Now(), false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tag {
|
|
|
|
case TimePickerMin:
|
|
|
|
old, oldOK := getTimeProperty(picker, TimePickerMin, Min)
|
|
|
|
if time, ok := setTimeValue(TimePickerMin); ok {
|
|
|
|
if !oldOK || time != old {
|
2021-11-20 11:15:28 +03:00
|
|
|
if picker.created {
|
2022-10-30 17:22:33 +03:00
|
|
|
picker.session.updateProperty(picker.htmlID(), Min, time.Format(timeFormat))
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
|
|
|
picker.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
case TimePickerMax:
|
|
|
|
old, oldOK := getTimeProperty(picker, TimePickerMax, Max)
|
|
|
|
if time, ok := setTimeValue(TimePickerMax); ok {
|
|
|
|
if !oldOK || time != old {
|
2021-11-20 11:15:28 +03:00
|
|
|
if picker.created {
|
2022-10-30 17:22:33 +03:00
|
|
|
picker.session.updateProperty(picker.htmlID(), Max, time.Format(timeFormat))
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
|
|
|
picker.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
case TimePickerStep:
|
2022-08-31 22:17:46 +03:00
|
|
|
oldStep := GetTimePickerStep(picker)
|
2021-09-07 17:36:50 +03:00
|
|
|
if picker.setIntProperty(TimePickerStep, value) {
|
2022-08-31 22:17:46 +03:00
|
|
|
if step := GetTimePickerStep(picker); oldStep != step {
|
2021-11-20 11:15:28 +03:00
|
|
|
if picker.created {
|
|
|
|
if step > 0 {
|
2022-10-30 17:22:33 +03:00
|
|
|
picker.session.updateProperty(picker.htmlID(), Step, strconv.Itoa(step))
|
2021-11-20 11:15:28 +03:00
|
|
|
} else {
|
2022-10-30 17:22:33 +03:00
|
|
|
picker.session.removeProperty(picker.htmlID(), Step)
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
2021-11-20 11:15:28 +03:00
|
|
|
picker.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
case TimePickerValue:
|
2022-08-31 22:17:46 +03:00
|
|
|
oldTime := GetTimePickerValue(picker)
|
2022-05-03 18:34:26 +03:00
|
|
|
if time, ok := setTimeValue(TimePickerValue); ok {
|
2021-09-07 17:36:50 +03:00
|
|
|
if time != oldTime {
|
2021-11-20 11:15:28 +03:00
|
|
|
if picker.created {
|
2022-11-02 20:10:19 +03:00
|
|
|
picker.session.callFunc("setInputValue", picker.htmlID(), time.Format(timeFormat))
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
for _, listener := range picker.timeChangedListeners {
|
|
|
|
listener(picker, time)
|
|
|
|
}
|
2021-11-20 11:15:28 +03:00
|
|
|
picker.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
case TimeChangedEvent:
|
2022-07-27 20:31:57 +03:00
|
|
|
listeners, ok := valueToEventListeners[TimePicker, time.Time](value)
|
|
|
|
if !ok {
|
|
|
|
notCompatibleType(tag, value)
|
|
|
|
return false
|
|
|
|
} else if listeners == nil {
|
|
|
|
listeners = []func(TimePicker, time.Time){}
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
2022-07-27 20:31:57 +03:00
|
|
|
picker.timeChangedListeners = listeners
|
2021-11-20 11:15:28 +03:00
|
|
|
picker.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
return true
|
|
|
|
|
|
|
|
default:
|
2021-11-20 11:15:28 +03:00
|
|
|
return picker.viewData.set(tag, value)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (picker *timePickerData) Get(tag string) any {
|
2021-09-07 17:36:50 +03:00
|
|
|
return picker.get(picker.normalizeTag(tag))
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (picker *timePickerData) get(tag string) any {
|
2021-09-07 17:36:50 +03:00
|
|
|
switch tag {
|
|
|
|
case TimeChangedEvent:
|
|
|
|
return picker.timeChangedListeners
|
|
|
|
|
|
|
|
default:
|
|
|
|
return picker.viewData.get(tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *timePickerData) htmlTag() string {
|
|
|
|
return "input"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *timePickerData) htmlProperties(self View, buffer *strings.Builder) {
|
|
|
|
picker.viewData.htmlProperties(self, buffer)
|
|
|
|
|
|
|
|
buffer.WriteString(` type="time"`)
|
|
|
|
|
|
|
|
if min, ok := getTimeProperty(picker, TimePickerMin, Min); ok {
|
|
|
|
buffer.WriteString(` min="`)
|
|
|
|
buffer.WriteString(min.Format(timeFormat))
|
|
|
|
buffer.WriteByte('"')
|
|
|
|
}
|
|
|
|
|
|
|
|
if max, ok := getTimeProperty(picker, TimePickerMax, Max); ok {
|
|
|
|
buffer.WriteString(` max="`)
|
|
|
|
buffer.WriteString(max.Format(timeFormat))
|
|
|
|
buffer.WriteByte('"')
|
|
|
|
}
|
|
|
|
|
|
|
|
if step, ok := intProperty(picker, TimePickerStep, picker.Session(), 0); ok && step > 0 {
|
|
|
|
buffer.WriteString(` step="`)
|
|
|
|
buffer.WriteString(strconv.Itoa(step))
|
|
|
|
buffer.WriteByte('"')
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.WriteString(` value="`)
|
2022-08-31 22:17:46 +03:00
|
|
|
buffer.WriteString(GetTimePickerValue(picker).Format(timeFormat))
|
2021-09-07 17:36:50 +03:00
|
|
|
buffer.WriteByte('"')
|
|
|
|
|
|
|
|
buffer.WriteString(` oninput="editViewInputEvent(this)"`)
|
2022-04-15 15:41:44 +03:00
|
|
|
if picker.getRaw(ClickEvent) == nil {
|
|
|
|
buffer.WriteString(` onclick="stopEventPropagation(this, event)"`)
|
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *timePickerData) htmlDisabledProperties(self View, buffer *strings.Builder) {
|
2022-08-31 22:17:46 +03:00
|
|
|
if IsDisabled(self) {
|
2021-09-07 17:36:50 +03:00
|
|
|
buffer.WriteString(` disabled`)
|
|
|
|
}
|
|
|
|
picker.viewData.htmlDisabledProperties(self, buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *timePickerData) handleCommand(self View, command string, data DataObject) bool {
|
|
|
|
switch command {
|
|
|
|
case "textChanged":
|
|
|
|
if text, ok := data.PropertyValue("text"); ok {
|
|
|
|
if value, err := time.Parse(timeFormat, text); err == nil {
|
2022-08-31 22:17:46 +03:00
|
|
|
oldValue := GetTimePickerValue(picker)
|
2021-09-07 17:36:50 +03:00
|
|
|
picker.properties[TimePickerValue] = value
|
|
|
|
if value != oldValue {
|
|
|
|
for _, listener := range picker.timeChangedListeners {
|
|
|
|
listener(picker, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return picker.viewData.handleCommand(self, command, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTimeProperty(view View, mainTag, shortTag string) (time.Time, bool) {
|
2022-07-26 18:36:00 +03:00
|
|
|
valueToTime := func(value any) (time.Time, bool) {
|
2021-09-07 17:36:50 +03:00
|
|
|
if value != nil {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case time.Time:
|
|
|
|
return value, true
|
|
|
|
|
|
|
|
case string:
|
|
|
|
if text, ok := view.Session().resolveConstants(value); ok {
|
|
|
|
if result, err := time.Parse(timeFormat, text); err == nil {
|
|
|
|
return result, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.Now(), false
|
|
|
|
}
|
|
|
|
|
|
|
|
if view != nil {
|
|
|
|
if result, ok := valueToTime(view.getRaw(mainTag)); ok {
|
|
|
|
return result, true
|
|
|
|
}
|
|
|
|
|
2022-05-23 15:22:14 +03:00
|
|
|
if value := valueFromStyle(view, shortTag); value != nil {
|
2021-09-07 17:36:50 +03:00
|
|
|
if result, ok := valueToTime(value); ok {
|
|
|
|
return result, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Now(), false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTimePickerMin returns the min time of TimePicker subview and "true" as the second value if the min time is set,
|
|
|
|
// "false" as the second value otherwise.
|
2022-08-31 22:17:46 +03:00
|
|
|
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
|
|
|
|
func GetTimePickerMin(view View, subviewID ...string) (time.Time, bool) {
|
|
|
|
if len(subviewID) > 0 && subviewID[0] != "" {
|
|
|
|
view = ViewByID(view, subviewID[0])
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
if view != nil {
|
|
|
|
return getTimeProperty(view, TimePickerMin, Min)
|
|
|
|
}
|
|
|
|
return time.Now(), false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTimePickerMax returns the max time of TimePicker subview and "true" as the second value if the min time is set,
|
|
|
|
// "false" as the second value otherwise.
|
2022-08-31 22:17:46 +03:00
|
|
|
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
|
|
|
|
func GetTimePickerMax(view View, subviewID ...string) (time.Time, bool) {
|
|
|
|
if len(subviewID) > 0 && subviewID[0] != "" {
|
|
|
|
view = ViewByID(view, subviewID[0])
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
if view != nil {
|
|
|
|
return getTimeProperty(view, TimePickerMax, Max)
|
|
|
|
}
|
|
|
|
return time.Now(), false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTimePickerStep returns the time changing step in seconds of TimePicker subview.
|
2022-08-31 22:17:46 +03:00
|
|
|
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
|
|
|
|
func GetTimePickerStep(view View, subviewID ...string) int {
|
2022-07-28 12:11:27 +03:00
|
|
|
return intStyledProperty(view, subviewID, TimePickerStep, 60)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTimePickerValue returns the time of TimePicker subview.
|
2022-08-31 22:17:46 +03:00
|
|
|
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
|
|
|
|
func GetTimePickerValue(view View, subviewID ...string) time.Time {
|
|
|
|
if len(subviewID) > 0 && subviewID[0] != "" {
|
|
|
|
view = ViewByID(view, subviewID[0])
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
if view == nil {
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
time, _ := getTimeProperty(view, TimePickerValue, Value)
|
|
|
|
return time
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTimeChangedListeners returns the TimeChangedListener list of an TimePicker subview.
|
|
|
|
// If there are no listeners then the empty list is returned
|
2022-08-31 22:17:46 +03:00
|
|
|
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
|
|
|
|
func GetTimeChangedListeners(view View, subviewID ...string) []func(TimePicker, time.Time) {
|
2022-07-27 20:31:57 +03:00
|
|
|
return getEventListeners[TimePicker, time.Time](view, subviewID, TimeChangedEvent)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|