From d8d39ff7648ad825f567ca7a2c7cd88b6d4860e4 Mon Sep 17 00:00:00 2001 From: Mark Jaquith Date: Wed, 5 Dec 2012 09:43:10 +0000 Subject: [PATCH] When setting the featured image from the dedicated meta box, only show the featured image section in the media chooser. props koopersmith. fixes #22731 * Less distracting * Some of these sections won't apply for CPTs without an editor git-svn-id: http://core.svn.wordpress.org/trunk@23069 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/js/media-editor.js | 118 +++++++++++++++++++-------------- wp-includes/js/media-views.js | 44 +++++++----- 2 files changed, 97 insertions(+), 65 deletions(-) diff --git a/wp-includes/js/media-editor.js b/wp-includes/js/media-editor.js index 44916a13c5..60f6d4b4fc 100644 --- a/wp-includes/js/media-editor.js +++ b/wp-includes/js/media-editor.js @@ -320,6 +320,74 @@ }; }()); + wp.media.featuredImage = { + get: function() { + return wp.media.view.settings.post.featuredImageId; + }, + + set: function( id ) { + var settings = wp.media.view.settings; + + settings.post.featuredImageId = id; + + wp.media.post( 'set-post-thumbnail', { + json: true, + post_id: settings.post.id, + thumbnail_id: settings.post.featuredImageId, + _wpnonce: settings.post.nonce + }).done( function( html ) { + $( '.inside', '#postimagediv' ).html( html ); + }); + }, + + frame: function() { + if ( this._frame ) + return this._frame; + + this._frame = wp.media({ + state: 'featured-image', + states: [ new wp.media.controller.FeaturedImage() ] + }); + + this._frame.on( 'toolbar:create:featured-image', function( toolbar ) { + this.createSelectToolbar( toolbar, { + text: wp.media.view.l10n.setFeaturedImage + }); + }, this._frame ); + + this._frame.state('featured-image').on( 'select', this.select ); + return this._frame; + }, + + select: function() { + var settings = wp.media.view.settings, + selection = this.get('selection').single(); + + if ( ! settings.post.featuredImageId ) + return; + + wp.media.featuredImage.set( selection ? selection.id : -1 ); + }, + + init: function() { + // Open the content media manager to the 'featured image' tab when + // the post thumbnail is clicked. + $('#postimagediv').on( 'click', '#set-post-thumbnail', function( event ) { + event.preventDefault(); + // Stop propagation to prevent thickbox from activating. + event.stopPropagation(); + + wp.media.featuredImage.frame().open(); + + // Update the featured image id when the 'remove' link is clicked. + }).on( 'click', '#remove-post-thumbnail', function() { + wp.media.view.settings.post.featuredImageId = -1; + }); + } + }; + + $( wp.media.featuredImage.init ); + wp.media.editor = { insert: function( h ) { var mce = typeof(tinymce) != 'undefined', @@ -443,24 +511,7 @@ } }, this ); - workflow.state('featured-image').on( 'select', function() { - var settings = wp.media.view.settings, - selection = this.get('selection').single(); - - if ( ! settings.post.featuredImageId ) - return; - - settings.post.featuredImageId = selection ? selection.id : -1; - wp.media.post( 'set-post-thumbnail', { - json: true, - post_id: settings.post.id, - thumbnail_id: settings.post.featuredImageId, - _wpnonce: settings.post.nonce - }).done( function( html ) { - $( '.inside', '#postimagediv' ).html( html ); - }); - }); - + workflow.state('featured-image').on( 'select', wp.media.featuredImage.select ); workflow.setState( workflow.options.state ); return workflow; }, @@ -586,37 +637,6 @@ wp.media.editor.open( editor ); }); - - // Open the content media manager to the 'featured image' tab when - // the post thumbnail is clicked. - $('#postimagediv').on( 'click', '#set-post-thumbnail', function( event ) { - event.preventDefault(); - // Stop propagation to prevent thickbox from activating. - event.stopPropagation(); - - // Always get the 'content' frame, since this is tailored to post.php. - var frame = wp.media.editor.add('content'), - initialState = frame.state().id, - escape; - - escape = function() { - // Only run this event once. - this.off( 'escape', escape ); - - // If we're still on the 'featured-image' state, restore - // the initial state. - if ( 'featured-image' === this.state().id ) - this.setState( initialState ); - }; - - frame.on( 'escape', escape, frame ); - - frame.setState('featured-image').open(); - - // Update the featured image id when the 'remove' link is clicked. - }).on( 'click', '#remove-post-thumbnail', function() { - wp.media.view.settings.post.featuredImageId = -1; - }); } }; diff --git a/wp-includes/js/media-views.js b/wp-includes/js/media-views.js index c47c1d5fc0..c5bf73fa51 100644 --- a/wp-includes/js/media-views.js +++ b/wp-includes/js/media-views.js @@ -289,7 +289,7 @@ this.frame.router.render( mode ); view = router.get(); - if ( view.select ) + if ( view && view.select ) view.select( this.frame.content.mode() ); }, @@ -304,7 +304,7 @@ menu.mode( mode ); view = menu.get(); - if ( view.select ) + if ( view && view.select ) view.select( this.id ); }, @@ -357,6 +357,7 @@ sidebar: 'settings', content: 'upload', router: 'browse', + menu: 'default', searchable: true, filterable: false, sortable: true, @@ -638,7 +639,6 @@ id: 'featured-image', filterable: 'uploaded', multiple: false, - menu: 'main', toolbar: 'featured-image', title: l10n.featuredImageTitle, priority: 60 @@ -676,6 +676,17 @@ }, activate: function() { + this.updateSelection(); + this.frame.on( 'open', this.updateSelection, this ); + media.controller.Library.prototype.activate.apply( this, arguments ); + }, + + deactivate: function() { + this.frame.off( 'open', this.updateSelection, this ); + media.controller.Library.prototype.deactivate.apply( this, arguments ); + }, + + updateSelection: function() { var selection = this.get('selection'), id = media.view.settings.post.featuredImageId, attachment; @@ -686,7 +697,6 @@ } selection.reset( attachment ? [ attachment ] : [] ); - media.controller.Library.prototype.activate.apply( this, arguments ); } }); @@ -697,7 +707,7 @@ defaults: { id: 'embed', url: '', - menu: 'main', + menu: 'default', content: 'embed', toolbar: 'main-embed', type: 'link', @@ -1200,6 +1210,9 @@ model.frame = this; model.trigger('ready'); }, this ); + + if ( this.options.states ) + this.states.add( this.options.states ); }, reset: function() { @@ -1263,6 +1276,9 @@ // Bind default title creation. this.on( 'title:create:default', this.createTitle, this ); this.title.mode('default'); + + // Bind default menu. + this.on( 'menu:create:default', this.createMenu, this ); }, render: function() { @@ -1319,12 +1335,12 @@ src: tabUrl + '&tab=' + id, title: title, content: 'iframe', - menu: 'main' + menu: 'default' }, options ) ); }, this ); this.on( 'content:create:iframe', this.iframeContent, this ); - this.on( 'menu:render:main', this.iframeMenu, this ); + this.on( 'menu:render:default', this.iframeMenu, this ); this.on( 'open', this.hijackThickbox, this ); this.on( 'close', this.restoreThickbox, this ); }, @@ -1418,6 +1434,9 @@ createStates: function() { var options = this.options; + if ( this.options.states ) + return; + // Add the default states. this.states.add([ // Main states. @@ -1425,7 +1444,6 @@ selection: options.selection, library: media.query( options.library ), multiple: options.multiple, - menu: 'main', title: options.title, priority: 20 }) @@ -1433,7 +1451,6 @@ }, bindHandlers: function() { - this.on( 'menu:create:main', this.createMenu, this ); this.on( 'router:create:browse', this.createRouter, this ); this.on( 'router:render:browse', this.browseRouter, this ); this.on( 'content:create:browse', this.browseContent, this ); @@ -1519,7 +1536,6 @@ id: 'insert', title: l10n.insertMediaTitle, priority: 20, - menu: 'main', toolbar: 'main-insert', filterable: 'all', library: media.query( options.library ), @@ -1538,7 +1554,6 @@ id: 'gallery', title: l10n.createGalleryTitle, priority: 40, - menu: 'main', toolbar: 'main-gallery', filterable: 'uploaded', multiple: 'add', @@ -1568,10 +1583,7 @@ if ( media.view.settings.post.featuredImageId ) { - this.states.add( new media.controller.FeaturedImage({ - controller: this, - menu: 'main' - }) ); + this.states.add( new media.controller.FeaturedImage() ); } }, @@ -1585,7 +1597,7 @@ var handlers = { menu: { - 'main': 'mainMenu', + 'default': 'mainMenu', 'gallery': 'galleryMenu' },