From 09b06ed0e0188b8559620e4e0c4f7e54599044af Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 11:35:10 -0700 Subject: adding Tags input type and implementing a test on post --- content/post.go | 8 ++--- management/editor/elements.go | 68 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/content/post.go b/content/post.go index 7dc99c1..08b831d 100644 --- a/content/post.go +++ b/content/post.go @@ -68,12 +68,8 @@ func (p *Post) MarshalEditor() ([]byte, error) { }), }, editor.Field{ - View: editor.Checkbox("Category", p, map[string]string{ - "label": "Post Category", - }, map[string]string{ - "important": "Important", - "active": "Active", - "unplanned": "Unplanned", + View: editor.Tags("Category", p, map[string]string{ + "label": "Post Categories", }), }, editor.Field{ diff --git a/management/editor/elements.go b/management/editor/elements.go index 4d829ad..9f679ac 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -332,6 +332,74 @@ func Checkbox(fieldName string, p interface{}, attrs, options map[string]string) return domElementWithChildrenCheckbox(div, opts) } +// Tags returns the []byte of a tag input (in the style of Materialze 'Chips') 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 Tags(fieldName string, p interface{}, attrs map[string]string) []byte { + name := tagNameFromStructField(fieldName, p) + + // get the saved tags if this is already an existing post + values := valueFromStructField(fieldName, p) // returns refelct.Value + tags := values.Slice(0, values.Len()).Interface().([]string) // casts reflect.Value to []string + + html := ` +
+
+
+
+ ` + + var initial []string + i := 0 + for _, tag := range tags { + tagName := tagNameFromStructFieldMulti(fieldName, i, p) + html += `` + initial = append(initial, `{tag: `+tag+`}`) + i++ + } + + script := ` + + ` + + html += `
` + + return []byte(html + script) +} + // domElementSelfClose is a special DOM element which is parsed as a // self-closing tag and thus needs to be created differently func domElementSelfClose(e *element) []byte { -- cgit v1.2.3 From 48de0f15a493d104501b055a0b7aa88fff101b19 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 11:46:04 -0700 Subject: updating content type generator template and modifying default post --- cmd/ponzu/options.go | 23 +++++++++-------------- content/post.go | 16 ++++++++-------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/cmd/ponzu/options.go b/cmd/ponzu/options.go index cabe91a..6f42f0c 100644 --- a/cmd/ponzu/options.go +++ b/cmd/ponzu/options.go @@ -72,12 +72,12 @@ type {{ .name }} struct { editor editor.Editor // required: all maintained {{ .name }} fields must have json tags! - Title string ` + "`json:" + `"title"` + "`" + ` - Content string ` + "`json:" + `"content"` + "`" + ` - Author string ` + "`json:" + `"author"` + "`" + ` - Picture string ` + "`json:" + `"picture"` + "`" + ` - Category []string ` + "`json:" + `"category"` + "`" + ` - ThemeStyle string ` + "`json:" + `"theme"` + "`" + ` + Title string ` + "`json:" + `"title"` + "`" + ` + Content string ` + "`json:" + `"content"` + "`" + ` + Author string ` + "`json:" + `"author"` + "`" + ` + Photo string ` + "`json:" + `"picture"` + "`" + ` + Category []string ` + "`json:" + `"category"` + "`" + ` + Theme string ` + "`json:" + `"theme"` + "`" + ` } func init() { @@ -101,7 +101,6 @@ func ({{ .initial }} *{{ .name }}) Editor() *editor.Editor { return &{{ .initial // MarshalEditor writes a buffer of html to edit a {{ .name }} and partially implements editor.Editable func ({{ .initial }} *{{ .name }}) MarshalEditor() ([]byte, error) { -/* EXAMPLE CODE (from post.go, the default content type) */ view, err := editor.Form({{ .initial }}, editor.Field{ // Take careful note that the first argument to these Input-like methods @@ -127,22 +126,18 @@ func ({{ .initial }} *{{ .name }}) MarshalEditor() ([]byte, error) { }), }, editor.Field{ - View: editor.File("Picture", {{ .initial }}, map[string]string{ + View: editor.File("Photo", {{ .initial }}, map[string]string{ "label": "Author Photo", "placeholder": "Upload a profile picture for the author", }), }, editor.Field{ - View: editor.Checkbox("Category", {{ .initial }}, map[string]string{ + View: editor.Tags("Category", {{ .initial }}, map[string]string{ "label": "{{ .name }} Category", - }, map[string]string{ - "important": "Important", - "active": "Active", - "unplanned": "Unplanned", }), }, editor.Field{ - View: editor.Select("ThemeStyle", {{ .initial }}, map[string]string{ + View: editor.Select("Theme", {{ .initial }}, map[string]string{ "label": "Theme Style", }, map[string]string{ "dark": "Dark", diff --git a/content/post.go b/content/post.go index 08b831d..d121ed7 100644 --- a/content/post.go +++ b/content/post.go @@ -11,12 +11,12 @@ type Post struct { Item editor editor.Editor - Title string `json:"title"` - Content string `json:"content"` - Photo string `json:"photo"` - Author string `json:"author"` - Category []string `json:"category"` - ThemeStyle string `json:"theme"` + Title string `json:"title"` + Content string `json:"content"` + Photo string `json:"photo"` + Author string `json:"author"` + Category []string `json:"category"` + Theme string `json:"theme"` } func init() { @@ -55,7 +55,7 @@ func (p *Post) MarshalEditor() ([]byte, error) { }), }, editor.Field{ - View: editor.File("Picture", p, map[string]string{ + View: editor.File("Photo", p, map[string]string{ "label": "Author Photo", "placeholder": "Upload a profile picture for the author", }), @@ -73,7 +73,7 @@ func (p *Post) MarshalEditor() ([]byte, error) { }), }, editor.Field{ - View: editor.Select("ThemeStyle", p, map[string]string{ + View: editor.Select("Theme", p, map[string]string{ "label": "Theme Style", }, map[string]string{ "dark": "Dark", -- cgit v1.2.3 From ae0d79e8677eaa9006588232dbdea95df9fec4cf Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 11:49:10 -0700 Subject: fixing js issues in Tags script --- management/editor/elements.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/management/editor/elements.go b/management/editor/elements.go index 9f679ac..837997f 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -363,11 +363,11 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { -- cgit v1.2.3 From ab3f9f82e9a1ab1127ff0ed6d64ed5096473b274 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 12:08:11 -0700 Subject: debugging --- management/editor/elements.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/management/editor/elements.go b/management/editor/elements.go index 97a7684..b967f8f 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -384,6 +384,8 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { chips.on('chip.delete', function(e, chip) { var sel = '.tag-'+chip.tag; + console.log(sel); + console.log(chips.parent().find(sel)); chips.parent().find(sel).remove(); }); }); -- cgit v1.2.3 From 048a4f390bd99386479d84792d24a78ffe443b4c Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 12:10:49 -0700 Subject: debugging --- management/editor/elements.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/management/editor/elements.go b/management/editor/elements.go index b967f8f..1c0e7ed 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -371,7 +371,7 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { var chips = tags.find('.chips'); chips.on('chip.add', function(e, chip) { - var input = $('input'); + var input = $(''); input.attr({ class: 'tag-'+chip.tag, name: '` + name + `.'+tags.find('input[type=hidden]').length-1, -- cgit v1.2.3 From af8f15263a87b1703c2e3618b5f5222dc486bc50 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 12:21:09 -0700 Subject: debugging --- management/editor/elements.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/management/editor/elements.go b/management/editor/elements.go index 1c0e7ed..e4dff73 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -344,8 +344,8 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { tags := values.Slice(0, values.Len()).Interface().([]string) // casts reflect.Value to []string html := ` -
- +
+
` @@ -364,14 +364,15 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { var tags = $('.tags.` + name + `'); $('.chips.` + name + `').material_chip({ data: [` + strings.Join(initial, ",") + `], - placeholder: 'Type and press "Enter" to add ` + name + `' + secondaryPlaceholder: '+'` + name + ` }); // handle events specific to tags var chips = tags.find('.chips'); chips.on('chip.add', function(e, chip) { - var input = $(''); + console.log("id:", tags.find('input[type=hidden]').length-1); + var input = $(''); input.attr({ class: 'tag-'+chip.tag, name: '` + name + `.'+tags.find('input[type=hidden]').length-1, -- cgit v1.2.3 From 271497357a308ba15e59ac4e2c114478a6579210 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 12:23:43 -0700 Subject: debugging --- management/editor/elements.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/management/editor/elements.go b/management/editor/elements.go index e4dff73..be0fc7b 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -364,7 +364,7 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { var tags = $('.tags.` + name + `'); $('.chips.` + name + `').material_chip({ data: [` + strings.Join(initial, ",") + `], - secondaryPlaceholder: '+'` + name + ` + secondaryPlaceholder: '+More' }); // handle events specific to tags @@ -372,7 +372,7 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { chips.on('chip.add', function(e, chip) { console.log("id:", tags.find('input[type=hidden]').length-1); - var input = $(''); + var input = $(''); input.attr({ class: 'tag-'+chip.tag, name: '` + name + `.'+tags.find('input[type=hidden]').length-1, -- cgit v1.2.3 From 37156dc9a235644a6a25193ec67687d9e5c97dcf Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 12:26:56 -0700 Subject: debugging --- management/editor/elements.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/management/editor/elements.go b/management/editor/elements.go index be0fc7b..d6cf412 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -375,7 +375,7 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { var input = $(''); input.attr({ class: 'tag-'+chip.tag, - name: '` + name + `.'+tags.find('input[type=hidden]').length-1, + name: '` + name + `.'+String(tags.find('input[type=hidden]').length), value: chip.tag, type: 'hidden' }); -- cgit v1.2.3 From 8ea5294c9d138a074a71815d36a102766d8e0cc8 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 12:29:41 -0700 Subject: debugging --- management/editor/elements.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/management/editor/elements.go b/management/editor/elements.go index d6cf412..a482f3a 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -354,7 +354,7 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { for _, tag := range tags { tagName := tagNameFromStructFieldMulti(fieldName, i, p) html += `` - initial = append(initial, `{tag: `+tag+`}`) + initial = append(initial, `{tag: '`+tag+`'}`) i++ } @@ -364,14 +364,13 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { var tags = $('.tags.` + name + `'); $('.chips.` + name + `').material_chip({ data: [` + strings.Join(initial, ",") + `], - secondaryPlaceholder: '+More' + secondaryPlaceholder: '+ '` + name + ` }); // handle events specific to tags var chips = tags.find('.chips'); chips.on('chip.add', function(e, chip) { - console.log("id:", tags.find('input[type=hidden]').length-1); var input = $(''); input.attr({ class: 'tag-'+chip.tag, -- cgit v1.2.3 From 75ec840d32095b64356058d7c773fa23583de62f Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 12:30:58 -0700 Subject: debugging --- management/editor/elements.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/management/editor/elements.go b/management/editor/elements.go index a482f3a..9aad5ea 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -364,7 +364,7 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { var tags = $('.tags.` + name + `'); $('.chips.` + name + `').material_chip({ data: [` + strings.Join(initial, ",") + `], - secondaryPlaceholder: '+ '` + name + ` + secondaryPlaceholder: '+` + name + `' }); // handle events specific to tags -- cgit v1.2.3 From c18d826516e35b123dfefcebca18ecf2a7c3cab6 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 12:35:40 -0700 Subject: debugging --- management/editor/elements.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/management/editor/elements.go b/management/editor/elements.go index 9aad5ea..ac73be1 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -387,6 +387,12 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { console.log(sel); console.log(chips.parent().find(sel)); chips.parent().find(sel).remove(); + + // iterate through all hidden tag inputs to re-name them with the correct ` + name + `.index + var hidden = chips.parent().find('input[type=hidden]'); + for (var i = 0; i < hidden.length; i++) { + hidden[i].attr('name', '` + name + `.'+String(i)); + } }); }); -- cgit v1.2.3 From 7c6c5d247d38b1bb1b3e24098c87080ad8059cba Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 12:37:09 -0700 Subject: debugging --- management/editor/elements.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/management/editor/elements.go b/management/editor/elements.go index ac73be1..f37d576 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -391,7 +391,7 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { // iterate through all hidden tag inputs to re-name them with the correct ` + name + `.index var hidden = chips.parent().find('input[type=hidden]'); for (var i = 0; i < hidden.length; i++) { - hidden[i].attr('name', '` + name + `.'+String(i)); + $(hidden[i]).attr('name', '` + name + `.'+String(i)); } }); }); -- cgit v1.2.3 From 4bb6c4317d64dc3271b81294bc3a574e816beaf9 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 12:41:40 -0700 Subject: adding support for tags via editor.Tags input func --- system/admin/static/dashboard/css/admin.css | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/system/admin/static/dashboard/css/admin.css b/system/admin/static/dashboard/css/admin.css index 7acffab..1de966d 100644 --- a/system/admin/static/dashboard/css/admin.css +++ b/system/admin/static/dashboard/css/admin.css @@ -203,3 +203,7 @@ li:hover .quick-delete-post, li:hover .delete-user { -o-transform: translateY(-140%); transform: translateY(-140%); } + +.chips { + margin-top: 10px; +} \ No newline at end of file -- cgit v1.2.3 From 9f546ab16261a62ebdf7a8852e6cf6783d96b924 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 12:51:42 -0700 Subject: adding a empty tag if there are no tags to store a empty value and overwrite existing tags in db --- management/editor/elements.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/management/editor/elements.go b/management/editor/elements.go index f37d576..6908e0e 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -371,6 +371,8 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { var chips = tags.find('.chips'); chips.on('chip.add', function(e, chip) { + tags.find('.empty-tag').remove(); + var input = $(''); input.attr({ class: 'tag-'+chip.tag, @@ -390,6 +392,21 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { // iterate through all hidden tag inputs to re-name them with the correct ` + name + `.index var hidden = chips.parent().find('input[type=hidden]'); + + // if there are no tags, set a blank + if (hidden.length === 0) { + var input = $(''); + input.attr({ + class: 'empty-tag', + name: '` + name + `', + value: "", + type: 'hidden' + }); + + tags.append(input); + return; + } + for (var i = 0; i < hidden.length; i++) { $(hidden[i]).attr('name', '` + name + `.'+String(i)); } -- cgit v1.2.3 From 8e955198f66b62a95dddba39458e97ade59ffa89 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 12:55:14 -0700 Subject: debugging --- management/editor/elements.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/management/editor/elements.go b/management/editor/elements.go index 6908e0e..53c15b2 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -371,7 +371,7 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { var chips = tags.find('.chips'); chips.on('chip.add', function(e, chip) { - tags.find('.empty-tag').remove(); + chips.parent().find('.empty-tag').remove(); var input = $(''); input.attr({ @@ -385,9 +385,7 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { }); chips.on('chip.delete', function(e, chip) { - var sel = '.tag-'+chip.tag; - console.log(sel); - console.log(chips.parent().find(sel)); + var sel = '.tag-'+chip.tag; chips.parent().find(sel).remove(); // iterate through all hidden tag inputs to re-name them with the correct ` + name + `.index -- cgit v1.2.3 From 4a6e7b74d7572071c919a335a264c3a45c825e06 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 12:58:36 -0700 Subject: debugging --- management/editor/elements.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/management/editor/elements.go b/management/editor/elements.go index 53c15b2..0a26dd0 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -385,7 +385,9 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { }); chips.on('chip.delete', function(e, chip) { - var sel = '.tag-'+chip.tag; + var sel = '.tag-'+chip.tag; + console.log(sel); + console.log(chips.parent().find(sel)); chips.parent().find(sel).remove(); // iterate through all hidden tag inputs to re-name them with the correct ` + name + `.index @@ -397,7 +399,6 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { input.attr({ class: 'empty-tag', name: '` + name + `', - value: "", type: 'hidden' }); -- cgit v1.2.3 From cb8728204756bd14a4a43d4962af85f4ed59bc09 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 13:03:55 -0700 Subject: add fix for multi-word classes --- management/editor/elements.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/management/editor/elements.go b/management/editor/elements.go index 0a26dd0..161ac88 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -375,7 +375,7 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { var input = $(''); input.attr({ - class: 'tag-'+chip.tag, + class: 'tag '+chip.tag, name: '` + name + `.'+String(tags.find('input[type=hidden]').length), value: chip.tag, type: 'hidden' @@ -385,7 +385,8 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { }); chips.on('chip.delete', function(e, chip) { - var sel = '.tag-'+chip.tag; + // convert tag string to class-like selector "some tag" -> ".some.tag" + var sel = '.tag.'+chip.tag.split(' ').join('.'); console.log(sel); console.log(chips.parent().find(sel)); chips.parent().find(sel).remove(); -- cgit v1.2.3 From c81783e87070340701a818eaae59f2304f982f3f Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 25 Oct 2016 13:05:12 -0700 Subject: add fix for multi-word classes --- management/editor/elements.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/management/editor/elements.go b/management/editor/elements.go index 161ac88..2326358 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -353,7 +353,7 @@ func Tags(fieldName string, p interface{}, attrs map[string]string) []byte { i := 0 for _, tag := range tags { tagName := tagNameFromStructFieldMulti(fieldName, i, p) - html += `` + html += `` initial = append(initial, `{tag: '`+tag+`'}`) i++ } -- cgit v1.2.3