From 45e6c003c92b25a9622fd65aa4e25f0859c64502 Mon Sep 17 00:00:00 2001 From: Bwko Date: Sun, 20 Nov 2016 17:37:55 +0100 Subject: [PATCH] Added sorting to the labels & milestones page --- conf/locale/locale_en-US.ini | 8 ++++++++ models/issue.go | 18 +++++++++++++++++- models/issue_label.go | 20 +++++++++++++++----- routers/api/v1/repo/label.go | 2 +- routers/repo/issue.go | 17 ++++++++++------- templates/repo/issue/labels.tmpl | 15 +++++++++++++++ templates/repo/issue/milestones.tmpl | 17 +++++++++++++++++ 7 files changed, 83 insertions(+), 14 deletions(-) diff --git a/conf/locale/locale_en-US.ini b/conf/locale/locale_en-US.ini index b6e47594d1a64..4418584fb9989 100644 --- a/conf/locale/locale_en-US.ini +++ b/conf/locale/locale_en-US.ini @@ -551,6 +551,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"` @@ -604,6 +606,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/models/issue.go b/models/issue.go index 09ecb2e6d38c5..eb784de9bd521 100644 --- a/models/issue.go +++ b/models/issue.go @@ -1456,12 +1456,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 3eed8ed985f8e..03523c6cd2e38 100644 --- a/models/issue_label.go +++ b/models/issue_label.go @@ -144,12 +144,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/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go index 009e8d8a0b7b4..cc6b5f008740d 100644 --- a/routers/api/v1/repo/label.go +++ b/routers/api/v1/repo/label.go @@ -12,7 +12,7 @@ import ( ) 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 3b5abf896243d..ec1f027a71b38 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -77,7 +77,7 @@ func MustAllowPulls(ctx *context.Context) { } 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 @@ -87,6 +87,7 @@ func RetrieveLabels(ctx *context.Context) { } ctx.Data["Labels"] = labels ctx.Data["NumLabels"] = len(labels) + ctx.Data["SortType"] = ctx.Query("sort") } func Issues(ctx *context.Context) { @@ -257,12 +258,12 @@ func renderAttachmentSettings(ctx *context.Context) { 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 @@ -280,7 +281,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 @@ -557,7 +558,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 @@ -1039,6 +1040,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 @@ -1052,7 +1054,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 @@ -1068,8 +1070,9 @@ func Milestones(ctx *context.Context) { ctx.Data["State"] = "open" } + ctx.Data["SortType"] = sortType ctx.Data["IsShowClosed"] = isShowClosed - ctx.HTML(200, MILESTONE) + ctx.HTML(200, tplMilestone) } func NewMilestone(ctx *context.Context) { diff --git a/templates/repo/issue/labels.tmpl b/templates/repo/issue/labels.tmpl index 64622945123a6..087894b2ad46f 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}}