|
|
|
|
|
|
|
|
|
package types |
|
|
|
import ( |
|
"bytes" |
|
"fmt" |
|
"html/template" |
|
"strconv" |
|
textTmpl "text/template" |
|
|
|
"github.com/GoAdminGroup/go-admin/context" |
|
"github.com/GoAdminGroup/go-admin/modules/config" |
|
"github.com/GoAdminGroup/go-admin/modules/menu" |
|
"github.com/GoAdminGroup/go-admin/modules/system" |
|
"github.com/GoAdminGroup/go-admin/modules/utils" |
|
"github.com/GoAdminGroup/go-admin/plugins/admin/models" |
|
) |
|
|
|
|
|
|
|
type Attribute struct { |
|
TemplateList map[string]string |
|
Separation bool |
|
} |
|
|
|
|
|
type Page struct { |
|
|
|
User models.UserModel |
|
|
|
|
|
Menu menu.Menu |
|
|
|
|
|
Panel Panel |
|
|
|
|
|
System SystemInfo |
|
|
|
|
|
UrlPrefix string |
|
|
|
|
|
Title string |
|
|
|
|
|
Logo template.HTML |
|
|
|
|
|
MiniLogo template.HTML |
|
|
|
|
|
ColorScheme string |
|
|
|
|
|
IndexUrl string |
|
|
|
|
|
CdnUrl string |
|
|
|
|
|
CustomHeadHtml template.HTML |
|
|
|
|
|
CustomFootHtml template.HTML |
|
|
|
TmplHeadHTML template.HTML |
|
TmplFootJS template.HTML |
|
|
|
|
|
AssetsList template.HTML |
|
|
|
|
|
FooterInfo template.HTML |
|
|
|
|
|
Iframe bool |
|
|
|
|
|
UpdateMenu bool |
|
|
|
|
|
navButtons Buttons |
|
NavButtonsHTML template.HTML |
|
} |
|
|
|
type NewPageParam struct { |
|
User models.UserModel |
|
Menu *menu.Menu |
|
UpdateMenu bool |
|
Panel Panel |
|
Logo template.HTML |
|
Assets template.HTML |
|
Buttons Buttons |
|
Iframe bool |
|
TmplHeadHTML template.HTML |
|
TmplFootJS template.HTML |
|
NavButtonsHTML template.HTML |
|
NavButtonsJS template.HTML |
|
} |
|
|
|
func (param *NewPageParam) NavButtonsAndJS(ctx *context.Context) (template.HTML, template.HTML) { |
|
navBtnFooter := template.HTML("") |
|
navBtn := template.HTML("") |
|
btnJS := template.JS("") |
|
|
|
for _, btn := range param.Buttons { |
|
if btn.IsType(ButtonTypeNavDropDown) { |
|
content, js := btn.Content(ctx) |
|
navBtn += content |
|
btnJS += js |
|
for _, item := range btn.(*NavDropDownButton).Items { |
|
navBtnFooter += item.GetAction().FooterContent(ctx) |
|
_, js := item.Content(ctx) |
|
btnJS += js |
|
} |
|
} else { |
|
navBtnFooter += btn.GetAction().FooterContent(ctx) |
|
content, js := btn.Content(ctx) |
|
navBtn += content |
|
btnJS += js |
|
} |
|
} |
|
|
|
return template.HTML(ParseTableDataTmpl(navBtn)), |
|
navBtnFooter + template.HTML(ParseTableDataTmpl(`<script>`+btnJS+`</script>`)) |
|
} |
|
|
|
func NewPage(ctx *context.Context, param *NewPageParam) *Page { |
|
|
|
if param.NavButtonsHTML == template.HTML("") { |
|
param.NavButtonsHTML, param.NavButtonsJS = param.NavButtonsAndJS(ctx) |
|
} |
|
|
|
logo := param.Logo |
|
if logo == template.HTML("") { |
|
logo = config.GetLogo() |
|
} |
|
|
|
return &Page{ |
|
User: param.User, |
|
Menu: *param.Menu, |
|
Panel: param.Panel, |
|
UpdateMenu: param.UpdateMenu, |
|
System: SystemInfo{ |
|
Version: system.Version(), |
|
Theme: config.GetTheme(), |
|
}, |
|
UrlPrefix: config.AssertPrefix(), |
|
Title: config.GetTitle(), |
|
Logo: logo, |
|
MiniLogo: config.GetMiniLogo(), |
|
ColorScheme: config.GetColorScheme(), |
|
IndexUrl: config.GetIndexURL(), |
|
CdnUrl: config.GetAssetUrl(), |
|
CustomHeadHtml: config.GetCustomHeadHtml(), |
|
CustomFootHtml: config.GetCustomFootHtml() + param.NavButtonsJS, |
|
FooterInfo: config.GetFooterInfo(), |
|
AssetsList: param.Assets, |
|
navButtons: param.Buttons, |
|
Iframe: param.Iframe, |
|
NavButtonsHTML: param.NavButtonsHTML, |
|
TmplHeadHTML: param.TmplHeadHTML, |
|
TmplFootJS: param.TmplFootJS, |
|
} |
|
} |
|
|
|
func (page *Page) AddButton(ctx *context.Context, title template.HTML, icon string, action Action) *Page { |
|
page.navButtons = append(page.navButtons, GetNavButton(title, icon, action)) |
|
page.CustomFootHtml += action.FooterContent(ctx) |
|
return page |
|
} |
|
|
|
func NewPagePanel(panel Panel) *Page { |
|
return &Page{ |
|
Panel: panel, |
|
System: SystemInfo{ |
|
Version: system.Version(), |
|
}, |
|
} |
|
} |
|
|
|
|
|
type SystemInfo struct { |
|
Version string |
|
Theme string |
|
} |
|
|
|
type TableRowData struct { |
|
Id template.HTML |
|
Ids template.HTML |
|
Value map[string]InfoItem |
|
} |
|
|
|
func ParseTableDataTmpl(content interface{}) string { |
|
var ( |
|
c string |
|
ok bool |
|
) |
|
if c, ok = content.(string); !ok { |
|
if cc, ok := content.(template.HTML); ok { |
|
c = string(cc) |
|
} else { |
|
c = string(content.(template.JS)) |
|
} |
|
} |
|
t := template.New("row_data_tmpl") |
|
t, _ = t.Parse(c) |
|
buf := new(bytes.Buffer) |
|
_ = t.Execute(buf, TableRowData{Ids: `typeof(selectedRows)==="function" ? selectedRows().join() : ""`}) |
|
return buf.String() |
|
} |
|
|
|
func ParseTableDataTmplWithID(id template.HTML, content string, value ...map[string]InfoItem) string { |
|
t := textTmpl.New("row_data_tmpl") |
|
t, _ = t.Parse(content) |
|
buf := new(bytes.Buffer) |
|
v := make(map[string]InfoItem) |
|
if len(value) > 0 { |
|
v = value[0] |
|
} |
|
_ = t.Execute(buf, TableRowData{ |
|
Id: id, |
|
Ids: `typeof(selectedRows)==="function" ? selectedRows().join() : ""`, |
|
Value: v, |
|
}) |
|
return buf.String() |
|
} |
|
|
|
|
|
type Panel struct { |
|
Title template.HTML |
|
Description template.HTML |
|
Content template.HTML |
|
|
|
CSS template.CSS |
|
JS template.JS |
|
Url string |
|
|
|
|
|
MiniSidebar bool |
|
|
|
|
|
AutoRefresh bool |
|
|
|
RefreshInterval []int |
|
|
|
Callbacks Callbacks |
|
} |
|
|
|
type Component interface { |
|
GetContent() template.HTML |
|
GetJS() template.JS |
|
GetCSS() template.CSS |
|
GetCallbacks() Callbacks |
|
} |
|
|
|
func (p Panel) AddComponent(comp Component) Panel { |
|
p.JS += comp.GetJS() |
|
p.CSS += comp.GetCSS() |
|
p.Content += comp.GetContent() |
|
p.Callbacks = append(p.Callbacks, comp.GetCallbacks()...) |
|
return p |
|
} |
|
|
|
func (p Panel) AddJS(js template.JS) Panel { |
|
p.JS += js |
|
return p |
|
} |
|
|
|
func (p Panel) GetContent(params ...bool) Panel { |
|
|
|
compress := false |
|
|
|
if len(params) > 0 { |
|
compress = params[0] |
|
} |
|
|
|
var ( |
|
animation, style, remove = template.HTML(""), template.HTML(""), template.HTML("") |
|
ani = config.GetAnimation() |
|
) |
|
|
|
if ani.Type != "" && (len(params) < 2 || params[1]) { |
|
animation = template.HTML(` class='pjax-container-content animated ` + ani.Type + `'`) |
|
if ani.Delay != 0 { |
|
style = template.HTML(fmt.Sprintf(`animation-delay: %fs;-webkit-animation-delay: %fs;`, ani.Delay, ani.Delay)) |
|
} |
|
if ani.Duration != 0 { |
|
style = template.HTML(fmt.Sprintf(`animation-duration: %fs;-webkit-animation-duration: %fs;`, ani.Duration, ani.Duration)) |
|
} |
|
if style != "" { |
|
style = ` style="` + style + `"` |
|
} |
|
remove = template.HTML(`<script> |
|
$('.pjax-container-content .modal.fade').on('show.bs.modal', function (event) { |
|
// Fix Animate.css |
|
$('.pjax-container-content').removeClass('` + ani.Type + `'); |
|
}); |
|
</script>`) |
|
} |
|
|
|
p.Content = `<div` + animation + style + ">" + p.Content + "</div>" + remove |
|
if p.MiniSidebar { |
|
p.Content += `<script>$("body").addClass("sidebar-collapse")</script>` |
|
} |
|
if p.AutoRefresh { |
|
refreshTime := 60 |
|
if len(p.RefreshInterval) > 0 { |
|
refreshTime = p.RefreshInterval[0] |
|
} |
|
|
|
p.Content += `<script> |
|
window.setTimeout(function(){ |
|
$.pjax.reload('#pjax-container'); |
|
}, ` + template.HTML(strconv.Itoa(refreshTime*1000)) + `); |
|
</script>` |
|
} |
|
|
|
if compress { |
|
utils.CompressedContent(&p.Content) |
|
} |
|
|
|
return p |
|
} |
|
|
|
type GetPanelFn func(ctx interface{}) (Panel, error) |
|
|
|
type GetPanelInfoFn func(ctx *context.Context) (Panel, error) |
|
|