forked from YaoApp/gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.go
55 lines (47 loc) · 1.51 KB
/
widget.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
package gou
import (
"strings"
"github.com/yaoapp/gou/widget"
"github.com/yaoapp/kun/exception"
"github.com/yaoapp/kun/log"
)
// WidgetHandlers widget process handlers
var WidgetHandlers = map[string]ProcessHandler{
"reload": processReloadWidgetInstance,
}
// WidgetCustomHandlers custom widget handlers
var WidgetCustomHandlers = map[string]map[string]ProcessHandler{}
// LoadWidget load widgets
func LoadWidget(path string, name string, register widget.ModuleRegister) (*widget.Widget, error) {
_, err := widget.Load(path, Yao, customProcessRegister(), register)
if err != nil {
return nil, err
}
return widget.Widgets[name], nil
}
func customProcessRegister() widget.ProcessRegister {
return func(widget, name string, handler func(args ...interface{}) interface{}) error {
widget = strings.ToLower(widget)
name = strings.ToLower(name)
log.Info("[Widget] Register Process widgets.%s.%s", widget, name)
if _, has := WidgetCustomHandlers[widget]; !has {
WidgetCustomHandlers[widget] = map[string]ProcessHandler{}
}
WidgetCustomHandlers[widget][name] = func(process *Process) interface{} {
return handler(process.Args...)
}
return nil
}
}
func processReloadWidgetInstance(process *Process) interface{} {
process.ValidateArgNums(2)
widgetName := process.Class
name := process.ArgsString(0)
source := process.ArgsString(1)
w, has := widget.Widgets[widgetName]
if !has {
exception.New("widget %s does not load", 400, widgetName).Throw()
return nil
}
return w.ReloadInstance([]byte(source), name)
}