github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgView/kmgBootstrap/button.go (about)

     1  package kmgBootstrap
     2  
     3  import (
     4  	"github.com/bronze1man/kmg/kmgErr"
     5  	"github.com/bronze1man/kmg/kmgRand"
     6  	"github.com/bronze1man/kmg/kmgView"
     7  	"net/url"
     8  )
     9  
    10  type ButtonSize string
    11  
    12  const (
    13  	ButtonSizeLarge      ButtonSize = "btn-lg"
    14  	ButtonSizeDefault    ButtonSize = ""
    15  	ButtonSizeSmall      ButtonSize = "btn-sm"
    16  	ButtonSizeExtraSmall ButtonSize = "btn-xs"
    17  )
    18  
    19  type ButtonColor string
    20  
    21  const (
    22  	ButtonColorDefault ButtonColor = "btn-default"
    23  	ButtonColorPrimary ButtonColor = "btn-primary"
    24  	ButtonColorSuccess ButtonColor = "btn-success"
    25  	ButtonColorInfo    ButtonColor = "btn-info"
    26  	ButtonColorWarning ButtonColor = "btn-warning"
    27  	ButtonColorDanger  ButtonColor = "btn-danger"
    28  	ButtonColorLink    ButtonColor = "btn-link"
    29  )
    30  
    31  type ButtonType string
    32  
    33  const (
    34  	ButtonTypeA      ButtonType = "a"
    35  	ButtonTypeButton ButtonType = "button"
    36  )
    37  
    38  type Button struct {
    39  	Type          ButtonType
    40  	Url           string
    41  	Color         ButtonColor
    42  	Size          ButtonSize
    43  	Content       kmgView.HtmlRenderer
    44  	AttributeNode kmgView.HtmlRenderer
    45  	ClassName     string
    46  	Id            string
    47  	FormId        string //HTML5 Attribute
    48  	Name          string
    49  	Value         string
    50  }
    51  
    52  func (b Button) HtmlRender() string {
    53  	if b.Color == "" {
    54  		b.Color = ButtonColorDefault
    55  	}
    56  	if b.Type == "" {
    57  		b.Type = ButtonTypeButton
    58  	}
    59  	if b.Content == nil {
    60  		panic("kmgBootstrap.Button.Content must not be empty")
    61  	}
    62  	return tplButton(b)
    63  }
    64  
    65  // 一个发起Get请求的按钮
    66  // url: 点击后的跳转链接
    67  // title: 按钮上面的字
    68  // 在当前页面打开连接
    69  func NewGetButton(url string, title string) Button {
    70  	return Button{
    71  		Type:    ButtonTypeA,
    72  		Url:     url,
    73  		Content: kmgView.String(title),
    74  		Size:    ButtonSizeExtraSmall,
    75  		Color:   ButtonColorInfo,
    76  	}
    77  }
    78  
    79  // 一个发起Post请求的按钮
    80  // url: 将把url转化为表单并使用Post方法提交
    81  // title: 按钮上面的字
    82  // 在当前页面打开连接
    83  //不支持一个 key 里塞多个 value 的 url
    84  func NewPostButton(urlStr string, title string) kmgView.HtmlRenderer {
    85  	formAction, err := url.Parse(urlStr)
    86  	kmgErr.PanicIfError(err)
    87  	query, err := url.ParseQuery(formAction.RawQuery)
    88  	kmgErr.PanicIfError(err)
    89  	formAction.RawQuery = ""
    90  	id := kmgRand.MustCryptoRandToReadableAlphaNum(10)
    91  	form := Form{
    92  		Id:        id,
    93  		NoSubmit:  true,
    94  		Url:       formAction.String(),
    95  		InputList: []kmgView.HtmlRenderer{},
    96  		IsHidden:  true,
    97  	}
    98  	for k, v := range query {
    99  		form.InputList = append(form.InputList, InputHidden{
   100  			Name:  k,
   101  			Value: v[0],
   102  		})
   103  	}
   104  	button := Button{
   105  		FormId:  id,
   106  		Type:    ButtonTypeButton,
   107  		Content: kmgView.String(title),
   108  		Size:    ButtonSizeExtraSmall,
   109  		Color:   ButtonColorInfo,
   110  	}
   111  	return kmgView.Html(form.HtmlRender() + button.HtmlRender())
   112  }