优化代码

This commit is contained in:
xing 2023-02-28 23:59:17 +08:00
parent 7d27668159
commit 4d50f60c62
4 changed files with 45 additions and 18 deletions

View File

@ -174,11 +174,17 @@ func (h *Handle) PreCodeAndStats() {
func (h *Handle) Render() { func (h *Handle) Render() {
h.PreCodeAndStats() h.PreCodeAndStats()
h.PreTemplate() h.PreTemplate()
h.ExecHandleFns()
h.PushHeadScript(Components{CalSiteIcon, 10}, Components{CalCustomCss, -1})
h.AddComponent("customLogo", CalCustomLogo) h.AddComponent("customLogo", CalCustomLogo)
h.PushHeadScript(Components{CalSiteIcon, 100}, Components{CalCustomCss, 0})
h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) {
h.CalMultipleComponents() h.CalMultipleComponents()
h.CalBodyClass() h.CalBodyClass()
}, 5))
h.ExecHandleFns()
h.C.HTML(h.Code, h.templ, h.ginH) h.C.HTML(h.Code, h.templ, h.ginH)
} }

View File

@ -101,9 +101,12 @@ func (d *DetailHandle) ContextPost() {
} }
func (d *DetailHandle) Render() { func (d *DetailHandle) Render() {
d.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) {
d.PasswordProject() d.PasswordProject()
d.RenderComment() d.RenderComment()
d.ginH["post"] = d.Post d.ginH["post"] = d.Post
}, 10))
d.Handle.Render() d.Handle.Render()
} }

View File

@ -18,9 +18,25 @@ type IndexHandle struct {
*Handle *Handle
Param *IndexParams Param *IndexParams
Posts []models.Posts Posts []models.Posts
PageEle pagination.Elements pageEle pagination.Elements
TotalRows int TotalRows int
PostsPlugins map[string]Plugin[models.Posts, *Handle] postsPlugins map[string]Plugin[models.Posts, *Handle]
}
func (i *IndexHandle) PageEle() pagination.Elements {
return i.pageEle
}
func (i *IndexHandle) SetPageEle(pageEle pagination.Elements) {
i.pageEle = pageEle
}
func (i *IndexHandle) PostsPlugins() map[string]Plugin[models.Posts, *Handle] {
return i.postsPlugins
}
func (i *IndexHandle) SetPostsPlugins(postsPlugins map[string]Plugin[models.Posts, *Handle]) {
i.postsPlugins = postsPlugins
} }
func NewIndexHandle(handle *Handle) *IndexHandle { func NewIndexHandle(handle *Handle) *IndexHandle {
@ -80,15 +96,15 @@ func (i *IndexHandle) GetIndexData() (posts []models.Posts, totalRaw int, err er
} }
func (i *IndexHandle) Pagination() { func (i *IndexHandle) Pagination() {
if i.PageEle == nil { if i.pageEle == nil {
i.PageEle = plugins.TwentyFifteenPagination() i.pageEle = plugins.TwentyFifteenPagination()
} }
q := i.C.Request.URL.Query().Encode() q := i.C.Request.URL.Query().Encode()
if q != "" { if q != "" {
q = fmt.Sprintf("?%s", q) q = fmt.Sprintf("?%s", q)
} }
paginations := pagination.NewParsePagination(i.TotalRows, i.Param.PageSize, i.Param.Page, i.Param.PaginationStep, q, i.C.Request.URL.Path) paginations := pagination.NewParsePagination(i.TotalRows, i.Param.PageSize, i.Param.Page, i.Param.PaginationStep, q, i.C.Request.URL.Path)
i.ginH["pagination"] = pagination.Paginate(i.PageEle, paginations) i.ginH["pagination"] = pagination.Paginate(i.pageEle, paginations)
} }
@ -115,7 +131,7 @@ func (i *IndexHandle) ExecPostsPlugin(calls ...func(*models.Posts)) {
pluginConf := config.GetConfig().ListPagePlugins pluginConf := config.GetConfig().ListPagePlugins
postsPlugins := i.PostsPlugins postsPlugins := i.postsPlugins
if postsPlugins == nil { if postsPlugins == nil {
postsPlugins = pluginFns postsPlugins = pluginFns
} }
@ -126,8 +142,10 @@ func (i *IndexHandle) ExecPostsPlugin(calls ...func(*models.Posts)) {
} }
func (i *IndexHandle) Render() { func (i *IndexHandle) Render() {
i.PushHandleFn(constraints.Ok, NewHandleFn(func(h *Handle) {
i.ExecPostsPlugin() i.ExecPostsPlugin()
i.Pagination() i.Pagination()
}, 10))
i.Handle.Render() i.Handle.Render()
} }

View File

@ -36,7 +36,7 @@ func Hook(h *common.Handle) {
func ready(next common.HandleFn[*common.Handle], h *common.Handle) { func ready(next common.HandleFn[*common.Handle], h *common.Handle) {
h.WidgetAreaData() h.WidgetAreaData()
h.GetPassword() h.GetPassword()
h.PushHandleFn(constraints.AllStats, common.NewHandleFn(calClass, 10)) h.PushHandleFn(constraints.AllStats, common.NewHandleFn(calClass, 15))
h.PushHeadScript( h.PushHeadScript(
common.NewComponents(colorScheme, 10), common.NewComponents(colorScheme, 10),
common.NewComponents(customHeader, 10), common.NewComponents(customHeader, 10),
@ -68,8 +68,8 @@ func index(next common.HandleFn[*common.Handle], i *common.IndexHandle) {
i.Render() i.Render()
return return
} }
i.PostsPlugins = listPostsPlugins i.SetPageEle(paginate)
i.PageEle = paginate i.SetPostsPlugins(listPostsPlugins)
next(i.Handle) next(i.Handle)
} }