common.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package containers
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/qor5/admin/media/media_library"
  6. "github.com/qor5/admin/presets"
  7. v "github.com/qor5/ui/vuetify"
  8. "github.com/qor5/web"
  9. . "github.com/theplant/htmlgo"
  10. )
  11. const (
  12. Blue = "blue"
  13. Orange = "orange"
  14. White = "white"
  15. Grey = "grey"
  16. )
  17. var BackgroundColors = []string{White, Grey, Blue}
  18. var FontColors = []string{Blue, Orange, White}
  19. const LINK_ARROW_SVG = RawHTML(`<svg height=".7em" viewBox="0 0 10 12" fill="none" xmlns="http://www.w3.org/2000/svg">
  20. <path d="M0 11.4381V0.561882C0 0.315846 0.133038 0.14941 0.399113 0.0625736C0.67997 -0.0387357 0.938655 -0.017027 1.17517 0.1277L9.31264 4.99053C9.51959 5.12078 9.68219 5.26551 9.80044 5.42471C9.93348 5.58391 10 5.77929 10 6.01085C10 6.24242 9.93348 6.4378 9.80044 6.597C9.68219 6.74173 9.51959 6.87922 9.31264 7.00947L1.17517 11.8723C0.938655 12.017 0.67997 12.0387 0.399113 11.9374C0.133038 11.8361 0 11.6697 0 11.4381Z" fill="currentColor"/>
  21. </svg>`)
  22. var TextArea = func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) HTMLComponent {
  23. return v.VTextarea().FieldName(field.Name).Label(field.Label).Value(field.Value(obj))
  24. }
  25. func ContainerWrapper(containerID, anchorID, classes,
  26. backgroundColor, transitionBackgroundColor, fontColor,
  27. imagePosition string, addTopSpace, addBottomSpace bool,
  28. isEditor bool, isReadonly bool, style string, comp ...HTMLComponent) HTMLComponent {
  29. r := Div(comp...).
  30. Id(anchorID).
  31. Class("container-instance").ClassIf(classes, classes != "").
  32. AttrIf("data-background-color", backgroundColor, backgroundColor != "").
  33. AttrIf("data-transition-background-color", transitionBackgroundColor, transitionBackgroundColor != "").
  34. AttrIf("data-font-color", fontColor, fontColor != "").
  35. AttrIf("data-image-position", imagePosition, imagePosition != "").
  36. AttrIf("data-container-top-space", "true", addTopSpace).
  37. AttrIf("data-container-bottom-space", "true", addBottomSpace).
  38. Attr("data-container-id", containerID).Style("position:relative;").StyleIf(style, style != "")
  39. if isEditor {
  40. if isReadonly {
  41. r.AppendChildren(RawHTML(`<div class="wrapper-shadow"></div>`))
  42. } else {
  43. r.AppendChildren(RawHTML(fmt.Sprintf(`<div class="wrapper-shadow" onclick="window.parent.postMessage('%s', '*');"><button><i aria-hidden="true" class="material-icons">edit</i></button></div>`, containerID)))
  44. }
  45. }
  46. return r
  47. }
  48. func LinkTextWithArrow(text, link string, class ...string) HTMLComponent {
  49. if text == "" || link == "" {
  50. return nil
  51. }
  52. c := "link-arrow"
  53. if len(class) > 0 {
  54. class = append(class, c)
  55. c = strings.Join(class, " ")
  56. }
  57. return A(Span(text), LINK_ARROW_SVG).Class(c).Href(link)
  58. }
  59. func LazyImageHtml(m media_library.MediaBox, class ...string) HTMLComponent {
  60. class = append(class, "lazyload")
  61. return Img("").Attr("data-src", m.URL()).Alt(m.Description).Class(class...)
  62. }
  63. func ImageHtml(m media_library.MediaBox, class ...string) HTMLComponent {
  64. return Img("").Attr("src", m.URL()).Alt(m.Description).Class(class...)
  65. }