panel/web/views/components/utils.go

53 lines
1.1 KiB
Go
Raw Normal View History

2024-12-20 23:29:45 +01:00
package components
import (
"context"
"github.com/a-h/templ"
"io"
)
//type ScriptAssetOptions struct {
// IsAsync bool
// IsDefer bool
// IsModule bool
// DoImport bool
//}
type NewScriptAssetOptions struct {
IsAsync bool
IsDefer bool
IsModule bool
FuncImports []string
}
func LoadJS(assetName string, opts *NewScriptAssetOptions) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
if opts == nil {
_, err := io.WriteString(w, `<script type="module" src="/assets/js/`+assetName+`"></script>`)
return err
} else {
if opts.IsModule {
str := `<script type="module" src="/assets/js/` + assetName + `"`
if opts.IsDefer {
str += ` defer`
}
if opts.IsAsync {
str += ` async`
}
_, err := io.WriteString(w, str+`></script>`)
return err
} else {
str := `<script src="/assets/js/` + assetName + `"`
if opts.IsDefer {
str += ` defer`
}
if opts.IsAsync {
str += ` async`
}
_, err := io.WriteString(w, str+`></script>`)
return err
}
}
})
}