<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[23024] trunk: Media: Disable 'Insert gallery' and 'Set featured image' if nothing is queued.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/23024">23024</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2012-12-04 16:21:57 +0000 (Tue, 04 Dec 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Media: Disable 'Insert gallery' and 'Set featured image' if nothing is queued. Converts selection toolbars to use the newer view manager. props koopersmith, fixes <a href="http://core.trac.wordpress.org/ticket/22714">#22714</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminjscustombackgroundjs">trunk/wp-admin/js/custom-background.js</a></li>
<li><a href="#trunkwpadminjscustomheaderjs">trunk/wp-admin/js/custom-header.js</a></li>
<li><a href="#trunkwpincludescssmediaviewscss">trunk/wp-includes/css/media-views.css</a></li>
<li><a href="#trunkwpincludesjsmediaviewsjs">trunk/wp-includes/js/media-views.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminjscustombackgroundjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/js/custom-background.js (23023 => 23024)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/js/custom-background.js        2012-12-04 16:18:53 UTC (rev 23023)
+++ trunk/wp-admin/js/custom-background.js        2012-12-04 16:21:57 UTC (rev 23024)
</span><span class="lines">@@ -25,39 +25,50 @@
</span><span class="cx"> 
</span><span class="cx">                         event.preventDefault();
</span><span class="cx"> 
</span><ins>+                        // If the media frame already exists, reopen it.
</ins><span class="cx">                         if ( frame ) {
</span><span class="cx">                                 frame.open();
</span><span class="cx">                                 return;
</span><span class="cx">                         }
</span><span class="cx"> 
</span><ins>+                        // Create the media frame.
</ins><span class="cx">                         frame = wp.media({
</span><del>-                                title:     $el.data('choose'),
-                                library:   {
</del><ins>+                                // Set the title of the modal.
+                                title: $el.data('choose'),
+
+                                // Tell the modal to show only images.
+                                library: {
</ins><span class="cx">                                         type: 'image'
</span><ins>+                                },
+
+                                // Customize the submit button.
+                                button: {
+                                        // Set the text of the button.
+                                        text: $el.data('update'),
+                                        // Tell the button not to close the modal, since we're
+                                        // going to refresh the page when the image is selected.
+                                        close: false
</ins><span class="cx">                                 }
</span><span class="cx">                         });
</span><span class="cx"> 
</span><del>-                        frame.on( 'toolbar:render:select', function( view ) {
-                                view.set({
-                                        select: {
-                                                style: 'primary',
-                                                text:  $el.data('update'),
</del><ins>+                        // When an image is selected, run a callback.
+                        frame.on( 'select', function() {
+                                // Grab the selected attachment.
+                                var attachment = frame.state().get('selection').first();
</ins><span class="cx"> 
</span><del>-                                                click: function() {
-                                                        var attachment = frame.state().get('selection').first();
-                                                        $.post( ajaxurl, {
-                                                                action: 'set-background-image',
-                                                                attachment_id: attachment.id,
-                                                                size: 'full'
-                                                        }, function() {
-                                                                window.location.reload();
-                                                        });
-                                                }
-                                        }
</del><ins>+                                // Run an AJAX request to set the background image.
+                                $.post( ajaxurl, {
+                                        action: 'set-background-image',
+                                        attachment_id: attachment.id,
+                                        size: 'full'
+                                }).done( function() {
+                                        // When the request completes, reload the window.
+                                        window.location.reload();
</ins><span class="cx">                                 });
</span><span class="cx">                         });
</span><span class="cx"> 
</span><del>-                        frame.setState('library').open();
</del><ins>+                        // Finally, open the modal.
+                        frame.open();
</ins><span class="cx">                 });
</span><span class="cx">         });
</span><span class="cx"> })(jQuery);
</span><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkwpadminjscustomheaderjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/js/custom-header.js (23023 => 23024)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/js/custom-header.js        2012-12-04 16:18:53 UTC (rev 23023)
+++ trunk/wp-admin/js/custom-header.js        2012-12-04 16:21:57 UTC (rev 23024)
</span><span class="lines">@@ -18,30 +18,37 @@
</span><span class="cx">                         var $el = $(this);
</span><span class="cx">                         event.preventDefault();
</span><span class="cx"> 
</span><ins>+                        // Create the media frame.
</ins><span class="cx">                         frame = wp.media({
</span><del>-                                title:     $el.data('choose'),
-                                library:   {
</del><ins>+                                // Set the title of the modal.
+                                title: $el.data('choose'),
+
+                                // Tell the modal to show only images.
+                                library: {
</ins><span class="cx">                                         type: 'image'
</span><ins>+                                },
+
+                                // Customize the submit button.
+                                button: {
+                                        // Set the text of the button.
+                                        text: $el.data('update'),
+                                        // Tell the button not to close the modal, since we're
+                                        // going to refresh the page when the image is selected.
+                                        close: false
</ins><span class="cx">                                 }
</span><span class="cx">                         });
</span><span class="cx"> 
</span><del>-                        frame.on( 'toolbar:render:select', function( view ) {
-                                view.set({
-                                        select: {
-                                                style: 'primary',
-                                                text:  $el.data('update'),
</del><ins>+                        // When an image is selected, run a callback.
+                        frame.on( 'select', function() {
+                                // Grab the selected attachment.
+                                var attachment = frame.state().get('selection').first(),
+                                        link = $el.data('updateLink');
</ins><span class="cx"> 
</span><del>-                                                click: function() {
-                                                        var attachment = frame.state().get('selection').first(),
-                                                                link = $el.data('updateLink');
-
-                                                        window.location = link + '&amp;file=' + attachment.id;
-                                                }
-                                        }
-                                });
</del><ins>+                                // Tell the browser to navigate to the crop step.
+                                window.location = link + '&amp;file=' + attachment.id;
</ins><span class="cx">                         });
</span><span class="cx"> 
</span><del>-                        frame.setState('library').open();
</del><ins>+                        frame.open();
</ins><span class="cx">                 });
</span><span class="cx">         });
</span><span class="cx"> }(jQuery));
</span></span></pre></div>
<a id="trunkwpincludescssmediaviewscss"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/css/media-views.css (23023 => 23024)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/css/media-views.css        2012-12-04 16:18:53 UTC (rev 23023)
+++ trunk/wp-includes/css/media-views.css        2012-12-04 16:21:57 UTC (rev 23024)
</span><span class="lines">@@ -1174,6 +1174,10 @@
</span><span class="cx">         background: red;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+.media-selection .selection-view {
+        display: inline-block;
+}
+
</ins><span class="cx"> .media-selection .attachments {
</span><span class="cx">         display: inline-block;
</span><span class="cx">         height: 48px;
</span></span></pre></div>
<a id="trunkwpincludesjsmediaviewsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-views.js (23023 => 23024)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-views.js        2012-12-04 16:18:53 UTC (rev 23023)
+++ trunk/wp-includes/js/media-views.js        2012-12-04 16:21:57 UTC (rev 23024)
</span><span class="lines">@@ -389,10 +389,6 @@
</span><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 activate: function() {
</span><del>-                        var library = this.get('library'),
-                                selection = this.get('selection'),
-                                mode;
-
</del><span class="cx">                         if ( this.get('syncLastSelection') ) {
</span><span class="cx">                                 this.getLastSelection();
</span><span class="cx">                         }
</span><span class="lines">@@ -404,7 +400,7 @@
</span><span class="cx"> 
</span><span class="cx">                         wp.Uploader.queue.on( 'add', this.uploading, this );
</span><span class="cx"> 
</span><del>-                        selection.on( 'add remove reset', this.refreshSelection, this );
</del><ins>+                        this.get('selection').on( 'add remove reset', this.refreshContent, this );
</ins><span class="cx"> 
</span><span class="cx">                         this.on( 'insert', this._insertDisplaySettings, this );
</span><span class="cx"> 
</span><span class="lines">@@ -490,12 +486,6 @@
</span><span class="cx">                         selection.reset( lastSelection.toArray() ).single( lastSelection.single() );
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                refreshSelection: function() {
-                        this.frame.toolbar.get().refresh();
-                        this.trigger( 'refresh:selection', this, this.get('selection') );
-                        this.refreshContent();
-                },
-
</del><span class="cx">                 refreshContent: function() {
</span><span class="cx">                         var selection = this.get('selection'),
</span><span class="cx">                                 frame = this.frame,
</span><span class="lines">@@ -1317,7 +1307,7 @@
</span><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 createToolbar: function( toolbar ) {
</span><del>-                        menu.view = new media.view.Toolbar({
</del><ins>+                        toolbar.view = new media.view.Toolbar({
</ins><span class="cx">                                 controller: this
</span><span class="cx">                         });
</span><span class="cx">                 },
</span><span class="lines">@@ -1425,7 +1415,8 @@
</span><span class="cx">                         _.defaults( this.options, {
</span><span class="cx">                                 selection: [],
</span><span class="cx">                                 library:   {},
</span><del>-                                multiple:  false
</del><ins>+                                multiple:  false,
+                                state:    'library'
</ins><span class="cx">                         });
</span><span class="cx"> 
</span><span class="cx">                         this.createSelection();
</span><span class="lines">@@ -1468,8 +1459,6 @@
</span><span class="cx">                         this.on( 'content:create:browse', this.browseContent, this );
</span><span class="cx">                         this.on( 'content:render:upload', this.uploadContent, this );
</span><span class="cx">                         this.on( 'toolbar:create:select', this.createSelectToolbar, this );
</span><del>-
-                        this.on( 'refresh:selection', this.refreshSelectToolbar, this );
</del><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 // Routers
</span><span class="lines">@@ -1517,43 +1506,10 @@
</span><span class="cx"> 
</span><span class="cx">                 // Toolbars
</span><span class="cx">                 createSelectToolbar: function( toolbar, options ) {
</span><del>-                        options = _.defaults( options || {}, {
-                                event:  'select',
-                                silent: false,
-                                state:  false
-                        });
</del><ins>+                        options = options || this.options.button || {};
+                        options.controller = this;
</ins><span class="cx"> 
</span><del>-                        toolbar.view = new media.view.Toolbar({
-                                controller: this,
-                                silent:     options.silent,
-
-                                items: {
-                                        select: {
-                                                style:    'primary',
-                                                text:     l10n.select,
-                                                priority: 80,
-
-                                                click: function() {
-                                                        var controller = this.controller;
-
-                                                        controller.close();
-                                                        controller.state().trigger( options.event );
-                                                        controller.reset();
-                                                        if ( options.state )
-                                                                controller.setState( options.state );
-                                                }
-                                        }
-                                }
-                        });
-                },
-
-                refreshSelectToolbar: function() {
-                        var selection = this.state().get('selection');
-
-                        if ( ! selection || 'select' !== this.toolbar.mode() )
-                                return;
-
-                        this.toolbar.get().get('select').model.set( 'disabled', ! selection.length );
</del><ins>+                        toolbar.view = new media.view.Toolbar.Select( options );
</ins><span class="cx">                 }
</span><span class="cx">         });
</span><span class="cx"> 
</span><span class="lines">@@ -1564,7 +1520,8 @@
</span><span class="cx">                 initialize: function() {
</span><span class="cx">                         _.defaults( this.options, {
</span><span class="cx">                                 multiple:  true,
</span><del>-                                editing:   false
</del><ins>+                                editing:   false,
+                                state:    'insert'
</ins><span class="cx">                         });
</span><span class="cx"> 
</span><span class="cx">                         media.view.MediaFrame.Select.prototype.initialize.apply( this, arguments );
</span><span class="lines">@@ -1651,8 +1608,10 @@
</span><span class="cx">                 bindHandlers: function() {
</span><span class="cx">                         media.view.MediaFrame.Select.prototype.bindHandlers.apply( this, arguments );
</span><span class="cx">                         this.on( 'menu:create:gallery', this.createMenu, this );
</span><del>-                        this.on( 'toolbar:create:main-insert', this.createSelectionToolbar, this );
-                        this.on( 'toolbar:create:main-gallery', this.createSelectionToolbar, this );
</del><ins>+                        this.on( 'toolbar:create:main-insert', this.createToolbar, this );
+                        this.on( 'toolbar:create:main-gallery', this.createToolbar, this );
+                        this.on( 'toolbar:create:featured-image', this.featuredImageToolbar, this );
+                        this.on( 'toolbar:create:main-embed', this.mainEmbedToolbar, this );
</ins><span class="cx"> 
</span><span class="cx">                         var handlers = {
</span><span class="cx">                                         menu: {
</span><span class="lines">@@ -1668,8 +1627,6 @@
</span><span class="cx">                                         toolbar: {
</span><span class="cx">                                                 'main-insert':      'mainInsertToolbar',
</span><span class="cx">                                                 'main-gallery':     'mainGalleryToolbar',
</span><del>-                                                'main-embed':       'mainEmbedToolbar',
-                                                'featured-image':   'featuredImageToolbar',
</del><span class="cx">                                                 'gallery-edit':     'galleryEditToolbar',
</span><span class="cx">                                                 'gallery-add':      'galleryAddToolbar'
</span><span class="cx">                                         }
</span><span class="lines">@@ -1757,21 +1714,32 @@
</span><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 // Toolbars
</span><del>-                createSelectionToolbar: function( toolbar ) {
-                        toolbar.view = new media.view.Toolbar.Selection({
</del><ins>+                selectionStatusToolbar: function( view ) {
+                        var editable = this.state().get('editable');
+
+                        view.set( 'selection', new media.view.Selection({
</ins><span class="cx">                                 controller: this,
</span><del>-                                editable:   this.state().get('editable')
-                        });
</del><ins>+                                collection: this.state().get('selection'),
+                                priority:   -40,
+
+                                // If the selection is editable, pass the callback to
+                                // switch the content mode.
+                                editable: editable &amp;&amp; function() {
+                                        this.controller.content.mode('edit-selection');
+                                }
+                        }).render() );
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 mainInsertToolbar: function( view ) {
</span><span class="cx">                         var controller = this;
</span><span class="cx"> 
</span><del>-                        view.button = 'insert';
</del><ins>+                        this.selectionStatusToolbar( view );
+
</ins><span class="cx">                         view.set( 'insert', {
</span><span class="cx">                                 style:    'primary',
</span><span class="cx">                                 priority: 80,
</span><span class="cx">                                 text:     l10n.insertIntoPost,
</span><ins>+                                requires: { selection: true },
</ins><span class="cx"> 
</span><span class="cx">                                 click: function() {
</span><span class="cx">                                         var state = controller.state(),
</span><span class="lines">@@ -1786,11 +1754,13 @@
</span><span class="cx">                 mainGalleryToolbar: function( view ) {
</span><span class="cx">                         var controller = this;
</span><span class="cx"> 
</span><del>-                        view.button = 'gallery';
</del><ins>+                        this.selectionStatusToolbar( view );
+
</ins><span class="cx">                         view.set( 'gallery', {
</span><span class="cx">                                 style:    'primary',
</span><span class="cx">                                 text:     l10n.createNewGallery,
</span><span class="cx">                                 priority: 60,
</span><ins>+                                requires: { selection: true },
</ins><span class="cx"> 
</span><span class="cx">                                 click: function() {
</span><span class="cx">                                         var selection = controller.state().get('selection'),
</span><span class="lines">@@ -1807,20 +1777,17 @@
</span><span class="cx">                         });
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                featuredImageToolbar: function() {
-                        this.toolbar.set( new media.view.Toolbar.Select({
-                                controller: this,
-                                text:       l10n.setFeaturedImage,
-                                state:      this.options.state || 'upload'
-                        }) );
</del><ins>+                featuredImageToolbar: function( toolbar ) {
+                        this.createSelectToolbar( toolbar, {
+                                text:  l10n.setFeaturedImage,
+                                state: this.options.state || 'upload'
+                        });
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                mainEmbedToolbar: function() {
-                        this.toolbar.set( new media.view.Toolbar.Embed({
</del><ins>+                mainEmbedToolbar: function( toolbar ) {
+                        toolbar.view = new media.view.Toolbar.Embed({
</ins><span class="cx">                                 controller: this
</span><del>-                        }) );
-
-                        this.$el.removeClass('hide-toolbar');
</del><ins>+                        });
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 galleryEditToolbar: function() {
</span><span class="lines">@@ -1832,6 +1799,7 @@
</span><span class="cx">                                                 style:    'primary',
</span><span class="cx">                                                 text:     editing ? l10n.updateGallery : l10n.insertGallery,
</span><span class="cx">                                                 priority: 80,
</span><ins>+                                                requires: { library: true },
</ins><span class="cx"> 
</span><span class="cx">                                                 click: function() {
</span><span class="cx">                                                         var controller = this.controller,
</span><span class="lines">@@ -2220,53 +2188,42 @@
</span><span class="cx">                 className: 'media-toolbar',
</span><span class="cx"> 
</span><span class="cx">                 initialize: function() {
</span><del>-                        this._views     = {};
-                        this.$primary   = $('&lt;div class=&quot;media-toolbar-primary&quot; /&gt;').prependTo( this.$el );
-                        this.$secondary = $('&lt;div class=&quot;media-toolbar-secondary&quot; /&gt;').prependTo( this.$el );
</del><ins>+                        var state = this.controller.state(),
+                                selection = this.selection = state.get('selection'),
+                                library = this.library = state.get('library');
</ins><span class="cx"> 
</span><ins>+                        this._views = {};
+
+                        // The toolbar is composed of two `PriorityList` views.
+                        this.primary   = new media.view.PriorityList();
+                        this.secondary = new media.view.PriorityList();
+                        this.primary.$el.addClass('media-toolbar-primary');
+                        this.secondary.$el.addClass('media-toolbar-secondary');
+
+                        this.views.set([ this.secondary, this.primary ]);
+
</ins><span class="cx">                         if ( this.options.items )
</span><span class="cx">                                 this.set( this.options.items, { silent: true });
</span><span class="cx"> 
</span><span class="cx">                         if ( ! this.options.silent )
</span><span class="cx">                                 this.render();
</span><ins>+
+                        selection.on( 'add remove reset', this.refresh, this );
+                        library.on( 'add remove reset', this.refresh, this );
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                destroy: function() {
-                        this.remove();
-
-                        if ( this.model )
-                                this.model.off( null, null, this );
-
-                        if ( this.collection )
-                                this.collection.off( null, null, this );
-
-                        this.controller.off( null, null, this );
-
-                        _.each( this._views, function( view ) {
-                                if ( view.destroy )
-                                        view.destroy();
-                        });
</del><ins>+                dispose: function() {
+                        this.selection.off( null, null, this );
+                        this.library.off( null, null, this );
+                        return media.View.prototype.dispose.apply( this, arguments );
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                render: function() {
-                        var views = _.chain( this._views ).sortBy( function( view ) {
-                                return view.options.priority || 10;
-                        }).groupBy( function( view ) {
-                                return ( view.options.priority || 10 ) &gt; 0 ? 'primary' : 'secondary';
-                        }).value();
-
-                        // Make sure to detach the elements we want to reuse.
-                        // Otherwise, `jQuery.html()` will unbind their events.
-                        $( _.pluck( this._views, 'el' ) ).detach();
-                        this.$primary.html( _.pluck( views.primary || [], 'el' ) );
-                        this.$secondary.html( _.pluck( views.secondary || [], 'el' ) );
-
</del><ins>+                ready: function() {
</ins><span class="cx">                         this.refresh();
</span><del>-
-                        return this;
</del><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 set: function( id, view, options ) {
</span><ins>+                        var list;
</ins><span class="cx">                         options = options || {};
</span><span class="cx"> 
</span><span class="cx">                         // Accept an object with an `id` : `view` mapping.
</span><span class="lines">@@ -2284,10 +2241,14 @@
</span><span class="cx">                                 view.controller = view.controller || this.controller;
</span><span class="cx"> 
</span><span class="cx">                                 this._views[ id ] = view;
</span><ins>+
+                                list = view.options.priority &lt; 0 ? 'secondary' : 'primary';
+                                this[ list ].set( id, view, options );
</ins><span class="cx">                         }
</span><span class="cx"> 
</span><span class="cx">                         if ( ! options.silent )
</span><del>-                                this.render();
</del><ins>+                                this.refresh();
+
</ins><span class="cx">                         return this;
</span><span class="cx">                 },
</span><span class="cx"> 
</span><span class="lines">@@ -2297,12 +2258,34 @@
</span><span class="cx"> 
</span><span class="cx">                 unset: function( id, options ) {
</span><span class="cx">                         delete this._views[ id ];
</span><ins>+                        this.primary.unset( id, options );
+                        this.secondary.unset( id, options );
+
</ins><span class="cx">                         if ( ! options || ! options.silent )
</span><del>-                                this.render();
</del><ins>+                                this.refresh();
</ins><span class="cx">                         return this;
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                refresh: function() {}
</del><ins>+                refresh: function() {
+                        var state = this.controller.state(),
+                                library = state.get('library'),
+                                selection = state.get('selection');
+
+                        _.each( this._views, function( button ) {
+                                if ( ! button.model || ! button.options || ! button.options.requires )
+                                        return;
+
+                                var requires = button.options.requires,
+                                        disabled = false;
+
+                                if ( requires.selection &amp;&amp; ! selection.length )
+                                        disabled = true;
+                                else if ( requires.library &amp;&amp; ! library.length )
+                                        disabled = true;
+
+                                button.model.set( 'disabled', disabled );
+                        });
+                }
</ins><span class="cx">         });
</span><span class="cx"> 
</span><span class="cx">         // wp.media.view.Toolbar.Select
</span><span class="lines">@@ -2320,7 +2303,12 @@
</span><span class="cx">                                 state: false,
</span><span class="cx">                                 reset: true,
</span><span class="cx">                                 close: true,
</span><del>-                                text:  l10n.select
</del><ins>+                                text:  l10n.select,
+
+                                // Does the button rely on the selection?
+                                requires: {
+                                        selection: true
+                                }
</ins><span class="cx">                         });
</span><span class="cx"> 
</span><span class="cx">                         options.items = _.defaults( options.items || {}, {
</span><span class="lines">@@ -2328,7 +2316,8 @@
</span><span class="cx">                                         style:    'primary',
</span><span class="cx">                                         text:     options.text,
</span><span class="cx">                                         priority: 80,
</span><del>-                                        click:    this.clickSelect
</del><ins>+                                        click:    this.clickSelect,
+                                        requires: options.requires
</ins><span class="cx">                                 }
</span><span class="cx">                         });
</span><span class="cx"> 
</span><span class="lines">@@ -2365,45 +2354,16 @@
</span><span class="cx">                         this.controller.state().props.on( 'change:url', this.refresh, this );
</span><span class="cx">                 },
</span><span class="cx"> 
</span><ins>+                dispose: function() {
+                        this.controller.state().props.off( 'change:url', this.refresh, this );
+                        media.view.Toolbar.Select.prototype.dispose.apply( this, arguments );
+                },
+
</ins><span class="cx">                 refresh: function() {
</span><span class="cx">                         var url = this.controller.state().props.get('url');
</span><span class="cx">                         this.get('select').model.set( 'disabled', ! url || /^https?:\/\/$/.test(url) );
</span><del>-                }
-        });
</del><span class="cx"> 
</span><del>-        // wp.media.view.Toolbar.Selection
-        // -------------------------------
-        media.view.Toolbar.Selection = media.view.Toolbar.extend({
-                button: 'insert',
-
-                initialize: function() {
-                        var controller = this.controller;
-
-                        this.options.items = _.defaults( this.options.items || {}, {
-                                selection: new media.view.Selection({
-                                        controller: controller,
-                                        collection: controller.state().get('selection'),
-                                        priority:   -40,
-
-                                        // If the selection is editable, pass the callback to
-                                        // switch the content mode.
-                                        editable: this.options.editable &amp;&amp; function() {
-                                                this.controller.content.mode('edit-selection');
-                                        }
-                                }).render()
-                        });
-
-                        media.view.Toolbar.prototype.initialize.apply( this, arguments );
-                },
-
-                refresh: function() {
-                        var selection = this.controller.state().get('selection'),
-                                button = this.get( this.button );
-
-                        if ( ! button )
-                                return;
-
-                        button.model.set( 'disabled', ! selection.length );
</del><ins>+                        media.view.Toolbar.Select.prototype.refresh.apply( this, arguments );
</ins><span class="cx">                 }
</span><span class="cx">         });
</span><span class="cx"> 
</span><span class="lines">@@ -2516,8 +2476,6 @@
</span><span class="cx">                                 this.render();
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                destroy: this.dispose,
-
</del><span class="cx">                 set: function( id, view, options ) {
</span><span class="cx">                         var priority, views, index;
</span><span class="cx"> 
</span><span class="lines">@@ -3649,24 +3607,12 @@
</span><span class="cx">                                 AttachmentView: media.view.Attachment.Selection
</span><span class="cx">                         });
</span><span class="cx"> 
</span><ins>+                        this.views.set( '.selection-view', this.attachments );
</ins><span class="cx">                         this.collection.on( 'add remove reset', this.refresh, this );
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                destroy: function() {
-                        this.remove();
-                        this.collection.off( 'add remove reset', this.refresh, this );
-                        this.attachments.destroy();
-                },
-
-                render: function() {
-                        this.attachments.$el.detach();
-                        this.attachments.render();
-
-                        this.$el.html( this.template( this.options ) );
-
-                        this.$('.selection-view').replaceWith( this.attachments.$el );
</del><ins>+                ready: function() {
</ins><span class="cx">                         this.refresh();
</span><del>-                        return this;
</del><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 refresh: function() {
</span><span class="lines">@@ -3728,10 +3674,6 @@
</span><span class="cx">                         this.model.on( 'change', this.updateChanges, this );
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                destroy: function() {
-                        this.model.off( null, null, this );
-                },
-
</del><span class="cx">                 render: function() {
</span><span class="cx">                         this.$el.html( this.template( _.defaults({
</span><span class="cx">                                 model: this.model.toJSON()
</span><span class="lines">@@ -3934,10 +3876,6 @@
</span><span class="cx">                         this.model.on( 'change:compat', this.render, this );
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                destroy: function() {
-                        this.model.off( null, null, this );
-                },
-
</del><span class="cx">                 render: function() {
</span><span class="cx">                         var compat = this.model.get('compat');
</span><span class="cx">                         if ( ! compat || ! compat.item )
</span><span class="lines">@@ -4005,8 +3943,6 @@
</span><span class="cx">                 settings: function( view ) {
</span><span class="cx">                         view.render();
</span><span class="cx">                         this._settings.$el.replaceWith( view.$el );
</span><del>-                        if ( this._settings.destroy )
-                                this._settings.destroy();
</del><span class="cx">                         this._settings.remove();
</span><span class="cx">                         this._settings = view;
</span><span class="cx">                 },
</span><span class="lines">@@ -4055,10 +3991,6 @@
</span><span class="cx">                         this.model.on( 'change:url', this.render, this );
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                destroy: function() {
-                        this.model.off( null, null, this );
-                },
-
</del><span class="cx">                 render: function() {
</span><span class="cx">                         var $input = this.$input;
</span><span class="cx"> 
</span><span class="lines">@@ -4101,11 +4033,6 @@
</span><span class="cx">                         this.model.on( 'change:url', this.updateImage, this );
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                destroy: function() {
-                        this.model.off( null, null, this );
-                        media.view.Settings.AttachmentDisplay.prototype.destroy.apply( this, arguments );
-                },
-
</del><span class="cx">                 updateImage: function() {
</span><span class="cx">                         this.$('img').attr( 'src', this.model.get('url') );
</span><span class="cx">                 }
</span></span></pre>
</div>
</div>

</body>
</html>