diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-12-18 12:13:08 -0800 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-12-18 12:13:08 -0800 |
commit | 7d87d40f8d0550331d1b19dea2c374ecfbc77d48 (patch) | |
tree | b0b0e2b346119ec525530f83b4995fe966f2b1dd /addons | |
parent | 5b9f00b427f74d447a3a685a8b7779c86b5a1b9d (diff) |
adding beginning of addon api (cooresponding to previous commit w/ reference addon)
Diffstat (limited to 'addons')
-rw-r--r-- | addons/reference/reference.go | 53 |
1 files changed, 19 insertions, 34 deletions
diff --git a/addons/reference/reference.go b/addons/reference/reference.go index b6cede8..78e46eb 100644 --- a/addons/reference/reference.go +++ b/addons/reference/reference.go @@ -1,44 +1,49 @@ +// Package reference is a Ponzu addon to enable content editors to create +// references to other content types which are stored as query strings within +// the referencer's content DB package reference import ( "bytes" "encoding/json" "fmt" + "html/template" "log" - "net/http" - "text/template" "github.com/bosssauce/ponzu/management/editor" + "github.com/bosssauce/ponzu/system/addon" ) -// Referenceable enures there is a way to reference the implenting type from -// within another type's editor and from type-scoped API calls -type Referenceable interface { - Referenced() []byte -} - // Select returns the []byte of a <select> HTML element plus internal <options> with a label. // IMPORTANT: // The `fieldName` argument will cause a panic if it is not exactly the string // form of the struct field that this editor input is representing -func Select(fieldName string, p interface{}, attrs map[string]string, contentType string, tmpl *template.Template) []byte { +func Select(fieldName string, p interface{}, attrs map[string]string, contentType, tmplString string) []byte { // decode all content type from db into options map // map["?type=<contentType>&id=<id>"]t.String() options := make(map[string]string) - var data []map[string]interface{} - j := ContentAll(contentType) + var all map[string]interface{} + j := addon.ContentAll(contentType) - err := json.Unmarshal(j, data) + err := json.Unmarshal(j, &all) if err != nil { return nil } + // make template for option html display + tmpl := template.Must(template.New(contentType).Parse(tmplString)) + + // make data something usable to iterate over and assign options + data := all["data"].([]interface{}) + for i := range data { - k := fmt.Sprintf("?type=%s&id=%s", contentType, data[i]["id"].(string)) + item := data[i].(map[string]interface{}) + k := fmt.Sprintf("?type=%s&id=%.0f", contentType, item["id"].(float64)) v := &bytes.Buffer{} - err := tmpl.Execute(v, data[i]) + err := tmpl.Execute(v, item) if err != nil { + log.Println("Error executing template for reference of:", contentType) return nil } @@ -47,23 +52,3 @@ func Select(fieldName string, p interface{}, attrs map[string]string, contentTyp return editor.Select(fieldName, p, attrs, options) } - -// ContentAll retrives all items from the HTTP API within the provided namespace -func ContentAll(namespace string) []byte { - endpoint := "http://0.0.0.0:8080/api/contents?type=" - buf := []byte{} - r := bytes.NewReader(buf) - req, err := http.NewRequest(http.MethodGet, endpoint+namespace, r) - if err != nil { - log.Println("Error creating request for reference from:", namespace) - return nil - } - - c := http.Client{} - res, err := c.Do(req) - defer res.Body.Close() - - fmt.Println(res, string(buf)) - - return buf -} |