2023-05-18 21:17:17 +00:00
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.d3nexus.de/Dadido3/D3vugu-components/components/navigation"
|
|
|
|
"github.com/vugu/vugu"
|
2023-05-19 21:45:39 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2023-05-18 21:17:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Container can be embedded in your root component and provides a way to show overlays like modals and toasts.
|
|
|
|
type Container struct {
|
2023-05-19 21:45:39 +00:00
|
|
|
modal vugu.Builder `vugu:"data"`
|
|
|
|
modalTitle string `vugu:"data"`
|
|
|
|
modalSignalClasses string // Additional classes that are applied to the modal menu bar.
|
|
|
|
modalContainerClasses string // Additional classes that are applied to the whole modal body.
|
|
|
|
|
|
|
|
toasts []ContainerToast `vugu:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ContainerToast struct {
|
|
|
|
body vugu.Builder `vugu:"data"`
|
|
|
|
|
|
|
|
signalClasses string // Additional classes that are applied to the small bar to the left.
|
|
|
|
containerClasses string // Additional classes that are applied to the whole toast.
|
2023-05-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) handleModalClose(event vugu.DOMEvent) {
|
|
|
|
c.modal = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) SetModal(component vugu.Builder) {
|
|
|
|
c.modal = component
|
|
|
|
|
|
|
|
c.modalTitle = ""
|
2023-05-19 21:45:39 +00:00
|
|
|
c.modalContainerClasses = ""
|
|
|
|
c.modalSignalClasses = "d3c-color-accent"
|
2023-05-18 21:17:17 +00:00
|
|
|
// Retrieve title from component if it implements the PageTitleGetter interface.
|
|
|
|
if pageInfo, ok := component.(navigation.PageTitleGetter); ok {
|
|
|
|
c.modalTitle, _, _ = pageInfo.PageTitle()
|
|
|
|
}
|
2023-05-19 21:45:39 +00:00
|
|
|
// Retrieve additional CSS classes from component.
|
|
|
|
if overlayClassesGetter, ok := component.(OverlayClassesGetter); ok {
|
|
|
|
c.modalSignalClasses, c.modalContainerClasses = overlayClassesGetter.OverlayClasses()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) handleToastClose(event vugu.DOMEvent, toast vugu.Builder) {
|
|
|
|
c.CloseToast(toast)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) AddToast(component vugu.Builder) {
|
|
|
|
toast := ContainerToast{
|
|
|
|
body: component,
|
|
|
|
signalClasses: "d3c-color-accent",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve additional classes.
|
|
|
|
if overlayClassesGetter, ok := component.(OverlayClassesGetter); ok {
|
|
|
|
toast.signalClasses, toast.containerClasses = overlayClassesGetter.OverlayClasses()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.toasts = append(c.toasts, toast)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) CloseToast(component vugu.Builder) {
|
|
|
|
for i, toast := range c.toasts {
|
|
|
|
if toast.body == component {
|
|
|
|
c.toasts = slices.Delete(c.toasts, i, i+1)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-05-18 21:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type OverlayContainerRef struct {
|
|
|
|
*Container
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *OverlayContainerRef) OverlayContainerSet(container *Container) {
|
|
|
|
p.Container = container
|
|
|
|
}
|
|
|
|
|
|
|
|
type OverlayContainerSetter interface {
|
|
|
|
OverlayContainerSet(*Container)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ OverlayContainerSetter = &OverlayContainerRef{}
|