-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_url.go
95 lines (85 loc) · 2.19 KB
/
context_url.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
package admin
import (
"path"
"github.com/go-aorm/aorm"
)
// RoutePrefix return route prefix of resource
func (this *Resource) RoutePrefix() string {
var params string
for this.ParentResource != nil {
params = path.Join(this.ParentResource.ToParam(), this.ParentResource.ParamIDPattern(), params)
this = this.ParentResource
}
return params
}
// URLFor generate url for resource value
// context.URLFor(&Product{})
// context.URLFor(&Product{ID: 111})
// context.URLFor(productResource)
func (this *Context) URLFor(value interface{}, resources ...*Resource) string {
if admin, ok := value.(*Admin); ok {
return this.Path(admin.Config.MountPath)
} else if urler, ok := value.(interface {
URL(*Context) string
}); ok {
return urler.URL(this)
} else if res, ok := value.(*Resource); ok {
return res.GetContextIndexURI(this)
} else {
if len(resources) == 0 {
return ""
}
res := resources[0]
if res.Config.Singleton {
return res.GetIndexLink(this, this.ParentResourceID)
}
/*
var (
ids []aorm.ID
self = this
i = len(res.Parents)
)
for ;i > 0;i++ {
if pres := self.Resource; pres == res.Parents[i] {
ids = append(ids, self.ResourceID)
self = self.Parent
}
}
*/
uri := res.GetLink(this, value, this.Context, this.ParentResourceID)
return uri
}
return this.Path("")
}
// URLFor generate url for resource value
// context.URLFor(&Product{})
// context.URLFor(&Product{ID: 111})
// context.URLFor(productResource)
func (this *Context) TopURLFor(value interface{}, resources ...*Resource) string {
var res *Resource
if len(resources) == 0 {
res = this.Resource
} else {
res = resources[0]
}
res = res.Top()
if value == nil {
return this.Path(res.GetIndexURI(this))
}
if vs, ok := value.(string); ok {
if vs == "" {
return this.Path(res.GetIndexURI(this))
}
if id, err := res.ParseID(vs); err != nil {
return "[[parse id failed: " + err.Error() + "]]"
} else {
return this.Path(res.GetURI(this, id))
}
} else if vs, ok := value.(aorm.ID); ok {
if vs == nil {
return this.Path(res.GetIndexURI(this))
}
return this.Path(res.GetURI(this, vs))
}
return this.Path(res.URLFor(this, value))
}