diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-12-17 06:20:09 -0800 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-12-17 06:20:09 -0800 |
commit | 8b9d19d5a7212d3e0d493ad6a0570434f9049b43 (patch) | |
tree | 18b4c77d198df8b84288f13bde612dff53d6a00b | |
parent | 432652e9d1dc668526bdd57ddf0c6060659c65d1 (diff) |
moving reference to addons
-rw-r--r-- | addons/reference/reference.go | 58 | ||||
-rw-r--r-- | system/api/handlers.go | 2 |
2 files changed, 59 insertions, 1 deletions
diff --git a/addons/reference/reference.go b/addons/reference/reference.go new file mode 100644 index 0000000..6a46e75 --- /dev/null +++ b/addons/reference/reference.go @@ -0,0 +1,58 @@ +package api + +import ( + "encoding/json" + "fmt" + "log" + + "github.com/bosssauce/ponzu/content" + "github.com/bosssauce/ponzu/management/editor" + "github.com/bosssauce/ponzu/system/api" +) + +// 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) []byte { + ct, ok := content.Types[contentType] + if !ok { + log.Println("Cannot reference an invalid content type:", contentType) + return nil + } + + // get a handle to the underlying interface type for decoding + t := ct() + + // decode all content type from db into options map + // map["?type=<contentType>&id=<id>"]t.String() + options := make(map[string]string) + // jj := db.ContentAll(contentType + "__sorted") // make this an API call + jj := api.ContentAll(contentType) + + for i := range jj { + err := json.Unmarshal(jj[i], t) + if err != nil { + log.Println("Error decoding into reference handle:", contentType, err) + } + + // make sure it is a content.Identifiable + item, ok := t.(content.Identifiable) + if !ok { + log.Println("Cannot use type", contentType, "as a reference since it does not implement content.Identifiable") + return nil + } + + k := fmt.Sprintf("?type=%s&id=%d", contentType, item.ItemID()) + v := item.String() + options[k] = v + } + + return editor.Select(fieldName, p, attrs, options) +} diff --git a/system/api/handlers.go b/system/api/handlers.go index 7a2073d..fae3ef6 100644 --- a/system/api/handlers.go +++ b/system/api/handlers.go @@ -224,7 +224,7 @@ func SendJSON(res http.ResponseWriter, j map[string]interface{}) { sendData(res, data, 200) } -// CORS wraps a HandleFunc to response to OPTIONS requests properly +// CORS wraps a HandleFunc to respond to OPTIONS requests properly func CORS(next http.HandlerFunc) http.HandlerFunc { return db.CacheControl(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { if req.Method == http.MethodOptions { |