<!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>[22494] trunk/wp-includes: Media: Add a basic starting frame for upload/library selection.</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/22494">22494</a></dd>
<dt>Author</dt> <dd>koopersmith</dd>
<dt>Date</dt> <dd>2012-11-09 09:47:12 +0000 (Fri, 09 Nov 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Media: Add a basic starting frame for upload/library selection.

`media.view.MediaFrame.Select` is a frame class with a handful of sensible defaults to handle selecting and uploading items in the media library.

see <a href="http://core.trac.wordpress.org/ticket/21390">#21390</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesjsmediamodelsjs">trunk/wp-includes/js/media-models.js</a></li>
<li><a href="#trunkwpincludesjsmediaviewsjs">trunk/wp-includes/js/media-views.js</a></li>
<li><a href="#trunkwpincludesscriptloaderphp">trunk/wp-includes/script-loader.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesjsmediamodelsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-models.js (22493 => 22494)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-models.js        2012-11-09 06:15:25 UTC (rev 22493)
+++ trunk/wp-includes/js/media-models.js        2012-11-09 09:47:12 UTC (rev 22494)
</span><span class="lines">@@ -643,7 +643,7 @@
</span><span class="cx">                 // selected attachments, reset the selection.
</span><span class="cx">                 add: function( models, options ) {
</span><span class="cx">                         if ( ! this.multiple ) {
</span><del>-                                models = _.isArray( models ) ? _.first( models ) : models;
</del><ins>+                                models = _.isArray( models ) &amp;&amp; models.length ? _.first( models ) : models;
</ins><span class="cx">                                 this.clear( options );
</span><span class="cx">                         }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludesjsmediaviewsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-views.js (22493 => 22494)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-views.js        2012-11-09 06:15:25 UTC (rev 22493)
+++ trunk/wp-includes/js/media-views.js        2012-11-09 09:47:12 UTC (rev 22494)
</span><span class="lines">@@ -356,6 +356,7 @@
</span><span class="cx">                 refresh: function() {
</span><span class="cx">                         this.frame.$el.toggleClass( 'hide-sidebar hide-toolbar', this.get('empty') );
</span><span class="cx">                         this.content();
</span><ins>+                        this.refreshSelection();
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 _updateEmpty: function() {
</span><span class="lines">@@ -375,6 +376,9 @@
</span><span class="cx">                 toggleSelection: function( model ) {
</span><span class="cx">                         var selection = this.get('selection');
</span><span class="cx"> 
</span><ins>+                        if ( ! model )
+                                return;
+
</ins><span class="cx">                         if ( selection.has( model ) ) {
</span><span class="cx">                                 // If the model is the single model, remove it.
</span><span class="cx">                                 // If it is not the same as the single model,
</span><span class="lines">@@ -564,6 +568,7 @@
</span><span class="cx"> 
</span><span class="cx">                 reset: function() {
</span><span class="cx">                         this.states.invoke( 'trigger', 'reset' );
</span><ins>+                        return this;
</ins><span class="cx">                 }
</span><span class="cx">         });
</span><span class="cx"> 
</span><span class="lines">@@ -628,11 +633,10 @@
</span><span class="cx">                 };
</span><span class="cx">         });
</span><span class="cx"> 
</span><del>-
</del><span class="cx">         /**
</span><del>-         * wp.media.view.MediaFrame.Post
</del><ins>+         * wp.media.view.MediaFrame.Select
</ins><span class="cx">          */
</span><del>-        media.view.MediaFrame.Post = media.view.MediaFrame.extend({
</del><ins>+        media.view.MediaFrame.Select = media.view.MediaFrame.extend({
</ins><span class="cx">                 initialize: function() {
</span><span class="cx">                         media.view.MediaFrame.prototype.initialize.apply( this, arguments );
</span><span class="cx"> 
</span><span class="lines">@@ -640,8 +644,7 @@
</span><span class="cx">                                 state:     'upload',
</span><span class="cx">                                 selection: [],
</span><span class="cx">                                 library:   {},
</span><del>-                                multiple:  false,
-                                editing:   false
</del><ins>+                                multiple:  false
</ins><span class="cx">                         });
</span><span class="cx"> 
</span><span class="cx">                         this.createSelection();
</span><span class="lines">@@ -652,69 +655,158 @@
</span><span class="cx">                         this.state( this.options.state );
</span><span class="cx">                 },
</span><span class="cx"> 
</span><ins>+                createSelection: function() {
+                        var controller = this,
+                                selection = this.options.selection;
+
+                        if ( ! (selection instanceof media.model.Selection) ) {
+                                this.options.selection = new media.model.Selection( selection, {
+                                        multiple: this.options.multiple
+                                });
+                        }
+                },
+
+                createStates: function() {
+                        var options = this.options,
+                                attributes;
+
+                        attributes = {
+                                multiple: this.options.multiple,
+                                menu:     'main',
+                                toolbar:  'select'
+                        };
+
+                        // Add the default states.
+                        this.states.add([
+                                // Main states.
+                                new media.controller.Library( _.defaults({
+                                        selection: options.selection,
+                                        library:   media.query( options.library )
+                                }, attributes ) ),
+
+                                new media.controller.Upload( attributes )
+                        ]);
+                },
+
</ins><span class="cx">                 bindHandlers: function() {
</span><del>-                        var handlers = {
-                                        menu: {
-                                                main:    'mainMenu',
-                                                batch:   'batchMenu',
-                                                gallery: 'galleryMenu'
-                                        },
</del><ins>+                        this.menu.on( 'activate:main', this.mainMenu, this );
+                        this.content.on( 'activate:browse', this.browseContent, this );
+                        this.content.on( 'activate:upload', this.uploadContent, this );
+                        this.sidebar.on( 'activate:clear', this.clearSidebar, this );
+                        this.sidebar.on( 'activate:settings', this.settingsSidebar, this );
+                        this.toolbar.on( 'activate:select', this.selectToolbar, this );
</ins><span class="cx"> 
</span><del>-                                        content: {
-                                                browse: 'browseContent',
-                                                upload: 'uploadContent',
-                                                embed:  'embedContent'
-                                        },
</del><ins>+                        this.on( 'refresh:selection', this.refreshSelectToolbar, this );
+                },
</ins><span class="cx"> 
</span><del>-                                        sidebar: {
-                                                'clear':               'clearSidebar',
-                                                'settings':            'settingsSidebar',
-                                                'attachment-settings': 'attachmentSettingsSidebar'
</del><ins>+                mainMenu: function( options ) {
+                        this.menu.view( new media.view.Menu({
+                                controller: this,
+                                silent:     options &amp;&amp; options.silent,
+
+                                views: {
+                                        upload: {
+                                                text: l10n.uploadFilesTitle,
+                                                priority: 20
</ins><span class="cx">                                         },
</span><del>-
-                                        toolbar: {
-                                                'main-attachments': 'mainAttachmentsToolbar',
-                                                'main-embed':       'mainEmbedToolbar',
-                                                'batch-edit':       'batchEditToolbar',
-                                                'batch-add':        'batchAddToolbar',
-                                                'gallery-edit':     'galleryEditToolbar',
-                                                'gallery-add':      'galleryAddToolbar'
</del><ins>+                                        library: {
+                                                text: l10n.mediaLibraryTitle,
+                                                priority: 40
</ins><span class="cx">                                         }
</span><del>-                                };
</del><ins>+                                }
+                        }) );
+                },
</ins><span class="cx"> 
</span><del>-                        _.each( handlers, function( regionHandlers, region ) {
-                                _.each( regionHandlers, function( callback, handler ) {
-                                        this[ region ].on( 'activate:' + handler, this[ callback ], this );
-                                }, this );
-                        }, this );
</del><ins>+                // Content
+                browseContent: function() {
+                        var state = this.state();
</ins><span class="cx"> 
</span><del>-                        _.each(['library', 'upload'], function( id ) {
-                                this.get( id ).on( 'refresh:selection', function( state, selection ) {
-                                        var sidebar = this.sidebar;
</del><ins>+                        // Browse our library of attachments.
+                        this.content.view( new media.view.AttachmentsBrowser({
+                                controller: this,
+                                collection: state.get('library'),
+                                model:      state,
+                                sortable:   state.get('sortable'),
</ins><span class="cx"> 
</span><del>-                                        if ( ! selection.length )
-                                                sidebar.mode('clear');
-                                        else if ( selection.length === 1 )
-                                                sidebar.mode('attachment-settings');
-                                        else
-                                                sidebar.mode('settings');
-                                }, this );
-                        }, this );
</del><ins>+                                AttachmentView: state.get('AttachmentView')
+                        }).render() );
+                },
</ins><span class="cx"> 
</span><del>-                        this.sidebar.on( 'gallery-settings', this.onSidebarGallerySettings, this );
</del><ins>+                uploadContent: function() {
+                        // In the meantime, render an inline uploader.
+                        this.content.view( new media.view.UploaderInline({
+                                controller: this
+                        }).render() );
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                createSelection: function() {
-                        var controller = this,
-                                selection = this.options.selection;
</del><ins>+                // Sidebars
+                clearSidebar: function() {
+                        this.sidebar.view( new media.view.Sidebar({
+                                controller: this
+                        }) );
+                },
</ins><span class="cx"> 
</span><del>-                        if ( ! (selection instanceof media.model.Selection) ) {
-                                selection = this.options.selection = new media.model.Selection( selection, {
-                                        multiple: this.options.multiple
-                                });
-                        }
</del><ins>+                settingsSidebar: function( options ) {
+                        this.sidebar.view( new media.view.Sidebar({
+                                controller: this,
+                                silent:     options &amp;&amp; options.silent,
+
+                                views: {
+                                        details: new media.view.Attachment.Details({
+                                                controller: this,
+                                                model:      this.state().get('selection').single(),
+                                                priority:   80
+                                        }).render()
+                                }
+                        }) );
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><ins>+                // Toolbars
+                selectToolbar: function() {
+                        this.toolbar.view( new media.view.Toolbar({
+                                controller: this,
+                                items: {
+                                        select: {
+                                                style:    'primary',
+                                                text:     l10n.select,
+                                                priority: 80,
+
+                                                click: function() {
+                                                        var controller = this.controller;
+
+                                                        controller.close();
+                                                        controller.state().trigger('select');
+                                                        controller.reset().state( controller.options.state );
+                                                }
+                                        }
+                                }
+                        }) );
+                },
+
+                refreshSelectToolbar: function() {
+                        var selection = this.state().get('selection');
+
+                        if ( ! selection || 'select' !== this.toolbar.mode() )
+                                return;
+
+                        this.toolbar.view().get('select').model.set( 'disabled', ! selection.length );
+                }
+        });
+
+        /**
+         * wp.media.view.MediaFrame.Post
+         */
+        media.view.MediaFrame.Post = media.view.MediaFrame.Select.extend({
+                initialize: function() {
+                        _.defaults( this.options, {
+                                state:     'upload',
+                                multiple:  true,
+                                editing:   false
+                        });
+
+                        media.view.MediaFrame.Select.prototype.initialize.apply( this, arguments );
+                },
+
</ins><span class="cx">                 createStates: function() {
</span><span class="cx">                         var options = this.options,
</span><span class="cx">                                 main, gallery, batch;
</span><span class="lines">@@ -790,29 +882,69 @@
</span><span class="cx">                         ]);
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                // Menus
-                mainMenu: function() {
-                        this.menu.view( new media.view.Menu({
-                                controller: this,
-                                views: {
-                                        upload: {
-                                                text: l10n.uploadFilesTitle,
-                                                priority: 20
</del><ins>+                bindHandlers: function() {
+                        media.view.MediaFrame.Select.prototype.bindHandlers.apply( this, arguments );
+
+                        var handlers = {
+                                        menu: {
+                                                batch:   'batchMenu',
+                                                gallery: 'galleryMenu'
</ins><span class="cx">                                         },
</span><del>-                                        library: {
-                                                text: l10n.mediaLibraryTitle,
-                                                priority: 40
</del><ins>+
+                                        content: {
+                                                embed:  'embedContent'
</ins><span class="cx">                                         },
</span><del>-                                        separateLibrary: new Backbone.View({
-                                                className: 'separator',
-                                                priority: 60
-                                        }),
-                                        embed: {
-                                                text: l10n.embedFromUrlTitle,
-                                                priority: 80
</del><ins>+
+                                        sidebar: {
+                                                'attachment-settings': 'attachmentSettingsSidebar'
+                                        },
+
+                                        toolbar: {
+                                                'main-attachments': 'mainAttachmentsToolbar',
+                                                'main-embed':       'mainEmbedToolbar',
+                                                'batch-edit':       'batchEditToolbar',
+                                                'batch-add':        'batchAddToolbar',
+                                                'gallery-edit':     'galleryEditToolbar',
+                                                'gallery-add':      'galleryAddToolbar'
</ins><span class="cx">                                         }
</span><ins>+                                };
+
+                        _.each( handlers, function( regionHandlers, region ) {
+                                _.each( regionHandlers, function( callback, handler ) {
+                                        this[ region ].on( 'activate:' + handler, this[ callback ], this );
+                                }, this );
+                        }, this );
+
+                        _.each(['library', 'upload'], function( id ) {
+                                this.get( id ).on( 'refresh:selection', function( state, selection ) {
+                                        var sidebar = this.sidebar;
+
+                                        if ( ! selection.length )
+                                                sidebar.mode('clear');
+                                        else if ( selection.length === 1 )
+                                                sidebar.mode('attachment-settings');
+                                        else
+                                                sidebar.mode('settings');
+                                }, this );
+                        }, this );
+
+                        this.sidebar.on( 'gallery-settings', this.onSidebarGallerySettings, this );
+                },
+
+                // Menus
+                mainMenu: function() {
+                        media.view.MediaFrame.Select.prototype.mainMenu.call( this, { silent: true });
+
+                        this.menu.view().add({
+                                separateLibrary: new Backbone.View({
+                                        className: 'separator',
+                                        priority: 60
+                                }),
+                                embed: {
+                                        text: l10n.embedFromUrlTitle,
+                                        priority: 80
</ins><span class="cx">                                 }
</span><del>-                        }) );
</del><ins>+                        });
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 batchMenu: function() {
</span><span class="lines">@@ -890,51 +1022,9 @@
</span><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 // Content
</span><del>-                browseContent: function() {
-                        var state = this.state();
-
-                        // Browse our library of attachments.
-                        this.content.view( new media.view.AttachmentsBrowser({
-                                controller: this,
-                                collection: state.get('library'),
-                                model:      state,
-                                sortable:   state.get('sortable'),
-
-                                AttachmentView: state.get('AttachmentView')
-                        }).render() );
-                },
-
-                uploadContent: function() {
-                        // In the meantime, render an inline uploader.
-                        this.content.view( new media.view.UploaderInline({
-                                controller: this
-                        }).render() );
-                },
-
</del><span class="cx">                 embedContent: function() {},
</span><span class="cx"> 
</span><span class="cx">                 // Sidebars
</span><del>-                clearSidebar: function() {
-                        this.sidebar.view( new media.view.Sidebar({
-                                controller: this
-                        }) );
-                },
-
-                settingsSidebar: function( options ) {
-                        this.sidebar.view( new media.view.Sidebar({
-                                controller: this,
-                                silent:     options &amp;&amp; options.silent,
-
-                                views: {
-                                        details: new media.view.Attachment.Details({
-                                                controller: this,
-                                                model:      this.state().get('selection').single(),
-                                                priority:   80
-                                        }).render()
-                                }
-                        }) );
-                },
-
</del><span class="cx">                 onSidebarGallerySettings: function( options ) {
</span><span class="cx">                         this.sidebar.view().add({
</span><span class="cx">                                 gallery: new media.view.Settings.Gallery({
</span></span></pre></div>
<a id="trunkwpincludesscriptloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/script-loader.php (22493 => 22494)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/script-loader.php        2012-11-09 06:15:25 UTC (rev 22493)
+++ trunk/wp-includes/script-loader.php        2012-11-09 09:47:12 UTC (rev 22494)
</span><span class="lines">@@ -328,6 +328,7 @@
</span><span class="cx">                 // Generic
</span><span class="cx">                 'insertMedia' =&gt; __( 'Insert Media' ),
</span><span class="cx">                 'search'      =&gt; __( 'Search' ),
</span><ins>+                'select'      =&gt; __( 'Select' ),
</ins><span class="cx">                 'cancel'      =&gt; __( 'Cancel' ),
</span><span class="cx">                 'addImages'   =&gt; __( 'Add images' ),
</span><span class="cx">                 'selected'    =&gt; __( 'selected' ),
</span></span></pre>
</div>
</div>

</body>
</html>