jplayer.builder.es6.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @file
  3. * Audiofield build jPlayer audio players of various types.
  4. */
  5. (($, Drupal) => {
  6. 'use strict';
  7. Drupal.AudiofieldJplayer = {};
  8. /**
  9. * Generate a jplayer player for the default single file layout.
  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.AudiofieldJplayer.generate = (context, settings) => {
  17. // Create the media player.
  18. $(`#jquery_jplayer_${settings.unique_id}`, context).once('generate-jplayer').jPlayer(
  19. {
  20. cssSelectorAncestor: `#jp_container_${settings.unique_id}`,
  21. },
  22. {
  23. ready: () => {
  24. const mediaArray = {
  25. title: settings.description,
  26. };
  27. mediaArray[settings.filetype] = settings.file;
  28. $(`#jquery_jplayer_${settings.unique_id}`, context).jPlayer('setMedia', mediaArray);
  29. },
  30. canplay: () => {
  31. // Handle autoplay.
  32. if (!!settings.autoplay) {
  33. $(`#jquery_jplayer_${settings.unique_id}`, context).jPlayer('play');
  34. }
  35. },
  36. swfPath: '/libraries/jplayer/dist/jplayer',
  37. supplied: settings.filetype,
  38. wmode: 'window',
  39. useStateClassSkin: true,
  40. autoBlur: false,
  41. preload: settings.lazyload,
  42. smoothPlayBar: true,
  43. keyEnabled: true,
  44. remainingDuration: false,
  45. toggleDuration: false,
  46. volume: settings.volume,
  47. },
  48. );
  49. };
  50. /**
  51. * Generate a jplayer formatter for a player.
  52. *
  53. * @param {jQuery} context
  54. * The Drupal context for which we are finding and generating this player.
  55. * @param {jQuery} settings
  56. * The Drupal settings for this player.
  57. */
  58. Drupal.AudiofieldJplayer.generatePlaylist = (context, settings) => {
  59. $.each($(context).find(`#jquery_jplayer_${settings.unique_id}`).once('generate-jplayer'), (index, item) => {
  60. // Initialize the container audio player.
  61. const thisPlaylist = new jPlayerPlaylist({
  62. jPlayer: $(item),
  63. cssSelectorAncestor: `#jp_container_${settings.unique_id}`,
  64. }, [], {
  65. canplay: () => {
  66. // Handle autoplay.
  67. if (!!settings.autoplay) {
  68. $(item).jPlayer('play');
  69. }
  70. },
  71. playlistOptions: {
  72. enableRemoveControls: false,
  73. },
  74. swfPath: '/libraries/jplayer/dist/jplayer',
  75. wmode: 'window',
  76. useStateClassSkin: true,
  77. autoBlur: false,
  78. preload: settings.lazyload,
  79. smoothPlayBar: true,
  80. keyEnabled: true,
  81. volume: settings.volume,
  82. });
  83. // Loop over each file.
  84. $.each(settings.files, (key, fileEntry) => {
  85. // Build the media array.
  86. const mediaArray = {
  87. title: fileEntry.description,
  88. };
  89. mediaArray[fileEntry.filetype] = fileEntry.file;
  90. // Add the file to the playlist.
  91. thisPlaylist.add(mediaArray);
  92. });
  93. });
  94. };
  95. /**
  96. * Generate a jplayer circle player.
  97. *
  98. * @param {jQuery} context
  99. * The Drupal context for which we are finding and generating this player.
  100. * @param {array} file
  101. * The audio file for which we are generating a player.
  102. */
  103. Drupal.AudiofieldJplayer.generateCircle = (context, file) => {
  104. // Create the media player.
  105. $.each($(context).find(`#jquery_jplayer_${file.fid}`).once('generate-jplayer'), (index, item) => {
  106. // Build the media array for this player.
  107. const mediaArray = {};
  108. mediaArray[file.filetype] = file.file;
  109. // Initialize the player.
  110. new CirclePlayer(
  111. $(item),
  112. mediaArray,
  113. {
  114. cssSelectorAncestor: `#cp_container_${file.fid}`,
  115. canplay: () => {
  116. // Handle autoplay.
  117. if (!!file.autoplay) {
  118. $(item).jPlayer('play');
  119. }
  120. },
  121. swfPath: '/libraries/jplayer/dist/jplayer',
  122. wmode: 'window',
  123. preload: settings.lazyload,
  124. keyEnabled: true,
  125. supplied: file.filetype,
  126. },
  127. );
  128. });
  129. };
  130. /**
  131. * Attach the behaviors to generate the audio player.
  132. *
  133. * @type {Drupal~behavior}
  134. *
  135. * @prop {Drupal~behaviorAttach} attach
  136. * Attaches generation of Jplayer audio players.
  137. */
  138. Drupal.behaviors.audiofieldjplayer = {
  139. attach: (context, settings) => {
  140. $.each(settings.audiofieldjplayer, (key, settingEntry) => {
  141. // Default audio player.
  142. if (settingEntry.playertype === 'default') {
  143. // We can just initialize the audio player direcly.
  144. Drupal.AudiofieldJplayer.generate(context, settingEntry);
  145. }
  146. // Playlist audio player.
  147. else if (settingEntry.playertype === 'playlist') {
  148. Drupal.AudiofieldJplayer.generatePlaylist(context, settingEntry);
  149. }
  150. // Circle audio player.
  151. else if (settingEntry.playertype === 'circle') {
  152. // Loop over the files.
  153. $.each(settingEntry.files, (key2, fileEntry) => {
  154. // Create the player.
  155. Drupal.AudiofieldJplayer.generateCircle(context, fileEntry);
  156. });
  157. }
  158. });
  159. },
  160. };
  161. })(jQuery, Drupal);