media-audio-widget.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @output wp-admin/js/widgets/media-audio-widget.js
  3. */
  4. /* eslint consistent-this: [ "error", "control" ] */
  5. (function( component ) {
  6. 'use strict';
  7. var AudioWidgetModel, AudioWidgetControl, AudioDetailsMediaFrame;
  8. /**
  9. * Custom audio details frame that removes the replace-audio state.
  10. *
  11. * @class wp.mediaWidgets.controlConstructors~AudioDetailsMediaFrame
  12. * @augments wp.media.view.MediaFrame.AudioDetails
  13. */
  14. AudioDetailsMediaFrame = wp.media.view.MediaFrame.AudioDetails.extend(/** @lends wp.mediaWidgets.controlConstructors~AudioDetailsMediaFrame.prototype */{
  15. /**
  16. * Create the default states.
  17. *
  18. * @return {void}
  19. */
  20. createStates: function createStates() {
  21. this.states.add([
  22. new wp.media.controller.AudioDetails({
  23. media: this.media
  24. }),
  25. new wp.media.controller.MediaLibrary({
  26. type: 'audio',
  27. id: 'add-audio-source',
  28. title: wp.media.view.l10n.audioAddSourceTitle,
  29. toolbar: 'add-audio-source',
  30. media: this.media,
  31. menu: false
  32. })
  33. ]);
  34. }
  35. });
  36. /**
  37. * Audio widget model.
  38. *
  39. * See WP_Widget_Audio::enqueue_admin_scripts() for amending prototype from PHP exports.
  40. *
  41. * @class wp.mediaWidgets.modelConstructors.media_audio
  42. * @augments wp.mediaWidgets.MediaWidgetModel
  43. */
  44. AudioWidgetModel = component.MediaWidgetModel.extend({});
  45. /**
  46. * Audio widget control.
  47. *
  48. * See WP_Widget_Audio::enqueue_admin_scripts() for amending prototype from PHP exports.
  49. *
  50. * @class wp.mediaWidgets.controlConstructors.media_audio
  51. * @augments wp.mediaWidgets.MediaWidgetControl
  52. */
  53. AudioWidgetControl = component.MediaWidgetControl.extend(/** @lends wp.mediaWidgets.controlConstructors.media_audio.prototype */{
  54. /**
  55. * Show display settings.
  56. *
  57. * @type {boolean}
  58. */
  59. showDisplaySettings: false,
  60. /**
  61. * Map model props to media frame props.
  62. *
  63. * @param {Object} modelProps - Model props.
  64. * @return {Object} Media frame props.
  65. */
  66. mapModelToMediaFrameProps: function mapModelToMediaFrameProps( modelProps ) {
  67. var control = this, mediaFrameProps;
  68. mediaFrameProps = component.MediaWidgetControl.prototype.mapModelToMediaFrameProps.call( control, modelProps );
  69. mediaFrameProps.link = 'embed';
  70. return mediaFrameProps;
  71. },
  72. /**
  73. * Render preview.
  74. *
  75. * @return {void}
  76. */
  77. renderPreview: function renderPreview() {
  78. var control = this, previewContainer, previewTemplate, attachmentId, attachmentUrl;
  79. attachmentId = control.model.get( 'attachment_id' );
  80. attachmentUrl = control.model.get( 'url' );
  81. if ( ! attachmentId && ! attachmentUrl ) {
  82. return;
  83. }
  84. previewContainer = control.$el.find( '.media-widget-preview' );
  85. previewTemplate = wp.template( 'wp-media-widget-audio-preview' );
  86. previewContainer.html( previewTemplate({
  87. model: {
  88. attachment_id: control.model.get( 'attachment_id' ),
  89. src: attachmentUrl
  90. },
  91. error: control.model.get( 'error' )
  92. }));
  93. wp.mediaelement.initialize();
  94. },
  95. /**
  96. * Open the media audio-edit frame to modify the selected item.
  97. *
  98. * @return {void}
  99. */
  100. editMedia: function editMedia() {
  101. var control = this, mediaFrame, metadata, updateCallback;
  102. metadata = control.mapModelToMediaFrameProps( control.model.toJSON() );
  103. // Set up the media frame.
  104. mediaFrame = new AudioDetailsMediaFrame({
  105. frame: 'audio',
  106. state: 'audio-details',
  107. metadata: metadata
  108. });
  109. wp.media.frame = mediaFrame;
  110. mediaFrame.$el.addClass( 'media-widget' );
  111. updateCallback = function( mediaFrameProps ) {
  112. // Update cached attachment object to avoid having to re-fetch. This also triggers re-rendering of preview.
  113. control.selectedAttachment.set( mediaFrameProps );
  114. control.model.set( _.extend(
  115. control.model.defaults(),
  116. control.mapMediaToModelProps( mediaFrameProps ),
  117. { error: false }
  118. ) );
  119. };
  120. mediaFrame.state( 'audio-details' ).on( 'update', updateCallback );
  121. mediaFrame.state( 'replace-audio' ).on( 'replace', updateCallback );
  122. mediaFrame.on( 'close', function() {
  123. mediaFrame.detach();
  124. });
  125. mediaFrame.open();
  126. }
  127. });
  128. // Exports.
  129. component.controlConstructors.media_audio = AudioWidgetControl;
  130. component.modelConstructors.media_audio = AudioWidgetModel;
  131. })( wp.mediaWidgets );