wavesurfer.builder.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal) {
  8. 'use strict';
  9. Drupal.AudiofieldWavesurfer = {};
  10. Drupal.AudiofieldWavesurfer.generate = function (context, file, settings) {
  11. $.each($(context).find('#' + file.id).once('generate-waveform'), function (index, wavecontainer) {
  12. var wavesurfer = WaveSurfer.create({
  13. container: '#' + $(wavecontainer).attr('id') + ' .waveform'
  14. });
  15. wavesurfer.load(file.path);
  16. wavesurfer.setVolume(settings.volume);
  17. $(wavecontainer).find('.player-button.playpause').on('click', function () {
  18. Drupal.AudiofieldWavesurfer.PlayPause(wavecontainer, wavesurfer);
  19. });
  20. $(wavecontainer).find('.volume').on('change', function () {
  21. wavesurfer.setVolume($(event.currentTarget).val() / 10);
  22. });
  23. if (!!settings.autoplay) {
  24. wavesurfer.on('ready', wavesurfer.play.bind(wavesurfer));
  25. }
  26. });
  27. };
  28. Drupal.AudiofieldWavesurfer.generatePlaylist = function (context, settings) {
  29. $.each($(context).find('#wavesurfer_playlist').once('generate-waveform'), function (index, wavecontainer) {
  30. var wavesurfer = WaveSurfer.create({
  31. container: '#' + $(wavecontainer).attr('id') + ' .waveform'
  32. });
  33. wavesurfer.setVolume(settings.volume);
  34. var first = $(wavecontainer).find('.playlist .track').first();
  35. var label = $(wavecontainer).find('label').first();
  36. label.html('Playing: ' + first.html());
  37. first.addClass('playing');
  38. wavesurfer.load(first.attr('data-src'));
  39. $(wavecontainer).find('.player-button.playpause').on('click', function () {
  40. Drupal.AudiofieldWavesurfer.PlayPause(wavecontainer, wavesurfer);
  41. });
  42. $(wavecontainer).find('.player-button.next').on('click', function () {
  43. Drupal.AudiofieldWavesurfer.Next(wavecontainer, wavesurfer);
  44. });
  45. $(wavecontainer).find('.player-button.previous').on('click', function () {
  46. Drupal.AudiofieldWavesurfer.Previous(wavecontainer, wavesurfer);
  47. });
  48. $(wavecontainer).find('.playlist .track').on('click', function () {
  49. Drupal.AudiofieldWavesurfer.Load(wavecontainer, wavesurfer, $(event.currentTarget));
  50. Drupal.AudiofieldWavesurfer.PlayPause(wavecontainer, wavesurfer);
  51. });
  52. $(wavecontainer).find('.volume').on('change', function () {
  53. wavesurfer.setVolume($(event.currentTarget).val() / 10);
  54. });
  55. if (!!settings.autoplay) {
  56. wavesurfer.on('ready', wavesurfer.play.bind(wavesurfer));
  57. }
  58. wavesurfer.on('finish', function () {
  59. Drupal.AudiofieldWavesurfer.Next(wavecontainer, wavesurfer);
  60. });
  61. });
  62. };
  63. Drupal.AudiofieldWavesurfer.PlayPause = function (wavecontainer, wavesurfer) {
  64. wavesurfer.playPause();
  65. var button = $(wavecontainer).find('.player-button.playpause');
  66. if (wavesurfer.isPlaying()) {
  67. $(wavecontainer).addClass('playing');
  68. button.html('Pause');
  69. } else {
  70. $(wavecontainer).removeClass('playing');
  71. button.html('Play');
  72. }
  73. };
  74. Drupal.AudiofieldWavesurfer.Load = function (wavecontainer, wavesurfer, track) {
  75. wavesurfer.on('ready', function () {
  76. wavesurfer.play();
  77. $(wavecontainer).removeClass('playing');
  78. $(wavecontainer).addClass('playing');
  79. $(wavecontainer).find('.player-button.playpause').html('Pause');
  80. });
  81. wavesurfer.load(track.attr('data-src'));
  82. $(wavecontainer).find('.track').removeClass('playing');
  83. track.addClass('playing');
  84. $(wavecontainer).find('label').first().html('Playing: ' + track.html());
  85. };
  86. Drupal.AudiofieldWavesurfer.Next = function (wavecontainer, wavesurfer) {
  87. if (wavesurfer.isPlaying()) {
  88. Drupal.AudiofieldWavesurfer.PlayPause(wavecontainer, wavesurfer);
  89. }
  90. var track = $(wavecontainer).find('.track.playing').next();
  91. if (typeof track.attr('data-src') === 'undefined') {
  92. track = $(wavecontainer).find('.track').first();
  93. }
  94. Drupal.AudiofieldWavesurfer.Load(wavecontainer, wavesurfer, track);
  95. };
  96. Drupal.AudiofieldWavesurfer.Previous = function (wavecontainer, wavesurfer) {
  97. if (wavesurfer.isPlaying()) {
  98. Drupal.AudiofieldWavesurfer.PlayPause(wavecontainer, wavesurfer);
  99. }
  100. var track = $(wavecontainer).find('.track.playing').prev();
  101. if (typeof track.attr('data-src') === 'undefined') {
  102. track = $(wavecontainer).find('.track').last();
  103. }
  104. Drupal.AudiofieldWavesurfer.Load(wavecontainer, wavesurfer, track);
  105. };
  106. Drupal.behaviors.audiofieldwavesurfer = {
  107. attach: function attach(context, settings) {
  108. $.each(settings.audiofieldwavesurfer, function (key, settingEntry) {
  109. if (settingEntry.playertype === 'default') {
  110. $.each(settingEntry.files, function (key2, file) {
  111. Drupal.AudiofieldWavesurfer.generate(context, file, settingEntry);
  112. });
  113. } else if (settingEntry.playertype === 'playlist') {
  114. Drupal.AudiofieldWavesurfer.generatePlaylist(context, settingEntry);
  115. }
  116. });
  117. }
  118. };
  119. })(jQuery, Drupal);