Static site generator for making web mixtapes in 2020.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.0 KiB

  1. {{define "player"}}
  2. {{with $x := index . 0}}
  3. <audio id="player" preload="auto" tabindex="0" controls>
  4. <source src="{{$x.Filename}}">
  5. </audio>
  6. {{end}}
  7. {{end}}
  8. {{define "playlist"}}
  9. <ol id="playlist">
  10. {{range $i, $el := .}}
  11. <li{{if eq $i 0}} class="active"{{end}}>
  12. <a href="{{$el.Filename}}">{{$el.Artist}} - {{$el.Title}}</a>
  13. </li>
  14. {{end}}
  15. </ol>
  16. {{end}}
  17. {{define "full-player"}}
  18. {{template "player" .}}
  19. {{template "playlist" .}}
  20. {{template "playlist-js"}}
  21. {{end}}
  22. {{define "playlist-js"}}
  23. <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
  24. <script type="text/javascript">
  25. $(document).ready(function() {
  26. init();
  27. function init() {
  28. var current = 0;
  29. var $audio = $('#player');
  30. var $playlist = $('#playlist');
  31. var $tracks = $playlist.find('li a');
  32. var len = $tracks.length - 1;
  33. $playlist.on('click', 'a', function (e) {
  34. e.preventDefault();
  35. link = $(this);
  36. current = link.parent().index();
  37. run(link, $audio[0]);
  38. });
  39. $audio[0].addEventListener('ended', function (e) {
  40. current++;
  41. if (current == len) {
  42. current = 0;
  43. link = $playlist.find('a')[0];
  44. } else {
  45. link = $playlist.find('a')[current];
  46. }
  47. run($(link), $audio[0]);
  48. });
  49. }
  50. function run($link, $player) {
  51. $player.src = $link.attr('href');
  52. par = $link.parent();
  53. par.addClass('active').siblings().removeClass('active');
  54. $player.load();
  55. $player.play();
  56. }
  57. });
  58. </script>
  59. {{end}}