github.com/kotovmak/go-admin@v1.1.1/template/components/button.go (about)

     1  package components
     2  
     3  import (
     4  	"fmt"
     5  	"html/template"
     6  
     7  	"github.com/kotovmak/go-admin/modules/language"
     8  	"github.com/kotovmak/go-admin/modules/utils"
     9  	"github.com/kotovmak/go-admin/template/icon"
    10  	"github.com/kotovmak/go-admin/template/types"
    11  )
    12  
    13  type ButtonAttribute struct {
    14  	Name        string
    15  	Content     template.HTML
    16  	Orientation string
    17  	LoadingText template.HTML
    18  	Theme       string
    19  	Type        string
    20  	Size        string
    21  	Href        string
    22  	Class       string
    23  	ID          string
    24  	Style       template.HTMLAttr
    25  	MarginLeft  int
    26  	MarginRight int
    27  	types.Attribute
    28  }
    29  
    30  func (compo *ButtonAttribute) SetContent(value template.HTML) types.ButtonAttribute {
    31  	compo.Content = value
    32  	return compo
    33  }
    34  
    35  func (compo *ButtonAttribute) SetOrientationRight() types.ButtonAttribute {
    36  	compo.Orientation = "pull-right"
    37  	return compo
    38  }
    39  
    40  func (compo *ButtonAttribute) SetOrientationLeft() types.ButtonAttribute {
    41  	compo.Orientation = "pull-left"
    42  	return compo
    43  }
    44  
    45  func (compo *ButtonAttribute) SetMarginLeft(px int) types.ButtonAttribute {
    46  	compo.MarginLeft = px
    47  	return compo
    48  }
    49  
    50  func (compo *ButtonAttribute) SetSmallSize() types.ButtonAttribute {
    51  	compo.Size = "btn-sm"
    52  	return compo
    53  }
    54  
    55  func (compo *ButtonAttribute) SetMiddleSize() types.ButtonAttribute {
    56  	compo.Size = "btn-md"
    57  	return compo
    58  }
    59  
    60  func (compo *ButtonAttribute) SetMarginRight(px int) types.ButtonAttribute {
    61  	compo.MarginRight = px
    62  	return compo
    63  }
    64  
    65  func (compo *ButtonAttribute) SetLoadingText(value template.HTML) types.ButtonAttribute {
    66  	compo.LoadingText = value
    67  	return compo
    68  }
    69  
    70  func (compo *ButtonAttribute) SetThemePrimary() types.ButtonAttribute {
    71  	compo.Theme = "primary"
    72  	return compo
    73  }
    74  
    75  func (compo *ButtonAttribute) SetThemeDefault() types.ButtonAttribute {
    76  	compo.Theme = "default"
    77  	return compo
    78  }
    79  
    80  func (compo *ButtonAttribute) SetThemeWarning() types.ButtonAttribute {
    81  	compo.Theme = "warning"
    82  	return compo
    83  }
    84  
    85  func (compo *ButtonAttribute) SetHref(href string) types.ButtonAttribute {
    86  	compo.Href = href
    87  	return compo
    88  }
    89  
    90  func (compo *ButtonAttribute) AddClass(class string) types.ButtonAttribute {
    91  	compo.Class += " " + class
    92  	return compo
    93  }
    94  
    95  func (compo *ButtonAttribute) SetID(id string) types.ButtonAttribute {
    96  	compo.ID = id
    97  	return compo
    98  }
    99  
   100  func (compo *ButtonAttribute) SetTheme(value string) types.ButtonAttribute {
   101  	compo.Theme = value
   102  	return compo
   103  }
   104  
   105  func (compo *ButtonAttribute) SetType(value string) types.ButtonAttribute {
   106  	compo.Type = value
   107  	return compo
   108  }
   109  
   110  func (compo *ButtonAttribute) GetContent() template.HTML {
   111  
   112  	if compo.MarginLeft != 0 {
   113  		compo.Style = template.HTMLAttr(fmt.Sprintf(`style="margin-left:%dpx;"`, compo.MarginLeft))
   114  	}
   115  
   116  	if compo.MarginRight != 0 {
   117  		compo.Style = template.HTMLAttr(fmt.Sprintf(`style="margin-right:%dpx;"`, compo.MarginRight))
   118  	}
   119  
   120  	if compo.LoadingText == "" {
   121  		compo.LoadingText = icon.Icon(icon.Spinner, 1) + language.GetFromHtml(`Save`)
   122  	}
   123  
   124  	if compo.ID == "" {
   125  		compo.ID = utils.Uuid(15) + "_btn"
   126  	}
   127  
   128  	return ComposeHtml(compo.TemplateList, compo.Separation, *compo, "button")
   129  }