page-transition.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package e00_basics
  2. import (
  3. "fmt"
  4. "net/url"
  5. "github.com/qor5/web"
  6. . "github.com/theplant/htmlgo"
  7. )
  8. var page1Title = "Page 1"
  9. // @snippet_begin(PageTransitionSample)
  10. const Page1Path = "/samples/page_1"
  11. const Page2Path = "/samples/page_2"
  12. func Page1(ctx *web.EventContext) (pr web.PageResponse, err error) {
  13. pr.Body = Div(
  14. H1(page1Title),
  15. Ul(
  16. Li(
  17. A().Href(Page2Path).
  18. Text("To Page 2 With Normal Link"),
  19. ),
  20. Li(
  21. A().Href("javascript:;").
  22. Text("To Page 2 With Push State Link").
  23. Attr("@click", web.POST().PushStateURL(Page2Path).Go()),
  24. ),
  25. ),
  26. fromParam(ctx),
  27. ).Style("color: green; font-size: 24px;")
  28. return
  29. }
  30. func Page2(ctx *web.EventContext) (pr web.PageResponse, err error) {
  31. pr.Body = Div(
  32. H1("Page 2"),
  33. Ul(
  34. Li(
  35. A().Href("javascript:;").
  36. Text("To Page 1 With Normal Link").
  37. Attr("@click", web.POST().
  38. PushStateURL(Page1Path).
  39. Queries(url.Values{"from": []string{"page 2 link 1"}}).
  40. Go()),
  41. ),
  42. Li(
  43. Button("Do an action then go to Page 1 with push state and parameters").
  44. Attr("@click", web.POST().EventFunc("doAction2").Query("id", "42").Go()),
  45. ),
  46. Li(
  47. Button("Do an action then go to Page 1 with redirect url").
  48. Attr("@click", web.POST().EventFunc("doAction1").Query("id", "41").Go()),
  49. ),
  50. ),
  51. ).Style("color: orange; font-size: 24px;")
  52. return
  53. }
  54. func fromParam(ctx *web.EventContext) HTMLComponent {
  55. var from HTMLComponent
  56. val := ctx.R.FormValue("from")
  57. if len(val) > 0 {
  58. from = Components(
  59. B("from:"),
  60. Text(val),
  61. )
  62. }
  63. return from
  64. }
  65. func doAction1(ctx *web.EventContext) (er web.EventResponse, err error) {
  66. updateDatabase(ctx.QueryAsInt("id"))
  67. er.RedirectURL = Page1Path + "?" + url.Values{"from": []string{"page2 with redirect"}}.Encode()
  68. return
  69. }
  70. func doAction2(ctx *web.EventContext) (er web.EventResponse, err error) {
  71. updateDatabase(ctx.QueryAsInt("id"))
  72. er.PushState = web.Location(url.Values{"from": []string{"page2"}}).
  73. URL(Page1Path)
  74. return
  75. }
  76. var Page1PB = web.Page(Page1)
  77. var Page2PB = web.Page(Page2).
  78. EventFunc("doAction1", doAction1).
  79. EventFunc("doAction2", doAction2)
  80. // @snippet_end
  81. func updateDatabase(val int) {
  82. page1Title = fmt.Sprintf("Page 1 (Updated by Page2 to %d)", val)
  83. }