2021-01-26 15:36:53 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-26 15:36:53 +00:00
|
|
|
|
2021-01-30 08:55:53 +00:00
|
|
|
package middleware
|
2021-01-26 15:36:53 +00:00
|
|
|
|
|
|
|
import "net/url"
|
|
|
|
|
|
|
|
// Flash represents a one time data transfer between two requests.
|
|
|
|
type Flash struct {
|
2023-05-04 06:36:34 +00:00
|
|
|
DataStore ContextDataStore
|
2021-01-26 15:36:53 +00:00
|
|
|
url.Values
|
|
|
|
ErrorMsg, WarningMsg, InfoMsg, SuccessMsg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Flash) set(name, msg string, current ...bool) {
|
2021-01-27 13:33:32 +00:00
|
|
|
if f.Values == nil {
|
|
|
|
f.Values = make(map[string][]string)
|
|
|
|
}
|
2023-07-15 08:52:03 +00:00
|
|
|
showInCurrentPage := len(current) > 0 && current[0]
|
|
|
|
if showInCurrentPage {
|
|
|
|
// assign it to the context data, then the template can use ".Flash.XxxMsg" to render the message
|
2023-05-04 06:36:34 +00:00
|
|
|
f.DataStore.GetData()["Flash"] = f
|
2021-01-26 15:36:53 +00:00
|
|
|
} else {
|
2023-07-15 08:52:03 +00:00
|
|
|
// the message map will be saved into the cookie and be shown in next response (a new page response which decodes the cookie)
|
2021-01-26 15:36:53 +00:00
|
|
|
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...)
|
|
|
|
}
|