2021-01-26 23:36:53 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-26 23:36:53 +08:00
|
|
|
|
2021-01-30 16:55:53 +08:00
|
|
|
package middleware
|
2021-01-26 23:36:53 +08:00
|
|
|
|
|
|
|
import "net/url"
|
|
|
|
|
|
|
|
// flashes enumerates all the flash types
|
|
|
|
const (
|
|
|
|
SuccessFlash = "SuccessMsg"
|
|
|
|
ErrorFlash = "ErrorMsg"
|
|
|
|
WarnFlash = "WarningMsg"
|
|
|
|
InfoFlash = "InfoMsg"
|
|
|
|
)
|
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
// FlashNow FIXME:
|
|
|
|
var FlashNow bool
|
2021-01-26 23:36:53 +08:00
|
|
|
|
|
|
|
// Flash represents a one time data transfer between two requests.
|
|
|
|
type Flash struct {
|
|
|
|
DataStore
|
|
|
|
url.Values
|
|
|
|
ErrorMsg, WarningMsg, InfoMsg, SuccessMsg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Flash) set(name, msg string, current ...bool) {
|
2021-01-27 21:33:32 +08:00
|
|
|
if f.Values == nil {
|
|
|
|
f.Values = make(map[string][]string)
|
|
|
|
}
|
2021-01-26 23:36:53 +08:00
|
|
|
isShow := false
|
|
|
|
if (len(current) == 0 && FlashNow) ||
|
|
|
|
(len(current) > 0 && current[0]) {
|
|
|
|
isShow = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if isShow {
|
|
|
|
f.GetData()["Flash"] = f
|
|
|
|
} else {
|
|
|
|
f.Set(name, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error sets error message
|
|
|
|
func (f *Flash) Error(msg string, current ...bool) {
|
|
|
|
f.ErrorMsg = msg
|
|
|
|
f.set("error", msg, current...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warning sets warning message
|
|
|
|
func (f *Flash) Warning(msg string, current ...bool) {
|
|
|
|
f.WarningMsg = msg
|
|
|
|
f.set("warning", msg, current...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info sets info message
|
|
|
|
func (f *Flash) Info(msg string, current ...bool) {
|
|
|
|
f.InfoMsg = msg
|
|
|
|
f.set("info", msg, current...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success sets success message
|
|
|
|
func (f *Flash) Success(msg string, current ...bool) {
|
|
|
|
f.SuccessMsg = msg
|
|
|
|
f.set("success", msg, current...)
|
|
|
|
}
|