2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// Constants for [DatePicker] specific properties and events.
|
2021-09-07 17:36:50 +03:00
|
|
|
const (
|
2024-09-12 14:05:11 +03:00
|
|
|
// DateChangedEvent is the constant for "date-changed" property tag.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// Used by `DatePicker`.
|
|
|
|
// Occur when date picker value has been changed.
|
|
|
|
//
|
|
|
|
// General listener format:
|
|
|
|
// `func(picker rui.DatePicker, newDate, oldDate time.Time)`.
|
|
|
|
//
|
|
|
|
// where:
|
|
|
|
// picker - Interface of a date picker which generated this event,
|
|
|
|
// newDate - New date value,
|
|
|
|
// oldDate - Old date value.
|
|
|
|
//
|
|
|
|
// Allowed listener formats:
|
|
|
|
// `func(picker rui.DatePicker, newDate time.Time)`,
|
|
|
|
// `func(newDate, oldDate time.Time)`,
|
|
|
|
// `func(newDate time.Time)`,
|
|
|
|
// `func(picker rui.DatePicker)`,
|
|
|
|
// `func()`.
|
2021-09-07 17:36:50 +03:00
|
|
|
DateChangedEvent = "date-changed"
|
2024-09-12 14:05:11 +03:00
|
|
|
|
|
|
|
// DatePickerMin is the constant for "date-picker-min" property tag.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// Used by `DatePicker`.
|
|
|
|
// Minimum date value.
|
|
|
|
//
|
|
|
|
// Supported types: `time.Time`, `string`.
|
|
|
|
//
|
|
|
|
// Internal type is `time.Time`, other types converted to it during assignment.
|
|
|
|
//
|
|
|
|
// Conversion rules:
|
|
|
|
// `string` - values of this type parsed and converted to `time.Time`. The following formats are supported:
|
|
|
|
// "YYYYMMDD" - "20240102".
|
|
|
|
// "Mon-DD-YYYY" - "Jan-02-24".
|
|
|
|
// "Mon-DD-YY" - "Jan-02-2024".
|
|
|
|
// "DD-Mon-YYYY" - "02-Jan-2024".
|
|
|
|
// "YYYY-MM-DD" - "2024-01-02".
|
|
|
|
// "Month DD, YYYY" - "January 02, 2024".
|
|
|
|
// "DD Month YYYY" - "02 January 2024".
|
|
|
|
// "MM/DD/YYYY" - "01/02/2024".
|
|
|
|
// "MM/DD/YY" - "01/02/24".
|
|
|
|
// "MMDDYY" - "010224".
|
2024-09-12 14:05:11 +03:00
|
|
|
DatePickerMin = "date-picker-min"
|
|
|
|
|
|
|
|
// DatePickerMax is the constant for "date-picker-max" property tag.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// Used by `DatePicker`.
|
|
|
|
// Maximum date value.
|
|
|
|
//
|
|
|
|
// Supported types: `time.Time`, `string`.
|
|
|
|
//
|
|
|
|
// Internal type is `time.Time`, other types converted to it during assignment.
|
|
|
|
//
|
|
|
|
// Conversion rules:
|
|
|
|
// `string` - values of this type parsed and converted to `time.Time`. The following formats are supported:
|
|
|
|
// "YYYYMMDD" - "20240102".
|
|
|
|
// "Mon-DD-YYYY" - "Jan-02-24".
|
|
|
|
// "Mon-DD-YY" - "Jan-02-2024".
|
|
|
|
// "DD-Mon-YYYY" - "02-Jan-2024".
|
|
|
|
// "YYYY-MM-DD" - "2024-01-02".
|
|
|
|
// "Month DD, YYYY" - "January 02, 2024".
|
|
|
|
// "DD Month YYYY" - "02 January 2024".
|
|
|
|
// "MM/DD/YYYY" - "01/02/2024".
|
|
|
|
// "MM/DD/YY" - "01/02/24".
|
|
|
|
// "MMDDYY" - "010224".
|
2024-09-12 14:05:11 +03:00
|
|
|
DatePickerMax = "date-picker-max"
|
|
|
|
|
|
|
|
// DatePickerStep is the constant for "date-picker-step" property tag.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// Used by `DatePicker`.
|
|
|
|
// Date change step in days.
|
|
|
|
//
|
|
|
|
// Supported types: `int`, `string`.
|
|
|
|
//
|
|
|
|
// Values:
|
|
|
|
// >= `0` or >= "0" - Step value in days used to increment or decrement date.
|
2024-09-12 14:05:11 +03:00
|
|
|
DatePickerStep = "date-picker-step"
|
|
|
|
|
|
|
|
// DatePickerValue is the constant for "date-picker-value" property tag.
|
2024-09-18 13:50:06 +03:00
|
|
|
//
|
|
|
|
// Used by `DatePicker`.
|
|
|
|
// Current value.
|
|
|
|
//
|
|
|
|
// Supported types: `time.Time`, `string`.
|
|
|
|
//
|
|
|
|
// Internal type is `time.Time`, other types converted to it during assignment.
|
|
|
|
//
|
|
|
|
// Conversion rules:
|
|
|
|
// `string` - values of this type parsed and converted to `time.Time`. The following formats are supported:
|
|
|
|
// "YYYYMMDD" - "20240102".
|
|
|
|
// "Mon-DD-YYYY" - "Jan-02-24".
|
|
|
|
// "Mon-DD-YY" - "Jan-02-2024".
|
|
|
|
// "DD-Mon-YYYY" - "02-Jan-2024".
|
|
|
|
// "YYYY-MM-DD" - "2024-01-02".
|
|
|
|
// "Month DD, YYYY" - "January 02, 2024".
|
|
|
|
// "DD Month YYYY" - "02 January 2024".
|
|
|
|
// "MM/DD/YYYY" - "01/02/2024".
|
|
|
|
// "MM/DD/YY" - "01/02/24".
|
|
|
|
// "MMDDYY" - "010224".
|
2024-09-12 14:05:11 +03:00
|
|
|
DatePickerValue = "date-picker-value"
|
|
|
|
|
|
|
|
dateFormat = "2006-01-02"
|
2021-09-07 17:36:50 +03:00
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// DatePicker represent a DatePicker view
|
2021-09-07 17:36:50 +03:00
|
|
|
type DatePicker interface {
|
|
|
|
View
|
|
|
|
}
|
|
|
|
|
|
|
|
type datePickerData struct {
|
|
|
|
viewData
|
2024-05-18 18:57:41 +03:00
|
|
|
dataList
|
2023-04-23 18:47:07 +03:00
|
|
|
dateChangedListeners []func(DatePicker, time.Time, time.Time)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDatePicker create new DatePicker object and return it
|
|
|
|
func NewDatePicker(session Session, params Params) DatePicker {
|
|
|
|
view := new(datePickerData)
|
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 newDatePicker(session Session) View {
|
|
|
|
return NewDatePicker(session, nil)
|
|
|
|
}
|
|
|
|
|
2022-09-01 11:04:50 +03:00
|
|
|
func (picker *datePickerData) init(session Session) {
|
|
|
|
picker.viewData.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
picker.tag = "DatePicker"
|
2024-04-23 18:24:51 +03:00
|
|
|
picker.hasHtmlDisabled = true
|
2023-04-23 18:47:07 +03:00
|
|
|
picker.dateChangedListeners = []func(DatePicker, time.Time, time.Time){}
|
2024-05-18 18:57:41 +03:00
|
|
|
picker.dataListInit()
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
2022-05-22 12:54:02 +03:00
|
|
|
func (picker *datePickerData) String() string {
|
2024-05-02 15:07:57 +03:00
|
|
|
return getViewString(picker, nil)
|
2022-05-22 12:54:02 +03:00
|
|
|
}
|
|
|
|
|
2022-01-15 01:20:04 +03:00
|
|
|
func (picker *datePickerData) Focusable() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
func (picker *datePickerData) normalizeTag(tag string) string {
|
|
|
|
tag = strings.ToLower(tag)
|
|
|
|
switch tag {
|
|
|
|
case Type, Min, Max, Step, Value:
|
|
|
|
return "date-picker-" + tag
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *datePickerData) Remove(tag string) {
|
|
|
|
picker.remove(picker.normalizeTag(tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *datePickerData) remove(tag string) {
|
|
|
|
switch tag {
|
|
|
|
case DateChangedEvent:
|
|
|
|
if len(picker.dateChangedListeners) > 0 {
|
2023-04-23 18:47:07 +03:00
|
|
|
picker.dateChangedListeners = []func(DatePicker, time.Time, 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 DatePickerMin:
|
|
|
|
delete(picker.properties, DatePickerMin)
|
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 DatePickerMax:
|
|
|
|
delete(picker.properties, DatePickerMax)
|
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 DatePickerStep:
|
2022-05-03 18:34:26 +03:00
|
|
|
delete(picker.properties, DatePickerStep)
|
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 DatePickerValue:
|
2021-11-20 11:15:28 +03:00
|
|
|
if _, ok := picker.properties[DatePickerValue]; ok {
|
2023-04-23 18:47:07 +03:00
|
|
|
oldDate := GetDatePickerValue(picker)
|
2021-11-20 11:15:28 +03:00
|
|
|
delete(picker.properties, DatePickerValue)
|
2022-08-31 22:17:46 +03:00
|
|
|
date := GetDatePickerValue(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(), date.Format(dateFormat))
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
|
|
|
for _, listener := range picker.dateChangedListeners {
|
2023-04-23 18:47:07 +03:00
|
|
|
listener(picker, date, oldDate)
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
|
2024-05-18 18:57:41 +03:00
|
|
|
case DataList:
|
|
|
|
if len(picker.dataList.dataList) > 0 {
|
|
|
|
picker.setDataList(picker, []string{}, true)
|
|
|
|
}
|
|
|
|
|
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 *datePickerData) 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 *datePickerData) 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
|
|
|
format := "20060102"
|
|
|
|
if strings.ContainsRune(text, '-') {
|
|
|
|
if part := strings.Split(text, "-"); len(part) == 3 {
|
|
|
|
if part[0] != "" && part[0][0] > '9' {
|
|
|
|
if len(part[2]) == 2 {
|
|
|
|
format = "Jan-02-06"
|
|
|
|
} else {
|
|
|
|
format = "Jan-02-2006"
|
|
|
|
}
|
|
|
|
} else if part[1] != "" && part[1][0] > '9' {
|
|
|
|
format = "02-Jan-2006"
|
|
|
|
} else {
|
|
|
|
format = "2006-01-02"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if strings.ContainsRune(text, ' ') {
|
|
|
|
if part := strings.Split(text, " "); len(part) == 3 {
|
|
|
|
if part[0] != "" && part[0][0] > '9' {
|
|
|
|
format = "January 02, 2006"
|
|
|
|
} else {
|
|
|
|
format = "02 January 2006"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if strings.ContainsRune(text, '/') {
|
|
|
|
if part := strings.Split(text, "/"); len(part) == 3 {
|
|
|
|
if len(part[2]) == 2 {
|
|
|
|
format = "01/02/06"
|
|
|
|
} else {
|
|
|
|
format = "01/02/2006"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if len(text) == 6 {
|
|
|
|
format = "010206"
|
|
|
|
}
|
|
|
|
|
|
|
|
if date, err := time.Parse(format, text); err == nil {
|
2021-09-07 17:36:50 +03:00
|
|
|
picker.properties[tag] = value
|
|
|
|
return date, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notCompatibleType(tag, value)
|
|
|
|
return time.Now(), false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tag {
|
|
|
|
case DatePickerMin:
|
|
|
|
old, oldOK := getDateProperty(picker, DatePickerMin, Min)
|
|
|
|
if date, ok := setTimeValue(DatePickerMin); ok {
|
|
|
|
if !oldOK || date != 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, date.Format(dateFormat))
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
|
|
|
picker.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
case DatePickerMax:
|
|
|
|
old, oldOK := getDateProperty(picker, DatePickerMax, Max)
|
|
|
|
if date, ok := setTimeValue(DatePickerMax); ok {
|
|
|
|
if !oldOK || date != 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, date.Format(dateFormat))
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
|
|
|
picker.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
case DatePickerStep:
|
2022-08-31 22:17:46 +03:00
|
|
|
oldStep := GetDatePickerStep(picker)
|
2021-09-07 17:36:50 +03:00
|
|
|
if picker.setIntProperty(DatePickerStep, value) {
|
2022-08-31 22:17:46 +03:00
|
|
|
if step := GetDatePickerStep(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 DatePickerValue:
|
2022-08-31 22:17:46 +03:00
|
|
|
oldDate := GetDatePickerValue(picker)
|
2022-05-03 18:34:26 +03:00
|
|
|
if date, ok := setTimeValue(DatePickerValue); ok {
|
2021-09-07 17:36:50 +03:00
|
|
|
if date != oldDate {
|
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(), date.Format(dateFormat))
|
2021-11-20 11:15:28 +03:00
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
for _, listener := range picker.dateChangedListeners {
|
2023-04-23 18:47:07 +03:00
|
|
|
listener(picker, date, oldDate)
|
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 DateChangedEvent:
|
2023-04-23 18:47:07 +03:00
|
|
|
listeners, ok := valueToEventWithOldListeners[DatePicker, time.Time](value)
|
2022-07-27 20:31:57 +03:00
|
|
|
if !ok {
|
|
|
|
notCompatibleType(tag, value)
|
|
|
|
return false
|
|
|
|
} else if listeners == nil {
|
2023-04-23 18:47:07 +03:00
|
|
|
listeners = []func(DatePicker, time.Time, time.Time){}
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
2022-07-27 20:31:57 +03:00
|
|
|
picker.dateChangedListeners = listeners
|
2021-11-20 11:15:28 +03:00
|
|
|
picker.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
return true
|
|
|
|
|
2024-05-18 18:57:41 +03:00
|
|
|
case DataList:
|
|
|
|
return picker.setDataList(picker, value, picker.created)
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
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 *datePickerData) 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 *datePickerData) get(tag string) any {
|
2021-09-07 17:36:50 +03:00
|
|
|
switch tag {
|
|
|
|
case DateChangedEvent:
|
|
|
|
return picker.dateChangedListeners
|
|
|
|
|
2024-05-18 18:57:41 +03:00
|
|
|
case DataList:
|
|
|
|
return picker.dataList.dataList
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
default:
|
|
|
|
return picker.viewData.get(tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *datePickerData) htmlTag() string {
|
|
|
|
return "input"
|
|
|
|
}
|
|
|
|
|
2024-05-18 18:57:41 +03:00
|
|
|
func (picker *datePickerData) htmlSubviews(self View, buffer *strings.Builder) {
|
|
|
|
picker.dataListHtmlSubviews(self, buffer)
|
|
|
|
}
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
func (picker *datePickerData) htmlProperties(self View, buffer *strings.Builder) {
|
|
|
|
picker.viewData.htmlProperties(self, buffer)
|
|
|
|
|
|
|
|
buffer.WriteString(` type="date"`)
|
|
|
|
|
|
|
|
if min, ok := getDateProperty(picker, DatePickerMin, Min); ok {
|
|
|
|
buffer.WriteString(` min="`)
|
|
|
|
buffer.WriteString(min.Format(dateFormat))
|
|
|
|
buffer.WriteByte('"')
|
|
|
|
}
|
|
|
|
|
|
|
|
if max, ok := getDateProperty(picker, DatePickerMax, Max); ok {
|
|
|
|
buffer.WriteString(` max="`)
|
|
|
|
buffer.WriteString(max.Format(dateFormat))
|
|
|
|
buffer.WriteByte('"')
|
|
|
|
}
|
|
|
|
|
|
|
|
if step, ok := intProperty(picker, DatePickerStep, 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(GetDatePickerValue(picker).Format(dateFormat))
|
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)"`)
|
|
|
|
}
|
2024-05-18 18:57:41 +03:00
|
|
|
|
2024-05-29 15:31:58 +03:00
|
|
|
picker.dataListHtmlProperties(picker, buffer)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *datePickerData) handleCommand(self View, command string, data DataObject) bool {
|
|
|
|
switch command {
|
|
|
|
case "textChanged":
|
|
|
|
if text, ok := data.PropertyValue("text"); ok {
|
|
|
|
if value, err := time.Parse(dateFormat, text); err == nil {
|
2022-08-31 22:17:46 +03:00
|
|
|
oldValue := GetDatePickerValue(picker)
|
2021-09-07 17:36:50 +03:00
|
|
|
picker.properties[DatePickerValue] = value
|
|
|
|
if value != oldValue {
|
|
|
|
for _, listener := range picker.dateChangedListeners {
|
2023-04-23 18:47:07 +03:00
|
|
|
listener(picker, value, oldValue)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return picker.viewData.handleCommand(self, command, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDateProperty(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(dateFormat, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDatePickerMin returns the min date of DatePicker subview and "true" as the second value if the min date 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 GetDatePickerMin(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 getDateProperty(view, DatePickerMin, Min)
|
|
|
|
}
|
|
|
|
return time.Now(), false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDatePickerMax returns the max date of DatePicker subview and "true" as the second value if the min date 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 GetDatePickerMax(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 getDateProperty(view, DatePickerMax, Max)
|
|
|
|
}
|
|
|
|
return time.Now(), false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDatePickerStep returns the date changing step in days of DatePicker 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 GetDatePickerStep(view View, subviewID ...string) int {
|
2022-07-28 12:11:27 +03:00
|
|
|
return intStyledProperty(view, subviewID, DatePickerStep, 0)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDatePickerValue returns the date of DatePicker 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 GetDatePickerValue(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()
|
|
|
|
}
|
|
|
|
date, _ := getDateProperty(view, DatePickerValue, Value)
|
|
|
|
return date
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDateChangedListeners returns the DateChangedListener list of an DatePicker 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.
|
2023-04-23 18:47:07 +03:00
|
|
|
func GetDateChangedListeners(view View, subviewID ...string) []func(DatePicker, time.Time, time.Time) {
|
|
|
|
return getEventWithOldListeners[DatePicker, time.Time](view, subviewID, DateChangedEvent)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|