-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsort_order_page.go
148 lines (133 loc) · 2.95 KB
/
sort_order_page.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package jiraui
import (
"fmt"
ui "gopkg.in/gizak/termui.v2"
)
type Sort struct {
Name string
JQL string
}
type SortOrderPage struct {
BaseListPage
cachedResults []Sort
}
var baseSorts = []Sort{
Sort{"default", " "},
Sort{"created, oldest first", "ORDER BY created ASC"},
Sort{"updated, newest first", "ORDER BY updated DESC"},
Sort{"updated, oldest first", "ORDER BY updated ASC"},
Sort{"rank", "ORDER BY Rank DESC"},
Sort{"---", ""}, // no-op line in UI
}
func getSorts() (sorts []Sort) {
opts := getJiraOpts()
if q := opts["sorts"]; q != nil {
qList := q.([]interface{})
for _, v := range qList {
q1 := v.(map[interface{}]interface{})
q2 := make(map[string]string)
for k, v := range q1 {
switch k := k.(type) {
case string:
switch v := v.(type) {
case string:
q2[k] = v
}
}
}
sorts = append(sorts, Sort{q2["name"], q2["jql"]})
}
}
return append(baseSorts, sorts...)
}
func (p *SortOrderPage) IsPopulated() bool {
if len(p.cachedResults) > 0 {
return true
} else {
return false
}
}
func (p *SortOrderPage) itemizeResults() []string {
items := make([]string, len(p.cachedResults))
for i, v := range p.cachedResults {
items[i] = fmt.Sprintf("%s", v.Name)
}
return items
}
func (p *SortOrderPage) PreviousPara() {
newDisplayLine := 0
sl := p.uiList.Cursor
if sl == 0 {
return
}
for i := sl - 1; i > 0; i-- {
if p.cachedResults[i].JQL == "" {
newDisplayLine = i
break
}
}
p.PreviousLine(sl - newDisplayLine)
}
func (p *SortOrderPage) NextPara() {
newDisplayLine := len(p.cachedResults) - 1
sl := p.uiList.Cursor
if sl == newDisplayLine {
return
}
for i := sl + 1; i < len(p.cachedResults); i++ {
if p.cachedResults[i].JQL == "" {
newDisplayLine = i
break
}
}
p.NextLine(newDisplayLine - sl)
}
func (p *SortOrderPage) SelectedSort() Sort {
return p.cachedResults[p.uiList.Cursor]
}
func (p *SortOrderPage) SelectItem() {
if p.SelectedSort().JQL == "" {
return
}
q := new(TicketListPage)
// pop old page, we're going to 'replace' it
var oldTicketListPage Navigable
if len(previousPages) > 0 {
oldTicketListPage, previousPages = previousPages[len(previousPages)-1], previousPages[:len(previousPages)-1]
switch a := oldTicketListPage.(type) {
case *TicketListPage:
q.ActiveQuery = a.ActiveQuery
q.ActiveSort = p.SelectedSort()
currentPage = q
}
}
changePage()
}
func (p *SortOrderPage) Update() {
ls := p.uiList
ui.Render(ls)
}
func (p *SortOrderPage) Refresh() {
pDeref := &p
q := *pDeref
q.cachedResults = make([]Sort, 0)
changePage()
q.Create()
}
func (p *SortOrderPage) Create() {
ls := NewScrollableList()
p.uiList = ls
p.uiList.Cursor = 0
if len(p.cachedResults) == 0 {
p.cachedResults = getSorts()
}
ls.Items = p.itemizeResults()
ls.ItemFgColor = ui.ColorGreen
ls.BorderLabel = "Sort By..."
ls.BorderFg = ui.ColorRed
ls.Height = 10
ls.Width = 50
ls.X = ui.TermWidth()/2 - ls.Width/2
ls.Y = ui.TermHeight()/2 - ls.Height/2
p.Update()
}