forked from thebaer/cdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
149 lines (137 loc) · 3.78 KB
/
templates.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
// Code generated by "inline -o templates.go -p cdr mixtape.tmpl templates/parts.tmpl" -- DO NOT EDIT --
package cdr
import (
"fmt"
"io/ioutil"
)
func ReadAsset(file string, useLocal bool) ([]byte, error) {
if useLocal {
return ioutil.ReadFile(file)
}
if f, ok := files[file]; ok {
return []byte(f), nil
}
return nil, fmt.Errorf("file doesn't exist.")
}
var files = map[string]string{
"mixtape.tmpl": `{{define "mixtape"}}
<html>
<head>
<title>Mixtape</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style type="text/css">
body {
font-size: 1.2em;
margin: 1em;
}
#playlist {
list-style: decimal-leading-zero;
margin: 1em 0;
}
#playlist li {
margin: 0.5em 0;
}
li p {
display: none;
font-style: italic;
}
li.active a {
font-weight: bold;
}
li.active p {
display: block;
}
</style>
</head>
<body>
{{template "full-player" .Tracks}}
</body>
</html>
{{end}}
{{define "track-info"}}
{{if eq .Num 1}}
<p>[Here I might introduce this mix.]</p>
<p>[Some notes about track 1.]</p>
{{else if eq .Num 2}}
<p>[Some notes about track 2.]</p>
{{else if eq .Num 5}}
<p>[Some notes about track 5.]</p>
{{end}}
{{end}}`,
"templates/parts.tmpl": `{{define "player"}}
{{with $x := index . 0}}
<audio id="player" preload="auto" tabindex="0" controls>
<source src="{{$x.Filename}}">
</audio>
{{end}}
{{end}}
{{define "playlist"}}
<ol id="playlist">
{{range $i, $el := .}}
<li{{if eq $i 0}} class="active"{{end}}>
<a class="track" href="{{$el.Filename}}">{{$el.Artist}} - {{$el.Title}}</a>
{{template "track-info" $el}}
</li>
{{end}}
</ol>
{{end}}
{{define "track-info"}}{{end}}
{{define "full-player"}}
{{template "player" .}}
{{template "playlist" .}}
{{template "playlist-js"}}
{{end}}
{{define "playlist-js"}}
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function() {
var current = 0;
var $audio = $('#player');
var $playlist = $('#playlist');
var $tracks = $playlist.find('li a.track');
var len = $tracks.length;
$playlist.on('click', 'a.track', function (e) {
e.preventDefault();
link = $(this);
current = link.parent().index();
play(link, $audio[0]);
});
$audio[0].addEventListener('ended', function (e) {
playNext();
});
$('a#next').on('click', function (e) {
e.preventDefault();
playNext();
});
$('a#prev').on('click', function (e) {
e.preventDefault();
playPrev();
});
function playPrev() {
current--;
if (current <= 0) {
current = len - 1;
}
link = $playlist.find('a.track')[current];
play($(link), $audio[0]);
}
function playNext() {
current++;
if (current == len) {
current = 0;
}
link = $playlist.find('a.track')[current];
play($(link), $audio[0]);
}
function play($link, $player) {
$player.src = $link.attr('href');
par = $link.parent();
par.addClass('active').siblings().removeClass('active');
$player.load();
$player.play();
}
});
</script>
{{end}}`,
}