コード例 #1
0
ファイル: editable.go プロジェクト: juztin/dingo
// Execute invokes the editor for the wrapped view.
func (e *EditableView) Execute(ctx dingo.Context, data interface{}) error {
	ctx.ParseForm()
	if _, ok := ctx.Form["edit"]; !ok || !CanEdit(ctx) {
		return e.View.Execute(ctx, data)
	}

	if ctx.Method == "GET" {
		return e.tmpl.Execute(ctx.Response, editViewData(ctx, e.View))
	} else if ctx.Method != "POST" {
		http.Error(ctx.Response, "Invalid Method", http.StatusMethodNotAllowed)
		return nil
	}

	c := []byte(ctx.FormValue("content"))
	d := new(EditTemplateData)
	d.DingoVer = dingo.VERSION
	d.IsAction = true
	d.DoneURL = ctx.URL.Path
	d.Stylesheets = codeMirrorCSS()
	d.Scripts = codeMirrorJS()
	if err := e.View.Save(ctx, c); err != nil {
		d.Error = err
		d.Content = c
	} else {
		d.WasSaved = true
		d.Content = []byte(e.View.Data(ctx))
	}

	return e.tmpl.Execute(ctx.Response, d)
	// TODO check if this has been updated
	// https://groups.google.com/forum/?fromgroups#!topic/golang-nuts/7Ks1iq2s7FA
	// len(edit) == 0 vs edit == ""
}
コード例 #2
0
ファイル: editable.go プロジェクト: juztin/dingo
// EditHandler is a dingo.Handler that edits/saves a given template.
func EditHandler(ctx dingo.Context) {
	ctx.ParseForm()
	if !CanEdit(ctx) {
		ctx.HttpError(401)
		return
	}

	var v View
	d := editCtxData(ctx)
	if n, ok := ctx.Form["name"]; !ok {
		editTempl.Execute(ctx.Response, d)
		return
	} else if v, ok = editableViews[n[0]]; !ok {
		d.Error = errors.New(fmt.Sprintf("Template name: `%s` does not exist.", n[0]))
		editTempl.Execute(ctx.Response, d)
		return
	}

	if ctx.Method == "POST" {
		d.IsAction = true
		c := []byte(ctx.FormValue("content"))
		if err := v.Save(ctx, c); err != nil {
			d.Error = err
			d.Content = c
		} else {
			d.WasSaved = true
			d.Content = []byte(v.Data(ctx))
		}
	} else {
		d.Content = []byte(v.Data(ctx))
	}

	editTempl.Execute(ctx.Response, d)
}