audiojs.builder.es6.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @file
  3. * Audiofield build AudioJs audio player.
  4. */
  5. (($, Drupal) => {
  6. 'use strict';
  7. Drupal.AudiofieldAudiojs = {};
  8. /**
  9. * Generate an audio.js player.
  10. *
  11. * @param {jQuery} context
  12. * The Drupal context for which we are finding and generating this player.
  13. * @param {jQuery} settings
  14. * The Drupal settings for this player.
  15. */
  16. Drupal.AudiofieldAudiojs.generate = (context, settings) => {
  17. // Create the media player.
  18. $.each($(context).find(`#${settings.element}`).once('generate-audiojs'), (index, item) => {
  19. // Initialize the audio player.
  20. const audioPlayer = audiojs.create($(item).find('audio').get(0), {
  21. css: false,
  22. createPlayer: {
  23. markup: false,
  24. playPauseClass: 'play-pauseZ',
  25. scrubberClass: 'scrubberZ',
  26. progressClass: 'progressZ',
  27. loaderClass: 'loadedZ',
  28. timeClass: 'timeZ',
  29. durationClass: 'durationZ',
  30. playedClass: 'playedZ',
  31. errorMessageClass: 'error-messageZ',
  32. playingClass: 'playingZ',
  33. loadingClass: 'loadingZ',
  34. errorClass: 'errorZ',
  35. },
  36. // Handle the end of a track.
  37. trackEnded: () => {
  38. let next = $(context).find(`#${settings.element} ol li.playing`).next();
  39. if (!next.length) {
  40. next = $(context).find(`#${settings.element} ol li:first`);
  41. }
  42. next.addClass('playing').siblings().removeClass('playing');
  43. audioPlayer.load($('a', next).attr('data-src'));
  44. audioPlayer.play();
  45. },
  46. });
  47. // Load in the first track.
  48. $(item).find('ol li:first').addClass('playing');
  49. audioPlayer.load($(item).find('ol a:first').attr('data-src'));
  50. // Load in a track on click.
  51. $(item).find('ol li').click((event) => {
  52. event.preventDefault();
  53. $(event.currentTarget).addClass('playing').siblings().removeClass('playing');
  54. audioPlayer.load($('a', event.currentTarget).attr('data-src'));
  55. audioPlayer.play();
  56. });
  57. });
  58. };
  59. /**
  60. * Attach the behaviors to generate the audio player.
  61. *
  62. * @type {Drupal~behavior}
  63. *
  64. * @prop {Drupal~behaviorAttach} attach
  65. * Attaches generation of audio.js audio players.
  66. */
  67. Drupal.behaviors.audiofieldaudiojs = {
  68. attach: (context, settings) => {
  69. $.each(settings.audiofieldaudiojs, (key, settingEntry) => {
  70. Drupal.AudiofieldAudiojs.generate(context, settingEntry);
  71. });
  72. },
  73. };
  74. })(jQuery, Drupal);