wavesurfer.builder.es6.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * @file
  3. * Audiofield build Wavesurfer audio player.
  4. */
  5. (($, Drupal) => {
  6. 'use strict';
  7. Drupal.AudiofieldWavesurfer = {};
  8. /**
  9. * Generate a wavesurfer player.
  10. *
  11. * @param {jQuery} context
  12. * The Drupal context for which we are finding and generating this player.
  13. * @param {array} file
  14. * The audio file for which we are generating a player.
  15. * @param {jQuery} settings
  16. * The Drupal settings for this player..
  17. */
  18. Drupal.AudiofieldWavesurfer.generate = (context, file, settings) => {
  19. $.each($(context).find(`#${file.id}`).once('generate-waveform'), (index, wavecontainer) => {
  20. // Create waveform.
  21. const wavesurfer = WaveSurfer.create({
  22. container: `#${$(wavecontainer).attr('id')} .waveform`,
  23. });
  24. // Load the file.
  25. wavesurfer.load(file.path);
  26. // Set the default volume.
  27. wavesurfer.setVolume(settings.volume);
  28. // Handle play/pause.
  29. $(wavecontainer).find('.player-button.playpause').on('click', () => {
  30. Drupal.AudiofieldWavesurfer.PlayPause(wavecontainer, wavesurfer);
  31. });
  32. // Handle volume change.
  33. $(wavecontainer).find('.volume').on('change', () => {
  34. wavesurfer.setVolume(($(event.currentTarget).val() / 10));
  35. });
  36. // Handle autoplay.
  37. if (!!settings.autoplay) {
  38. wavesurfer.on('ready', wavesurfer.play.bind(wavesurfer));
  39. }
  40. });
  41. };
  42. /**
  43. * Generate a wavesurfer playlist player.
  44. *
  45. * @param {jQuery} context
  46. * The Drupal context for which we are finding and generating this player.
  47. * @param {jQuery} settings
  48. * The Drupal settings for this player.
  49. */
  50. Drupal.AudiofieldWavesurfer.generatePlaylist = (context, settings) => {
  51. $.each($(context).find('#wavesurfer_playlist').once('generate-waveform'), (index, wavecontainer) => {
  52. // Create waveform.
  53. const wavesurfer = WaveSurfer.create({
  54. container: `#${$(wavecontainer).attr('id')} .waveform`,
  55. });
  56. // Set the default volume.
  57. wavesurfer.setVolume(settings.volume);
  58. // Load the first file.
  59. const first = $(wavecontainer).find('.playlist .track').first();
  60. // Get the label and update it with the first filename.
  61. const label = $(wavecontainer).find('label').first();
  62. label.html(`Playing: ${first.html()}`);
  63. // Set the playing class on the first element.
  64. first.addClass('playing');
  65. // Load the file.
  66. wavesurfer.load(first.attr('data-src'));
  67. // Handle play/pause.
  68. $(wavecontainer).find('.player-button.playpause').on('click', () => {
  69. Drupal.AudiofieldWavesurfer.PlayPause(wavecontainer, wavesurfer);
  70. });
  71. // Handle next/previous.
  72. $(wavecontainer).find('.player-button.next').on('click', () => {
  73. Drupal.AudiofieldWavesurfer.Next(wavecontainer, wavesurfer);
  74. });
  75. $(wavecontainer).find('.player-button.previous').on('click', () => {
  76. Drupal.AudiofieldWavesurfer.Previous(wavecontainer, wavesurfer);
  77. });
  78. // Handle clicking track.
  79. $(wavecontainer).find('.playlist .track').on('click', () => {
  80. // Load the track.
  81. Drupal.AudiofieldWavesurfer.Load(wavecontainer, wavesurfer, $(event.currentTarget));
  82. // Play the track.
  83. Drupal.AudiofieldWavesurfer.PlayPause(wavecontainer, wavesurfer);
  84. });
  85. // Handle volume change.
  86. $(wavecontainer).find('.volume').on('change', () => {
  87. wavesurfer.setVolume(($(event.currentTarget).val() / 10));
  88. });
  89. // Handle autoplay.
  90. if (!!settings.autoplay) {
  91. wavesurfer.on('ready', wavesurfer.play.bind(wavesurfer));
  92. }
  93. // Handle track finishing.
  94. wavesurfer.on('finish', () => {
  95. Drupal.AudiofieldWavesurfer.Next(wavecontainer, wavesurfer);
  96. });
  97. });
  98. };
  99. /**
  100. * Play or pause the wavesurfer and set appropriate classes.
  101. *
  102. * @param {jQuery} wavecontainer
  103. * The container of the wavesurfer element we are accessing.
  104. * @param {jQuery} wavesurfer
  105. * The wavesurfer player we are accessing.
  106. */
  107. Drupal.AudiofieldWavesurfer.PlayPause = (wavecontainer, wavesurfer) => {
  108. wavesurfer.playPause();
  109. const button = $(wavecontainer).find('.player-button.playpause');
  110. if (wavesurfer.isPlaying()) {
  111. $(wavecontainer).addClass('playing');
  112. button.html('Pause');
  113. }
  114. else {
  115. $(wavecontainer).removeClass('playing');
  116. button.html('Play');
  117. }
  118. };
  119. /**
  120. * Load track on wavesurfer and set appropriate classes.
  121. *
  122. * @param {jQuery} wavecontainer
  123. * The container of the wavesurfer element we are accessing.
  124. * @param {jQuery} wavesurfer
  125. * The wavesurfer player we are accessing.
  126. * @param {jQuery} track
  127. * The track being loaded into the player.
  128. */
  129. Drupal.AudiofieldWavesurfer.Load = (wavecontainer, wavesurfer, track) => {
  130. // Load the track.
  131. wavesurfer.on('ready', () => {
  132. wavesurfer.play();
  133. $(wavecontainer).removeClass('playing');
  134. $(wavecontainer).addClass('playing');
  135. $(wavecontainer).find('.player-button.playpause').html('Pause');
  136. });
  137. wavesurfer.load(track.attr('data-src'));
  138. // Remove playing from all other tracks.
  139. $(wavecontainer).find('.track').removeClass('playing');
  140. // Set the class on this track.
  141. track.addClass('playing');
  142. // Show what's playing.
  143. $(wavecontainer).find('label').first().html(`Playing: ${track.html()}`);
  144. };
  145. /**
  146. * Skip track forward on wavesurfer and set appropriate classes.
  147. *
  148. * @param {jQuery} wavecontainer
  149. * The container of the wavesurfer element we are accessing.
  150. * @param {jQuery} wavesurfer
  151. * The wavesurfer player we are accessing.
  152. */
  153. Drupal.AudiofieldWavesurfer.Next = (wavecontainer, wavesurfer) => {
  154. if (wavesurfer.isPlaying()) {
  155. Drupal.AudiofieldWavesurfer.PlayPause(wavecontainer, wavesurfer);
  156. }
  157. // Find the next track.
  158. let track = $(wavecontainer).find('.track.playing').next();
  159. if (typeof track.attr('data-src') === 'undefined') {
  160. track = $(wavecontainer).find('.track').first();
  161. }
  162. // Load the track.
  163. Drupal.AudiofieldWavesurfer.Load(wavecontainer, wavesurfer, track);
  164. };
  165. /**
  166. * Skip track back on wavesurfer and set appropriate classes.
  167. *
  168. * @param {jQuery} wavecontainer
  169. * The container of the wavesurfer element we are accessing.
  170. * @param {jQuery} wavesurfer
  171. * The wavesurfer player we are accessing.
  172. */
  173. Drupal.AudiofieldWavesurfer.Previous = (wavecontainer, wavesurfer) => {
  174. if (wavesurfer.isPlaying()) {
  175. Drupal.AudiofieldWavesurfer.PlayPause(wavecontainer, wavesurfer);
  176. }
  177. // Find the next track.
  178. let track = $(wavecontainer).find('.track.playing').prev();
  179. if (typeof track.attr('data-src') === 'undefined') {
  180. track = $(wavecontainer).find('.track').last();
  181. }
  182. // Load the track.
  183. Drupal.AudiofieldWavesurfer.Load(wavecontainer, wavesurfer, track);
  184. };
  185. /**
  186. * Attach the behaviors to generate the audio player.
  187. *
  188. * @type {Drupal~behavior}
  189. *
  190. * @prop {Drupal~behaviorAttach} attach
  191. * Attaches generation of Wavesurfer audio players.
  192. */
  193. Drupal.behaviors.audiofieldwavesurfer = {
  194. attach: (context, settings) => {
  195. $.each(settings.audiofieldwavesurfer, (key, settingEntry) => {
  196. // Default audio player.
  197. if (settingEntry.playertype === 'default') {
  198. // Loop over the files.
  199. $.each(settingEntry.files, (key2, file) => {
  200. Drupal.AudiofieldWavesurfer.generate(context, file, settingEntry);
  201. });
  202. }
  203. else if (settingEntry.playertype === 'playlist') {
  204. Drupal.AudiofieldWavesurfer.generatePlaylist(context, settingEntry);
  205. }
  206. });
  207. },
  208. };
  209. })(jQuery, Drupal);