github.com/kotovmak/go-admin@v1.1.1/template/types/action/base.go (about)

     1  package action
     2  
     3  import (
     4  	"encoding/json"
     5  	"html/template"
     6  
     7  	"github.com/kotovmak/go-admin/context"
     8  	"github.com/kotovmak/go-admin/modules/config"
     9  	"github.com/kotovmak/go-admin/modules/utils"
    10  	"github.com/kotovmak/go-admin/template/types"
    11  )
    12  
    13  type AjaxData map[string]interface{}
    14  
    15  func NewAjaxData() AjaxData {
    16  	return AjaxData{"ids": "{{.Ids}}"}
    17  }
    18  
    19  func (a AjaxData) Add(m map[string]interface{}) AjaxData {
    20  	for k, v := range m {
    21  		a[k] = v
    22  	}
    23  	return a
    24  }
    25  
    26  func (a AjaxData) JSON() string {
    27  	b, _ := json.Marshal(a)
    28  	return utils.ReplaceAll(string(b), `"{%id}"`, "{{.Id}}",
    29  		`"{%ids}"`, "{{.Ids}}",
    30  		`"{{.Ids}}"`, "{{.Ids}}",
    31  		`"{{.Id}}"`, "{{.Id}}")
    32  }
    33  
    34  type BaseAction struct {
    35  	BtnId   string
    36  	BtnData interface{}
    37  	JS      template.JS
    38  }
    39  
    40  func (base *BaseAction) SetBtnId(btnId string) {
    41  	if btnId[0] != '.' && btnId[0] != '#' {
    42  		base.BtnId = "." + btnId
    43  	} else {
    44  		base.BtnId = btnId
    45  	}
    46  }
    47  func (base *BaseAction) Js() template.JS              { return base.JS }
    48  func (base *BaseAction) BtnClass() template.HTML      { return "" }
    49  func (base *BaseAction) BtnAttribute() template.HTML  { return "" }
    50  func (base *BaseAction) GetCallbacks() context.Node   { return context.Node{} }
    51  func (base *BaseAction) ExtContent() template.HTML    { return template.HTML(``) }
    52  func (base *BaseAction) FooterContent() template.HTML { return template.HTML(``) }
    53  func (base *BaseAction) SetBtnData(data interface{})  { base.BtnData = data }
    54  
    55  var _ types.Action = (*AjaxAction)(nil)
    56  var _ types.Action = (*PopUpAction)(nil)
    57  var _ types.Action = (*JumpAction)(nil)
    58  var _ types.Action = (*JumpSelectBoxAction)(nil)
    59  
    60  func URL(id string) string {
    61  	return config.Url("/operation/" + utils.WrapURL(id))
    62  }