rui_orig/editView.go

516 lines
14 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"strconv"
"strings"
)
// Constants for [EditView] specific properties and events
2021-09-07 17:36:50 +03:00
const (
// EditTextChangedEvent is the constant for "edit-text-changed" property tag.
//
// Used by `EditView`.
// Occur when edit view text has been changed.
//
// General listener format:
// `func(editView rui.EditView, newText, oldText string)`.
//
// where:
// editView - Interface of an edit view which generated this event,
// newText - New edit view text,
// oldText - Previous edit view text.
//
// Allowed listener formats:
// `func(editView rui.EditView, newText string)`,
// `func(newText, oldText string)`,
// `func(newText string)`,
// `func(editView rui.EditView)`,
// `func()`.
2024-11-13 12:56:39 +03:00
EditTextChangedEvent PropertyName = "edit-text-changed"
2024-05-18 18:57:41 +03:00
// EditViewType is the constant for "edit-view-type" property tag.
//
// Used by `EditView`.
// Type of the text input. Default value is "text".
//
// Supported types: `int`, `string`.
//
// Values:
// `0`(`SingleLineText`) or "text" - One-line text editor.
// `1`(`PasswordText`) or "password" - Password editor. The text is hidden by asterisks.
// `2`(`EmailText`) or "email" - Single e-mail editor.
// `3`(`EmailsText`) or "emails" - Multiple e-mail editor.
// `4`(`URLText`) or "url" - Internet address input editor.
// `5`(`PhoneText`) or "phone" - Phone number editor.
// `6`(`MultiLineText`) or "multiline" - Multi-line text editor.
2024-11-13 12:56:39 +03:00
EditViewType PropertyName = "edit-view-type"
2024-05-18 18:57:41 +03:00
// EditViewPattern is the constant for "edit-view-pattern" property tag.
//
// Used by `EditView`.
// Regular expression to limit editing of a text.
//
// Supported types: `string`.
2024-11-13 12:56:39 +03:00
EditViewPattern PropertyName = "edit-view-pattern"
2024-05-18 18:57:41 +03:00
// Spellcheck is the constant for "spellcheck" property tag.
//
// Used by `EditView`.
2024-11-13 12:56:39 +03:00
// Enable or disable spell checker. Available in `SingleLineText` and `MultiLineText` types of edit view. Default value is
// `false`.
//
// Supported types: `bool`, `int`, `string`.
//
// Values:
// `true` or `1` or "true", "yes", "on", "1" - Enable spell checker for text.
// `false` or `0` or "false", "no", "off", "0" - Disable spell checker for text.
2024-11-13 12:56:39 +03:00
Spellcheck PropertyName = "spellcheck"
2021-09-07 17:36:50 +03:00
)
// Constants for the values of an [EditView] "edit-view-type" property
2021-09-07 17:36:50 +03:00
const (
// SingleLineText - single-line text type of EditView
SingleLineText = 0
2021-09-07 17:36:50 +03:00
// PasswordText - password type of EditView
PasswordText = 1
2021-09-07 17:36:50 +03:00
// EmailText - e-mail type of EditView. Allows to enter one email
EmailText = 2
2022-11-23 15:10:29 +03:00
// EmailsText - e-mail type of EditView. Allows to enter multiple emails separated by comma
2021-09-07 17:36:50 +03:00
EmailsText = 3
2021-09-07 17:36:50 +03:00
// URLText - url type of EditView. Allows to enter one url
URLText = 4
2021-09-07 17:36:50 +03:00
// PhoneText - telephone type of EditView. Allows to enter one phone number
PhoneText = 5
2021-09-07 17:36:50 +03:00
// MultiLineText - multi-line text type of EditView
MultiLineText = 6
)
// EditView represent an EditView view
2021-09-07 17:36:50 +03:00
type EditView interface {
View
// AppendText appends text to the current text of an EditView view
2021-09-07 17:36:50 +03:00
AppendText(text string)
2024-11-13 12:56:39 +03:00
textChanged(newText, oldText string)
2021-09-07 17:36:50 +03:00
}
type editViewData struct {
viewData
}
// NewEditView create new EditView object and return it
func NewEditView(session Session, params Params) EditView {
view := new(editViewData)
view.init(session)
2021-09-07 17:36:50 +03:00
setInitParams(view, params)
return view
}
func newEditView(session Session) View {
2024-11-13 12:56:39 +03:00
return new(editViewData) // NewEditView(session, nil)
2021-09-07 17:36:50 +03:00
}
func (edit *editViewData) init(session Session) {
edit.viewData.init(session)
2024-04-23 18:24:51 +03:00
edit.hasHtmlDisabled = true
2021-09-07 17:36:50 +03:00
edit.tag = "EditView"
2024-11-13 12:56:39 +03:00
edit.normalize = normalizeEditViewTag
edit.set = edit.setFunc
edit.changed = edit.propertyChanged
2022-05-22 12:54:02 +03:00
}
func (edit *editViewData) Focusable() bool {
return true
}
2024-11-13 12:56:39 +03:00
func normalizeEditViewTag(tag PropertyName) PropertyName {
tag = defaultNormalize(tag)
2021-09-07 17:36:50 +03:00
switch tag {
case Type, "edit-type":
return EditViewType
case Pattern, "edit-pattern":
return EditViewPattern
case "maxlength", "maxlen":
return MaxLength
case "wrap":
return EditWrap
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
return normalizeDataListTag(tag)
2021-09-07 17:36:50 +03:00
}
func (edit *editViewData) setFunc(tag PropertyName, value any) []PropertyName {
switch tag {
case Text:
2024-11-13 12:56:39 +03:00
if text, ok := value.(string); ok {
old := ""
if val := edit.getRaw(Text); val != nil {
2024-11-13 12:56:39 +03:00
if txt, ok := val.(string); ok {
old = txt
}
2021-09-07 17:36:50 +03:00
}
edit.setRaw("old-text", old)
edit.setRaw(tag, text)
2024-11-13 12:56:39 +03:00
return []PropertyName{tag}
}
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
notCompatibleType(tag, value)
return nil
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
case Hint:
if text, ok := value.(string); ok {
return setStringPropertyValue(edit, tag, strings.Trim(text, " \t\n"))
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
notCompatibleType(tag, value)
return nil
2024-05-18 18:57:41 +03:00
case DataList:
setDataList(edit, value, "")
2024-05-18 18:57:41 +03:00
2024-11-13 12:56:39 +03:00
case EditTextChangedEvent:
return setTwoArgEventListener[EditView, string](edit, tag, value)
2021-09-07 17:36:50 +03:00
}
return edit.viewData.setFunc(tag, value)
2021-09-07 17:36:50 +03:00
}
func (edit *editViewData) propertyChanged(tag PropertyName) {
session := edit.Session()
2021-09-07 17:36:50 +03:00
switch tag {
case Text:
text := GetText(edit)
session.callFunc("setInputValue", edit.htmlID(), text)
2024-11-13 12:56:39 +03:00
old := ""
if val := edit.getRaw("old-text"); val != nil {
if txt, ok := val.(string); ok {
old = txt
2021-09-07 17:36:50 +03:00
}
}
edit.textChanged(text, old)
2021-09-07 17:36:50 +03:00
case Hint:
if text := GetHint(edit); text != "" {
session.updateProperty(edit.htmlID(), "placeholder", text)
2024-11-13 12:56:39 +03:00
} else {
session.removeProperty(edit.htmlID(), "placeholder")
2021-09-07 17:36:50 +03:00
}
case MaxLength:
if maxLength := GetMaxLength(edit); maxLength > 0 {
session.updateProperty(edit.htmlID(), "maxlength", strconv.Itoa(maxLength))
2024-11-13 12:56:39 +03:00
} else {
session.removeProperty(edit.htmlID(), "maxlength")
2021-09-07 17:36:50 +03:00
}
case ReadOnly:
if IsReadOnly(edit) {
session.updateProperty(edit.htmlID(), "readonly", "")
2024-11-13 12:56:39 +03:00
} else {
session.removeProperty(edit.htmlID(), "readonly")
2021-09-07 17:36:50 +03:00
}
case Spellcheck:
session.updateProperty(edit.htmlID(), "spellcheck", IsSpellcheck(edit))
2021-09-07 17:36:50 +03:00
case EditViewPattern:
if text := GetEditViewPattern(edit); text != "" {
session.updateProperty(edit.htmlID(), "pattern", text)
2024-11-13 12:56:39 +03:00
} else {
session.removeProperty(edit.htmlID(), "pattern")
2021-09-07 17:36:50 +03:00
}
case EditViewType:
updateInnerHTML(edit.parentHTMLID(), session)
2021-09-07 17:36:50 +03:00
case EditWrap:
if wrap := IsEditViewWrap(edit); wrap {
session.updateProperty(edit.htmlID(), "wrap", "soft")
2024-11-13 12:56:39 +03:00
} else {
session.updateProperty(edit.htmlID(), "wrap", "off")
2021-09-07 17:36:50 +03:00
}
2024-05-18 18:57:41 +03:00
case DataList:
updateInnerHTML(edit.htmlID(), session)
2021-09-07 17:36:50 +03:00
2024-11-13 12:56:39 +03:00
default:
edit.viewData.propertyChanged(tag)
2022-07-27 20:31:57 +03:00
}
2021-09-07 17:36:50 +03:00
}
func (edit *editViewData) AppendText(text string) {
if GetEditViewType(edit) == MultiLineText {
2021-09-07 17:36:50 +03:00
if value := edit.getRaw(Text); value != nil {
if textValue, ok := value.(string); ok {
oldText := textValue
2021-09-07 17:36:50 +03:00
textValue += text
edit.properties[Text] = textValue
2022-11-02 20:10:19 +03:00
edit.session.callFunc("appendToInnerHTML", edit.htmlID(), text)
2024-11-13 12:56:39 +03:00
edit.session.callFunc("appendToInputValue", edit.htmlID(), text)
edit.textChanged(textValue, oldText)
2021-09-07 17:36:50 +03:00
return
}
}
2024-11-13 12:56:39 +03:00
edit.setRaw(Text, text)
2021-09-07 17:36:50 +03:00
} else {
2024-11-13 12:56:39 +03:00
edit.setRaw(Text, GetText(edit)+text)
2021-09-07 17:36:50 +03:00
}
}
func (edit *editViewData) textChanged(newText, oldText string) {
2024-11-13 12:56:39 +03:00
for _, listener := range GetTextChangedListeners(edit) {
listener(edit, newText, oldText)
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
if listener, ok := edit.changeListener[Text]; ok {
listener(edit, Text)
}
2021-09-07 17:36:50 +03:00
}
func (edit *editViewData) htmlTag() string {
if GetEditViewType(edit) == MultiLineText {
2021-09-07 17:36:50 +03:00
return "textarea"
}
return "input"
}
2024-05-18 18:57:41 +03:00
func (edit *editViewData) htmlSubviews(self View, buffer *strings.Builder) {
2024-05-29 15:31:58 +03:00
if GetEditViewType(edit) == MultiLineText {
if text := GetText(edit); text != "" {
buffer.WriteString(text)
}
}
2024-11-13 12:56:39 +03:00
dataListHtmlSubviews(self, buffer, func(text string, session Session) string {
return text
})
2024-05-18 18:57:41 +03:00
}
2021-09-07 17:36:50 +03:00
func (edit *editViewData) htmlProperties(self View, buffer *strings.Builder) {
edit.viewData.htmlProperties(self, buffer)
writeSpellcheck := func() {
if spellcheck := IsSpellcheck(edit); spellcheck {
2021-09-07 17:36:50 +03:00
buffer.WriteString(` spellcheck="true"`)
} else {
buffer.WriteString(` spellcheck="false"`)
}
}
editType := GetEditViewType(edit)
2021-09-07 17:36:50 +03:00
switch editType {
case SingleLineText:
buffer.WriteString(` type="text" inputmode="text"`)
writeSpellcheck()
case PasswordText:
buffer.WriteString(` type="password" inputmode="text"`)
case EmailText:
buffer.WriteString(` type="email" inputmode="email"`)
case EmailsText:
buffer.WriteString(` type="email" inputmode="email" multiple`)
case URLText:
buffer.WriteString(` type="url" inputmode="url"`)
case PhoneText:
buffer.WriteString(` type="tel" inputmode="tel"`)
case MultiLineText:
if IsEditViewWrap(edit) {
2021-09-07 17:36:50 +03:00
buffer.WriteString(` wrap="soft"`)
} else {
buffer.WriteString(` wrap="off"`)
}
writeSpellcheck()
}
if IsReadOnly(edit) {
2021-09-07 17:36:50 +03:00
buffer.WriteString(` readonly`)
}
if maxLength := GetMaxLength(edit); maxLength > 0 {
2021-09-07 17:36:50 +03:00
buffer.WriteString(` maxlength="`)
buffer.WriteString(strconv.Itoa(maxLength))
buffer.WriteByte('"')
}
2022-05-26 12:01:49 +03:00
convertText := func(text string) string {
if strings.ContainsRune(text, '"') {
text = strings.ReplaceAll(text, `"`, `"`)
}
2024-05-16 11:13:45 +03:00
if strings.ContainsRune(text, '\n') {
text = strings.ReplaceAll(text, "\n", `\n`)
}
2022-10-29 21:08:51 +03:00
return text
2022-05-26 12:01:49 +03:00
}
if hint := GetHint(edit); hint != "" {
2021-09-07 17:36:50 +03:00
buffer.WriteString(` placeholder="`)
2022-05-26 12:01:49 +03:00
buffer.WriteString(convertText(hint))
2021-09-07 17:36:50 +03:00
buffer.WriteByte('"')
}
buffer.WriteString(` oninput="editViewInputEvent(this)"`)
if pattern := GetEditViewPattern(edit); pattern != "" {
2021-09-07 17:36:50 +03:00
buffer.WriteString(` pattern="`)
2022-05-26 12:01:49 +03:00
buffer.WriteString(convertText(pattern))
2021-09-07 17:36:50 +03:00
buffer.WriteByte('"')
}
2024-05-29 15:31:58 +03:00
if editType != MultiLineText {
if text := GetText(edit); text != "" {
buffer.WriteString(` value="`)
buffer.WriteString(convertText(text))
buffer.WriteByte('"')
}
2021-09-07 17:36:50 +03:00
}
2024-05-18 18:57:41 +03:00
2024-11-13 12:56:39 +03:00
dataListHtmlProperties(edit, buffer)
2021-09-07 17:36:50 +03:00
}
2024-11-13 12:56:39 +03:00
func (edit *editViewData) handleCommand(self View, command PropertyName, data DataObject) bool {
2021-09-07 17:36:50 +03:00
switch command {
case "textChanged":
oldText := GetText(edit)
2021-09-07 17:36:50 +03:00
if text, ok := data.PropertyValue("text"); ok {
2024-11-13 12:56:39 +03:00
edit.setRaw(Text, text)
if text != oldText {
edit.textChanged(text, oldText)
2021-09-07 17:36:50 +03:00
}
}
return true
}
return edit.viewData.handleCommand(self, command, data)
}
2022-05-17 10:46:00 +03:00
// GetText returns a text of the EditView subview.
// If the second argument (subviewID) is not specified or it is "" then a text of the first argument (view) is returned.
func GetText(view View, subviewID ...string) string {
2024-11-24 16:43:31 +03:00
if view = getSubview(view, subviewID); view != nil {
2021-11-17 12:47:14 +03:00
if value := view.getRaw(Text); value != nil {
if text, ok := value.(string); ok {
return text
}
2021-09-07 17:36:50 +03:00
}
}
return ""
}
// GetHint returns a hint text of the subview.
// If the second argument (subviewID) is not specified or it is "" then a text of the first argument (view) is returned.
func GetHint(view View, subviewID ...string) string {
2024-11-24 16:43:31 +03:00
view = getSubview(view, subviewID)
2024-04-22 16:35:18 +03:00
session := view.Session()
text := ""
2021-09-07 17:36:50 +03:00
if view != nil {
2024-04-22 16:35:18 +03:00
var ok bool
text, ok = stringProperty(view, Hint, view.Session())
if !ok {
if value := valueFromStyle(view, Hint); value != nil {
if text, ok = value.(string); ok {
if text, ok = session.resolveConstants(text); !ok {
text = ""
}
} else {
text = ""
2022-05-23 15:22:14 +03:00
}
2021-09-07 17:36:50 +03:00
}
}
}
2024-04-22 16:35:18 +03:00
if text != "" && !GetNotTranslate(view) {
text, _ = session.GetString(text)
}
return text
2021-09-07 17:36:50 +03:00
}
2022-11-23 15:10:29 +03:00
// GetMaxLength returns a maximal length of EditView. If a maximal length is not limited then 0 is returned
// If the second argument (subviewID) is not specified or it is "" then a value of the first argument (view) is returned.
func GetMaxLength(view View, subviewID ...string) int {
2022-07-28 12:11:27 +03:00
return intStyledProperty(view, subviewID, MaxLength, 0)
2021-09-07 17:36:50 +03:00
}
// IsReadOnly returns the true if a EditView works in read only mode.
// If the second argument (subviewID) is not specified or it is "" then a value of the first argument (view) is returned.
func IsReadOnly(view View, subviewID ...string) bool {
2022-07-28 12:11:27 +03:00
return boolStyledProperty(view, subviewID, ReadOnly, false)
2021-09-07 17:36:50 +03:00
}
// IsSpellcheck returns a value of the Spellcheck property of EditView.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func IsSpellcheck(view View, subviewID ...string) bool {
2022-07-28 12:11:27 +03:00
return boolStyledProperty(view, subviewID, Spellcheck, false)
2021-09-07 17:36:50 +03:00
}
// GetTextChangedListeners returns the TextChangedListener list of an EditView or MultiLineEditView subview.
// If there are no listeners then the empty list is returned
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetTextChangedListeners(view View, subviewID ...string) []func(EditView, string, string) {
return getTwoArgEventListeners[EditView, string](view, subviewID, EditTextChangedEvent)
2021-09-07 17:36:50 +03:00
}
// GetEditViewType returns a value of the Type property of EditView.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetEditViewType(view View, subviewID ...string) int {
2022-07-28 12:41:50 +03:00
return enumStyledProperty(view, subviewID, EditViewType, SingleLineText, false)
2021-09-07 17:36:50 +03:00
}
// GetEditViewPattern returns a value of the Pattern property of EditView.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetEditViewPattern(view View, subviewID ...string) string {
2024-11-24 16:43:31 +03:00
if view = getSubview(view, subviewID); view != nil {
2021-09-07 17:36:50 +03:00
if pattern, ok := stringProperty(view, EditViewPattern, view.Session()); ok {
return pattern
}
2022-05-23 15:22:14 +03:00
if value := valueFromStyle(view, EditViewPattern); value != nil {
if pattern, ok := value.(string); ok {
if pattern, ok = view.Session().resolveConstants(pattern); ok {
return pattern
}
2021-09-07 17:36:50 +03:00
}
}
}
return ""
}
// IsEditViewWrap returns a value of the EditWrap property of MultiLineEditView.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func IsEditViewWrap(view View, subviewID ...string) bool {
2022-07-28 12:11:27 +03:00
return boolStyledProperty(view, subviewID, EditWrap, false)
2021-09-07 17:36:50 +03:00
}
// AppendEditText appends the text to the EditView content.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
2021-09-07 17:36:50 +03:00
func AppendEditText(view View, subviewID string, text string) {
if subviewID != "" {
if edit := EditViewByID(view, subviewID); edit != nil {
edit.AppendText(text)
return
}
}
if edit, ok := view.(EditView); ok {
edit.AppendText(text)
}
}
2022-11-23 15:10:29 +03:00
// GetCaretColor returns the color of the text input caret.
// If the second argument (subviewID) is not specified or it is "" then a value from the first argument (view) is returned.
func GetCaretColor(view View, subviewID ...string) Color {
2022-07-28 12:11:27 +03:00
return colorStyledProperty(view, subviewID, CaretColor, false)
}