forked from Shiloh/githaven
Refactor AddParam to AddParamIfExist (#29834)
When read the code: `pager.AddParam(ctx, "search", "search")`, the question always comes: What is it doing? Where is the value from? Why "search" / "search" ? Now it is clear: `pager.AddParamIfExist("search", ctx.Data["search"])`
This commit is contained in:
parent
6ead30dbc4
commit
e0ea3811c4
@ -84,7 +84,7 @@ func UnadoptedRepos(ctx *context.Context) {
|
|||||||
if !doSearch {
|
if !doSearch {
|
||||||
pager := context.NewPagination(0, opts.PageSize, opts.Page, 5)
|
pager := context.NewPagination(0, opts.PageSize, opts.Page, 5)
|
||||||
pager.SetDefaultParams(ctx)
|
pager.SetDefaultParams(ctx)
|
||||||
pager.AddParam(ctx, "search", "search")
|
pager.AddParamIfExist("search", ctx.Data["search"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
ctx.HTML(http.StatusOK, tplUnadoptedRepos)
|
ctx.HTML(http.StatusOK, tplUnadoptedRepos)
|
||||||
return
|
return
|
||||||
@ -98,7 +98,7 @@ func UnadoptedRepos(ctx *context.Context) {
|
|||||||
ctx.Data["Dirs"] = repoNames
|
ctx.Data["Dirs"] = repoNames
|
||||||
pager := context.NewPagination(count, opts.PageSize, opts.Page, 5)
|
pager := context.NewPagination(count, opts.PageSize, opts.Page, 5)
|
||||||
pager.SetDefaultParams(ctx)
|
pager.SetDefaultParams(ctx)
|
||||||
pager.AddParam(ctx, "search", "search")
|
pager.AddParamIfExist("search", ctx.Data["search"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
ctx.HTML(http.StatusOK, tplUnadoptedRepos)
|
ctx.HTML(http.StatusOK, tplUnadoptedRepos)
|
||||||
}
|
}
|
||||||
|
@ -127,7 +127,7 @@ func Code(ctx *context.Context) {
|
|||||||
|
|
||||||
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
|
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
|
||||||
pager.SetDefaultParams(ctx)
|
pager.SetDefaultParams(ctx)
|
||||||
pager.AddParam(ctx, "l", "Language")
|
pager.AddParamIfExist("l", ctx.Data["Language"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, tplExploreCode)
|
ctx.HTML(http.StatusOK, tplExploreCode)
|
||||||
|
@ -169,8 +169,8 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
|
|||||||
|
|
||||||
pager := context.NewPagination(int(count), opts.PageSize, page, 5)
|
pager := context.NewPagination(int(count), opts.PageSize, page, 5)
|
||||||
pager.SetDefaultParams(ctx)
|
pager.SetDefaultParams(ctx)
|
||||||
pager.AddParam(ctx, "topic", "TopicOnly")
|
pager.AddParamIfExist("topic", ctx.Data["TopicOnly"])
|
||||||
pager.AddParam(ctx, "language", "Language")
|
pager.AddParamIfExist("language", ctx.Data["Language"])
|
||||||
pager.AddParamString(relevantReposOnlyParam, fmt.Sprint(opts.OnlyShowRelevant))
|
pager.AddParamString(relevantReposOnlyParam, fmt.Sprint(opts.OnlyShowRelevant))
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
|
@ -154,7 +154,7 @@ func Home(ctx *context.Context) {
|
|||||||
|
|
||||||
pager := context.NewPagination(int(count), setting.UI.User.RepoPagingNum, page, 5)
|
pager := context.NewPagination(int(count), setting.UI.User.RepoPagingNum, page, 5)
|
||||||
pager.SetDefaultParams(ctx)
|
pager.SetDefaultParams(ctx)
|
||||||
pager.AddParam(ctx, "language", "Language")
|
pager.AddParamIfExist("language", ctx.Data["Language"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
ctx.Data["ShowMemberAndTeamTab"] = ctx.Org.IsMember || len(members) > 0
|
ctx.Data["ShowMemberAndTeamTab"] = ctx.Org.IsMember || len(members) > 0
|
||||||
|
@ -120,7 +120,7 @@ func Projects(ctx *context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pager := context.NewPagination(int(total), setting.UI.IssuePagingNum, page, numPages)
|
pager := context.NewPagination(int(total), setting.UI.IssuePagingNum, page, numPages)
|
||||||
pager.AddParam(ctx, "state", "State")
|
pager.AddParamIfExist("state", ctx.Data["State"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
ctx.Data["CanWriteProjects"] = canWriteProjects(ctx)
|
ctx.Data["CanWriteProjects"] = canWriteProjects(ctx)
|
||||||
|
@ -163,8 +163,8 @@ func Graph(ctx *context.Context) {
|
|||||||
ctx.Data["CommitCount"] = commitsCount
|
ctx.Data["CommitCount"] = commitsCount
|
||||||
|
|
||||||
paginator := context.NewPagination(int(graphCommitsCount), setting.UI.GraphMaxCommitNum, page, 5)
|
paginator := context.NewPagination(int(graphCommitsCount), setting.UI.GraphMaxCommitNum, page, 5)
|
||||||
paginator.AddParam(ctx, "mode", "Mode")
|
paginator.AddParamIfExist("mode", ctx.Data["Mode"])
|
||||||
paginator.AddParam(ctx, "hide-pr-refs", "HidePRRefs")
|
paginator.AddParamIfExist("hide-pr-refs", ctx.Data["HidePRRefs"])
|
||||||
for _, branch := range branches {
|
for _, branch := range branches {
|
||||||
paginator.AddParamString("branch", branch)
|
paginator.AddParamString("branch", branch)
|
||||||
}
|
}
|
||||||
|
@ -472,16 +472,16 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption opt
|
|||||||
}
|
}
|
||||||
ctx.Data["ShowArchivedLabels"] = archived
|
ctx.Data["ShowArchivedLabels"] = archived
|
||||||
|
|
||||||
pager.AddParam(ctx, "q", "Keyword")
|
pager.AddParamIfExist("q", ctx.Data["Keyword"])
|
||||||
pager.AddParam(ctx, "type", "ViewType")
|
pager.AddParamIfExist("type", ctx.Data["ViewType"])
|
||||||
pager.AddParam(ctx, "sort", "SortType")
|
pager.AddParamIfExist("sort", ctx.Data["SortType"])
|
||||||
pager.AddParam(ctx, "state", "State")
|
pager.AddParamIfExist("state", ctx.Data["State"])
|
||||||
pager.AddParam(ctx, "labels", "SelectLabels")
|
pager.AddParamIfExist("labels", ctx.Data["SelectLabels"])
|
||||||
pager.AddParam(ctx, "milestone", "MilestoneID")
|
pager.AddParamIfExist("milestone", ctx.Data["MilestoneID"])
|
||||||
pager.AddParam(ctx, "project", "ProjectID")
|
pager.AddParamIfExist("project", ctx.Data["ProjectID"])
|
||||||
pager.AddParam(ctx, "assignee", "AssigneeID")
|
pager.AddParamIfExist("assignee", ctx.Data["AssigneeID"])
|
||||||
pager.AddParam(ctx, "poster", "PosterID")
|
pager.AddParamIfExist("poster", ctx.Data["PosterID"])
|
||||||
pager.AddParam(ctx, "archived", "ShowArchivedLabels")
|
pager.AddParamIfExist("archived", ctx.Data["ShowArchivedLabels"])
|
||||||
|
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
}
|
}
|
||||||
|
@ -106,8 +106,8 @@ func Milestones(ctx *context.Context) {
|
|||||||
ctx.Data["IsShowClosed"] = isShowClosed
|
ctx.Data["IsShowClosed"] = isShowClosed
|
||||||
|
|
||||||
pager := context.NewPagination(int(total), setting.UI.IssuePagingNum, page, 5)
|
pager := context.NewPagination(int(total), setting.UI.IssuePagingNum, page, 5)
|
||||||
pager.AddParam(ctx, "state", "State")
|
pager.AddParamIfExist("state", ctx.Data["State"])
|
||||||
pager.AddParam(ctx, "q", "Keyword")
|
pager.AddParamIfExist("q", ctx.Data["Keyword"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, tplMilestone)
|
ctx.HTML(http.StatusOK, tplMilestone)
|
||||||
|
@ -70,8 +70,8 @@ func Packages(ctx *context.Context) {
|
|||||||
ctx.Data["RepositoryAccessMap"] = map[int64]bool{ctx.Repo.Repository.ID: true} // There is only the current repository
|
ctx.Data["RepositoryAccessMap"] = map[int64]bool{ctx.Repo.Repository.ID: true} // There is only the current repository
|
||||||
|
|
||||||
pager := context.NewPagination(int(total), setting.UI.PackagesPagingNum, page, 5)
|
pager := context.NewPagination(int(total), setting.UI.PackagesPagingNum, page, 5)
|
||||||
pager.AddParam(ctx, "q", "Query")
|
pager.AddParamIfExist("q", ctx.Data["Query"])
|
||||||
pager.AddParam(ctx, "type", "PackageType")
|
pager.AddParamIfExist("type", ctx.Data["PackageType"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, tplPackagesList)
|
ctx.HTML(http.StatusOK, tplPackagesList)
|
||||||
|
@ -118,7 +118,7 @@ func Projects(ctx *context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, numPages)
|
pager := context.NewPagination(total, setting.UI.IssuePagingNum, page, numPages)
|
||||||
pager.AddParam(ctx, "state", "State")
|
pager.AddParamIfExist("state", ctx.Data["State"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects)
|
ctx.Data["CanWriteProjects"] = ctx.Repo.Permission.CanWrite(unit.TypeProjects)
|
||||||
|
@ -59,7 +59,7 @@ func Search(ctx *context.Context) {
|
|||||||
|
|
||||||
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
|
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
|
||||||
pager.SetDefaultParams(ctx)
|
pager.SetDefaultParams(ctx)
|
||||||
pager.AddParam(ctx, "l", "Language")
|
pager.AddParamIfExist("l", ctx.Data["Language"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, tplSearch)
|
ctx.HTML(http.StatusOK, tplSearch)
|
||||||
|
@ -112,7 +112,7 @@ func CodeSearch(ctx *context.Context) {
|
|||||||
|
|
||||||
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
|
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
|
||||||
pager.SetDefaultParams(ctx)
|
pager.SetDefaultParams(ctx)
|
||||||
pager.AddParam(ctx, "l", "Language")
|
pager.AddParamIfExist("l", ctx.Data["Language"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, tplUserCode)
|
ctx.HTML(http.StatusOK, tplUserCode)
|
||||||
|
@ -133,7 +133,7 @@ func Dashboard(ctx *context.Context) {
|
|||||||
ctx.Data["Feeds"] = feeds
|
ctx.Data["Feeds"] = feeds
|
||||||
|
|
||||||
pager := context.NewPagination(int(count), setting.UI.FeedPagingNum, page, 5)
|
pager := context.NewPagination(int(count), setting.UI.FeedPagingNum, page, 5)
|
||||||
pager.AddParam(ctx, "date", "Date")
|
pager.AddParamIfExist("date", ctx.Data["Date"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, tplDashboard)
|
ctx.HTML(http.StatusOK, tplDashboard)
|
||||||
@ -329,10 +329,10 @@ func Milestones(ctx *context.Context) {
|
|||||||
ctx.Data["IsShowClosed"] = isShowClosed
|
ctx.Data["IsShowClosed"] = isShowClosed
|
||||||
|
|
||||||
pager := context.NewPagination(pagerCount, setting.UI.IssuePagingNum, page, 5)
|
pager := context.NewPagination(pagerCount, setting.UI.IssuePagingNum, page, 5)
|
||||||
pager.AddParam(ctx, "q", "Keyword")
|
pager.AddParamIfExist("q", ctx.Data["Keyword"])
|
||||||
pager.AddParam(ctx, "repos", "RepoIDs")
|
pager.AddParamIfExist("repos", ctx.Data["RepoIDs"])
|
||||||
pager.AddParam(ctx, "sort", "SortType")
|
pager.AddParamIfExist("sort", ctx.Data["SortType"])
|
||||||
pager.AddParam(ctx, "state", "State")
|
pager.AddParamIfExist("state", ctx.Data["State"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, tplMilestones)
|
ctx.HTML(http.StatusOK, tplMilestones)
|
||||||
@ -632,13 +632,13 @@ func buildIssueOverview(ctx *context.Context, unitType unit.Type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pager := context.NewPagination(shownIssues, setting.UI.IssuePagingNum, page, 5)
|
pager := context.NewPagination(shownIssues, setting.UI.IssuePagingNum, page, 5)
|
||||||
pager.AddParam(ctx, "q", "Keyword")
|
pager.AddParamIfExist("q", ctx.Data["Keyword"])
|
||||||
pager.AddParam(ctx, "type", "ViewType")
|
pager.AddParamIfExist("type", ctx.Data["ViewType"])
|
||||||
pager.AddParam(ctx, "sort", "SortType")
|
pager.AddParamIfExist("sort", ctx.Data["SortType"])
|
||||||
pager.AddParam(ctx, "state", "State")
|
pager.AddParamIfExist("state", ctx.Data["State"])
|
||||||
pager.AddParam(ctx, "labels", "SelectLabels")
|
pager.AddParamIfExist("labels", ctx.Data["SelectLabels"])
|
||||||
pager.AddParam(ctx, "milestone", "MilestoneID")
|
pager.AddParamIfExist("milestone", ctx.Data["MilestoneID"])
|
||||||
pager.AddParam(ctx, "assignee", "AssigneeID")
|
pager.AddParamIfExist("assignee", ctx.Data["AssigneeID"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, tplIssues)
|
ctx.HTML(http.StatusOK, tplIssues)
|
||||||
|
@ -344,8 +344,8 @@ func NotificationSubscriptions(ctx *context.Context) {
|
|||||||
ctx.Redirect(fmt.Sprintf("/notifications/subscriptions?page=%d", pager.Paginater.Current()))
|
ctx.Redirect(fmt.Sprintf("/notifications/subscriptions?page=%d", pager.Paginater.Current()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pager.AddParam(ctx, "sort", "SortType")
|
pager.AddParamIfExist("sort", ctx.Data["SortType"])
|
||||||
pager.AddParam(ctx, "state", "State")
|
pager.AddParamIfExist("state", ctx.Data["State"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, tplNotificationSubscriptions)
|
ctx.HTML(http.StatusOK, tplNotificationSubscriptions)
|
||||||
|
@ -125,8 +125,8 @@ func ListPackages(ctx *context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pager := context.NewPagination(int(total), setting.UI.PackagesPagingNum, page, 5)
|
pager := context.NewPagination(int(total), setting.UI.PackagesPagingNum, page, 5)
|
||||||
pager.AddParam(ctx, "q", "Query")
|
pager.AddParamIfExist("q", ctx.Data["Query"])
|
||||||
pager.AddParam(ctx, "type", "PackageType")
|
pager.AddParamIfExist("type", ctx.Data["PackageType"])
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, tplPackagesList)
|
ctx.HTML(http.StatusOK, tplPackagesList)
|
||||||
|
@ -324,12 +324,12 @@ func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDb
|
|||||||
|
|
||||||
pager := context.NewPagination(total, pagingNum, page, 5)
|
pager := context.NewPagination(total, pagingNum, page, 5)
|
||||||
pager.SetDefaultParams(ctx)
|
pager.SetDefaultParams(ctx)
|
||||||
pager.AddParam(ctx, "tab", "TabName")
|
pager.AddParamIfExist("tab", ctx.Data["TabName"])
|
||||||
if tab != "followers" && tab != "following" && tab != "activity" && tab != "projects" {
|
if tab != "followers" && tab != "following" && tab != "activity" && tab != "projects" {
|
||||||
pager.AddParam(ctx, "language", "Language")
|
pager.AddParamIfExist("language", ctx.Data["Language"])
|
||||||
}
|
}
|
||||||
if tab == "activity" {
|
if tab == "activity" {
|
||||||
pager.AddParam(ctx, "date", "Date")
|
pager.AddParamIfExist("date", ctx.Data["Date"])
|
||||||
}
|
}
|
||||||
ctx.Data["Page"] = pager
|
ctx.Data["Page"] = pager
|
||||||
}
|
}
|
||||||
|
@ -26,14 +26,13 @@ func NewPagination(total, pagingNum, current, numPages int) *Pagination {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddParam adds a value from context identified by ctxKey as link param under a given paramKey
|
// AddParamIfExist adds a value to the query parameters if the value is not nil
|
||||||
func (p *Pagination) AddParam(ctx *Context, paramKey, ctxKey string) {
|
func (p *Pagination) AddParamIfExist(key string, val any) {
|
||||||
_, exists := ctx.Data[ctxKey]
|
if val == nil {
|
||||||
if !exists {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
paramData := fmt.Sprintf("%v", ctx.Data[ctxKey]) // cast any to string
|
paramData := fmt.Sprint(val)
|
||||||
urlParam := fmt.Sprintf("%s=%v", url.QueryEscape(paramKey), url.QueryEscape(paramData))
|
urlParam := fmt.Sprintf("%s=%v", url.QueryEscape(key), url.QueryEscape(paramData))
|
||||||
p.urlParams = append(p.urlParams, urlParam)
|
p.urlParams = append(p.urlParams, urlParam)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,8 +49,8 @@ func (p *Pagination) GetParams() template.URL {
|
|||||||
|
|
||||||
// SetDefaultParams sets common pagination params that are often used
|
// SetDefaultParams sets common pagination params that are often used
|
||||||
func (p *Pagination) SetDefaultParams(ctx *Context) {
|
func (p *Pagination) SetDefaultParams(ctx *Context) {
|
||||||
p.AddParam(ctx, "sort", "SortType")
|
p.AddParamIfExist("sort", ctx.Data["SortType"])
|
||||||
p.AddParam(ctx, "q", "Keyword")
|
p.AddParamIfExist("q", ctx.Data["Keyword"])
|
||||||
// do not add any more uncommon params here!
|
// do not add any more uncommon params here!
|
||||||
p.AddParam(ctx, "fuzzy", "IsFuzzy")
|
p.AddParamIfExist("fuzzy", ctx.Data["IsFuzzy"])
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user