2021-09-07 17:36:50 +03:00
|
|
|
package rui
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
2024-09-12 14:05:11 +03:00
|
|
|
// Constants for [DetailsView] specific properties and events
|
2021-09-07 17:36:50 +03:00
|
|
|
const (
|
2024-09-18 13:50:06 +03:00
|
|
|
// Summary is the constant for "summary" property tag.
|
|
|
|
//
|
|
|
|
// Used by `DetailsView`.
|
|
|
|
// The content of this property is used as the label for the disclosure widget.
|
|
|
|
//
|
|
|
|
// Supported types: `string`, `View`.
|
|
|
|
//
|
|
|
|
// `string` - Summary as a text.
|
|
|
|
// `View` - Summary as a view, in this case it can be quite complex if needed.
|
2021-09-07 17:36:50 +03:00
|
|
|
Summary = "summary"
|
2024-09-18 13:50:06 +03:00
|
|
|
|
|
|
|
// Expanded is the constant for "expanded" property tag.
|
|
|
|
//
|
|
|
|
// Used by `DetailsView`.
|
|
|
|
// Controls the content expanded state of the details view. Default value is `false`.
|
|
|
|
//
|
|
|
|
// Supported types: `bool`, `int`, `string`.
|
|
|
|
//
|
|
|
|
// Values:
|
|
|
|
// `true` or `1` or "true", "yes", "on", "1" - Content is visible.
|
|
|
|
// `false` or `0` or "false", "no", "off", "0" - Content is collapsed(hidden).
|
2021-09-07 17:36:50 +03:00
|
|
|
Expanded = "expanded"
|
|
|
|
)
|
|
|
|
|
2024-09-12 14:05:11 +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 {
|
|
|
|
view := new(detailsViewData)
|
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 newDetailsView(session Session) View {
|
|
|
|
return NewDetailsView(session, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initialize fields of DetailsView by default values
|
2022-09-01 11:04:50 +03:00
|
|
|
func (detailsView *detailsViewData) init(session Session) {
|
|
|
|
detailsView.viewsContainerData.init(session)
|
2021-09-07 17:36:50 +03:00
|
|
|
detailsView.tag = "DetailsView"
|
|
|
|
//detailsView.systemClass = "ruiDetailsView"
|
|
|
|
}
|
|
|
|
|
2022-05-12 11:05:50 +03:00
|
|
|
func (detailsView *detailsViewData) Views() []View {
|
|
|
|
views := detailsView.viewsContainerData.Views()
|
|
|
|
if summary := detailsView.get(Summary); summary != nil {
|
|
|
|
switch summary := summary.(type) {
|
|
|
|
case View:
|
|
|
|
return append([]View{summary}, views...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return views
|
|
|
|
}
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
func (detailsView *detailsViewData) Remove(tag string) {
|
|
|
|
detailsView.remove(strings.ToLower(tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (detailsView *detailsViewData) remove(tag string) {
|
2021-11-20 11:15:28 +03:00
|
|
|
detailsView.viewsContainerData.remove(tag)
|
|
|
|
if detailsView.created {
|
2021-09-07 17:36:50 +03:00
|
|
|
switch tag {
|
|
|
|
case Summary:
|
|
|
|
updateInnerHTML(detailsView.htmlID(), detailsView.Session())
|
|
|
|
|
|
|
|
case Expanded:
|
2022-10-30 17:22:33 +03:00
|
|
|
detailsView.session.removeProperty(detailsView.htmlID(), "open")
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (detailsView *detailsViewData) Set(tag string, value any) bool {
|
2021-09-07 17:36:50 +03:00
|
|
|
return detailsView.set(strings.ToLower(tag), value)
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (detailsView *detailsViewData) set(tag string, value any) bool {
|
2021-11-20 11:15:28 +03:00
|
|
|
if value == nil {
|
|
|
|
detailsView.remove(tag)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-09-07 17:36:50 +03:00
|
|
|
switch tag {
|
|
|
|
case Summary:
|
|
|
|
switch value := value.(type) {
|
|
|
|
case string:
|
|
|
|
detailsView.properties[Summary] = value
|
|
|
|
|
|
|
|
case View:
|
|
|
|
detailsView.properties[Summary] = value
|
2022-05-12 11:05:50 +03:00
|
|
|
value.setParentID(detailsView.htmlID())
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case DataObject:
|
|
|
|
if view := CreateViewFromObject(detailsView.Session(), value); view != nil {
|
|
|
|
detailsView.properties[Summary] = view
|
2022-05-12 11:05:50 +03:00
|
|
|
view.setParentID(detailsView.htmlID())
|
2021-09-07 17:36:50 +03:00
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
notCompatibleType(tag, value)
|
|
|
|
return false
|
|
|
|
}
|
2021-11-20 11:15:28 +03:00
|
|
|
if detailsView.created {
|
|
|
|
updateInnerHTML(detailsView.htmlID(), detailsView.Session())
|
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
|
|
|
|
case Expanded:
|
2021-11-20 11:15:28 +03:00
|
|
|
if !detailsView.setBoolProperty(tag, value) {
|
|
|
|
notCompatibleType(tag, value)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if detailsView.created {
|
2022-08-31 22:17:46 +03:00
|
|
|
if IsDetailsExpanded(detailsView) {
|
2022-10-30 17:22:33 +03:00
|
|
|
detailsView.session.updateProperty(detailsView.htmlID(), "open", "")
|
2021-09-07 17:36:50 +03:00
|
|
|
} else {
|
2022-10-30 17:22:33 +03:00
|
|
|
detailsView.session.removeProperty(detailsView.htmlID(), "open")
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-20 11:15:28 +03:00
|
|
|
|
2022-05-22 12:54:02 +03:00
|
|
|
case NotTranslate:
|
|
|
|
if !detailsView.viewData.set(tag, value) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if detailsView.created {
|
|
|
|
updateInnerHTML(detailsView.htmlID(), detailsView.Session())
|
|
|
|
}
|
|
|
|
|
2021-11-20 11:15:28 +03:00
|
|
|
default:
|
|
|
|
return detailsView.viewsContainerData.Set(tag, value)
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
2021-11-20 11:15:28 +03:00
|
|
|
detailsView.propertyChangedEvent(tag)
|
|
|
|
return true
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (detailsView *detailsViewData) Get(tag string) any {
|
2021-09-07 17:36:50 +03:00
|
|
|
return detailsView.get(strings.ToLower(tag))
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:36:00 +03:00
|
|
|
func (detailsView *detailsViewData) get(tag string) any {
|
2021-09-07 17:36:50 +03:00
|
|
|
return detailsView.viewsContainerData.get(tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)"`)
|
2022-08-31 22:17:46 +03:00
|
|
|
if IsDetailsExpanded(detailsView) {
|
2021-09-07 17:36:50 +03:00
|
|
|
buffer.WriteString(` open`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (detailsView *detailsViewData) htmlSubviews(self View, buffer *strings.Builder) {
|
|
|
|
if value, ok := detailsView.properties[Summary]; ok {
|
|
|
|
switch value := value.(type) {
|
|
|
|
case string:
|
2022-08-31 22:17:46 +03:00
|
|
|
if !GetNotTranslate(detailsView) {
|
2022-05-22 12:54:02 +03:00
|
|
|
value, _ = detailsView.session.GetString(value)
|
|
|
|
}
|
2021-09-07 17:36:50 +03:00
|
|
|
buffer.WriteString("<summary>")
|
|
|
|
buffer.WriteString(value)
|
|
|
|
buffer.WriteString("</summary>")
|
|
|
|
|
|
|
|
case View:
|
|
|
|
buffer.WriteString("<summary>")
|
|
|
|
viewHTML(value, buffer)
|
|
|
|
buffer.WriteString("</summary>")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
detailsView.viewsContainerData.htmlSubviews(self, buffer)
|
|
|
|
}
|
|
|
|
|
2021-11-20 16:53:45 +03:00
|
|
|
func (detailsView *detailsViewData) handleCommand(self View, command string, data DataObject) bool {
|
|
|
|
if command == "details-open" {
|
|
|
|
if n, ok := dataIntProperty(data, "open"); ok {
|
|
|
|
detailsView.properties[Expanded] = (n != 0)
|
|
|
|
detailsView.propertyChangedEvent(Expanded)
|
|
|
|
}
|
|
|
|
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.
|
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 GetDetailsSummary(view View, subviewID ...string) View {
|
|
|
|
if len(subviewID) > 0 && subviewID[0] != "" {
|
|
|
|
view = ViewByID(view, subviewID[0])
|
2021-09-07 17:36:50 +03:00
|
|
|
}
|
|
|
|
if view != nil {
|
|
|
|
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.
|
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 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
|
|
|
}
|