From a345a03d99f6e3d46a20620869e29424a0c55499 Mon Sep 17 00:00:00 2001 From: Bwko Date: Sat, 24 Dec 2016 15:41:09 +0100 Subject: [PATCH] Added sorting to the labels & milestones page (#199) --- models/issue.go | 18 +++++++++++++++++- models/issue_label.go | 20 +++++++++++++++----- options/locale/locale_en-US.ini | 8 ++++++++ routers/api/v1/repo/label.go | 2 +- routers/repo/issue.go | 15 +++++++++------ templates/repo/issue/labels.tmpl | 15 +++++++++++++++ templates/repo/issue/milestones.tmpl | 17 +++++++++++++++++ 7 files changed, 82 insertions(+), 13 deletions(-) diff --git a/models/issue.go b/models/issue.go index 57490bbc5b997..86becdbbaef39 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1503,12 +1503,28 @@ func GetMilestonesByRepoID(repoID int64) ([]*Milestone, error) { } // GetMilestones returns a list of milestones of given repository and status. -func GetMilestones(repoID int64, page int, isClosed bool) ([]*Milestone, error) { +func GetMilestones(repoID int64, page int, isClosed bool, sortType string) ([]*Milestone, error) { miles := make([]*Milestone, 0, setting.UI.IssuePagingNum) sess := x.Where("repo_id = ? AND is_closed = ?", repoID, isClosed) if page > 0 { sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum) } + + switch sortType { + case "furthestduedate": + sess.Desc("deadline_unix") + case "leastcomplete": + sess.Asc("completeness") + case "mostcomplete": + sess.Desc("completeness") + case "leastissues": + sess.Asc("num_issues") + case "mostissues": + sess.Desc("num_issues") + default: + sess.Asc("deadline_unix") + } + return miles, sess.Find(&miles) } diff --git a/models/issue_label.go b/models/issue_label.go index 39805ba80deb0..f06f0f97c3237 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -171,12 +171,22 @@ func GetLabelsInRepoByIDs(repoID int64, labelIDs []int64) ([]*Label, error) { } // GetLabelsByRepoID returns all labels that belong to given repository by ID. -func GetLabelsByRepoID(repoID int64) ([]*Label, error) { +func GetLabelsByRepoID(repoID int64, sortType string) ([]*Label, error) { labels := make([]*Label, 0, 10) - return labels, x. - Where("repo_id = ?", repoID). - Asc("name"). - Find(&labels) + sess := x.Where("repo_id = ?", repoID) + + switch sortType { + case "reversealphabetically": + sess.Desc("name") + case "leastissues": + sess.Asc("num_issues") + case "mostissues": + sess.Desc("num_issues") + default: + sess.Asc("name") + } + + return labels, sess.Find(&labels) } func getLabelsByIssueID(e Engine, issueID int64) ([]*Label, error) { diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 1be1528118ac5..97725a950ae14 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -557,6 +557,8 @@ issues.label_modify = Label Modification issues.label_deletion = Label Deletion issues.label_deletion_desc = Deleting this label will remove its information in all related issues. Do you want to continue? issues.label_deletion_success = Label has been deleted successfully! +issues.label.filter_sort.alphabetically = Alphabetically +issues.label.filter_sort.reverse_alphabetically = Reverse alphabetically issues.num_participants = %d Participants issues.attachment.open_tab = `Click to see "%s" in a new tab` issues.attachment.download = `Click to download "%s"` @@ -610,6 +612,12 @@ milestones.edit_success = Changes of milestone '%s' has been saved successfully! milestones.deletion = Milestone Deletion milestones.deletion_desc = Deleting this milestone will remove its information in all related issues. Do you want to continue? milestones.deletion_success = Milestone has been deleted successfully! +milestones.filter_sort.closest_due_date = Closest due date +milestones.filter_sort.furthest_due_date = Furthest due date +milestones.filter_sort.least_complete = Least complete +milestones.filter_sort.most_complete = Most complete +milestones.filter_sort.most_issues = Most issues +milestones.filter_sort.least_issues = Least issues wiki = Wiki wiki.welcome = Welcome to Wiki! diff --git a/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go index a2bf3a9e409a3..c22db517c3c56 100644 --- a/routers/api/v1/repo/label.go +++ b/routers/api/v1/repo/label.go @@ -15,7 +15,7 @@ import ( // ListLabels list all the labels of a repository func ListLabels(ctx *context.APIContext) { - labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID) + labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.Query("sort")) if err != nil { ctx.Error(500, "GetLabelsByRepoID", err) return diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 14c1ea1aa7845..1cf5722ed16d1 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -82,7 +82,7 @@ func MustAllowPulls(ctx *context.Context) { // RetrieveLabels find all the labels of a repository func RetrieveLabels(ctx *context.Context) { - labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID) + labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID, ctx.Query("sort")) if err != nil { ctx.Handle(500, "RetrieveLabels.GetLabels", err) return @@ -92,6 +92,7 @@ func RetrieveLabels(ctx *context.Context) { } ctx.Data["Labels"] = labels ctx.Data["NumLabels"] = len(labels) + ctx.Data["SortType"] = ctx.Query("sort") } // Issues render issues page @@ -274,12 +275,12 @@ func renderAttachmentSettings(ctx *context.Context) { // RetrieveRepoMilestonesAndAssignees find all the milestones and assignees of a repository func RetrieveRepoMilestonesAndAssignees(ctx *context.Context, repo *models.Repository) { var err error - ctx.Data["OpenMilestones"], err = models.GetMilestones(repo.ID, -1, false) + ctx.Data["OpenMilestones"], err = models.GetMilestones(repo.ID, -1, false, "") if err != nil { ctx.Handle(500, "GetMilestones", err) return } - ctx.Data["ClosedMilestones"], err = models.GetMilestones(repo.ID, -1, true) + ctx.Data["ClosedMilestones"], err = models.GetMilestones(repo.ID, -1, true, "") if err != nil { ctx.Handle(500, "GetMilestones", err) return @@ -298,7 +299,7 @@ func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository) []*models. return nil } - labels, err := models.GetLabelsByRepoID(repo.ID) + labels, err := models.GetLabelsByRepoID(repo.ID, "") if err != nil { ctx.Handle(500, "GetLabelsByRepoID", err) return nil @@ -583,7 +584,7 @@ func ViewIssue(ctx *context.Context) { for i := range issue.Labels { labelIDMark[issue.Labels[i].ID] = true } - labels, err := models.GetLabelsByRepoID(repo.ID) + labels, err := models.GetLabelsByRepoID(repo.ID, "") if err != nil { ctx.Handle(500, "GetLabelsByRepoID", err) return @@ -1079,6 +1080,7 @@ func Milestones(ctx *context.Context) { ctx.Data["OpenCount"] = openCount ctx.Data["ClosedCount"] = closedCount + sortType := ctx.Query("sort") page := ctx.QueryInt("page") if page <= 1 { page = 1 @@ -1092,7 +1094,7 @@ func Milestones(ctx *context.Context) { } ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5) - miles, err := models.GetMilestones(ctx.Repo.Repository.ID, page, isShowClosed) + miles, err := models.GetMilestones(ctx.Repo.Repository.ID, page, isShowClosed, sortType) if err != nil { ctx.Handle(500, "GetMilestones", err) return @@ -1108,6 +1110,7 @@ func Milestones(ctx *context.Context) { ctx.Data["State"] = "open" } + ctx.Data["SortType"] = sortType ctx.Data["IsShowClosed"] = isShowClosed ctx.HTML(200, tplMilestone) } diff --git a/templates/repo/issue/labels.tmpl b/templates/repo/issue/labels.tmpl index eef82b80a8b23..ab3afe59763d3 100644 --- a/templates/repo/issue/labels.tmpl +++ b/templates/repo/issue/labels.tmpl @@ -34,6 +34,21 @@
+ {{template "base/alert" .}}
{{.i18n.Tr "repo.issues.label_count" .NumLabels}}
diff --git a/templates/repo/issue/milestones.tmpl b/templates/repo/issue/milestones.tmpl index 038eaabd8f79e..3703301e19d27 100644 --- a/templates/repo/issue/milestones.tmpl +++ b/templates/repo/issue/milestones.tmpl @@ -23,6 +23,23 @@
+
{{range .Milestones}}