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