-
Notifications
You must be signed in to change notification settings - Fork 6
/
function.go
65 lines (55 loc) · 1.78 KB
/
function.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
package javascript
import (
"encoding/gob"
"encoding/json"
)
// Function represents the result of a function call to a global function or function in an object referenced from
// global space. The purpose
// of this is to immediately use the results of the function call, as opposed to a Closure, which stores a pointer
// to a function that is used later.
// context will become the "this" value inside the closure
// args will be passed as values, and strings will be quoted. To pass a variable name, wrap the name with a JsCode call.
func Function(name string, context string, args... interface{}) Ωfunction {
return Ωfunction{name, context, args}
}
type Ωfunction struct {
// The function name
Name string
// If given, the object in the window object which contains the function and is the context for the function.
// Use dot '.' notation to traverse the object tree. i.e. "obj1.obj2" refers to window.obj1.obj2 in javascript
Context string
// Function arguments. Strings will be quoted. Use a JsCode object to output the name of a javascript variable.
Args []interface{}
}
func (f Ωfunction) JavaScript() string {
var args string
if f.Args != nil {
args = Arguments(f.Args).JavaScript()
}
fName := f.Name
if f.Context != "" {
fName = f.Context + "." + fName
}
return fName + "(" + args + ")"
}
/**
* Returns this as a json object to be sent to qcubed.js during ajax drawing.
* @return mixed
*/
func (f Ωfunction) MarshalJSON() (buf []byte, err error) {
var obj = map[string]interface{}{}
obj[JsonObjectType] = "function"
obj["func"] = f.Name
if f.Context != "" {
obj["context"] = f.Context
}
if f.Args != nil {
obj["params"] = f.Args
}
buf, err = json.Marshal(obj)
return
}
func init() {
// Register objects so they can be serialized
gob.Register(Ωfunction{})
}