rui_orig/viewsContainer.go

436 lines
10 KiB
Go
Raw Normal View History

2021-09-07 17:36:50 +03:00
package rui
import (
"iter"
2026-07-14 11:25:58 +03:00
"slices"
"strings"
)
2021-09-07 17:36:50 +03:00
// ParentView describe a view which can have a child views
2022-11-23 15:10:29 +03:00
type ParentView interface {
// Views returns a list of child views
2021-09-07 17:36:50 +03:00
Views() []View
// ViewSeq returns an iterator over all child views
ViewSeq() iter.Seq[View]
// ViewCount returns a number of child views
ViewCount() int
2021-09-07 17:36:50 +03:00
}
// ViewsContainer represent a mutable list-container of views
2021-09-07 17:36:50 +03:00
type ViewsContainer interface {
View
2022-11-23 15:10:29 +03:00
ParentView
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
// Append appends a view to the end of the list of a view children
Append(view View)
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
// Insert inserts a view to the "index" position in the list of a view children
Insert(view View, index int)
2024-04-23 19:34:36 +03:00
2021-09-07 17:36:50 +03:00
// Remove removes a view from the list of a view children and return it
RemoveView(index int) View
2024-04-23 19:34:36 +03:00
// Remove removes a view from the list of a view children and return it
RemoveViewByID(id string) View
// ViewIndex returns the index of view, -1 overwise
ViewIndex(view View) int
2024-11-13 12:56:39 +03:00
setContent(value any) bool
2021-09-07 17:36:50 +03:00
}
type viewsContainerData struct {
viewData
views []View
}
// Init initialize fields of ViewsContainer by default values
func (container *viewsContainerData) init(session Session) {
container.viewData.init(session)
2021-09-07 17:36:50 +03:00
container.tag = "ViewsContainer"
container.views = []View{}
container.get = container.getFunc
2024-11-13 12:56:39 +03:00
container.set = container.setFunc
container.remove = container.removeFunc
container.changed = container.propertyChanged
2022-05-22 12:54:02 +03:00
}
2021-09-07 17:36:50 +03:00
func (container *viewsContainerData) setParentID(parentID string) {
container.viewData.setParentID(parentID)
htmlID := container.htmlID()
for _, view := range container.views {
view.setParentID(htmlID)
}
}
// Views return a list of child views
func (container *viewsContainerData) Views() []View {
if container.views == nil {
container.views = []View{}
2022-05-17 10:46:00 +03:00
}
2026-07-14 11:25:58 +03:00
return slices.Clone(container.views)
2021-09-07 17:36:50 +03:00
}
func (container *viewsContainerData) ViewSeq() iter.Seq[View] {
if container.views == nil {
container.views = []View{}
}
return func(yield func(View) bool) {
for _, view := range container.views {
if !yield(view) {
return
}
}
}
}
func (container *viewsContainerData) ViewCount() int {
return len(container.views)
}
2025-05-25 17:40:35 +03:00
func (container *viewsContainerData) append(view View) bool {
2021-09-07 17:36:50 +03:00
if view != nil {
2025-05-25 17:40:35 +03:00
view.setParentID(container.htmlID())
if len(container.views) == 0 {
2021-09-07 17:36:50 +03:00
container.views = []View{view}
} else {
container.views = append(container.views, view)
}
2025-05-25 17:40:35 +03:00
return true
}
return false
}
2024-11-24 18:23:24 +03:00
2025-05-25 17:40:35 +03:00
// Append appends a view to the end of the list of a view children
func (container *viewsContainerData) Append(view View) {
if container.append(view) && container.created {
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
2024-11-24 18:23:24 +03:00
2025-05-25 17:40:35 +03:00
viewHTML(view, buffer, "")
2026-08-30 12:43:58 +03:00
container.Session().callFunc("appendElement", container.htmlID(), buffer.String())
2025-07-06 11:20:37 +03:00
container.runChangeListener(Content)
2021-09-07 17:36:50 +03:00
}
}
2026-08-30 13:03:47 +03:00
func (container *viewsContainerData) insert(view View, index int) int {
2021-09-07 17:36:50 +03:00
if view != nil {
2026-08-30 13:03:47 +03:00
count := len(container.views)
if count == 0 || index < 0 || index >= count {
if container.append(view) {
return len(container.views) - 1
}
return -1
2024-11-24 18:23:24 +03:00
}
2025-05-25 17:40:35 +03:00
view.setParentID(container.htmlID())
2024-11-24 18:23:24 +03:00
if index > 0 {
2021-09-07 17:36:50 +03:00
container.views = append(container.views[:index], append([]View{view}, container.views[index:]...)...)
} else {
container.views = append([]View{view}, container.views...)
2024-11-24 18:23:24 +03:00
}
2026-08-30 13:03:47 +03:00
return index
2025-05-25 17:40:35 +03:00
}
2026-08-30 13:03:47 +03:00
return -1
2025-05-25 17:40:35 +03:00
}
2024-11-25 23:13:29 +03:00
2025-05-25 17:40:35 +03:00
// Insert inserts a view to the "index" position in the list of a view children
func (container *viewsContainerData) Insert(view View, index int) {
2026-08-30 13:03:47 +03:00
index = container.insert(view, index)
if index >= 0 && container.created {
html := allocStringBuilder()
defer freeStringBuilder(html)
viewHTML(view, html, "")
if index == 0 {
container.Session().callFunc("insertElementAtStart", container.htmlID(), html.String())
} else if index >= len(container.views)-1 {
container.Session().callFunc("appendElement", container.htmlID(), html.String())
} else {
container.Session().callFunc("appendElementAfter", container.views[index-1].htmlID(), html.String())
}
//updateInnerHTML(container.htmlID(), container.Session())
2025-07-06 11:20:37 +03:00
container.runChangeListener(Content)
2021-09-07 17:36:50 +03:00
}
}
2025-05-25 17:40:35 +03:00
func (container *viewsContainerData) removeView(index int) View {
2021-09-07 17:36:50 +03:00
if container.views == nil {
container.views = []View{}
return nil
}
count := len(container.views)
if index < 0 || index >= count {
2021-09-07 17:36:50 +03:00
return nil
}
view := container.views[index]
2025-06-20 14:56:42 +03:00
switch index {
case 0:
2021-09-07 17:36:50 +03:00
container.views = container.views[1:]
2025-06-20 14:56:42 +03:00
case count - 1:
2021-09-07 17:36:50 +03:00
container.views = container.views[:index]
2025-06-20 14:56:42 +03:00
default:
2021-09-07 17:36:50 +03:00
container.views = append(container.views[:index], container.views[index+1:]...)
}
view.setParentID("")
2025-05-25 17:40:35 +03:00
return view
}
2024-11-24 18:23:24 +03:00
2025-05-25 17:40:35 +03:00
// Remove removes view from list and return it
func (container *viewsContainerData) RemoveView(index int) View {
view := container.removeView(index)
if view != nil && container.created {
2024-11-25 23:13:29 +03:00
container.Session().callFunc("removeView", view.htmlID())
2025-07-06 11:20:37 +03:00
container.runChangeListener(Content)
2024-11-24 18:23:24 +03:00
}
2021-09-07 17:36:50 +03:00
return view
}
func (container *viewsContainerData) RemoveViewByID(id string) View {
for index, view := range container.views {
if view.ID() == id {
return container.RemoveView(index)
}
}
return nil
}
func (container *viewsContainerData) ViewIndex(view View) int {
for index, v := range container.views {
if v == view {
return index
}
}
return -1
}
2021-09-07 17:36:50 +03:00
func (container *viewsContainerData) htmlSubviews(self View, buffer *strings.Builder) {
if container.views != nil {
for _, view := range container.views {
2024-11-21 09:25:46 +03:00
viewHTML(view, buffer, "")
2021-09-07 17:36:50 +03:00
}
}
}
func viewFromTextValue(text string, session Session) View {
if data, err := ParseDataText(text); err == nil {
if view := CreateViewFromObject(session, data, nil); view != nil {
return view
2021-09-07 17:36:50 +03:00
}
}
2025-07-06 11:20:37 +03:00
if ok, constName := isConstantName(text); ok {
if view := CreateViewFromResources(session, constName); view != nil {
return view
}
}
2021-09-07 17:36:50 +03:00
return NewTextView(session, Params{Text: text})
}
func (container *viewsContainerData) removeFunc(tag PropertyName) []PropertyName {
2021-09-07 17:36:50 +03:00
switch tag {
case Content:
2024-11-13 12:56:39 +03:00
if len(container.views) > 0 {
2021-09-07 17:36:50 +03:00
container.views = []View{}
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
return []PropertyName{}
case Disabled:
if container.getRaw(Disabled) != nil {
container.setRaw(Disabled, nil)
2024-11-13 12:56:39 +03:00
for _, view := range container.views {
view.Remove(Disabled)
}
2024-11-13 12:56:39 +03:00
return []PropertyName{tag}
}
2021-09-07 17:36:50 +03:00
}
return container.viewData.removeFunc(tag)
2021-09-07 17:36:50 +03:00
}
func (container *viewsContainerData) setFunc(tag PropertyName, value any) []PropertyName {
switch tag {
case Content:
2024-11-13 12:56:39 +03:00
if container.setContent(value) {
return []PropertyName{tag}
}
return nil
case Disabled:
oldDisabled := IsDisabled(container)
result := container.viewData.setFunc(Disabled, value)
2024-11-13 12:56:39 +03:00
if result != nil {
disabled := IsDisabled(container)
if oldDisabled != disabled {
2024-11-13 12:56:39 +03:00
for _, view := range container.views {
view.Set(Disabled, disabled)
}
}
}
2024-11-13 12:56:39 +03:00
return result
2021-09-07 17:36:50 +03:00
}
return container.viewData.setFunc(tag, value)
2024-11-13 12:56:39 +03:00
}
func (container *viewsContainerData) propertyChanged(tag PropertyName) {
2024-11-13 12:56:39 +03:00
switch tag {
case Content:
updateInnerHTML(container.htmlID(), container.Session())
2024-11-13 12:56:39 +03:00
default:
container.viewData.propertyChanged(tag)
2024-11-13 12:56:39 +03:00
}
}
func (container *viewsContainerData) setContent(value any) bool {
2021-09-07 17:36:50 +03:00
session := container.Session()
switch value := value.(type) {
case View:
container.views = []View{value}
case []View:
container.views = value
case string:
container.views = []View{viewFromTextValue(value, session)}
case []string:
views := []View{}
for _, text := range value {
views = append(views, viewFromTextValue(text, session))
}
container.views = views
2022-07-26 18:36:00 +03:00
case []any:
2021-09-07 17:36:50 +03:00
views := []View{}
for _, v := range value {
switch v := v.(type) {
case View:
views = append(views, v)
case string:
views = append(views, viewFromTextValue(v, session))
default:
notCompatibleType(Content, value)
2021-09-07 17:36:50 +03:00
return false
}
}
container.views = views
case DataObject:
if view := CreateViewFromObject(session, value, nil); view != nil {
2021-09-07 17:36:50 +03:00
container.views = []View{view}
} else {
return false
}
case []DataValue:
views := []View{}
for _, data := range value {
if data.IsObject() {
if view := CreateViewFromObject(session, data.Object(), nil); view != nil {
2021-09-07 17:36:50 +03:00
views = append(views, view)
}
} else {
views = append(views, viewFromTextValue(data.Value(), session))
}
}
container.views = views
default:
notCompatibleType(Content, value)
2021-09-07 17:36:50 +03:00
return false
}
htmlID := container.htmlID()
isDisabled := IsDisabled(container)
2021-09-07 17:36:50 +03:00
for _, view := range container.views {
view.setParentID(htmlID)
if isDisabled {
view.Set(Disabled, true)
}
2021-09-07 17:36:50 +03:00
}
return true
}
func (container *viewsContainerData) getFunc(tag PropertyName) any {
2021-09-07 17:36:50 +03:00
switch tag {
case Content:
return container.views
default:
return container.viewData.getFunc(tag)
2021-09-07 17:36:50 +03:00
}
}
// AppendView appends a view to the end of the list of a view children
func AppendView(rootView View, containerID string, view View) bool {
var container ViewsContainer = nil
if containerID != "" {
container = ViewsContainerByID(rootView, containerID)
} else {
if cont, ok := rootView.(ViewsContainer); ok {
container = cont
} else {
ErrorLogF(`Unable to add a view to "%s"`, rootView.Tag())
}
}
if container != nil {
container.Append(view)
return true
}
return false
}
// Insert inserts a view to the "index" position in the list of a view children
func InsertView(rootView View, containerID string, view View, index int) bool {
2021-09-07 17:36:50 +03:00
var container ViewsContainer = nil
if containerID != "" {
container = ViewsContainerByID(rootView, containerID)
} else {
if cont, ok := rootView.(ViewsContainer); ok {
container = cont
} else {
ErrorLogF(`Unable to add a view to "%s"`, rootView.Tag())
}
}
if container != nil {
container.Insert(view, index)
return true
}
return false
}
// Remove removes a view from the list of a view children and return it
func RemoveView(rootView View, containerID string, index int) View {
2021-09-07 17:36:50 +03:00
var container ViewsContainer = nil
if containerID != "" {
container = ViewsContainerByID(rootView, containerID)
} else {
if cont, ok := rootView.(ViewsContainer); ok {
container = cont
} else {
ErrorLogF(`Unable to add a view to "%s"`, rootView.Tag())
}
}
if container != nil {
return container.RemoveView(index)
}
return nil
}