code.gitea.io/gitea@v1.19.3/modules/web/middleware/flash.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package middleware
     5  
     6  import "net/url"
     7  
     8  // flashes enumerates all the flash types
     9  const (
    10  	SuccessFlash = "SuccessMsg"
    11  	ErrorFlash   = "ErrorMsg"
    12  	WarnFlash    = "WarningMsg"
    13  	InfoFlash    = "InfoMsg"
    14  )
    15  
    16  // FlashNow FIXME:
    17  var FlashNow bool
    18  
    19  // Flash represents a one time data transfer between two requests.
    20  type Flash struct {
    21  	DataStore
    22  	url.Values
    23  	ErrorMsg, WarningMsg, InfoMsg, SuccessMsg string
    24  }
    25  
    26  func (f *Flash) set(name, msg string, current ...bool) {
    27  	if f.Values == nil {
    28  		f.Values = make(map[string][]string)
    29  	}
    30  	isShow := false
    31  	if (len(current) == 0 && FlashNow) ||
    32  		(len(current) > 0 && current[0]) {
    33  		isShow = true
    34  	}
    35  
    36  	if isShow {
    37  		f.GetData()["Flash"] = f
    38  	} else {
    39  		f.Set(name, msg)
    40  	}
    41  }
    42  
    43  // Error sets error message
    44  func (f *Flash) Error(msg string, current ...bool) {
    45  	f.ErrorMsg = msg
    46  	f.set("error", msg, current...)
    47  }
    48  
    49  // Warning sets warning message
    50  func (f *Flash) Warning(msg string, current ...bool) {
    51  	f.WarningMsg = msg
    52  	f.set("warning", msg, current...)
    53  }
    54  
    55  // Info sets info message
    56  func (f *Flash) Info(msg string, current ...bool) {
    57  	f.InfoMsg = msg
    58  	f.set("info", msg, current...)
    59  }
    60  
    61  // Success sets success message
    62  func (f *Flash) Success(msg string, current ...bool) {
    63  	f.SuccessMsg = msg
    64  	f.set("success", msg, current...)
    65  }