github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/examples/sites/present/app.go (about) 1 // Template generated by reactGen 2 3 package main 4 5 import ( 6 "bytes" 7 "net/url" 8 "strconv" 9 "strings" 10 11 "honnef.co/go/js/dom" 12 "honnef.co/go/js/xhr" 13 "myitcv.io/react" 14 ) 15 16 const ( 17 queryParamHideAddressBar = "hideAddressBar" 18 queryParamURL = "url" 19 ) 20 21 type AppDef struct { 22 react.ComponentDef 23 } 24 25 type AppState struct { 26 URL string 27 Slides string 28 Error string 29 Status string 30 HideAddressBar bool 31 } 32 33 func App() *AppElem { 34 return buildAppElem() 35 } 36 37 func (a AppDef) GetInitialState() AppState { 38 loc := dom.GetWindow().Location() 39 u, err := url.Parse(loc.String()) 40 if err != nil { 41 panic(err) 42 } 43 44 res := AppState{} 45 46 res.URL = u.Query().Get(queryParamURL) 47 48 if b, err := strconv.ParseBool(u.Query().Get(queryParamHideAddressBar)); err == nil && b { 49 res.HideAddressBar = true 50 } 51 52 return res 53 } 54 55 func (a AppDef) ComponentWillMount() { 56 go func() { 57 if err := initTemplates("."); err != nil { 58 panic(err) 59 } 60 61 if u := a.State().URL; u != "" { 62 a.newUrl(u) 63 } 64 }() 65 } 66 67 func (a AppDef) Render() react.Element { 68 s := a.State() 69 70 var contents []react.Element 71 72 if s.Slides != "" { 73 contents = append(contents, react.IFrame(&react.IFrameProps{ 74 SrcDoc: s.Slides, 75 Style: &react.CSS{ 76 Width: "100%", 77 Height: "100%", 78 }, 79 })) 80 } else { 81 if s.Error != "" { 82 contents = append(contents, 83 react.Div(&react.DivProps{ClassName: "placeholder error"}, react.S(s.Error)), 84 ) 85 } else if s.Status != "" { 86 contents = append(contents, 87 react.Div(&react.DivProps{ClassName: "placeholder status"}, react.S(s.Status)), 88 ) 89 } else { 90 contents = append(contents, 91 react.Div(&react.DivProps{ClassName: "placeholder arrow"}, react.S("\u21E7")), 92 react.Div(&react.DivProps{ClassName: "placeholder text"}, react.S("Enter slides URL")), 93 ) 94 } 95 } 96 97 var addressBar react.Element 98 99 if !s.HideAddressBar { 100 addressBar = react.Input(&react.InputProps{ 101 Placeholder: "Slides URL", 102 ID: "addressbar", 103 OnChange: urlChange{a}, 104 Value: s.URL, 105 }) 106 } 107 108 return react.Div( 109 &react.DivProps{ClassName: "box"}, 110 react.Div(&react.DivProps{ClassName: "row header"}, 111 addressBar, 112 ), 113 react.Div(&react.DivProps{ClassName: "row content"}, 114 contents..., 115 ), 116 ) 117 } 118 119 func (a AppDef) newUrl(u string) { 120 st := a.State() 121 st.URL = u 122 st.Status = "Loading..." 123 a.SetState(st) 124 125 if u == "" { 126 return 127 } 128 129 go func() { 130 req := xhr.NewRequest("GET", u) 131 132 err := req.Send(nil) 133 134 st := a.State() 135 136 if err != nil { 137 st.Error = "Invalid URL" 138 a.SetState(st) 139 return 140 } 141 142 st.Status = "Loading..." 143 a.SetState(st) 144 145 out := new(bytes.Buffer) 146 in := strings.NewReader(req.ResponseText) 147 148 err = renderDoc(out, u, in) 149 if err != nil { 150 panic(err) 151 } 152 153 st = a.State() 154 st.Slides = out.String() 155 a.SetState(st) 156 }() 157 } 158 159 type urlChange struct{ AppDef } 160 161 func (uc urlChange) OnChange(se *react.SyntheticEvent) { 162 target := se.Target().(*dom.HTMLInputElement) 163 u := target.Value 164 165 uc.newUrl(u) 166 }