summaryrefslogtreecommitdiff
path: root/addons
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2016-12-17 00:41:08 -0800
committerSteve Manuel <nilslice@gmail.com>2016-12-17 00:41:08 -0800
commit09312721db0713479c6c99a7a495be6fd8212871 (patch)
treed24285f159c6097021d1485d844753808f15fe3e /addons
parentb20c5bdee38682edc851e646d815a34689c3c923 (diff)
adding addons dir and sample addon which enables the use of a new input element in forms for referencing other content. "addons" is a conceptual plugin-like feature, similar to wordpress "plugins" dir, but not as sophisticated
Diffstat (limited to 'addons')
-rw-r--r--addons/reference/reference.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/addons/reference/reference.go b/addons/reference/reference.go
new file mode 100644
index 0000000..2681146
--- /dev/null
+++ b/addons/reference/reference.go
@@ -0,0 +1,90 @@
+package reference
+
+import (
+ "encoding/json"
+ "fmt"
+ "log"
+
+ "github.com/bosssauce/ponzu/content"
+ "github.com/bosssauce/ponzu/management/editor"
+ "github.com/bosssauce/ponzu/system/db"
+)
+
+// New 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 New(fieldName string, p interface{}, attrs map[string]string, contentType, fmtString 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()
+
+ fmt.Println(t)
+
+ // // 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")
+
+ 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 an Identifiable
+ item, ok := t.(content.Identifiable)
+ if !ok {
+ log.Println("Cannot use type", contentType, "as reference as it does not implement content.Identifiable")
+ return nil
+ }
+
+ k := fmt.Sprintf("?type=%s&id=%d", contentType, item.ItemID())
+ v := item.String()
+ options[k] = v
+ }
+
+ options[""] = contentType + "Content loading..."
+
+ return editor.Select(fieldName, p, attrs, options)
+}
+
+/*
+<script>
+ // fmtString = "{name} - ( Age: {age} | Power: {power} )"
+ // $(function() {
+ // var API = '/api/contents?type=` + contentType + `';
+ // var select = $('select[name="` + name + `"]);
+
+ // $.getJSON(API, function(resp, status) {
+ // if (status !== '200' || status !== '304') {
+ // console.log('Error loading Reference for', '` + contentType + `')
+ // return
+ // }
+
+ // var data = resp.data,
+ // options = [],
+ // re = /{(.*?)}/g,
+ // tmpl = '` + fmtString + `'
+ // tags = tmpl.match(re),
+ // keys = [];
+
+ // // get keys from tags ({x} -> x)
+ // for (var i = 0; i < tags.length; i++) {
+ // var key = tags[i].slice(1, tags[i].length-1);
+ // keys.push(key);
+ // }
+
+ // // create options as objects of "?type=<contentType>&id=<id>":displayName
+ // for (var i = 0; i < data.length; i++) {
+
+ // }
+ // });
+ // });
+ </script>
+*/