event-handling.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package e00_basics
  2. import (
  3. "fmt"
  4. "net/url"
  5. "time"
  6. . "github.com/qor5/ui/vuetify"
  7. "github.com/qor5/web"
  8. . "github.com/theplant/htmlgo"
  9. )
  10. // @snippet_begin(EventHandlingURLSample)
  11. func EventHandlingURL(ctx *web.EventContext) (pr web.PageResponse, err error) {
  12. pr.Body = Div(
  13. VCard(
  14. VCardTitle(Text("URL")),
  15. VCardActions(VBtn("Go").Attr("@click", web.GET().URL(EventExamplePagePath).Go())),
  16. ),
  17. )
  18. return
  19. }
  20. // @snippet_end
  21. // @snippet_begin(EventHandlingPushStateSample)
  22. func EventHandlingPushState(ctx *web.EventContext) (pr web.PageResponse, err error) {
  23. pr.Body = Div(
  24. VCard(
  25. VCardTitle(Text("PushState")),
  26. VCardActions(VBtn("Go").Attr("@click", web.GET().URL(EventExamplePagePath).PushState(true).Go())),
  27. ),
  28. )
  29. return
  30. }
  31. // @snippet_end
  32. // @snippet_begin(EventHandlingReloadSample)
  33. func EventHandlingReload(ctx *web.EventContext) (pr web.PageResponse, err error) {
  34. pr.Body = Div(
  35. VCard(
  36. VCardTitle(Text("Reload")),
  37. Text(fmt.Sprintf("Now: %s", time.Now().Format(time.RFC3339Nano))),
  38. VCardActions(VBtn("Reload").Attr("@click", web.POST().Reload().Go())),
  39. ),
  40. )
  41. return
  42. }
  43. // @snippet_end
  44. // @snippet_begin(EventHandlingQuerySample)
  45. func EventHandlingQuery(ctx *web.EventContext) (pr web.PageResponse, err error) {
  46. pr.Body = Div(
  47. VCard(
  48. VCardTitle(Text("Query")),
  49. VCardActions(VBtn("Go").Attr("@click", web.GET().URL(EventExamplePagePath).PushState(true).Query("address", "tokyo").Go())),
  50. ),
  51. )
  52. return
  53. }
  54. // @snippet_end
  55. // @snippet_begin(EventHandlingMergeQuerySample)
  56. func EventHandlingMergeQuery(ctx *web.EventContext) (pr web.PageResponse, err error) {
  57. pr.Body = Div(
  58. VCard(
  59. VCardTitle(Text("MergeQuery")),
  60. 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())),
  61. ),
  62. )
  63. return
  64. }
  65. // @snippet_end
  66. // @snippet_begin(EventHandlingClearMergeQuerySample)
  67. func EventHandlingClearMergeQueryQuery(ctx *web.EventContext) (pr web.PageResponse, err error) {
  68. pr.Body = Div(
  69. VCard(
  70. VCardTitle(Text("ClearMergeQuery")),
  71. 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())),
  72. ),
  73. )
  74. return
  75. }
  76. // @snippet_end
  77. // @snippet_begin(EventHandlingStringQuerySample)
  78. func EventHandlingStringQuery(ctx *web.EventContext) (pr web.PageResponse, err error) {
  79. pr.Body = Div(
  80. VCard(
  81. VCardTitle(Text("StringQuery")),
  82. VCardActions(VBtn("Go").Attr("@click", web.GET().URL(EventExamplePagePath).PushState(true).StringQuery("address=tokyo").Go())),
  83. ),
  84. )
  85. return
  86. }
  87. // @snippet_end
  88. // @snippet_begin(EventHandlingQueriesSample)
  89. func EventHandlingQueries(ctx *web.EventContext) (pr web.PageResponse, err error) {
  90. pr.Body = Div(
  91. VCard(
  92. VCardTitle(Text("Queries")),
  93. VCardActions(VBtn("Go").Attr("@click", web.GET().URL(EventExamplePagePath).PushState(true).Queries(url.Values{"address": []string{"tokyo"}}).Go())),
  94. ),
  95. )
  96. return
  97. }
  98. // @snippet_end
  99. // @snippet_begin(EventHandlingPushStateURLSample)
  100. func EventHandlingPushStateURL(ctx *web.EventContext) (pr web.PageResponse, err error) {
  101. pr.Body = Div(
  102. VCard(
  103. VCardTitle(Text("PushStateURL")),
  104. VCardActions(VBtn("Go").Attr("@click", web.GET().PushStateURL(EventExamplePagePath).Go())),
  105. ),
  106. )
  107. return
  108. }
  109. // @snippet_end
  110. // @snippet_begin(EventHandlingLocationSample)
  111. func EventHandlingLocation(ctx *web.EventContext) (pr web.PageResponse, err error) {
  112. pr.Body = Div(
  113. VCard(
  114. VCardTitle(Text("Location")),
  115. VCardActions(VBtn("Go").Attr("@click", web.POST().PushState(true).Location(&web.LocationBuilder{MyURL: EventExamplePagePath, MyStringQuery: "address=test"}).Go())),
  116. ),
  117. )
  118. return
  119. }
  120. // @snippet_end
  121. // @snippet_begin(EventHandlingFieldValueSample)
  122. func EventHandlingFileValue(ctx *web.EventContext) (pr web.PageResponse, err error) {
  123. pr.Body = Div(
  124. VCard(
  125. VCardTitle(Text("FieldValue")),
  126. VCardActions(VBtn("Go").Attr("@click", web.POST().EventFunc("form").FieldValue("name", "qor5").Go())),
  127. ),
  128. )
  129. return
  130. }
  131. // @snippet_end
  132. // @snippet_begin(EventHandlingFormClearSample)
  133. func EventHandlingFormClear(ctx *web.EventContext) (pr web.PageResponse, err error) {
  134. pr.Body = Div(
  135. VCard(
  136. VCardTitle(Text("FormClear")),
  137. VCardActions(VBtn("Go").Attr("@click", web.POST().EventFunc("form").FieldValue("name", "qor5").FormClear().Go())),
  138. ),
  139. )
  140. return
  141. }
  142. // @snippet_end
  143. // @snippet_begin(EventHandlingEventFuncSample)
  144. func EventHandlingEventFunc(ctx *web.EventContext) (pr web.PageResponse, err error) {
  145. pr.Body = Div(
  146. VBtn("Go").Attr("@click", web.POST().EventFunc("hello").Go()),
  147. )
  148. return
  149. }
  150. // @snippet_end
  151. // @snippet_begin(EventHandlingBeforeScriptSample)
  152. func EventHandlingScript(ctx *web.EventContext) (pr web.PageResponse, err error) {
  153. pr.Body = Div(
  154. VCard(
  155. VCardTitle(Text("Script")),
  156. 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())),
  157. ),
  158. )
  159. return
  160. }
  161. // @snippet_end
  162. // @snippet_begin(EventHandlingRawSample)
  163. func EventHandlingRaw(ctx *web.EventContext) (pr web.PageResponse, err error) {
  164. pr.Body = Div(
  165. VCard(
  166. VCardTitle(Text("Raw")),
  167. VCardActions(VBtn("Go").Attr("@click", web.POST().Raw(`pushStateURL("/samples/event_handling/example")`).Go())),
  168. ),
  169. )
  170. return
  171. }
  172. // @snippet_end
  173. func EventHandlingPage(ctx *web.EventContext) (pr web.PageResponse, err error) {
  174. api := ctx.R.URL.Query().Get("api")
  175. switch api {
  176. case "url":
  177. return EventHandlingURL(ctx)
  178. case "pushstate":
  179. return EventHandlingPushState(ctx)
  180. case "eventfunc":
  181. return EventHandlingEventFunc(ctx)
  182. case "reload":
  183. return EventHandlingReload(ctx)
  184. case "query":
  185. return EventHandlingQuery(ctx)
  186. case "merge_query":
  187. return EventHandlingMergeQuery(ctx)
  188. case "clear_merge_query":
  189. return EventHandlingClearMergeQueryQuery(ctx)
  190. case "string_query":
  191. return EventHandlingStringQuery(ctx)
  192. case "queries":
  193. return EventHandlingQueries(ctx)
  194. case "pushstateurl":
  195. return EventHandlingPushStateURL(ctx)
  196. case "fieldvalue":
  197. return EventHandlingFileValue(ctx)
  198. case "formclear":
  199. return EventHandlingFormClear(ctx)
  200. case "script":
  201. return EventHandlingScript(ctx)
  202. case "location":
  203. return EventHandlingLocation(ctx)
  204. case "raw":
  205. return EventHandlingRaw(ctx)
  206. default:
  207. pr.Body = Div()
  208. return
  209. }
  210. }
  211. func ExamplePage(ctx *web.EventContext) (pr web.PageResponse, err error) {
  212. pr.Body = Div(
  213. H1("ExamplePage"),
  214. )
  215. return
  216. }
  217. var ExamplePagePB = web.Page(ExamplePage).
  218. EventFunc("form", func(ctx *web.EventContext) (r web.EventResponse, err error) {
  219. r.VarsScript = fmt.Sprintf(`alert("form data is %s")`, ctx.R.FormValue("name"))
  220. return
  221. }).
  222. EventFunc("hello", func(ctx *web.EventContext) (r web.EventResponse, err error) {
  223. r.VarsScript = `alert("Hello World")`
  224. return
  225. })
  226. var EventHandlingPagePB = web.Page(EventHandlingPage)
  227. const EventHandlingPagePath = "/samples/event_handling"
  228. const EventExamplePagePath = "/samples/event_handling/example"