summaryrefslogtreecommitdiff
path: root/management/manager/manager.go
blob: 083f946eb28f4e8f6ea49e55c4017c1bac20f830 (plain)
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
package manager

import (
	"bytes"
	"fmt"
	"html/template"

	"github.com/bosssauce/ponzu/management/editor"
)

const managerHTML = `
<div class="card editor">
    <form method="post" action="/admin/edit" enctype="multipart/form-data">
		<input type="hidden" name="id" value="{{.ID}}"/>
		<input type="hidden" name="type" value="{{.Kind}}"/>
		<input type="hidden" name="timestamp" class="timestamp __ponzu" value="" />
		<input type="hidden" name="updated" class="updated __ponzu" value="" />
		{{ .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 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);
				$ts.val(unix);
				$up.val(unix);
			}

			// 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 = timestamp.val();
			} else {
				time = (new Date()).getTime();
			}

			setDefaultTimeAndDate(getFields(), timestamp, updated, time);
			
		});
	</script>
</div>
`

type manager struct {
	ID     int
	Kind   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 %T. %s", e, err.Error())
	}

	m := manager{
		ID:     e.ContentID(),
		Kind:   typeName,
		Editor: template.HTML(v),
	}

	// execute html template into buffer for func return val
	buf := &bytes.Buffer{}
	tmpl := template.Must(template.New("manager").Parse(managerHTML))
	tmpl.Execute(buf, m)

	return buf.Bytes(), nil
}