示例#1
0
func (f *datepickerField) Build(form formData) string {
	f.Class = append(f.Class, "form-control")

	attrs := map[string]string{
		"type":              "text",
		"id":                fmt.Sprintf("%s%s", form.GetName(), f.ID),
		"name":              fmt.Sprintf("%s%s", form.GetName(), f.ID),
		"placeholder":       f.PlaceHolder,
		"class":             strings.Join(f.Class, " "),
		"ng-model":          fmt.Sprintf("%s.%s", form.GetObjName(), f.ID),
		"datepicker-popup":  f.DateFormat,
		"datepicker-manual": "",
	}
	if len(f.IsOpen) > 0 {
		attrs["is-open"] = f.IsOpen
	}
	if len(f.Options) > 0 {
		attrs["datepicker-options"] = f.Options
	}
	utils.UpdateMap(attrs, f.Attrs)

	newAttrs, container := f.buildContainer(form)
	utils.UpdateMap(attrs, newAttrs)

	ctrl := utils.BuildCtrlTag("<input", ">", attrs)
	return fmt.Sprintf(container, ctrl)
}
示例#2
0
文件: input.go 项目: ernestoalejo/cb
func (f *inputField) Build(form formData) string {
	if f.Type == "" {
		panic("input type should not be empty: " + f.ID)
	}

	f.Class = append(f.Class, "form-control")

	attrs := map[string]string{
		"type":        f.Type,
		"id":          fmt.Sprintf("%s%s", form.GetName(), f.ID),
		"name":        fmt.Sprintf("%s%s", form.GetName(), f.ID),
		"placeholder": f.PlaceHolder,
		"class":       strings.Join(f.Class, " "),
		"ng-model":    fmt.Sprintf("%s.%s", form.GetObjName(), f.ID),
	}
	utils.UpdateMap(attrs, f.Attrs)

	newAttrs, container := f.buildContainer(form)
	utils.UpdateMap(attrs, newAttrs)

	ctrl := utils.BuildCtrlTag("<input", ">", attrs)
	if len(f.Prefix) > 0 {
		ctrl = templates.Run("input-field-prefix", map[string]interface{}{
			"Prefix": f.Prefix,
			"Ctrl":   ctrl,
		})
	}
	return fmt.Sprintf(container, ctrl)
}
示例#3
0
文件: static.go 项目: ernestoalejo/cb
func (f *staticField) Build(form formData) string {
	f.Class = append(f.Class, "form-control-static")

	attrs := map[string]string{
		"class": strings.Join(f.Class, " "),
	}
	utils.UpdateMap(attrs, f.Attrs)

	newAttrs, container := f.buildContainer(form)
	utils.UpdateMap(attrs, newAttrs)

	ctrl := utils.BuildCtrlTag("<p", ">", attrs) + f.Content + "</p>"
	return fmt.Sprintf(container, ctrl)
}
示例#4
0
func (f *textAreaField) Build(form formData) string {
	f.Class = append(f.Class, "form-control")

	attrs := map[string]string{
		"id":          fmt.Sprintf("%s%s", form.GetName(), f.ID),
		"name":        fmt.Sprintf("%s%s", form.GetName(), f.ID),
		"placeholder": f.PlaceHolder,
		"class":       strings.Join(f.Class, " "),
		"ng-model":    fmt.Sprintf("%s.%s", form.GetObjName(), f.ID),
		"rows":        fmt.Sprintf("%d", f.Rows),
	}
	utils.UpdateMap(attrs, f.Attrs)

	newAttrs, container := f.buildContainer(form)
	utils.UpdateMap(attrs, newAttrs)

	ctrl := utils.BuildCtrlTag("<textarea", "></textarea>", attrs)
	return fmt.Sprintf(container, ctrl)
}
示例#5
0
func (f *checkboxField) Build(form formData) string {
	label := f.Label
	f.Label = ""

	attrs := map[string]string{
		"type":     "checkbox",
		"id":       fmt.Sprintf("%s%s", form.GetName(), f.ID),
		"name":     fmt.Sprintf("%s%s", form.GetName(), f.ID),
		"class":    strings.Join(f.Class, " "),
		"ng-model": fmt.Sprintf("%s.%s", form.GetObjName(), f.ID),
	}
	utils.UpdateMap(attrs, f.Attrs)

	newAttrs, container := f.buildContainer(form)
	utils.UpdateMap(attrs, newAttrs)

	ctrl := utils.BuildCtrlTag("<input", ">", attrs)
	ctrl = fmt.Sprintf(`<label for=""%s>%s %s</label>`, attrs["id"], ctrl, label)
	return fmt.Sprintf(container, ctrl)
}
示例#6
0
文件: submit.go 项目: ernestoalejo/cb
func (f *submitField) Build(form formData) string {
	f.Class = append(f.Class, "btn")
	f.Class = append(f.Class, "btn-primary")

	label := f.Label
	f.Label = ""

	attrs := map[string]string{
		"ng-disabled": fmt.Sprintf("%s.val && !%s.$valid", form.GetName(), form.GetName()),
		"class":       strings.Join(f.Class, " "),
	}
	utils.UpdateMap(attrs, f.Attrs)

	newAttrs, container := f.buildContainer(form)
	utils.UpdateMap(attrs, newAttrs)

	ctrl := utils.BuildCtrlTag("<button", ">", attrs) + label + "</button>"
	ctrl = "<p>&nbsp;</p>\n      " + ctrl
	return fmt.Sprintf(container, ctrl)
}
示例#7
0
文件: base.go 项目: ernestoalejo/cb
func (f *BaseField) buildContainer(form formData) (map[string]string, string) {
	var messages, showErrs string
	attrs := map[string]string{}

	// Add the correct sizes to the container & label
	if len(f.LabelSize) == 0 {
		f.LabelSize = []string{"col-xs-3", "col-lg-2"}
	}
	if f.Label == "" && len(f.Size) == 0 {
		f.Size = []string{"col-xs-9", "col-sm-offset-3", "col-lg-10", "col-lg-offset-2"}
	}
	if len(f.Size) == 0 {
		f.Size = []string{"col-xs-9", "col-lg-10"}
	}

	// Add the sizes comments to the error block
	errsize := []string{}
	if f.Label == "" {
		for _, c := range f.Size {
			if colOffsetRe.MatchString(c) {
				errsize = append(errsize, c)
			}
		}
	} else {
		for _, c := range f.LabelSize {
			if colRe.MatchString(c) {
				parts := strings.Split(c, "-")
				errsize = append(errsize, fmt.Sprintf("col-%s-offset-%s", parts[1], parts[2]))
			}
		}
	}

	fid := fmt.Sprintf("['%s%s']", form.GetName(), f.ID)
	validators := form.GetValidators()[f.ID]
	if len(validators) > 0 {
		var errs string
		for _, val := range validators {
			utils.UpdateMap(attrs, val.Attrs)

			var e string
			if val.User {
				e = fmt.Sprintf("!%s%s.$dirty && !%s%s.$invalid && (%s)",
					form.GetName(), fid, form.GetName(), fid, val.Error)
				showErrs += fmt.Sprintf(" || (!%s%s.$dirty && (%s))", form.GetName(),
					fid, val.Error)
			} else {
				e = fmt.Sprintf("%s%s.$error.%s", form.GetName(), fid, val.Error)
			}
			errs += fmt.Sprintf(`      <span ng-show="%s">`, e)
			errs += "\n        " + val.Message + "\n      </span>\n"
		}

		messages = templates.Run("error-messages", map[string]interface{}{
			"Name":       form.GetName(),
			"Id":         fid,
			"ShowErrors": showErrs,
			"Errors":     errs,
			"Size":       strings.Join(errsize, " "),
		})
	}

	return attrs, templates.Run("field", map[string]interface{}{
		"Name":           form.GetName(),
		"Messages":       messages,
		"FieldId":        fid,
		"Id":             f.ID,
		"Label":          f.Label,
		"LabelSize":      strings.Join(f.LabelSize, " "),
		"Size":           strings.Join(f.Size, " "),
		"ShowErrors":     showErrs,
		"ContainerAttrs": utils.BuildAttrs(0, f.ContainerAttrs),
	})
}