From 1adafbe8bcb0087b08ed1f1755f43b36dac6d218 Mon Sep 17 00:00:00 2001 From: DaveLMSP Date: Tue, 7 Apr 2015 15:25:29 -0500 Subject: [PATCH 1/3] Removing redundant handler Set multiple: false on frame creation; prevent multiple selection instead of over-riding user input on select event --- js/media-modal.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/js/media-modal.js b/js/media-modal.js index 6530829..4b3f63b 100644 --- a/js/media-modal.js +++ b/js/media-modal.js @@ -23,22 +23,16 @@ 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(){ frame.state().set('filterable', 'uploaded'); }); - // When an image is selected, run the callback. - frame.on('select', function () { - // We set multiple to false so only get one image from the uploader - var attachment = frame.state().get('selection').first().toJSON(); - that.settings.cb(attachment); - }); - frame.on('open activate', function() { // Get the link/button/etc that called us var $caller = jQuery(that.settings.calling_selector); @@ -50,7 +44,7 @@ var MediaModal = function (options) { selection.add(Attachment.get($caller.data('thumbnail_id'))); } }); - + frame.open(); }; From 165eb288fb5b1948dfcb63998d2ced78c8646358 Mon Sep 17 00:00:00 2001 From: DaveLMSP Date: Tue, 7 Apr 2015 15:31:38 -0500 Subject: [PATCH 2/3] Fixing attachment selection Retrieve selected attachment via AJAX so details will populate even if attachment isn't in initial library query. Also overload library sort function so that a selected attachment will always show first in the library. Lastly, only add attachment on 'open' event as activate is redundant. --- js/media-modal.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/js/media-modal.js b/js/media-modal.js index 4b3f63b..c4300c0 100644 --- a/js/media-modal.js +++ b/js/media-modal.js @@ -33,15 +33,34 @@ var MediaModal = function (options) { frame.state().set('filterable', 'uploaded'); }); - 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 ); } }); From 728073608674fd4af634d9c13b5684a7abb4b97a Mon Sep 17 00:00:00 2001 From: DaveLMSP Date: Thu, 16 Apr 2015 10:36:41 -0500 Subject: [PATCH 3/3] Restoring callback Restoring select event handler; needed to save attachment --- js/media-modal.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/js/media-modal.js b/js/media-modal.js index c4300c0..037163e 100644 --- a/js/media-modal.js +++ b/js/media-modal.js @@ -33,6 +33,13 @@ var MediaModal = function (options) { frame.state().set('filterable', 'uploaded'); }); + // When an image is selected, run the callback. + frame.on('select', function () { + // We set multiple to false so only get one image from the uploader + var attachment = frame.state().get('selection').first().toJSON(); + that.settings.cb(attachment); + }); + frame.on( 'open', function() { // Get the link/button/etc that called us var $caller = jQuery( that.settings.calling_selector );