Esempio n. 1
0
func (self *setPostValuesStructVisitor) field(field *model.MetaData) error {
	if self.form.IsFieldDisabled(field) || self.form.IsFieldExcluded(field, self.ctx) {
		return nil
	}
	value := self.ctx.Request.FormValue(field.Selector())
	return self.form.GetFieldControllers().SetValue(value, self.ctx, field, self.form)
}
Esempio n. 2
0
func (self ModelTextController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	text := metaData.Value.Addr().Interface().(*model.Text)
	var cols int // will be zero if not available, which is OK
	if str, ok := metaData.Attrib(StructTagKey, "cols"); ok {
		cols, err = strconv.Atoi(str)
		if err != nil {
			panic("Error in StandardFormFieldFactory.NewInput(): " + err.Error())
		}
	}
	var rows int // will be zero if not available, which is OK
	if str, ok := metaData.Attrib(StructTagKey, "rows"); ok {
		rows, err = strconv.Atoi(str)
		if err != nil {
			panic("Error in StandardFormFieldFactory.NewInput(): " + err.Error())
		}
	}
	textArea := &TextArea{
		Class:       form.FieldInputClass(metaData),
		Name:        metaData.Selector(),
		Text:        text.Get(),
		Cols:        cols,
		Rows:        rows,
		Disabled:    form.IsFieldDisabled(metaData),
		Placeholder: form.InputFieldPlaceholder(metaData),
	}
	if metaData.BoolAttrib("view", "autofocus") {
		textArea.Autofocus = true
	}
	if withLabel {
		return AddStandardLabel(form, textArea, metaData), nil
	}
	return textArea, nil
}
func (self ModelPasswordController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	password := metaData.Value.Addr().Interface().(*model.Password)
	textField := &TextField{
		Class:       form.FieldInputClass(metaData),
		Name:        metaData.Selector(),
		Type:        PasswordTextField,
		Text:        password.Get(),
		Size:        form.GetInputSize(metaData),
		Disabled:    form.IsFieldDisabled(metaData),
		Placeholder: form.InputFieldPlaceholder(metaData),
		Required:    form.IsFieldRequired(metaData) && !metaData.IsSelfOrParentIndexed(),
		Title:       form.FieldLabel(metaData),
	}
	if maxlen, ok, _ := password.Maxlen(metaData); ok {
		textField.MaxLength = maxlen
		if maxlen < textField.Size {
			textField.Size = maxlen
		}
	}
	if metaData.BoolAttrib("view", "autofocus") {
		textField.Autofocus = true
	}
	if withLabel {
		return AddStandardLabel(form, textField, metaData), nil
	}
	return textField, nil
}
func (self *MongoRefController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	mongoRef := metaData.Value.Addr().Interface().(*mongo.Ref)

	options := []string{""}
	for doc := self.OptionsIterator.Next(); doc != nil; doc = self.OptionsIterator.Next() {
		name := self.getLabel(doc)
		for _, option := range options {
			if option == name {
				return nil, fmt.Errorf("Names of mongo documents must be unique, found double '%s'", name)
			}
		}
		options = append(options, name)
	}

	name := ""
	doc, err := mongoRef.Get()
	if err != nil {
		return nil, err
	}
	if doc != nil {
		name = self.getLabel(doc)
	}

	input = &Select{
		Class:    form.FieldInputClass(metaData),
		Name:     metaData.Selector(),
		Model:    &StringsSelectModel{options, name},
		Disabled: form.IsFieldDisabled(metaData),
		Size:     1,
	}
	if withLabel {
		return AddStandardLabel(form, input, metaData), nil
	}
	return input, nil
}
Esempio n. 5
0
func (self ModelStringController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	str := metaData.Value.Addr().Interface().(*model.String)
	textField := &TextField{
		Class:       form.FieldInputClass(metaData),
		Name:        metaData.Selector(),
		Text:        str.Get(),
		Size:        form.GetInputSize(metaData),
		Disabled:    form.IsFieldDisabled(metaData),
		Placeholder: form.InputFieldPlaceholder(metaData),
		Required:    form.IsFieldRequired(metaData),
		Title:       form.FieldLabel(metaData),
	}
	if maxlen, ok, _ := str.Maxlen(metaData); ok {
		textField.MaxLength = maxlen
		if maxlen < textField.Size {
			textField.Size = maxlen
		}
	}
	if metaData.BoolAttrib("view", "search") {
		textField.Type = SearchTextField
	}
	if metaData.BoolAttrib("view", "autofocus") {
		textField.Autofocus = true
	}
	if withLabel {
		return AddStandardLabel(form, textField, metaData), nil
	}
	return textField, nil
}
Esempio n. 6
0
func (self *Form) IsFieldRequired(metaData *model.MetaData) bool {
	if val, ok := metaData.ModelValue(); ok && val.Required(metaData) {
		return true
	}
	selector := metaData.Selector()
	wildcardSelector := metaData.WildcardSelector()
	return utils.StringIn(selector, self.RequiredFields) || utils.StringIn(wildcardSelector, self.RequiredFields)
}
Esempio n. 7
0
func (self *Form) IsFieldHidden(metaData *model.MetaData) bool {
	if metaData.BoolAttrib("hidden") {
		return true
	}
	selector := metaData.Selector()
	wildcardSelector := metaData.WildcardSelector()
	return utils.StringIn(selector, self.HiddenFields) || utils.StringIn(wildcardSelector, self.HiddenFields)
}
Esempio n. 8
0
func (self *Form) IsFieldDisabled(metaData *model.MetaData) bool {
	if metaData.BoolAttrib("disabled") {
		return true
	}
	selector := metaData.Selector()
	wildcardSelector := metaData.WildcardSelector()
	return utils.StringIn(selector, self.DisabledFields) || utils.StringIn(wildcardSelector, self.DisabledFields)
}
Esempio n. 9
0
// IsFieldExcluded returns weather a field will be excluded from the form.
// Fields will be excluded, if their selector matches one in Form.ExcludedFields
// or if a matching Authenticator from Form.ModelFieldAuth returns false.
// A field is also excluded when its parent field is excluded.
// This function is not restricted to model.Value, it works with all struct fields.
// This way a whole sub struct an be excluded by adding its selector to Form.ExcludedFields.
func (self *Form) IsFieldExcluded(field *model.MetaData, ctx *Context) bool {
	if field.Parent == nil {
		return false // can't exclude root
	}
	if self.IsFieldExcluded(field.Parent, ctx) || field.SelectorsMatch(self.ExcludedFields) {
		return true
	}
	if len(self.ModelFieldAuth) > 0 {
		auth, hasAuth := self.ModelFieldAuth[field.Selector()]
		if !hasAuth {
			auth, hasAuth = self.ModelFieldAuth[field.WildcardSelector()]
		}
		if hasAuth {
			ok, err := auth.Authenticate(ctx)
			if err != nil {
				fmt.Println("Error in view.Form.IsFieldExcluded(): " + err.Error())
			}
			if !ok {
				return true
			}
		}
	}
	if len(Config.NamedAuthenticators) > 0 {
		if authAttrib, ok := field.Attrib(StructTagKey, "auth"); ok {
			for _, name := range strings.Split(authAttrib, ",") {
				// if multi := strings.Split(name, "+"); len(multi) > 1 {
				// 	// Needs to pass all sub-Authenticators
				// 	for _, name := range multi {
				// 		if auth, ok := NamedAuthenticator(name); ok {
				// 			ok, err := auth.Authenticate(response)
				// 			if err != nil {
				// 				fmt.Println("Error in view.Form.IsFieldExcluded(): " + err.Error())
				// 			}
				// 			if !ok {
				// 				return true
				// 			}
				// 		}
				// 	}
				// } else {
				if auth, ok := NamedAuthenticator(name); ok {
					ok, err := auth.Authenticate(ctx)
					if ok {
						// Only needs to pass one Authenticator
						return false
					}
					if err != nil {
						log.Println("Error in view.Form.IsFieldExcluded(): " + err.Error())
					}
				}
				// }
			}
			// No Authenticators passed, thus exclude
			return true
		}
	}
	return false
}
Esempio n. 10
0
func (self BlobRefController) NewInput(withLabel bool, metaData *model.MetaData, form *view.Form) (input view.View, err error) {
	blobRef := metaData.Value.Addr().Interface().(*BlobRef)

	hiddenInput := &view.HiddenInput{Name: metaData.Selector(), Value: blobRef.String()}

	uploadList := &view.List{Class: "qq-upload-list"}

	removeButton := &view.Div{
		Class:   "qq-upload-button",
		Content: view.HTML("Remove"),
		OnClick: fmt.Sprintf(
			`jQuery("#%s").attr("value", "");
			jQuery("#%s").empty();`,
			hiddenInput.ID(),
			uploadList.ID(),
		),
	}

	chooseList := view.Views{
		view.JQueryUIAutocompleteFromURL(".gostart-select-blob", API.AllBlobs, 2),
	}

	uploadButton := UploadBlobButton(
		"",
		"#"+uploadList.ID(),
		fmt.Sprintf(
			`function(id, fileName, responseJSON) {
				jQuery("#%s").attr("value", responseJSON.blobID);
			}`,
			hiddenInput.ID(),
		),
	)

	editor := view.DIV(Config.BlobRefController.Class,
		view.RequireScriptURL("/media/media.js", 0),
		view.RequireStyle(
			`.qq-upload-button {
				margin: 0 0 5px 10px;
				cursor: pointer;
			}
			.qq-upload-button:hover {
				background-color: #cc0000;
			}`,
			10,
		),
		hiddenInput,
		view.DIV(Config.BlobRefController.ActionsClass, chooseList, removeButton, uploadButton),
		uploadList,
		view.DivClearBoth(),
	)

	if withLabel {
		return view.AddStandardLabel(form, editor, metaData), nil
	}
	return editor, nil
}
Esempio n. 11
0
func (self ModelBlobController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	input = &FileInput{
		Class:    form.FieldInputClass(metaData),
		Name:     metaData.Selector(),
		Disabled: form.IsFieldDisabled(metaData),
	}
	if withLabel {
		return AddStandardLabel(form, input, metaData), nil
	}
	return input, nil
}
Esempio n. 12
0
func (self ModelMultipleChoiceController) SetValue(value string, ctx *Context, metaData *model.MetaData, form *Form) error {
	m := metaData.Value.Addr().Interface().(*model.MultipleChoice)
	options := m.Options(metaData)
	*m = nil
	for i, option := range options {
		name := fmt.Sprintf("%s_%d", metaData.Selector(), i)
		if ctx.Request.FormValue(name) != "" {
			*m = append(*m, option)
		}
	}
	return nil
}
Esempio n. 13
0
func (self *setPostValuesStructVisitor) trySetFieldValue(field *model.MetaData) error {
	if self.form.IsFieldDisabled(field) || self.form.IsFieldExcluded(field, self.ctx) {
		return nil
	}

	switch s := field.Value.Addr().Interface().(type) {
	case model.Reference:
		// we don't handle references with forms yet

	case *model.Bool:
		s.Set(self.ctx.Request.FormValue(field.Selector()) != "")

	case *model.MultipleChoice:
		options := s.Options(field)
		*s = nil
		for i, option := range options {
			name := fmt.Sprintf("%s_%d", field.Selector(), i)
			if self.ctx.Request.FormValue(name) != "" {
				*s = append(*s, option)
			}
		}

	case *model.Blob:
		file, _, err := self.ctx.Request.FormFile(field.Selector())
		if err != nil {
			return err
		}
		defer file.Close()
		bytes, err := ioutil.ReadAll(file)
		if err != nil {
			return err
		}
		s.Set(bytes)

	case *model.File:
		file, header, err := self.ctx.Request.FormFile(field.Selector())
		if err != nil {
			return err
		}
		defer file.Close()
		bytes, err := ioutil.ReadAll(file)
		if err != nil {
			return err
		}
		s.Name = header.Filename
		s.Data = bytes

	case model.Value:
		// General case
		s.SetString(self.ctx.Request.FormValue(field.Selector()))
	}
	return nil
}
Esempio n. 14
0
func (self ModelIntController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	i := metaData.Value.Addr().Interface().(*model.Int)
	input = &TextField{
		Class:       form.FieldInputClass(metaData),
		Name:        metaData.Selector(),
		Text:        i.String(),
		Disabled:    form.IsFieldDisabled(metaData),
		Placeholder: form.InputFieldPlaceholder(metaData),
	}
	if withLabel {
		return AddStandardLabel(form, input, metaData), nil
	}
	return input, nil
}
Esempio n. 15
0
func (self *Form) GetFieldDescription(metaData *model.MetaData) string {
	selector := metaData.Selector()
	if desc, ok := self.FieldDescriptions[selector]; ok {
		return desc
	}
	wildcardSelector := metaData.WildcardSelector()
	if desc, ok := self.FieldDescriptions[wildcardSelector]; ok {
		return desc
	}
	if desc, ok := metaData.Attrib(StructTagKey, "description"); ok {
		return desc
	}
	return ""
}
Esempio n. 16
0
func (self ModelBlobController) SetValue(value string, ctx *Context, metaData *model.MetaData, form *Form) error {
	b := metaData.Value.Addr().Interface().(*model.Blob)
	file, _, err := ctx.Request.FormFile(metaData.Selector())
	if err != nil {
		return err
	}
	defer file.Close()
	bytes, err := ioutil.ReadAll(file)
	if err != nil {
		return err
	}
	b.Set(bytes)
	return nil
}
Esempio n. 17
0
func (self ModelFileController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	accept, _ := metaData.Attrib("view", "accept")
	form.Enctype = MultipartFormData
	input = &FileInput{
		Class:    form.FieldInputClass(metaData),
		Name:     metaData.Selector(),
		Accept:   accept,
		Disabled: form.IsFieldDisabled(metaData),
	}
	if withLabel {
		return AddStandardLabel(form, input, metaData), nil
	}
	return input, nil
}
Esempio n. 18
0
func (self *setPostValuesStructVisitor) BeginIndexedFields(indexedFields *model.MetaData) error {
	if indexedFields.Kind == model.SliceKind && indexedFields.Value.CanSet() && !self.form.IsFieldExcluded(indexedFields, self.ctx) {
		if lengthStr := self.ctx.Request.FormValue(indexedFields.Selector() + ".length"); lengthStr != "" {
			length, err := strconv.Atoi(lengthStr)
			if err != nil {
				panic(err.Error())
			}
			if length != indexedFields.Value.Len() {
				indexedFields.Value.Set(utils.SetSliceLengh(indexedFields.Value, length))
				mongo.InitRefs(self.formModel)
			}
		}
	}
	return nil
}
Esempio n. 19
0
func (self *setPostValuesStructVisitor) BeginSlice(slice *model.MetaData) error {
	if slice.Value.CanSet() && !self.form.IsFieldExcluded(slice) {
		if lengthStr := self.context.Request.FormValue(slice.Selector() + ".length"); lengthStr != "" {
			length, err := strconv.Atoi(lengthStr)
			if err != nil {
				panic(err.Error())
			}
			if length != slice.Value.Len() {
				slice.Value.Set(utils.SetSliceLengh(slice.Value, length))
				mongo.InitRefs(self.formModel)
			}
		}
	}
	return nil
}
Esempio n. 20
0
func (self ModelFileController) SetValue(value string, ctx *Context, metaData *model.MetaData, form *Form) error {
	f := metaData.Value.Addr().Interface().(*model.File)
	file, header, err := ctx.Request.FormFile(metaData.Selector())
	if err != nil {
		return err
	}
	defer file.Close()
	bytes, err := ioutil.ReadAll(file)
	if err != nil {
		return err
	}
	f.Name = header.Filename
	f.Data = bytes
	return nil
}
Esempio n. 21
0
func (self ModelEmailController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	email := metaData.Value.Addr().Interface().(*model.Email)
	input = &TextField{
		Class:       form.FieldInputClass(metaData),
		Name:        metaData.Selector(),
		Type:        EmailTextField,
		Text:        email.Get(),
		Size:        form.GetInputSize(metaData),
		Disabled:    form.IsFieldDisabled(metaData),
		Placeholder: form.InputFieldPlaceholder(metaData),
	}
	if withLabel {
		return AddStandardLabel(form, input, metaData), nil
	}
	return input, nil
}
Esempio n. 22
0
func (self ModelBoolController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	b := metaData.Value.Addr().Interface().(*model.Bool)
	checkbox := &Checkbox{
		Class:    form.FieldInputClass(metaData),
		Name:     metaData.Selector(),
		Disabled: form.IsFieldDisabled(metaData),
		Checked:  b.Get(),
	}
	if withLabel {
		labelContent := form.FieldLabel(metaData)
		if form.IsFieldRequired(metaData) {
			labelContent = "<span class='required'>*</span> " + labelContent
		}
		checkbox.Label = labelContent
	}
	return checkbox, nil
}
Esempio n. 23
0
func (self ModelMultipleChoiceController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	m := metaData.Value.Addr().Interface().(*model.MultipleChoice)
	options := m.Options(metaData)
	checkboxes := make(Views, len(options))
	for i, option := range options {
		checkboxes[i] = &Checkbox{
			Label:    option,
			Class:    form.FieldInputClass(metaData),
			Name:     fmt.Sprintf("%s_%d", metaData.Selector(), i),
			Disabled: form.IsFieldDisabled(metaData),
			Checked:  m.IsSet(option),
		}
	}
	if withLabel {
		return AddStandardLabel(form, checkboxes, metaData), nil
	}
	return checkboxes, nil
}
Esempio n. 24
0
func (self ModelChoiceController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	choice := metaData.Value.Addr().Interface().(*model.Choice)
	options := choice.Options(metaData)
	if len(options) == 0 || options[0] != "" {
		options = append([]string{""}, options...)
	}
	input = &Select{
		Class:    form.FieldInputClass(metaData),
		Name:     metaData.Selector(),
		Model:    &StringsSelectModel{options, choice.Get()},
		Disabled: form.IsFieldDisabled(metaData),
		Size:     1,
	}
	if withLabel {
		return AddStandardLabel(form, input, metaData), nil
	}
	return input, nil
}
Esempio n. 25
0
// FieldLabel returns a label for a form field generated from metaData.
// It creates the label from the names or label tags of metaData and
// all its parents, starting with the root parent, concanated with a space
// character.
func (self *Form) FieldLabel(metaData *model.MetaData) string {
	selector := metaData.Selector()
	if label, ok := self.Labels[selector]; ok {
		return label
	}
	wildcardSelector := metaData.WildcardSelector()
	if label, ok := self.Labels[wildcardSelector]; ok {
		return label
	}
	var buf bytes.Buffer
	for _, m := range metaData.Path()[1:] {
		if buf.Len() > 0 {
			buf.WriteByte(' ')
		}
		buf.WriteString(self.DirectFieldLabel(m))
	}
	return buf.String()
}
Esempio n. 26
0
func (self ModelDateTimeController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	dateTime := metaData.Value.Addr().Interface().(*model.DateTime)
	input = Views{
		HTML("(Format: " + model.DateTimeFormat + ")<br/>"),
		&TextField{
			Class:       form.FieldInputClass(metaData),
			Name:        metaData.Selector(),
			Text:        dateTime.Get(),
			Size:        len(model.DateTimeFormat),
			Disabled:    form.IsFieldDisabled(metaData),
			Placeholder: form.InputFieldPlaceholder(metaData),
		},
	}
	if withLabel {
		return AddStandardLabel(form, input, metaData), nil
	}
	return input, nil
}
Esempio n. 27
0
func (self *Form) GetInputSize(metaData *model.MetaData) int {
	selector := metaData.Selector()
	if size, ok := self.InputSizes[selector]; ok {
		return size
	}
	wildcardSelector := metaData.WildcardSelector()
	if size, ok := self.InputSizes[wildcardSelector]; ok {
		return size
	}
	if sizeStr, ok := metaData.Attrib("size"); ok {
		size, _ := strconv.Atoi(sizeStr)
		return size
	}
	layout := self.GetLayout()
	if layout != nil {
		return layout.GetDefaultInputSize(metaData)
	}
	return 0
}
Esempio n. 28
0
func (self ModelIntController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	i := metaData.Value.Addr().Interface().(*model.Int)
	textField := &TextField{
		Class:       form.FieldInputClass(metaData),
		Name:        metaData.Selector(),
		Text:        i.String(),
		Disabled:    form.IsFieldDisabled(metaData),
		Placeholder: form.InputFieldPlaceholder(metaData),
		Required:    form.IsFieldRequired(metaData),
		Title:       form.FieldLabel(metaData),
	}
	if metaData.BoolAttrib("view", "autofocus") {
		textField.Autofocus = true
	}
	if withLabel {
		return AddStandardLabel(form, textField, metaData), nil
	}
	return textField, nil
}
Esempio n. 29
0
func (self ModelPhoneController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	phone := metaData.Value.Addr().Interface().(*model.Phone)
	textField := &TextField{
		Class:       form.FieldInputClass(metaData),
		Name:        metaData.Selector(),
		Text:        phone.Get(),
		Size:        form.GetInputSize(metaData),
		Disabled:    form.IsFieldDisabled(metaData),
		Placeholder: form.InputFieldPlaceholder(metaData),
		Required:    form.IsFieldRequired(metaData) && !metaData.IsSelfOrParentIndexed(),
		Title:       form.FieldLabel(metaData),
	}
	if metaData.BoolAttrib("view", "autofocus") {
		textField.Autofocus = true
	}
	if withLabel {
		return AddStandardLabel(form, textField, metaData), nil
	}
	return textField, nil
}
Esempio n. 30
0
func (self ModelUrlController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (View, error) {
	url := metaData.Value.Addr().Interface().(*model.Url)
	input := &TextField{
		Class:       form.FieldInputClass(metaData),
		Name:        metaData.Selector(),
		Text:        url.Get(),
		Size:        form.GetInputSize(metaData),
		Disabled:    form.IsFieldDisabled(metaData),
		Placeholder: form.InputFieldPlaceholder(metaData),
		Required:    form.IsFieldRequired(metaData),
		Title:       form.FieldLabel(metaData),
	}
	if metaData.BoolAttrib("view", "autofocus") {
		input.Autofocus = true
	}
	if withLabel {
		return AddStandardLabel(form, input, metaData), nil
	}
	return input, nil
}