github.com/rpdict/ponzu@v0.10.1-0.20190226054626-477f29d6bf5e/management/manager/manager.go (about) 1 // Package manager contains the admin UI to the CMS which wraps all content editor 2 // interfaces to manage the create/edit/delete capabilities of Ponzu content. 3 package manager 4 5 import ( 6 "bytes" 7 "fmt" 8 "html/template" 9 10 "github.com/rpdict/ponzu/management/editor" 11 "github.com/rpdict/ponzu/system/item" 12 13 "github.com/gofrs/uuid" 14 ) 15 16 const managerHTML = ` 17 <div class="card editor"> 18 <form method="post" action="/admin/edit" enctype="multipart/form-data"> 19 <input type="hidden" name="uuid" value="{{.UUID}}"/> 20 <input type="hidden" name="id" value="{{.ID}}"/> 21 <input type="hidden" name="type" value="{{.Kind}}"/> 22 <input type="hidden" name="slug" value="{{.Slug}}"/> 23 {{ .Editor }} 24 </form> 25 <script> 26 $(function() { 27 // remove all bad chars from all inputs in the form, except file fields 28 $('form input:not([type=file]), form textarea').on('blur', function(e) { 29 var val = e.target.value; 30 e.target.value = replaceBadChars(val); 31 }); 32 33 var updateTimestamp = function(dt, $ts) { 34 var year = parseInt(dt.year.val()), 35 month = parseInt(dt.month.val())-1, 36 day = parseInt(dt.day.val()), 37 hour = parseInt(dt.hour.val()), 38 minute = parseInt(dt.minute.val()); 39 40 if (dt.period.val() === "PM") { 41 hour = hour + 12; 42 } 43 44 // add seconds to Date() to differentiate times more precisely, 45 // although not 100% accurately 46 var sec = (new Date()).getSeconds(); 47 var date = new Date(year, month, day, hour, minute, sec); 48 49 $ts.val(date.getTime()); 50 } 51 52 var setDefaultTimeAndDate = function(dt, unix) { 53 var time = getPartialTime(unix), 54 date = getPartialDate(unix); 55 56 dt.hour.val(time.hh); 57 dt.minute.val(time.mm); 58 dt.period.val(time.pd); 59 dt.year.val(date.yyyy); 60 dt.month.val(date.mm); 61 dt.day.val(date.dd); 62 } 63 64 // set time time and date inputs using the hidden timestamp input. 65 // if it is empty, set it to now and use that value for time and date 66 var publish_time_hh = $('input.__ponzu.hour'), 67 publish_time_mm = $('input.__ponzu.minute'), 68 publish_time_pd = $('select.__ponzu.period'), 69 publish_date_yyyy = $('input.__ponzu.year'), 70 publish_date_mm = $('select.__ponzu.month'), 71 publish_date_dd = $('input.__ponzu.day'), 72 timestamp = $('input.__ponzu.timestamp'), 73 updated = $('input.__ponzu.updated'), 74 getFields = function() { 75 return { 76 hour: publish_time_hh, 77 minute: publish_time_mm, 78 period: publish_time_pd, 79 year: publish_date_yyyy, 80 month: publish_date_mm, 81 day: publish_date_dd 82 } 83 }, 84 time; 85 86 if (timestamp.val() !== "") { 87 time = parseInt(timestamp.val()); 88 } else { 89 time = (new Date()).getTime(); 90 } 91 92 setDefaultTimeAndDate(getFields(), time); 93 94 var timeUpdated = false; 95 $('form').on('submit', function(e) { 96 if (timeUpdated === true) { 97 timeUpdated = false; 98 return; 99 } 100 101 e.preventDefault(); 102 103 updateTimestamp(getFields(), timestamp); 104 updated.val((new Date()).getTime()); 105 106 timeUpdated = true; 107 $('form').submit(); 108 }); 109 }); 110 111 </script> 112 </div> 113 ` 114 115 var managerTmpl = template.Must(template.New("manager").Parse(managerHTML)) 116 117 type manager struct { 118 ID int 119 UUID uuid.UUID 120 Kind string 121 Slug string 122 Editor template.HTML 123 } 124 125 // Manage ... 126 func Manage(e editor.Editable, typeName string) ([]byte, error) { 127 v, err := e.MarshalEditor() 128 if err != nil { 129 return nil, fmt.Errorf("Couldn't marshal editor for content %s. %s", typeName, err.Error()) 130 } 131 132 i, ok := e.(item.Identifiable) 133 if !ok { 134 return nil, fmt.Errorf("Content type %s does not implement item.Identifiable.", typeName) 135 } 136 137 s, ok := e.(item.Sluggable) 138 if !ok { 139 return nil, fmt.Errorf("Content type %s does not implement item.Sluggable.", typeName) 140 } 141 142 m := manager{ 143 ID: i.ItemID(), 144 UUID: i.UniqueID(), 145 Kind: typeName, 146 Slug: s.ItemSlug(), 147 Editor: template.HTML(v), 148 } 149 150 // execute html template into buffer for func return val 151 buf := &bytes.Buffer{} 152 if err := managerTmpl.Execute(buf, m); err != nil { 153 return nil, err 154 } 155 return buf.Bytes(), nil 156 }