Exemple #1
0
func (self *TextArea) Render(context *Context, writer *utils.XMLWriter) (err error) {
	writer.OpenTag("textarea").Attrib("id", self.id).AttribIfNotDefault("class", self.Class)

	cols := self.Cols
	if cols == 0 {
		cols = TextAreaDefaultCols
	}
	rows := self.Rows
	if rows == 0 {
		rows = TextAreaDefaultRows
	}

	writer.Attrib("name", self.Name)
	writer.Attrib("rows", rows)
	writer.Attrib("cols", cols)
	writer.AttribIfNotDefault("tabindex", self.TabIndex)
	if self.Readonly {
		writer.Attrib("readonly", "readonly")
	}
	if self.Disabled {
		writer.Attrib("disabled", "disabled")
	}

	writer.EscapeContent(self.Text)
	writer.CloseTag()
	return nil
}
Exemple #2
0
func (self *TextPreview) Render(context *Context, writer *utils.XMLWriter) (err error) {
	if len(self.PlainText) < self.MaxLength {
		writer.Content(self.PlainText)
	} else {
		shortLength := self.ShortLength
		if shortLength == 0 {
			shortLength = self.MaxLength
		}

		// If in the middle of a word, go back to space before it
		for shortLength > 0 && !unicode.IsSpace(rune(self.PlainText[shortLength-1])) {
			shortLength--
		}

		// If in the middle of space, go back to word before it
		for shortLength > 0 && unicode.IsSpace(rune(self.PlainText[shortLength-1])) {
			shortLength--
		}

		writer.Content(self.PlainText[:shortLength])
		writer.Content("... ")
		if self.MoreLink != nil {
			writer.OpenTag("a")
			writer.Attrib("href", self.MoreLink.URL(context.PathArgs...))
			writer.AttribIfNotDefault("title", self.MoreLink.LinkTitle(context))
			content := self.MoreLink.LinkContent(context)
			if content != nil {
				err = content.Render(context, writer)
			}
			writer.ForceCloseTag() // a
		}
	}
	return err
}
Exemple #3
0
func (self *Label) Render(context *Context, writer *utils.XMLWriter) (err error) {
	var forID string
	if self.For != nil {
		forID = self.For.ID()
	}
	writer.OpenTag("label").Attrib("id", self.id).AttribIfNotDefault("class", self.Class)
	writer.AttribIfNotDefault("for", forID)
	if self.Content != nil {
		err = self.Content.Render(context, writer)
	}
	writer.CloseTag()
	return err
}
Exemple #4
0
func (self *DummyImage) Render(context *Context, writer *utils.XMLWriter) (err error) {
	src := fmt.Sprintf("http://dummyimage.com/%dx%d", self.Width, self.Height)

	if self.BackgroundColor != "" || self.ForegroundColor != "" {
		if self.BackgroundColor != "" {
			src += "/" + self.BackgroundColor
		} else {
			src += "/ccc"
		}

		if self.ForegroundColor != "" {
			src += "/" + self.ForegroundColor
		}
	}

	src += ".png"

	if self.Text != "" {
		src += "&text=" + url.QueryEscape(self.Text)
	}

	writer.OpenTag("img").Attrib("id", self.id).AttribIfNotDefault("class", self.Class)
	writer.Attrib("src", src)
	writer.AttribIfNotDefault("width", self.Width)
	writer.AttribIfNotDefault("height", self.Height)
	writer.AttribIfNotDefault("alt", self.Text)
	writer.CloseTag()
	return nil
}
Exemple #5
0
func (self *TextField) Render(context *Context, writer *utils.XMLWriter) (err error) {
	writer.OpenTag("input").Attrib("id", self.id).AttribIfNotDefault("class", self.Class)

	writer.Attrib("name", self.Name)
	writer.AttribIfNotDefault("tabindex", self.TabIndex)
	if self.Readonly {
		writer.Attrib("readonly", "readonly")
	}
	if self.Disabled {
		writer.Attrib("disabled", "disabled")
	}

	switch self.Type {
	case PasswordTextField:
		writer.Attrib("type", "password")
	case EmailTextField:
		writer.Attrib("type", "email")
	default:
		writer.Attrib("type", "text")
	}

	writer.AttribIfNotDefault("size", self.Size)
	writer.AttribIfNotDefault("maxlength", self.MaxLength)
	writer.Attrib("value", self.Text)

	writer.CloseTag()
	return nil
}
Exemple #6
0
func (self *Select) Render(context *Context, writer *utils.XMLWriter) (err error) {
	writer.OpenTag("select").Attrib("id", self.id).AttribIfNotDefault("class", self.Class)
	writer.Attrib("name", self.Name)
	if self.Disabled {
		writer.Attrib("disabled", "disabled")
	}

	size := self.Size

	if self.Model != nil {
		numOptions := self.Model.NumOptions()
		if size == 0 {
			size = numOptions
		}
		writer.Attrib("size", size)

		for i := 0; i < numOptions; i++ {
			writer.OpenTag("option")
			writer.AttribIfNotDefault("value", self.Model.Value(i))
			if self.Model.Selected(i) {
				writer.Attrib("selected", "selected")
			}
			if self.Model.Disabled(i) {
				writer.Attrib("disabled", "disabled")
			}
			err = self.Model.RenderItem(i, context, writer)
			if err != nil {
				return err
			}
			writer.CloseTag() // option
		}
	} else {
		writer.Attrib("size", size)
	}

	writer.CloseTag() // select
	return nil
}
Exemple #7
0
func (self *Image) Render(context *Context, writer *utils.XMLWriter) (err error) {
	writer.OpenTag("img").Attrib("id", self.id).AttribIfNotDefault("class", self.Class)
	writer.Attrib("src", self.URL)
	writer.AttribIfNotDefault("width", self.Width)
	writer.AttribIfNotDefault("height", self.Height)
	writer.AttribIfNotDefault("alt", self.Description)
	writer.CloseTag()
	return nil
}
Exemple #8
0
func (self *Link) Render(context *Context, writer *utils.XMLWriter) (err error) {
	writer.OpenTag("a").AttribIfNotDefault("id", self.id)
	writer.AttribIfNotDefault("class", self.Class)
	if self.NewWindow {
		writer.Attrib("target", "_blank")
	}
	if self.Model != nil {
		writer.Attrib("href", self.Model.URL(context.PathArgs...))
		writer.AttribIfNotDefault("title", self.Model.LinkTitle(context))
		writer.AttribIfNotDefault("rel", self.Model.LinkRel(context))
		content := self.Model.LinkContent(context)
		if content != nil {
			err = content.Render(context, writer)
		}
	}
	writer.ForceCloseTag() // a
	return err
}
Exemple #9
0
func (self *Button) Render(context *Context, writer *utils.XMLWriter) (err error) {
	if self.Submit {
		writer.OpenTag("input").Attrib("id", self.id).AttribIfNotDefault("class", self.Class)
		writer.Attrib("type", "submit")
		writer.AttribIfNotDefault("name", self.Name)
		writer.AttribIfNotDefault("value", self.Value)
		if self.Disabled {
			writer.Attrib("disabled", "disabled")
		}
		writer.AttribIfNotDefault("tabindex", self.TabIndex)
		if self.OnClickConfirm != "" {
			writer.Attrib("onclick", "return confirm('", self.OnClickConfirm, "');")
		} else {
			writer.AttribIfNotDefault("onclick", self.OnClick)
		}
		writer.CloseTag()
	} else {
		writer.OpenTag("button").Attrib("id", self.id).AttribIfNotDefault("class", self.Class)
		writer.Attrib("type", "button")
		writer.AttribIfNotDefault("name", self.Name)
		writer.AttribIfNotDefault("value", self.Value)
		if self.Disabled {
			writer.Attrib("disabled", "disabled")
		}
		writer.AttribIfNotDefault("tabindex", self.TabIndex)
		if self.OnClickConfirm != "" {
			writer.Attrib("onclick", "return confirm('", self.OnClickConfirm, "');")
		} else {
			writer.AttribIfNotDefault("onclick", self.OnClick)
		}
		if self.Content != nil {
			err = self.Content.Render(context, writer)
		}
		writer.ForceCloseTag()
	}
	return nil
}
Exemple #10
0
func (self *Form) Render(context *Context, writer *utils.XMLWriter) (err error) {
	if self.OnSubmit == nil {
		panic("view.Form.OnSubmit must not be nil")
	}
	layout := self.GetLayout()
	if layout == nil {
		panic("view.Form.GetLayout() returned nil")
	}
	fieldFactory := self.GetFieldFactory()
	if fieldFactory == nil {
		panic("view.Form.GetFieldFactory() returned nil")
	}

	var formModel interface{}
	var hasErrors bool
	content := Views{&HiddenInput{Name: FormIDName, Value: self.FormID}}
	isPost := self.IsPost(context)

	if self.GetModel == nil {
		submitButton := self.GetFieldFactory().NewSubmitButton(self.GetSubmitButtonText(), self.SubmitButtonConfirm, self)
		content = append(content, submitButton)
	} else {
		formModel, err = self.GetModel(self, context)
		if err != nil {
			return err
		}
		if isPost {
			setPostValues := &setPostValuesStructVisitor{
				form:      self,
				formModel: formModel,
				context:   context,
			}
			err = model.Visit(formModel, setPostValues)
			if err != nil {
				return err
			}
		}
		validateAndFormLayout := &validateAndFormLayoutStructVisitor{
			form:        self,
			formLayout:  layout,
			formModel:   formModel,
			formContent: &content,
			context:     context,
			isPost:      isPost,
		}
		err = model.Visit(formModel, validateAndFormLayout)
		if err != nil {
			return err
		}
		hasErrors = len(validateAndFormLayout.fieldValidationErrors) > 0 || len(validateAndFormLayout.generalValidationErrors) > 0
	}

	if isPost && !hasErrors {
		message, redirect, err := self.OnSubmit(self, formModel, context)
		if err == nil {
			if redirect == nil {
				redirect = self.Redirect
			}
			if redirect != nil {
				return Redirect(redirect.URL(context.PathArgs...))
			}
			if message == "" {
				message = self.SuccessMessage
			}
			if message != "" {
				self.GetLayout().SubmitSuccess(message, self, context, &content)
			}
		} else {
			if message == "" {
				message = err.Error()
			}
			self.GetLayout().SubmitError(message, self, context, &content)
		}
	}

	// Render HTML form element
	method := strings.ToUpper(self.Method)
	if method == "" {
		method = "POST"
	}
	action := self.Action
	if action == "" {
		action = "."
		if i := strings.Index(context.Request.RequestURI, "?"); i != -1 {
			action += context.Request.RequestURI[i:]
		}
	}
	// Hack: Value of hidden input FormIDName is not available when
	// enctype is multipart/form-data (bug?), so pass the form id as
	// URL parameter
	if self.Enctype == MultipartFormData && context.Request.Method != "POST" {
		action = utils.AddUrlParam(action, FormIDName, self.FormID)
	}

	writer.OpenTag("form").Attrib("id", self.id).AttribIfNotDefault("class", self.Class)
	writer.Attrib("method", method)
	writer.Attrib("action", action)
	writer.AttribIfNotDefault("enctype", self.Enctype)
	if len(content) > 0 {
		content.Init(content)
		err = content.Render(context, writer)
		if err != nil {
			return err
		}
	}
	writer.ForceCloseTag() // form
	return nil
}
Exemple #11
0
func (self *Menu) Render(context *Context, writer *utils.XMLWriter) (err error) {
	writer.OpenTag("ul").Attrib("id", self.id).AttribIfNotDefault("class", self.Class)

	requestURL := context.RequestURL()

	// Find active item
	activeIndex := -1

	if self.ActiveItemClass != "" {
		// First try exact URL match
		for i := range self.Items {
			url := self.Items[i].URL(context.PathArgs...)
			if url == requestURL {
				activeIndex = i
				break
			}
		}

		// If no exact URL match is found, search for sub pages
		if activeIndex == -1 {
			for i := range self.Items {
				url := self.Items[i].URL(context.PathArgs...)
				if strings.HasPrefix(requestURL, url) {
					activeIndex = i
					// todo
					// not perfect, what if homepage matches first, but other matches better?
				}
			}
		}
	}

	for i := range self.Items {
		index := i
		if self.Reverse {
			index = len(self.Items) - 1 - i
		}
		itemClass := self.ItemClass
		linkModel := self.Items[index]
		url := linkModel.URL(context.PathArgs...)

		// use i instead of index
		if i == activeIndex {
			itemClass += " " + self.ActiveItemClass
		}

		writer.OpenTag("li").Attrib("id", self.id, "_", index).AttribIfNotDefault("class", itemClass)

		if i > 0 && self.BetweenItems != "" {
			writer.Content(self.BetweenItems)
		}

		writer.OpenTag("a")
		writer.Attrib("href", url)
		writer.AttribIfNotDefault("title", linkModel.LinkTitle(context))
		writer.AttribIfNotDefault("rel", linkModel.LinkRel(context))
		content := linkModel.LinkContent(context)
		if content != nil {
			err = content.Render(context, writer)
			if err != nil {
				return err
			}
		}
		writer.ForceCloseTag() // a

		writer.ForceCloseTag() // li
	}

	writer.ForceCloseTag() // ul
	return nil
}