Prettify number of issues (#17760)
* Prettify number of issues - Use the PrettyNumber function to add commas in large amount of issues. * Use client-side formatting * prettify on both server and client * remove unused i18n entries * handle more cases, support other int types in PrettyNumber * specify locale to avoid issues with node default locale * remove superfluos argument * introduce template helper, octicon tweaks, js refactor * Update modules/templates/helper.go * Apply some suggestions. * Add comment * Update templates/user/dashboard/issues.tmpl Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
parent
0097fbc2ac
commit
796c4eca0b
14 changed files with 118 additions and 45 deletions
|
@ -24,6 +24,7 @@ import (
|
|||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
)
|
||||
|
@ -143,9 +144,9 @@ func FileSize(s int64) string {
|
|||
}
|
||||
|
||||
// PrettyNumber produces a string form of the given number in base 10 with
|
||||
// commas after every three orders of magnitud
|
||||
func PrettyNumber(v int64) string {
|
||||
return humanize.Comma(v)
|
||||
// commas after every three orders of magnitude
|
||||
func PrettyNumber(i interface{}) string {
|
||||
return humanize.Comma(util.NumberIntoInt64(i))
|
||||
}
|
||||
|
||||
// Subtract deals with subtraction of all types of number.
|
||||
|
|
|
@ -117,6 +117,7 @@ func TestFileSize(t *testing.T) {
|
|||
|
||||
func TestPrettyNumber(t *testing.T) {
|
||||
assert.Equal(t, "23,342,432", PrettyNumber(23342432))
|
||||
assert.Equal(t, "23,342,432", PrettyNumber(int32(23342432)))
|
||||
assert.Equal(t, "0", PrettyNumber(0))
|
||||
assert.Equal(t, "-100,000", PrettyNumber(-100000))
|
||||
}
|
||||
|
|
|
@ -98,18 +98,19 @@ func NewFuncMap() []template.FuncMap {
|
|||
"CustomEmojis": func() map[string]string {
|
||||
return setting.UI.CustomEmojisMap
|
||||
},
|
||||
"Safe": Safe,
|
||||
"SafeJS": SafeJS,
|
||||
"JSEscape": JSEscape,
|
||||
"Str2html": Str2html,
|
||||
"TimeSince": timeutil.TimeSince,
|
||||
"TimeSinceUnix": timeutil.TimeSinceUnix,
|
||||
"RawTimeSince": timeutil.RawTimeSince,
|
||||
"FileSize": base.FileSize,
|
||||
"PrettyNumber": base.PrettyNumber,
|
||||
"Subtract": base.Subtract,
|
||||
"EntryIcon": base.EntryIcon,
|
||||
"MigrationIcon": MigrationIcon,
|
||||
"Safe": Safe,
|
||||
"SafeJS": SafeJS,
|
||||
"JSEscape": JSEscape,
|
||||
"Str2html": Str2html,
|
||||
"TimeSince": timeutil.TimeSince,
|
||||
"TimeSinceUnix": timeutil.TimeSinceUnix,
|
||||
"RawTimeSince": timeutil.RawTimeSince,
|
||||
"FileSize": base.FileSize,
|
||||
"PrettyNumber": base.PrettyNumber,
|
||||
"JsPrettyNumber": JsPrettyNumber,
|
||||
"Subtract": base.Subtract,
|
||||
"EntryIcon": base.EntryIcon,
|
||||
"MigrationIcon": MigrationIcon,
|
||||
"Add": func(a ...int) int {
|
||||
sum := 0
|
||||
for _, val := range a {
|
||||
|
@ -1005,3 +1006,11 @@ func mirrorRemoteAddress(ctx context.Context, m *repo_model.Repository, remoteNa
|
|||
|
||||
return a
|
||||
}
|
||||
|
||||
// JsPrettyNumber renders a number using english decimal separators, e.g. 1,200 and subsequent
|
||||
// JS will replace the number with locale-specific separators, based on the user's selected language
|
||||
func JsPrettyNumber(i interface{}) template.HTML {
|
||||
num := util.NumberIntoInt64(i)
|
||||
|
||||
return template.HTML(`<span class="js-pretty-number" data-value="` + strconv.FormatInt(num, 10) + `">` + base.PrettyNumber(num) + `</span>`)
|
||||
}
|
||||
|
|
|
@ -224,3 +224,21 @@ func Dedent(s string) string {
|
|||
}
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
// NumberIntoInt64 transform a given int into int64.
|
||||
func NumberIntoInt64(number interface{}) int64 {
|
||||
var value int64
|
||||
switch v := number.(type) {
|
||||
case int:
|
||||
value = int64(v)
|
||||
case int8:
|
||||
value = int64(v)
|
||||
case int16:
|
||||
value = int64(v)
|
||||
case int32:
|
||||
value = int64(v)
|
||||
case int64:
|
||||
value = v
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue