diff options
Diffstat (limited to 'management')
-rw-r--r-- | management/editor/editor.go | 70 | ||||
-rw-r--r-- | management/editor/elements.go | 25 | ||||
-rw-r--r-- | management/manager/manager.go | 84 |
3 files changed, 169 insertions, 10 deletions
diff --git a/management/editor/editor.go b/management/editor/editor.go index f8b1970..dc6f181 100644 --- a/management/editor/editor.go +++ b/management/editor/editor.go @@ -43,6 +43,58 @@ func Form(post Editable, fields ...Field) ([]byte, error) { // content items with Item embedded have some default fields we need to render editor.ViewBuf.Write([]byte(`<tr class="col s4 default-fields"><td>`)) + + publishTime := ` +<div class="row"> + <div class="input-field col s6"> + <label class="active">MM</label> + <select class="month __ponzu browser-default"> + <option value="1">Jan - 01</option> + <option value="2">Feb - 02</option> + <option value="3">Mar - 03</option> + <option value="4">Apr - 04</option> + <option value="5">May - 05</option> + <option value="6">Jun - 06</option> + <option value="7">Jul - 07</option> + <option value="8">Aug - 08</option> + <option value="9">Sep - 09</option> + <option value="10">Oct - 10</option> + <option value="11">Nov - 11</option> + <option value="12">Dec - 12</option> + </select> + </div> + <div class="input-field col s2"> + <label class="active">DD</label> + <input value="" class="day __ponzu" maxlength="2" type="text" placeholder="DD" /> + </div> + <div class="input-field col s4"> + <label class="active">YYYY</label> + <input value="" class="year __ponzu" maxlength="4" type="text" placeholder="YYYY" /> + </div> +</div> + +<div class="row"> + <div class="input-field col s3"> + <label class="active">HH</label> + <input value="" class="hour __ponzu" maxlength="2" type="text" placeholder="HH" /> + </div> + <div class="col s1">:</div> + <div class="input-field col s3"> + <label class="active">MM</label> + <input value="" class="minute __ponzu" maxlength="2" type="text" placeholder="MM" /> + </div> + <div class="input-field col s4"> + <label class="active">Period</label> + <select class="period __ponzu browser-default"> + <option value="AM">AM</option> + <option value="PM">PM</option> + </select> + </div> +</div> + ` + + editor.ViewBuf.Write([]byte(publishTime)) + addPostDefaultFieldsToEditorView(post, editor) submit := ` @@ -87,12 +139,6 @@ func addFieldToEditorView(e *Editor, f Field) { func addPostDefaultFieldsToEditorView(p Editable, e *Editor) { defaults := []Field{ Field{ - View: Input("Timestamp", p, map[string]string{ - "label": "Publish Date", - "type": "date", - }), - }, - Field{ View: Input("Slug", p, map[string]string{ "label": "URL Slug", "type": "text", @@ -100,6 +146,18 @@ func addPostDefaultFieldsToEditorView(p Editable, e *Editor) { "placeholder": "Will be set automatically", }), }, + Field{ + View: Timestamp("Timestamp", p, map[string]string{ + "type": "hidden", + "class": "timestamp __ponzu", + }), + }, + Field{ + View: Timestamp("Updated", p, map[string]string{ + "type": "hidden", + "class": "updated __ponzu", + }), + }, } for _, f := range defaults { diff --git a/management/editor/elements.go b/management/editor/elements.go index e8a2fab..390d8df 100644 --- a/management/editor/elements.go +++ b/management/editor/elements.go @@ -37,6 +37,31 @@ func Textarea(fieldName string, p interface{}, attrs map[string]string) []byte { return domElement(e) } +// Timestamp returns the []byte of an <input> HTML element 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 Timestamp(fieldName string, p interface{}, attrs map[string]string) []byte { + var data string + val := valueFromStructField(fieldName, p) + if val.Int() == 0 { + data = "" + } else { + data = fmt.Sprintf("%d", val.Int()) + } + + e := &element{ + TagName: "input", + Attrs: attrs, + Name: tagNameFromStructField(fieldName, p), + label: attrs["label"], + data: data, + viewBuf: &bytes.Buffer{}, + } + + return domElementSelfClose(e) +} + // File returns the []byte of a <input type="file"> HTML element with a label. // IMPORTANT: // The `fieldName` argument will cause a panic if it is not exactly the string diff --git a/management/manager/manager.go b/management/manager/manager.go index c0056ac..a8665ba 100644 --- a/management/manager/manager.go +++ b/management/manager/manager.go @@ -16,11 +16,87 @@ const managerHTML = ` {{ .Editor }} </form> <script> - // remove all bad chars from all inputs in the form, except file fields - $('form input:not([type=file]), form textarea').on('blur', function(e) { - var val = e.target.value; - e.target.value = replaceBadChars(val); + $(function() { + // remove all bad chars from all inputs in the form, except file fields + $('form input:not([type=file]), form textarea').on('blur', function(e) { + var val = e.target.value; + e.target.value = replaceBadChars(val); + }); + + var updateTimestamp = function(dt, $ts) { + var year = dt.year.val(), + month = dt.month.val()-1, + day = dt.day.val(), + hour = dt.hour.val(), + minute = dt.minute.val(); + + if (dt.period == "PM") { + hours = hours + 12; + } + + var date = new Date(year, month, day, hour, minute); + + $ts.val(date.getTime()); + } + + var setDefaultTimeAndDate = function(dt, $ts, $up, unix) { + var time = getPartialTime(unix), + date = getPartialDate(unix); + + dt.hour.val(time.hh); + dt.minute.val(time.mm); + dt.period.val(time.pd); + dt.year.val(date.yyyy); + dt.month.val(date.mm); + dt.day.val(date.dd); + } + + // set time time and date inputs using the hidden timestamp input. + // if it is empty, set it to now and use that value for time and date + var publish_time_hh = $('input.__ponzu.hour'), + publish_time_mm = $('input.__ponzu.minute'), + publish_time_pd = $('select.__ponzu.period'), + publish_date_yyyy = $('input.__ponzu.year'), + publish_date_mm = $('select.__ponzu.month'), + publish_date_dd = $('input.__ponzu.day'), + timestamp = $('input.__ponzu.timestamp'), + updated = $('input.__ponzu.updated'), + getFields = function() { + return { + hour: publish_time_hh, + minute: publish_time_mm, + period: publish_time_pd, + year: publish_date_yyyy, + month: publish_date_mm, + day: publish_date_dd + } + }, + time; + + if (timestamp.val() !== "") { + time = parseInt(timestamp.val()); + } else { + time = (new Date()).getTime(); + } + + setDefaultTimeAndDate(getFields(), timestamp, updated, time); + + var timeUpdated = false; + $('form').on('submit', function(e) { + if (timeUpdated === true) { + timeUpdated = false; + return; + } + + e.preventDefault(); + + updateTimestamp(getFields(), timestamp); + + timeUpdated = true; + $('form').submit(); + }); }); + </script> </div> ` |