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.

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