123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- package e00_basics
- import (
- "fmt"
- "net/url"
- "time"
- . "github.com/qor5/ui/vuetify"
- "github.com/qor5/web"
- . "github.com/theplant/htmlgo"
- )
- // @snippet_begin(EventHandlingURLSample)
- func EventHandlingURL(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("URL")),
- VCardActions(VBtn("Go").Attr("@click", web.GET().URL(EventExamplePagePath).Go())),
- ),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingPushStateSample)
- func EventHandlingPushState(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("PushState")),
- VCardActions(VBtn("Go").Attr("@click", web.GET().URL(EventExamplePagePath).PushState(true).Go())),
- ),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingReloadSample)
- func EventHandlingReload(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("Reload")),
- Text(fmt.Sprintf("Now: %s", time.Now().Format(time.RFC3339Nano))),
- VCardActions(VBtn("Reload").Attr("@click", web.POST().Reload().Go())),
- ),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingQuerySample)
- func EventHandlingQuery(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("Query")),
- VCardActions(VBtn("Go").Attr("@click", web.GET().URL(EventExamplePagePath).PushState(true).Query("address", "tokyo").Go())),
- ),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingMergeQuerySample)
- func EventHandlingMergeQuery(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("MergeQuery")),
- VCardActions(VBtn("Go").Attr("@click", web.GET().URL(EventExamplePagePath+"?address=beijing&name=qor5&email=qor5@theplant.jp").PushState(true).Query("address", "tokyo").MergeQuery(true).Go())),
- ),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingClearMergeQuerySample)
- func EventHandlingClearMergeQueryQuery(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("ClearMergeQuery")),
- VCardActions(VBtn("Go").Attr("@click", web.GET().URL(EventExamplePagePath+"?address=beijing&name=qor5&email=qor5@theplant.jp").PushState(true).Query("address", "tokyo").ClearMergeQuery([]string{"name"}).Go())),
- ),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingStringQuerySample)
- func EventHandlingStringQuery(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("StringQuery")),
- VCardActions(VBtn("Go").Attr("@click", web.GET().URL(EventExamplePagePath).PushState(true).StringQuery("address=tokyo").Go())),
- ),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingQueriesSample)
- func EventHandlingQueries(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("Queries")),
- VCardActions(VBtn("Go").Attr("@click", web.GET().URL(EventExamplePagePath).PushState(true).Queries(url.Values{"address": []string{"tokyo"}}).Go())),
- ),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingPushStateURLSample)
- func EventHandlingPushStateURL(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("PushStateURL")),
- VCardActions(VBtn("Go").Attr("@click", web.GET().PushStateURL(EventExamplePagePath).Go())),
- ),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingLocationSample)
- func EventHandlingLocation(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("Location")),
- VCardActions(VBtn("Go").Attr("@click", web.POST().PushState(true).Location(&web.LocationBuilder{MyURL: EventExamplePagePath, MyStringQuery: "address=test"}).Go())),
- ),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingFieldValueSample)
- func EventHandlingFileValue(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("FieldValue")),
- VCardActions(VBtn("Go").Attr("@click", web.POST().EventFunc("form").FieldValue("name", "qor5").Go())),
- ),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingFormClearSample)
- func EventHandlingFormClear(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("FormClear")),
- VCardActions(VBtn("Go").Attr("@click", web.POST().EventFunc("form").FieldValue("name", "qor5").FormClear().Go())),
- ),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingEventFuncSample)
- func EventHandlingEventFunc(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VBtn("Go").Attr("@click", web.POST().EventFunc("hello").Go()),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingBeforeScriptSample)
- func EventHandlingScript(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("Script")),
- VCardActions(VBtn("Go").Attr("@click", web.POST().ThenScript(`alert("this is then script")`).AfterScript(`alert("this is after script")`).BeforeScript(`alert("this is before script")`).Go())),
- ),
- )
- return
- }
- // @snippet_end
- // @snippet_begin(EventHandlingRawSample)
- func EventHandlingRaw(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- VCard(
- VCardTitle(Text("Raw")),
- VCardActions(VBtn("Go").Attr("@click", web.POST().Raw(`pushStateURL("/samples/event_handling/example")`).Go())),
- ),
- )
- return
- }
- // @snippet_end
- func EventHandlingPage(ctx *web.EventContext) (pr web.PageResponse, err error) {
- api := ctx.R.URL.Query().Get("api")
- switch api {
- case "url":
- return EventHandlingURL(ctx)
- case "pushstate":
- return EventHandlingPushState(ctx)
- case "eventfunc":
- return EventHandlingEventFunc(ctx)
- case "reload":
- return EventHandlingReload(ctx)
- case "query":
- return EventHandlingQuery(ctx)
- case "merge_query":
- return EventHandlingMergeQuery(ctx)
- case "clear_merge_query":
- return EventHandlingClearMergeQueryQuery(ctx)
- case "string_query":
- return EventHandlingStringQuery(ctx)
- case "queries":
- return EventHandlingQueries(ctx)
- case "pushstateurl":
- return EventHandlingPushStateURL(ctx)
- case "fieldvalue":
- return EventHandlingFileValue(ctx)
- case "formclear":
- return EventHandlingFormClear(ctx)
- case "script":
- return EventHandlingScript(ctx)
- case "location":
- return EventHandlingLocation(ctx)
- case "raw":
- return EventHandlingRaw(ctx)
- default:
- pr.Body = Div()
- return
- }
- }
- func ExamplePage(ctx *web.EventContext) (pr web.PageResponse, err error) {
- pr.Body = Div(
- H1("ExamplePage"),
- )
- return
- }
- var ExamplePagePB = web.Page(ExamplePage).
- EventFunc("form", func(ctx *web.EventContext) (r web.EventResponse, err error) {
- r.VarsScript = fmt.Sprintf(`alert("form data is %s")`, ctx.R.FormValue("name"))
- return
- }).
- EventFunc("hello", func(ctx *web.EventContext) (r web.EventResponse, err error) {
- r.VarsScript = `alert("Hello World")`
- return
- })
- var EventHandlingPagePB = web.Page(EventHandlingPage)
- const EventHandlingPagePath = "/samples/event_handling"
- const EventExamplePagePath = "/samples/event_handling/example"
|