` ),
initialize: function ( options ) {
this.controller = options.controller;
this.collection = this.controller.get( 'items' );
this.render();
this.listenTo( this.model, 'change', this.render );
this.$el.data( 'id', this.model.cid );
},
events: {
'click .rwmb-image-overlay': function ( e ) {
e.preventDefault();
this.trigger( 'click:switch', this.model );
},
'click .rwmb-remove-media': function ( e ) {
e.preventDefault();
this.trigger( 'click:remove', this.model );
},
'click .rwmb-edit-media': function ( e ) {
e.preventDefault();
this.trigger( 'click:edit', this.model );
}
},
render: function () {
var data = this.model.toJSON();
data.controller = this.controller.toJSON();
this.$el.html( this.template( data ) );
return this;
}
} );
/**
* Extend media frames to make things work right
*/
/**
* MediaDetails
* Custom version of TwoColumn view to prevent all video and audio from being unset
*/
MediaDetails = views.MediaDetails = media.view.Attachment.Details.TwoColumn.extend( {
render: function () {
var that = this;
media.view.Attachment.Details.prototype.render.apply( this, arguments );
this.players = this.players || [];
media.mixin.unsetPlayers.call( this );
this.$( 'audio, video' ).each( function ( i, elem ) {
var el = media.view.MediaDetails.prepareSrc( elem );
that.players.push( new window.MediaElementPlayer( el, media.mixin.mejsSettings ) );
} );
}
} );
/**
* MediaLibrary
* Custom version of Library to exclude already selected media in a media frame
*/
MediaLibrary = media.controller.Library.extend( {
defaults: _.defaults( {
multiple: 'add',
filterable: 'all',
priority: 100,
syncSelection: false
}, media.controller.Library.prototype.defaults ),
activate: function () {
var library = this.get( 'library' ),
edit = this.frame.options.edit;
if ( this.editLibrary && this.editLibrary !== edit ) {
library.unobserve( this.editLibrary );
}
// Accepts attachments that exist in the original library and
// that do not exist in gallery's library.
library.validator = function ( attachment ) {
return ! ! this.mirroring.get( attachment.cid ) && ! edit.get( attachment.cid ) && media.model.Selection.prototype.validator.apply( this, arguments );
};
// Reset the library to ensure that all attachments are re-added
// to the collection. Do so silently, as calling `observe` will
// trigger the `reset` event.
library.reset( library.mirroring.models, {silent: true} );
library.observe( edit );
this.editLibrary = edit;
media.controller.Library.prototype.activate.apply( this, arguments );
}
} );
/**
* MediaSelect
* Custom version of Select media frame that uses MediaLibrary
*/
MediaSelect = views.MediaSelect = MediaFrame.Select.extend( {
/**
* Create the default states on the frame.
*/
createStates: function () {
var options = this.options;
// Add reference so we know MediaFrame belongs to MB plugin.
this.$el.attr( 'data-class', 'rwmb-field' );
if ( this.options.states ) {
return;
}
// Add the default states.
this.states.add( [
// Main states.
new MediaLibrary( {
library: media.query( options.library ),
multiple: options.multiple,
priority: 20
} )
] );
}
} );
/***
* EditMedia
* Custom version of EditAttachments frame to prevent all video and audio from being unset
*/
EditMedia = views.EditMedia = MediaFrame.EditAttachments.extend( {
/**
* Content region rendering callback for the `edit-metadata` mode.
*
* @param {Object} contentRegion Basic object with a `view` property, which
* should be set with the proper region view.
*/
editMetadataMode: function ( contentRegion ) {
contentRegion.view = new MediaDetails( {
controller: this,
model: this.model
} );
/**
* Attach a subview to display fields added via the
* `attachment_fields_to_edit` filter.
*/
contentRegion.view.views.set( '.attachment-compat', new media.view.AttachmentCompat( {
controller: this,
model: this.model
} ) );
},
resetRoute: function() {}
} );
function initMediaField() {
var $this = $( this ),
view = $this.data( 'view' );
if ( view ) {
return;
}
view = new MediaField( { input: this } );
$this.siblings( '.rwmb-media-view' ).remove();
$this.after( view.el );
$this.data( 'view', view );
}
function init( e ) {
$( e.target ).find( '.rwmb-file_advanced' ).each( initMediaField );
}
rwmb.$document
.on( 'mb_ready', init )
.on( 'clone', '.rwmb-file_advanced', initMediaField );
wp?.hooks?.addAction( 'mb_ready', 'meta-box/ready/media', ref => {
init( { target: ref } );
} );
} )( jQuery, wp, _, rwmb, i18nRwmbMedia );