/
lib.go
74 lines (63 loc) · 1.53 KB
/
lib.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
package beeplus
import (
"github.com/fplib-go/fplib"
"path"
"strings"
)
type LibLoaderClass struct {
Store map[string][]string
Group map[string][]string
}
func (this *LibLoaderClass) Add(name string, libs ...string) {
if this.Store == nil {
this.Store = make(map[string][]string)
}
this.Store[name] = libs
}
func (this *LibLoaderClass) AddGroup(name string, libs ...string) {
if this.Group == nil {
this.Group = make(map[string][]string)
}
this.Group[name] = libs
}
func (this *LibLoaderClass) LoadCSS(name string, base ...string) string {
_, out_css := this.GetLoadHTML(name, base...)
return out_css
}
func (this *LibLoaderClass) LoadJS(name string, base ...string) string {
out_js, _ := this.GetLoadHTML(name, base...)
return out_js
}
func (this *LibLoaderClass) GetLoadHTML(name string, base ...string) (string, string) {
p := ""
if len(base) > 0 {
p = base[0]
}
libs := []string{}
names := fplib.Str.ToArr(name)
for _, v := range names {
G := this.Group[v]
if len(G) > 0 {
libs = append(libs, G...)
} else {
libs = append(libs, v)
}
}
files := []string{}
for _, v := range libs {
L := this.Store[v]
if len(L) > 0 {
files = append(files, L...)
}
}
files = fplib.Str.RemoveRepeatedFromArr(files)
out_js, out_css := "", ""
for _, v := range files {
if strings.HasSuffix(v, ".css") {
out_css += `<link href="` + path.Join(p, v) + `" rel="stylesheet">`
} else if strings.HasSuffix(v, ".js") {
out_js += `<script src="` + path.Join(p, v) + `"></script>`
}
}
return out_js, out_css
}