-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.go
106 lines (103 loc) · 2.74 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
96
97
98
99
100
101
102
103
104
105
106
package go2css
import (
"fmt"
)
type (
ComponentConfigurator struct {
rule *Rule
traitRules []*Rule
stylesheet *Stylesheet
nested map[string]*Rule
states []string
modifiers []string
}
)
func (cfg *ComponentConfigurator) State(name string, setup func(*StateConfigurator)) {
_, exists := cfg.nested[name]
if exists {
panic(fmt.Sprintf("\"%s\" already defined", name))
}
selector := fmt.Sprintf("%s-%s", cfg.rule.selectors[0], name)
rule := &Rule{
genesisID: STATE_RULE_GENESIS_ID,
selectors: []string{
selector,
},
declarations: []*Declaration{},
}
scfg := &StateConfigurator{
rule: rule,
stylesheet: cfg.stylesheet,
nested: map[string]*Rule{},
}
setup(scfg)
cfg.nested[name] = rule
cfg.states = append(cfg.states, name)
}
func (cfg *ComponentConfigurator) Modifier(name string, setup func(*ModifierConfigurator)) {
_, exists := cfg.nested[name]
if exists {
panic(fmt.Sprintf("\"%s\" already defined", name))
}
selector := fmt.Sprintf("%s-%s", cfg.rule.selectors[0], name)
rule := &Rule{
genesisID: MODIFIER_RULE_GENESIS_ID,
selectors: []string{
selector,
},
declarations: []*Declaration{},
}
mcfg := &ModifierConfigurator{
rule: rule,
stylesheet: cfg.stylesheet,
}
setup(mcfg)
cfg.nested[name] = rule
cfg.modifiers = append(cfg.modifiers, name)
}
func (cfg *ComponentConfigurator) D(property string, value string) {
newDeclaration := &Declaration{
property: property,
value: value,
}
for _, d := range cfg.rule.declarations {
if d.property == newDeclaration.property {
panic(fmt.Sprintf("declaration \"%s\" already exists", d.property))
}
}
cfg.rule.declarations = append(cfg.rule.declarations, newDeclaration)
}
func (cfg *ComponentConfigurator) Has(path []string) {
rule := cfg.stylesheet.includeTrait(path)
for _, newDeclaration := range rule.declarations {
for _, oldDeclaration := range cfg.rule.declarations {
if newDeclaration.property == oldDeclaration.property {
panic(
fmt.Sprintf(
"declaration \"%s\" already exists. old: %s | new: %s",
newDeclaration.property,
oldDeclaration.value,
newDeclaration.value,
))
}
}
for _, previousTraitRule := range cfg.traitRules {
for _, oldDeclaration := range previousTraitRule.declarations {
if newDeclaration.property == oldDeclaration.property {
panic(
fmt.Sprintf(
"declaration \"%s\" already exists. old: \"%s\" from trait \"%s\" | new: \"%s\" from trait \"%s\"",
newDeclaration.property,
oldDeclaration.value,
previousTraitRule.comment,
newDeclaration.value,
rule.comment,
),
)
}
}
}
}
rule.selectors = append(rule.selectors, cfg.rule.selectors[0])
cfg.traitRules = append(cfg.traitRules, rule)
}