Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions js/media-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ var MediaModal = function (options) {
},
library : {
type : 'image'
}
},
multiple: false
});

// Set filterable state to uploaded to get select to show (setting this
// when creating the frame doesn't work)
frame.on('toolbar:create:select', function(){
Expand All @@ -39,18 +40,37 @@ var MediaModal = function (options) {
that.settings.cb(attachment);
});

frame.on('open activate', function() {
frame.on( 'open', function() {
// Get the link/button/etc that called us
var $caller = jQuery(that.settings.calling_selector);
var $caller = jQuery( that.settings.calling_selector );

// Select the thumbnail if we have one
if ($caller.data('thumbnail_id')) {
var Attachment = wp.media.model.Attachment;
var selection = frame.state().get('selection');
selection.add(Attachment.get($caller.data('thumbnail_id')));
if ( $caller.data( 'thumbnail_id' ) ) {
var Attachment = wp.media.model.Attachment.get( $caller.data( 'thumbnail_id' ) );
Attachment.fetch();
var selection = frame.state().get( 'selection' );
selection.add( Attachment );

// Overload the library's comparator to push items that are not in
// the mirrored query to the front of the aggregate collection.
var library = frame.state().get( 'library' );
var comparator = library.comparator;
library.comparator = function( a, b ) {
var aInQuery = !! this.mirroring.get( a.cid ),
bInQuery = !! this.mirroring.get( b.cid );

if ( ! aInQuery && bInQuery ) {
return -1;
} else if ( aInQuery && ! bInQuery ) {
return 1;
} else {
return comparator.apply( this, arguments );
}
};
library.observe( selection );
}
});

frame.open();
};

Expand Down