helpers

package
v0.0.0-...-5f8c1fa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2023 License: BSD-2-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BadgeClassMap = map[string]string{
	constants.StatusCancelled: "is-cancelled",
	constants.StatusFailed:    "is-failed",
	constants.StatusPending:   "is-pending",
	constants.StatusStarted:   "is-started",
	constants.StatusSuccess:   "is-success",
	constants.StatusSuspended: "is-suspended",
}

BadgeClassMap maps work item status and other constant values to css badge classes.

View Source
var IconMap = map[string]string{

	constants.EventAccessAssignment:     "admin_panel_settings",
	constants.EventCreate:               "add_circle_outline",
	constants.EventDeletion:             "delete_forever",
	constants.EventDigestCalculation:    "description",
	constants.EventFixityCheck:          "fingerprint",
	constants.EventIdentifierAssignment: "search",
	constants.EventIngestion:            "file_upload",
	constants.EventReplication:          "library_books",

	constants.ActionDelete:         "delete_forever",
	constants.ActionIngest:         "file_upload",
	constants.ActionRestoreFile:    "file_download",
	constants.ActionRestoreObject:  "file_download",
	constants.ActionGlacierRestore: "file_download",
}

IconMap maps strings to Material icons.

View Source
var IconMissing = `help_outline`

IconMissing is the icon we show for items that are not in the IconMap. This appears as a question mark. If you see it on any web page, you should know to add an appropriate icon to the IconMap.

Functions

func BadgeClass

func BadgeClass(str string) template.HTML

BadgeClass returns the css class for the specified string, where string is a work item status or other value defined in Constants.

func CurrentUser

func CurrentUser(c CookieSetter) *pgmodels.User

func DateISO

func DateISO(date time.Time) string

DateISO returns a date in format "2006-01-02"

func DateTimeISO

func DateTimeISO(date time.Time) string

DateTimeISO returns a date in format "2006-01-02T15:04:05Z"

func DateTimeUS

func DateTimeUS(date time.Time) string

DateUS returns a date in format "Jan 2, 2006 15:04:05"

func DateUS

func DateUS(date time.Time) string

DateUS returns a date in format "Jan 2, 2006"

func DefaultString

func DefaultString(value, _default string) string

DefaultString returns value if it's non-empty. Otherwise, it returns _default.

func DeleteCSRFCookie

func DeleteCSRFCookie(c CookieSetter)

func DeleteCookie

func DeleteCookie(c CookieSetter, name string)

func DeleteFlashCookie

func DeleteFlashCookie(c CookieSetter)

func DeletePrefsCookie

func DeletePrefsCookie(c CookieSetter)

func DeleteSessionCookie

func DeleteSessionCookie(c CookieSetter)

func Dict

func Dict(values ...interface{}) (map[string]interface{}, error)

Dict returns an interface map suitable for passing into sub templates.

func EscapeAttr

func EscapeAttr(s string) template.HTMLAttr

EscapeAttr escapes an HTML attribute value. This helps avoid the ZgotmplZ problem.

func EscapeHTML

func EscapeHTML(s string) template.HTML

EscapeHTML returns an escaped HTML string. This helps avoid the ZgotmplZ problem.

func FormatFloat

func FormatFloat(value float64, scale int) string

FormatFloat formats a floating point number to have scale digits after the decimal point.

func FormatInt

func FormatInt(value int) string

FormatInt formats an integer value to include commas. 28635177 becomes 28,635,177

func FormatInt64

func FormatInt64(value int64) string

FormatInt64 formats a 64-bit integer value to include commas. 28635177 becomes 28,635,177

func HumanSize

func HumanSize(size int64) string

HumanSize returns a number of bytes in a human-readable format. Note that we use base 1024, not base 1000, because AWS uses 1024 to calculate the storage size of the objects we're reporting on.

func IconFor

func IconFor(str string) template.HTML

IconFor returns a FontAwesome icon for the specified string, as defined in helpers.IconMap. If the IconMap has no entry for the string, this returns helpers.IconMissing.

func LinkifyUrls

func LinkifyUrls(text string) template.HTML

LinkifyUrls converts urls in text to clickable links. That is, it replaces https://example.com with <a href="https://example.com">https://example.com</a>

URLs outside the current domain will open in a new tab (i.e. will have target="_blank").

This also replaces newlines with <br/> tags.

func RoleName

func RoleName(role string) string

RoleName transforms ugly DB role names into more readable ones.

func SetCSRFCookie

func SetCSRFCookie(c CookieSetter) (string, error)

func SetCookie

func SetCookie(c CookieSetter, name, value string) error

func SetFlashCookie

func SetFlashCookie(c CookieSetter, value string) error

func SetPrefsCookie

func SetPrefsCookie(c CookieSetter, value string) error

func SetSessionCookie

func SetSessionCookie(c CookieSetter, user *pgmodels.User) error

func SortIcon

func SortIcon(currentUrl *url.URL, colName string) string

SortIcon returns the name of the sort icon to display at the top of a table column. This will be either "keyboard_arrow_up" or "keyboard_arrow_down"

func SortUrl

func SortUrl(currentUrl *url.URL, colName string) string

SortUrl returns the url to sort results by the specified column. This is used in table column headers on index pages. Note that this returns a URL path and query string only. There's no hostname, port, or scheme.

func StrEq

func StrEq(val1, val2 interface{}) bool

StrEq compares the string representation of two values and returns true if they are equal.

func ToJSON

func ToJSON(v interface{}) template.JS

ToJSON converts an interface to JSON.

func Truncate

func Truncate(value string, length int) string

Truncate truncates the value to the given length, appending an ellipses to the end. If value contains HTML tags, they will be stripped because truncating HTML can result in unclosed tags that will ruin the page layout.

func TruncateMiddle

func TruncateMiddle(str string, maxLen int) string

TruncateStart trims str to maxLen by removing them from the middle of the string. It adds dots to the middle of the string to indicate text was trimmed.

func TruncateStart

func TruncateStart(str string, maxLen int) string

TruncateStart trims str to maxLen by removing them from the start of the string. It adds leading dots to indicate some text was trimmed.

func UnixToISO

func UnixToISO(ts int64) string

UnixToISO converts a Unix timestamp to ISO format.

func UserCan

func UserCan(user *pgmodels.User, permission constants.Permission, instID int64) bool

UserCan returns true if the user has the specified permission.

func YesNo

func YesNo(value bool) string

YesNo returns "Yes" if value is true, "No" if value is false.

Types

type ByLen

type ByLen []string

ByLen implements sorting by length

func (ByLen) Len

func (a ByLen) Len() int

Let returns the length of slice a.

func (ByLen) Less

func (a ByLen) Less(i, j int) bool

Less return true if i is less than j.

func (ByLen) Swap

func (a ByLen) Swap(i, j int)

Swap i and j in slice.

type CookieSetter

type CookieSetter interface {
	SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
	SetSameSite(samesite http.SameSite)
	Get(key string) (value interface{}, exists bool)
}

CookieSetter defines the methods used by gin.Context to set cookies. We define it as an interface so we don't have to mock the entire sprawling gin.Context when testing.

This also defines gin.Context's Get() method, which retrieves context values and SetSameSite, which is a security directive for cookie access.

type CoverPhoto

type CoverPhoto struct {
	Source       string
	Photographer string
	CreditURL    string
}

func GetCover

func GetCover() CoverPhoto

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL