rui_orig/detailsView.go

267 lines
7.4 KiB
Go
Raw Permalink Normal View History

2021-09-07 17:36:50 +03:00
package rui
2026-07-14 11:25:58 +03:00
import (
"iter"
"strings"
)
2021-09-07 17:36:50 +03:00
// Constants for [DetailsView] specific properties and events
2021-09-07 17:36:50 +03:00
const (
// Summary is the constant for "summary" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by DetailsView.
// The content of this property is used as the label for the disclosure widget.
//
2024-12-05 20:15:39 +03:00
// Supported types:
// - string - Summary as a text.
// - View - Summary as a view, in this case it can be quite complex if needed.
2024-11-13 12:56:39 +03:00
Summary PropertyName = "summary"
// Expanded is the constant for "expanded" property tag.
//
2024-12-05 20:15:39 +03:00
// Used by DetailsView.
// Controls the content expanded state of the details view. Default value is false.
//
2024-12-05 20:15:39 +03:00
// Supported types: bool, int, string.
//
// Values:
2024-12-05 20:15:39 +03:00
// - true, 1, "true", "yes", "on", or "1" - Content is visible.
// - false, 0, "false", "no", "off", or "0" - Content is collapsed (hidden).
2024-11-13 12:56:39 +03:00
Expanded PropertyName = "expanded"
// HideSummaryMarker is the constant for "hide-summary-marker" property tag.
//
// Used by DetailsView.
// Allows you to hide the summary marker (▶︎). Default value is false.
//
// Supported types: bool, int, string.
//
// Values:
// - true, 1, "true", "yes", "on", or "1" - The summary marker is hidden.
// - false, 0, "false", "no", "off", or "0" - The summary marker is displayed (default value).
HideSummaryMarker PropertyName = "hide-summary-marker"
2021-09-07 17:36:50 +03:00
)
// DetailsView represent a DetailsView view, which is a collapsible container of views
2021-09-07 17:36:50 +03:00
type DetailsView interface {
ViewsContainer
}
type detailsViewData struct {
viewsContainerData
}
// NewDetailsView create new DetailsView object and return it
func NewDetailsView(session Session, params Params) DetailsView {
2026-11-05 13:31:38 +03:00
if session == nil {
ErrorLog(`the Session argument cannot be nil`)
return nil
}
2021-09-07 17:36:50 +03:00
view := new(detailsViewData)
view.init(session)
2021-09-07 17:36:50 +03:00
setInitParams(view, params)
return view
}
func newDetailsView(session Session) View {
2024-11-13 12:56:39 +03:00
return new(detailsViewData)
2021-09-07 17:36:50 +03:00
}
// Init initialize fields of DetailsView by default values
func (detailsView *detailsViewData) init(session Session) {
detailsView.viewsContainerData.init(session)
2021-09-07 17:36:50 +03:00
detailsView.tag = "DetailsView"
2024-11-13 12:56:39 +03:00
detailsView.set = detailsView.setFunc
detailsView.changed = detailsView.propertyChanged
2021-09-07 17:36:50 +03:00
//detailsView.systemClass = "ruiDetailsView"
}
func (detailsView *detailsViewData) Views() []View {
views := detailsView.viewsContainerData.Views()
2024-11-13 12:56:39 +03:00
if summary := detailsView.Get(Summary); summary != nil {
switch summary := summary.(type) {
case View:
return append([]View{summary}, views...)
}
}
return views
}
2026-07-14 11:25:58 +03:00
func (detailsView *detailsViewData) ViewSeq() iter.Seq[View] {
if detailsView.views == nil {
detailsView.views = []View{}
}
return func(yield func(View) bool) {
if summary := detailsView.Get(Summary); summary != nil {
switch summary := summary.(type) {
case View:
if !yield(summary) {
return
}
}
}
for _, view := range detailsView.views {
if !yield(view) {
return
}
}
}
}
func (detailsView *detailsViewData) setFunc(tag PropertyName, value any) []PropertyName {
2021-09-07 17:36:50 +03:00
switch tag {
case Summary:
switch value := value.(type) {
case string:
2024-11-13 12:56:39 +03:00
detailsView.setRaw(Summary, value)
2021-09-07 17:36:50 +03:00
case View:
2024-11-13 12:56:39 +03:00
detailsView.setRaw(Summary, value)
value.setParentID(detailsView.htmlID())
2021-09-07 17:36:50 +03:00
case DataObject:
if view := CreateViewFromObject(detailsView.Session(), value, nil); view != nil {
2024-11-13 12:56:39 +03:00
detailsView.setRaw(Summary, view)
view.setParentID(detailsView.htmlID())
2021-09-07 17:36:50 +03:00
} else {
2024-11-13 12:56:39 +03:00
return nil
2021-09-07 17:36:50 +03:00
}
default:
notCompatibleType(tag, value)
2024-11-13 12:56:39 +03:00
return nil
}
2024-11-13 12:56:39 +03:00
return []PropertyName{tag}
}
return detailsView.viewsContainerData.setFunc(tag, value)
2024-11-13 12:56:39 +03:00
}
func (detailsView *detailsViewData) propertyChanged(tag PropertyName) {
2024-11-13 12:56:39 +03:00
switch tag {
case Summary, HideSummaryMarker:
updateInnerHTML(detailsView.htmlID(), detailsView.Session())
2021-09-07 17:36:50 +03:00
case Expanded:
if IsDetailsExpanded(detailsView) {
detailsView.Session().updateProperty(detailsView.htmlID(), "open", "")
2024-11-13 12:56:39 +03:00
} else {
detailsView.Session().removeProperty(detailsView.htmlID(), "open")
2021-09-07 17:36:50 +03:00
}
2022-05-22 12:54:02 +03:00
case NotTranslate:
updateInnerHTML(detailsView.htmlID(), detailsView.Session())
2022-05-22 12:54:02 +03:00
default:
detailsView.viewsContainerData.propertyChanged(tag)
2021-09-07 17:36:50 +03:00
}
}
func (detailsView *detailsViewData) htmlTag() string {
return "details"
}
func (detailsView *detailsViewData) htmlProperties(self View, buffer *strings.Builder) {
detailsView.viewsContainerData.htmlProperties(self, buffer)
2021-11-20 16:53:45 +03:00
buffer.WriteString(` ontoggle="detailsEvent(this)"`)
if IsDetailsExpanded(detailsView) {
2021-09-07 17:36:50 +03:00
buffer.WriteString(` open`)
}
}
func (detailsView *detailsViewData) htmlSubviews(self View, buffer *strings.Builder) {
summary := false
hidden := IsSummaryMarkerHidden(detailsView)
2026-08-15 11:33:22 +03:00
if value := detailsView.getRaw(Summary); value != nil {
2021-09-07 17:36:50 +03:00
switch value := value.(type) {
case string:
if !GetNotTranslate(detailsView) {
2022-05-22 12:54:02 +03:00
value, _ = detailsView.session.GetString(value)
}
if hidden {
buffer.WriteString(`<summary class="hiddenMarker">`)
} else {
buffer.WriteString("<summary>")
}
2026-05-26 16:06:17 +03:00
buffer.WriteString(textToHtml(value))
2021-09-07 17:36:50 +03:00
buffer.WriteString("</summary>")
summary = true
2021-09-07 17:36:50 +03:00
case View:
if hidden {
buffer.WriteString(`<summary class="hiddenMarker">`)
viewHTML(value, buffer, "")
buffer.WriteString("</summary>")
} else if value.htmlTag() == "div" {
2024-11-21 09:25:46 +03:00
viewHTML(value, buffer, "summary")
} else {
buffer.WriteString(`<summary><div style="display: inline-block;">`)
viewHTML(value, buffer, "")
buffer.WriteString("</div></summary>")
}
summary = true
}
}
if !summary {
if hidden {
buffer.WriteString(`<summary class="hiddenMarker"></summary>`)
} else {
buffer.WriteString("<summary></summary>")
2021-09-07 17:36:50 +03:00
}
}
detailsView.viewsContainerData.htmlSubviews(self, buffer)
}
2024-11-13 12:56:39 +03:00
func (detailsView *detailsViewData) handleCommand(self View, command PropertyName, data DataObject) bool {
2021-11-20 16:53:45 +03:00
if command == "details-open" {
if n, ok := dataIntProperty(data, "open"); ok {
2026-08-15 11:33:22 +03:00
detailsView.setRaw(Expanded, n != 0)
2025-07-06 11:20:37 +03:00
detailsView.runChangeListener(Expanded)
2021-11-20 16:53:45 +03:00
}
return true
}
return detailsView.viewsContainerData.handleCommand(self, command, data)
}
2021-09-07 17:36:50 +03:00
// GetDetailsSummary returns a value of the Summary property of DetailsView.
2025-06-17 21:08:16 +03:00
//
// The second argument (subviewID) specifies the path to the child element whose value needs to be returned.
// If it is not specified then a value from the first argument (view) is returned.
func GetDetailsSummary(view View, subviewID ...string) View {
2024-11-24 16:43:31 +03:00
if view = getSubview(view, subviewID); view != nil {
2021-09-07 17:36:50 +03:00
if value := view.Get(Summary); value != nil {
switch value := value.(type) {
case string:
return NewTextView(view.Session(), Params{Text: value})
case View:
return value
}
}
}
return nil
}
// IsDetailsExpanded returns a value of the Expanded property of DetailsView.
2025-06-17 21:08:16 +03:00
//
// The second argument (subviewID) specifies the path to the child element whose value needs to be returned.
// If it is not specified then a value from the first argument (view) is returned.
func IsDetailsExpanded(view View, subviewID ...string) bool {
2022-07-28 12:11:27 +03:00
return boolStyledProperty(view, subviewID, Expanded, false)
2021-09-07 17:36:50 +03:00
}
// IsDetailsExpanded returns a value of the HideSummaryMarker property of DetailsView.
2025-06-17 21:08:16 +03:00
//
// The second argument (subviewID) specifies the path to the child element whose value needs to be returned.
// If it is not specified then a value from the first argument (view) is returned.
func IsSummaryMarkerHidden(view View, subviewID ...string) bool {
return boolStyledProperty(view, subviewID, HideSummaryMarker, false)
}