2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ColorChangedEvent = "color-changed"
|
|
|
|
ColorPickerValue = "color-picker-value"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ColorPicker - ColorPicker view
|
|
|
|
type ColorPicker interface {
|
|
|
|
View
|
|
|
|
}
|
|
|
|
|
|
|
|
type colorPickerData struct {
|
|
|
|
viewData
|
|
|
|
colorChangedListeners []func(ColorPicker, Color)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewColorPicker create new ColorPicker object and return it
|
|
|
|
func NewColorPicker(session Session, params Params) ColorPicker {
|
|
|
|
view := new(colorPickerData)
|
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 newColorPicker(session Session) View {
|
|
|
|
return NewColorPicker(session, nil)
|
|
|
|
}
|
|
|
|
|
2022-09-01 11:04:50 +03:00
|
|
|
func (picker *colorPickerData) init(session Session) {
|
|
|
|
picker.viewData.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
picker.tag = "ColorPicker"
|
|
|
|
picker.colorChangedListeners = []func(ColorPicker, Color){}
|
|
|
|
picker.properties[Padding] = Px(0)
|
|
|
|
}
|
|
|
|
|
2022-05-22 12:54:02 +03:00
|
|
|
func (picker *colorPickerData) String() string {
|
|
|
|
return getViewString(picker)
|
|
|
|
}
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
func (picker *colorPickerData) normalizeTag(tag string) string {
|
|
|
|
tag = strings.ToLower(tag)
|
|
|
|
switch tag {
|
2021-10-04 18:05:57 +03:00
|
|
|
case Value, ColorTag:
|
2021-09-07 17:36:50 +03:00
|
|
|
return ColorPickerValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *colorPickerData) Remove(tag string) {
|
|
|
|
picker.remove(picker.normalizeTag(tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *colorPickerData) remove(tag string) {
|
|
|
|
switch tag {
|
|
|
|
case ColorChangedEvent:
|
2021-11-20 11:15:28 +03:00
|
|
|
if len(picker.colorChangedListeners) > 0 {
|
|
|
|
picker.colorChangedListeners = []func(ColorPicker, Color){}
|
|
|
|
picker.propertyChangedEvent(tag)
|
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case ColorPickerValue:
|
2022-08-31 22:17:46 +03:00
|
|
|
oldColor := GetColorPickerValue(picker)
|
2021-09-07 17:36:50 +03:00
|
|
|
delete(picker.properties, ColorPickerValue)
|
|
|
|
picker.colorChanged(oldColor)
|
|
|
|
|
|
|
|
default:
|
|
|
|
picker.viewData.remove(tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (picker *colorPickerData) 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 *colorPickerData) set(tag string, value any) bool {
|
2021-09-07 17:36:50 +03:00
|
|
|
if value == nil {
|
|
|
|
picker.remove(tag)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tag {
|
|
|
|
case ColorChangedEvent:
|
2022-07-27 20:31:57 +03:00
|
|
|
listeners, ok := valueToEventListeners[ColorPicker, Color](value)
|
|
|
|
if !ok {
|
|
|
|
notCompatibleType(tag, value)
|
|
|
|
return false
|
|
|
|
} else if listeners == nil {
|
|
|
|
listeners = []func(ColorPicker, Color){}
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
2022-07-27 20:31:57 +03:00
|
|
|
picker.colorChangedListeners = listeners
|
2021-11-20 11:15:28 +03:00
|
|
|
picker.propertyChangedEvent(tag)
|
2021-09-07 17:36:50 +03:00
|
|
|
return true
|
|
|
|
|
|
|
|
case ColorPickerValue:
|
2022-08-31 22:17:46 +03:00
|
|
|
oldColor := GetColorPickerValue(picker)
|
2021-09-07 17:36:50 +03:00
|
|
|
if picker.setColorProperty(ColorPickerValue, value) {
|
2021-11-20 11:15:28 +03:00
|
|
|
picker.colorChanged(oldColor)
|
2021-09-07 17:36:50 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return picker.viewData.set(tag, value)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *colorPickerData) colorChanged(oldColor Color) {
|
2022-08-31 22:17:46 +03:00
|
|
|
if newColor := GetColorPickerValue(picker); oldColor != newColor {
|
2021-11-20 11:15:28 +03:00
|
|
|
if picker.created {
|
|
|
|
picker.session.runScript(fmt.Sprintf(`setInputValue('%s', '%s')`, picker.htmlID(), newColor.rgbString()))
|
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
for _, listener := range picker.colorChangedListeners {
|
|
|
|
listener(picker, newColor)
|
|
|
|
}
|
2021-11-20 11:15:28 +03:00
|
|
|
picker.propertyChangedEvent(ColorTag)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (picker *colorPickerData) 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 *colorPickerData) get(tag string) any {
|
2021-09-07 17:36:50 +03:00
|
|
|
switch tag {
|
|
|
|
case ColorChangedEvent:
|
|
|
|
return picker.colorChangedListeners
|
|
|
|
|
|
|
|
default:
|
|
|
|
return picker.viewData.get(tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *colorPickerData) htmlTag() string {
|
|
|
|
return "input"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (picker *colorPickerData) htmlProperties(self View, buffer *strings.Builder) {
|
|
|
|
picker.viewData.htmlProperties(self, buffer)
|
|
|
|
|
|
|
|
buffer.WriteString(` type="color" value="`)
|
2022-08-31 22:17:46 +03:00
|
|
|
buffer.WriteString(GetColorPickerValue(picker).rgbString())
|
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 *colorPickerData) 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 *colorPickerData) handleCommand(self View, command string, data DataObject) bool {
|
|
|
|
switch command {
|
|
|
|
case "textChanged":
|
|
|
|
if text, ok := data.PropertyValue("text"); ok {
|
2022-08-31 22:17:46 +03:00
|
|
|
oldColor := GetColorPickerValue(picker)
|
2021-09-07 17:36:50 +03:00
|
|
|
if color, ok := StringToColor(text); ok {
|
|
|
|
picker.properties[ColorPickerValue] = color
|
|
|
|
if color != oldColor {
|
|
|
|
for _, listener := range picker.colorChangedListeners {
|
|
|
|
listener(picker, color)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return picker.viewData.handleCommand(self, command, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetColorPickerValue returns the value of ColorPicker 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 GetColorPickerValue(view View, subviewID ...string) Color {
|
|
|
|
if len(subviewID) > 0 && subviewID[0] != "" {
|
|
|
|
view = ViewByID(view, subviewID[0])
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
if view != nil {
|
2022-07-28 12:11:27 +03:00
|
|
|
if value, ok := colorProperty(view, ColorPickerValue, view.Session()); ok {
|
|
|
|
return value
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
2022-07-28 12:11:27 +03:00
|
|
|
for _, tag := range []string{ColorPickerValue, Value, ColorTag} {
|
2022-05-23 15:22:14 +03:00
|
|
|
if value := valueFromStyle(view, tag); value != nil {
|
2021-09-07 17:36:50 +03:00
|
|
|
if result, ok := valueToColor(value, view.Session()); ok {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetColorChangedListeners returns the ColorChangedListener list of an ColorPicker 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 GetColorChangedListeners(view View, subviewID ...string) []func(ColorPicker, Color) {
|
2022-07-27 20:31:57 +03:00
|
|
|
return getEventListeners[ColorPicker, Color](view, subviewID, ColorChangedEvent)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|