-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.go
73 lines (58 loc) · 1.32 KB
/
app.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
package wade
import (
"fmt"
gourl "net/url"
"path"
"github.com/gowade/wade/dom"
"github.com/gowade/wade/driver"
)
var (
app Application
)
type Application struct {
BasePath string
Router driver.Router
Container dom.Node
}
func App() Application {
return app
}
func (z Application) SetURLPath(newPath string) {
url, err := gourl.Parse(path.Join(app.BasePath, newPath))
if err != nil {
panic(err)
}
driver.GetRouteDriver().SetURL(url, true)
}
func InitApp(basepath string, router driver.Router, container dom.Node) {
if !path.IsAbs(basepath) {
panic(fmt.Errorf(`application base path `+
`must be a valid absolute path, got "%v"`, basepath))
}
app = Application{
BasePath: path.Clean(basepath),
Router: router,
Container: container,
}
driver.Init(router)
router.Build()
url := driver.GetRouteDriver().URL()
router.Render(url)
}
func Route(routeName string, params ...interface{}) string {
if app.Router == nil {
return "/"
}
route, ok := app.Router.RouteByName(routeName)
if !ok {
panic(fmt.Errorf(`there's no route named "%v"`, routeName))
}
return app.Router.PathFromRoute(route, params...)
}
func FindContainer(query string) dom.Node {
ret := dom.GetDocument().Find(query)
if len(ret) == 0 {
panic(fmt.Errorf(`No DOM element found for query "%v"`, query))
}
return ret[0]
}