forked from emad-elsaid/xlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgets.go
54 lines (47 loc) · 1.83 KB
/
widgets.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
package xlog
import (
"html/template"
"sort"
)
func init() {
RegisterHelper("widgets", RenderWidget)
}
type (
// WidgetSpace used to represent a widgets spaces. it's used to register
// widgets to be injected in the view or edit pages
WidgetSpace string
// WidgetFunc a function that takes the current page and returns the widget.
// This can be used by extensions to define new widgets to be rendered in
// view or edit pages. the extension should define this func type and
// register it to be rendered in a specific widgetSpace such as before or
// after the page.
WidgetFunc func(Page) template.HTML
)
// List of widgets spaces that extensions can use to register a WidgetFunc to
// inject content into.
var (
AFTER_VIEW_WIDGET WidgetSpace = "after_view" // widgets rendered after the content of the view page
BEFORE_VIEW_WIDGET WidgetSpace = "before_view" // widgets rendered before the content of the view page
HEAD_WIDGET WidgetSpace = "head" // widgets rendered in page <head> tag
)
// A map to keep track of list of widget functions registered in each widget space
var widgets = map[WidgetSpace]byPriority[WidgetFunc]{}
// RegisterWidget Register a function to a widget space. functions registered
// will be executed in order of priority lower to higher when rendering view or
// edit page. the return values of these widgetfuncs will pass down to the
// template and injected in reserved places.
func RegisterWidget(s WidgetSpace, priority float32, f WidgetFunc) {
widgets[s] = append(widgets[s], priorityItem[WidgetFunc]{
priority: priority,
value: f,
})
sort.Sort(widgets[s])
}
// This is used by view and edit routes to render all widgetfuncs registered for
// specific widget space.
func RenderWidget(s WidgetSpace, p Page) (o template.HTML) {
for _, f := range widgets[s] {
o += f.value(p)
}
return
}