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