-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.go
95 lines (83 loc) · 2.51 KB
/
component.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 main
import (
"fmt"
"strings"
)
type Component struct {
ResourceOptions
}
func (c *Component) GetDir() string {
return "components"
}
func (c *Component) GetFileName() string {
if c.IsApi {
return c.ResourceOptions.Domain + "s.js"
} else {
return c.ResourceOptions.Domain + ".js"
}
}
func (c *Component) GetFileContent() string {
domainFirstCharUpper := strings.ToUpper(c.Domain[:1]) + c.Domain[1:]
return fmt.Sprintf(c.getTemplate(), domainFirstCharUpper, c.Domain)
}
func (c *Component) getTemplate() string {
content := c.getHeaderTemplate()
content = content + c.getBodyTemplateVariation()
return content
}
func (c *Component) getHeaderTemplate() string {
return "" +
"var React = require('react');\n" +
"var %[1]sStore = require('../stores/%[2]s-store');\n\n"
}
func (c *Component) getBodyTemplateVariation() string {
common := c.getCommonBodyTemplate();
if c.IsApi {
return "" +
"function getState() {\n" +
" return {\n" +
" %[2]ss: ToastStore.get%[1]ss()\n" +
" };\n" +
"}\n\n" +
"var %[1]ss = React.createClass({\n" +
common +
" render: function() {\n" +
" return (\n" +
" <h1>%[1]ss Component</h1>\n" +
" );\n" +
" }\n" +
"});\n\n" +
"module.exports = %[1]ss;"
} else {
return "" +
"function getState() {\n" +
" return {\n" +
" %[2]s: ToastStore.get%[1]s()\n" +
" };\n" +
"}\n\n" +
"var %[1]s = React.createClass({\n" +
common +
" render: function() {\n" +
" return (\n" +
" <h1>%[1]s Component</h1>\n" +
" );\n" +
" }\n" +
"});\n\n" +
"module.exports = %[1]s;"
}
}
func (c *Component) getCommonBodyTemplate() string {
return "" +
" getInitialState: function() {\n" +
" return getState();\n" +
" },\n\n" +
" componentDidMount: function() {\n" +
" %[1]sStore.addChangeListener(this._onChange);\n" +
" },\n\n" +
" componentWillUnmount: function() {\n" +
" %[1]sStore.removeChangeListener(this._onChange);\n" +
" },\n\n" +
" _onChange: function() {\n" +
" this.setState(getState());\n" +
" },\n\n"
}