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

     1  package components
     2  
     3  import (
     4  	"fmt"
     5  	"html/template"
     6  	"strings"
     7  
     8  	"github.com/kotovmak/go-admin/modules/config"
     9  	"github.com/kotovmak/go-admin/modules/language"
    10  	"github.com/kotovmak/go-admin/modules/utils"
    11  	form2 "github.com/kotovmak/go-admin/plugins/admin/modules/form"
    12  	"github.com/kotovmak/go-admin/template/types"
    13  	"github.com/kotovmak/go-admin/template/types/form"
    14  )
    15  
    16  type FormAttribute struct {
    17  	Name            string
    18  	Id              string
    19  	Header          template.HTML
    20  	Content         types.FormFields
    21  	ContentList     []types.FormFields
    22  	Layout          form.Layout
    23  	TabContents     []types.FormFields
    24  	TabHeaders      []string
    25  	Footer          template.HTML
    26  	Url             string
    27  	FieldsHTML      template.HTML
    28  	Method          string
    29  	PrimaryKey      string
    30  	Ajax            bool
    31  	AjaxSuccessJS   template.JS
    32  	AjaxErrorJS     template.JS
    33  	HeadWidth       int
    34  	InputWidth      int
    35  	HiddenFields    map[string]string
    36  	Title           template.HTML
    37  	OperationFooter template.HTML
    38  	Prefix          string
    39  	CdnUrl          string
    40  	types.Attribute
    41  }
    42  
    43  func (compo *FormAttribute) SetHeader(value template.HTML) types.FormAttribute {
    44  	compo.Header = value
    45  	return compo
    46  }
    47  
    48  func (compo *FormAttribute) SetPrimaryKey(value string) types.FormAttribute {
    49  	compo.PrimaryKey = value
    50  	return compo
    51  }
    52  
    53  func (compo *FormAttribute) SetContent(value types.FormFields) types.FormAttribute {
    54  	compo.Content = value
    55  	return compo
    56  }
    57  
    58  func (compo *FormAttribute) SetId(id string) types.FormAttribute {
    59  	compo.Id = id
    60  	return compo
    61  }
    62  
    63  func (compo *FormAttribute) SetAjax(successJS, errorJS template.JS) types.FormAttribute {
    64  	if successJS != template.JS("") && errorJS != template.JS("") {
    65  		compo.Ajax = true
    66  		compo.AjaxErrorJS = errorJS
    67  		compo.AjaxSuccessJS = successJS
    68  	}
    69  	return compo
    70  }
    71  
    72  func (compo *FormAttribute) SetTabContents(value []types.FormFields) types.FormAttribute {
    73  	compo.TabContents = value
    74  	return compo
    75  }
    76  
    77  func (compo *FormAttribute) SetTabHeaders(value []string) types.FormAttribute {
    78  	compo.TabHeaders = value
    79  	return compo
    80  }
    81  
    82  func (compo *FormAttribute) SetHeadWidth(width int) types.FormAttribute {
    83  	if width > 0 {
    84  		if width > 12 {
    85  			width = 12
    86  		}
    87  		compo.HeadWidth = width
    88  	}
    89  	return compo
    90  }
    91  
    92  func (compo *FormAttribute) SetInputWidth(width int) types.FormAttribute {
    93  	if width > 0 {
    94  		if width > 12 {
    95  			width = 12
    96  		}
    97  		compo.InputWidth = width
    98  	}
    99  	return compo
   100  }
   101  
   102  func (compo *FormAttribute) SetFieldsHTML(html template.HTML) types.FormAttribute {
   103  	compo.FieldsHTML = html
   104  	return compo
   105  }
   106  
   107  func (compo *FormAttribute) SetFooter(value template.HTML) types.FormAttribute {
   108  	compo.Footer = value
   109  	return compo
   110  }
   111  
   112  func (compo *FormAttribute) SetLayout(layout form.Layout) types.FormAttribute {
   113  	compo.Layout = layout
   114  	return compo
   115  }
   116  
   117  func (compo *FormAttribute) SetPrefix(value string) types.FormAttribute {
   118  	compo.Prefix = value
   119  	return compo
   120  }
   121  
   122  func (compo *FormAttribute) SetUrl(value string) types.FormAttribute {
   123  	compo.Url = value
   124  	return compo
   125  }
   126  
   127  func (compo *FormAttribute) SetHiddenFields(fields map[string]string) types.FormAttribute {
   128  	compo.HiddenFields = fields
   129  	return compo
   130  }
   131  
   132  func (compo *FormAttribute) SetMethod(value string) types.FormAttribute {
   133  	compo.Method = value
   134  	return compo
   135  }
   136  
   137  func (compo *FormAttribute) SetTitle(value template.HTML) types.FormAttribute {
   138  	compo.Title = value
   139  	return compo
   140  }
   141  
   142  func (compo *FormAttribute) GetDefaultBoxHeader(hideBack bool) template.HTML {
   143  	if hideBack {
   144  		return template.HTML(fmt.Sprintf(`<h3 class="box-title">%s</h3>`, language.GetFromHtml(compo.Title)))
   145  	}
   146  	return template.HTML(fmt.Sprintf(`<h3 class="box-title">%s</h3>
   147              <div class="box-tools">
   148                  <div class="btn-group pull-right" style="margin-right: 10px">
   149                      <a href='%s' class="btn btn-sm btn-default form-history-back"><i
   150                                  class="fa fa-arrow-left"></i> %s</a>
   151                  </div>
   152              </div>`, language.GetFromHtml(compo.Title), compo.HiddenFields[form2.PreviousKey], language.Get("Back")))
   153  }
   154  
   155  func (compo *FormAttribute) GetDetailBoxHeader(editUrl, deleteUrl string) template.HTML {
   156  
   157  	var (
   158  		editBtn   string
   159  		deleteBtn string
   160  	)
   161  
   162  	if editUrl != "" {
   163  		editBtn = fmt.Sprintf(`
   164                  <div class="btn-group pull-right" style="margin-right: 10px">
   165                      <a href='%s' class="btn btn-sm btn-primary"><i
   166                                  class="fa fa-edit"></i> %s</a>
   167                  </div>`, editUrl, language.Get("Edit"))
   168  	}
   169  
   170  	if deleteUrl != "" {
   171  		deleteBtn = fmt.Sprintf(`
   172                  <div class="btn-group pull-right" style="margin-right: 10px">
   173                      <a href='javascript:;' class="btn btn-sm btn-danger delete-btn"><i
   174                                  class="fa fa-trash"></i> %s</a>
   175                  </div>`, language.Get("Delete"))
   176  	}
   177  
   178  	return template.HTML(`<h3 class="box-title">`) + language.GetFromHtml(compo.Title) + template.HTML(`</h3>
   179              <div class="box-tools">
   180  				`+deleteBtn+editBtn+`
   181                  <div class="btn-group pull-right" style="margin-right: 10px">
   182                      <a href='`+compo.HiddenFields[form2.PreviousKey]+`' class="btn btn-sm btn-default form-history-back"><i
   183                                  class="fa fa-arrow-left"></i> `+language.Get("Back")+`</a>
   184                  </div>
   185              </div>`)
   186  }
   187  
   188  func (compo *FormAttribute) GetBoxHeaderNoButton() template.HTML {
   189  	return template.HTML(fmt.Sprintf(`<h3 class="box-title">%s</h3>`, language.GetFromHtml(compo.Title)))
   190  }
   191  
   192  func (compo *FormAttribute) SetOperationFooter(value template.HTML) types.FormAttribute {
   193  	compo.OperationFooter = value
   194  	return compo
   195  }
   196  
   197  func (compo *FormAttribute) GetContent() template.HTML {
   198  	compo.CdnUrl = config.GetAssetUrl()
   199  	if compo.Id == "" {
   200  		compo.Id = utils.Uuid(10)
   201  	}
   202  
   203  	if col := compo.Layout.Col(); col > 0 {
   204  		compo.ContentList = make([]types.FormFields, col)
   205  		index := 0
   206  		for i := 0; i < len(compo.Content); i++ {
   207  			ii := index % col
   208  			compo.ContentList[ii] = append(compo.ContentList[ii], compo.Content[i])
   209  			if i < len(compo.Content)-1 {
   210  				if strings.Contains(compo.Content[i+1].Field, "__goadmin_operator__") {
   211  					compo.ContentList[ii] = append(compo.ContentList[ii], compo.Content[i+1])
   212  					i++
   213  				}
   214  			}
   215  			index++
   216  		}
   217  	}
   218  
   219  	return ComposeHtml(compo.TemplateList, compo.Separation, *compo, "form",
   220  		"form/default", "form/file", "form/multi_file", "form/textarea", "form/custom", "form/rate", "form/slider",
   221  		"form/selectbox", "form/text", "form/table", "form/radio", "form/switch", "form/checkbox", "form/checkbox_single",
   222  		"form/checkbox_stacked", "form/password", "form/code", "form/array", "form/select", "form/singleselect",
   223  		"form/richtext", "form/iconpicker", "form/datetime", "form/number", "form/number_range",
   224  		"form/email", "form/url", "form/ip", "form/color", "form/currency", "form_components", "form/datetime_range",
   225  		"form_layout_default", "form_layout_two_col", "form_layout_tab", "form_components_layout", "form_layout_flow")
   226  }