New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trac 32417: Media widget #215

Closed
wants to merge 22 commits into
base: master
from
Commits
Jump to file or symbol
Failed to load files and symbols.
+57 −7
Diff settings

Always

Just for now

Viewing a subset of changes. View all

Explicitly refresh widget partial when attachment has any changes.

  • Loading branch information...
taggon committed Feb 24, 2017
commit 0c237e1a3be5bdd93b197ae5ea6740a6767e9a5e
@@ -68,8 +68,14 @@
* @returns {void}
*/
openMediaManager: function( event ) {
var widgetFrame, widgetId;
var widgetFrame, widgetId, selection, prevAttachmentId;
widgetId = $( event.target ).data( 'id' );
selection = frame.getSelection( widgetId );
if ( selection.length > 0 ) {
prevAttachmentId = selection.first().get('id');
}
// Create the media frame.
widgetFrame = wp.media( {
@@ -80,7 +86,7 @@
states: new wp.media.controller.Library( {
library: wp.media.query( { type: [ 'image', 'audio', 'video' ] } ),
title: translate( 'selectMedia', 'Select Media' ), // Media frame title
selection: frame.getSelection( widgetId ),
selection: selection,
multiple: false,
priority: 20,
display: true, // Attachment display setting
@@ -90,22 +96,66 @@
// Render the attachment details.
widgetFrame.on( 'select', function() {
var props, attachment;
var attachment, props;
attachment = frame.getFirstAttachment( widgetFrame );
props = frame.getDisplayProps( widgetFrame );
// Only try to render the attachment details if a selection was made.
if ( widgetFrame.state().get( 'selection' ).length > 0 ) {
props = widgetFrame.content.get( '.attachments-browser' )
.sidebar.get( 'display' ).model.toJSON();
if ( props && attachment && prevAttachmentId !== attachment.id ) {
frame.renderFormView( widgetId, props, attachment );
}
} );
/*
* Try to render the form only if the selection doesn't change.
* This ensures that changes of props will reflect in the form and the preview
* even when user doesn't click the Add button.
*/
widgetFrame.on( 'close', function() {
var attachment, props;
attachment = widgetFrame.state().get( 'selection' ).first().toJSON();
attachment = frame.getFirstAttachment( widgetFrame );
if ( attachment && prevAttachmentId && prevAttachmentId === attachment.id ) {
props = frame.getDisplayProps( widgetFrame );
frame.renderFormView( widgetId, props, attachment );
}
} );
widgetFrame.open( widgetId );
},
/**
* Get the first attachment of the selection in the widget frame.
*
* @param {wp.media.view.MediaFrame} widgetFrame Widget frame
* @return {object|null} JSON object of the attachment if it exists, otherwise null
*/
getAttachment: function( widgetFrame ) {
var selection = widgetFrame.state().get( 'selection' );
if ( 0 === selection.length ) {
return null;
}
return selection.first().toJSON();
},
/**
* Get display props of the current selection from the widget frame.
*
* @param {wp.media.view.MediaFrame} widgetFrame Widget frame
* @return {object|null} JSON object of the props if possible, otherwise null
*/
getDisplayProps: function( widgetFrame ) {
if ( 0 === widgetFrame.state().get( 'selection' ).length ) {
return null;
}
return widgetFrame.content.get( '.attachments-browser' ).sidebar.get( 'display' ).model.toJSON();
},
/**
* Renders the attachment details from the media modal into the widget.
*
ProTip! Use n and p to navigate between commits in a pull request.