<!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>[23089] trunk/wp-includes/js/media-views.js: New method of managing media selections.</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/23089">23089</a></dd>
<dt>Author</dt> <dd>markjaquith</dd>
<dt>Date</dt> <dd>2012-12-06 03:55:37 +0000 (Thu, 06 Dec 2012)</dd>
</dl>
<h3>Log Message</h3>
<pre>New method of managing media selections. There is now a "master" selection in the background. props koopersmith. fixes <a href="http://core.trac.wordpress.org/ticket/22725">#22725</a>
* This especially helps when you switch between insert/gallery and have non-images selected (as galleries don't support non-images).
* The views now act as filters on the "master" selection, instead of having their own selections that get passed around.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesjsmediaviewsjs">trunk/wp-includes/js/media-views.js</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesjsmediaviewsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-views.js (23088 => 23089)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-views.js        2012-12-06 03:50:31 UTC (rev 23088)
+++ trunk/wp-includes/js/media-views.js        2012-12-06 03:55:37 UTC (rev 23089)
</span><span class="lines">@@ -367,19 +367,36 @@
</span><span class="cx">                         contentUserSetting: true,
</span><span class="cx">
</span><span class="cx">                         // Sync the selection from the last state when 'multiple' matches.
</span><del>-                        syncLastSelection: true
</del><ins>+                        syncSelection: true
</ins><span class="cx">                 },
</span><span class="cx">
</span><span class="cx">                 initialize: function() {
</span><del>-                        if ( ! this.get('selection') ) {
</del><ins>+                        var selection = this.get('selection'),
+                                props;
+
+                        // If a library isn't provided, query all media items.
+                        if ( ! this.get('library') )
+                                this.set( 'library', media.query() );
+
+                        // If a selection instance isn't provided, create one.
+                        if ( ! (selection instanceof media.model.Selection) ) {
+                                props = selection;
+
+                                if ( ! props ) {
+                                        props = this.get('library').props.toJSON();
+                                        props = _.omit( props, 'orderby', 'query' );
+                                }
+
+                                // If the `selection` attribute is set to an object,
+                                // it will use those values as the selection instance's
+                                // `props` model. Otherwise, it will copy the library's
+                                // `props` model.
</ins><span class="cx">                                 this.set( 'selection', new media.model.Selection( null, {
</span><del>-                                        multiple: this.get('multiple')
</del><ins>+                                        multiple: this.get('multiple'),
+                                        props: props
</ins><span class="cx">                                 }) );
</span><span class="cx">                         }
</span><span class="cx">
</span><del>-                        if ( ! this.get('library') )
-                                this.set( 'library', media.query() );
-
</del><span class="cx">                         if ( ! this.get('edge') )
</span><span class="cx">                                 this.set( 'edge', 120 );
</span><span class="cx">
</span><span class="lines">@@ -390,8 +407,7 @@
</span><span class="cx">                 },
</span><span class="cx">
</span><span class="cx">                 activate: function() {
</span><del>-                        if ( this.get('syncLastSelection') )
-                                this.getLastSelection();
</del><ins>+                        this.syncSelection();
</ins><span class="cx">
</span><span class="cx">                         wp.Uploader.queue.on( 'add', this.uploading, this );
</span><span class="cx">
</span><span class="lines">@@ -406,6 +422,8 @@
</span><span class="cx">                 },
</span><span class="cx">
</span><span class="cx">                 deactivate: function() {
</span><ins>+                        this.recordSelection();
+
</ins><span class="cx">                         this.frame.off( 'content:activate', this.saveContentMode, this );
</span><span class="cx">
</span><span class="cx">                         // Unbind all event handlers that use this state as the context
</span><span class="lines">@@ -455,25 +473,47 @@
</span><span class="cx">                         setUserSetting( 'urlbutton', display.link );
</span><span class="cx">                 },
</span><span class="cx">
</span><del>-                getLastSelection: function() {
</del><ins>+                syncSelection: function() {
</ins><span class="cx">                         var selection = this.get('selection'),
</span><del>-                                lastState = this.frame.lastState(),
-                                lastSelection = lastState && lastState.get('selection'),
-                                lastMultiple, thisMultiple;
</del><ins>+                                manager = this.frame._selection;
</ins><span class="cx">
</span><del>-                        if ( ! lastSelection )
</del><ins>+                        if ( ! this.get('syncSelection') || ! manager || ! selection )
</ins><span class="cx">                                 return;
</span><span class="cx">
</span><del>-                        // We don't care about the method of multiple selection the
-                        // selections use, just that they both support (or don't support)
-                        // multiple selection.
-                        lastMultiple = !! lastSelection.multiple;
-                        thisMultiple = !! selection.multiple;
</del><ins>+                        // If the selection supports multiple items, validate the stored
+                        // attachments based on the new selection's conditions. Record
+                        // the attachments that are not included; we'll maintain a
+                        // reference to those. Other attachments are considered in flux.
+                        if ( selection.multiple ) {
+                                selection.reset( [], { silent: true });
+                                selection.validateAll( manager.attachments );
+                                manager.difference = _.difference( manager.attachments.models, selection.models );
+                        }
</ins><span class="cx">
</span><del>-                        if ( lastMultiple !== thisMultiple )
</del><ins>+                        // Sync the selection's single item with the master.
+                        selection.single( manager.single );
+                },
+
+                recordSelection: function() {
+                        var selection = this.get('selection'),
+                                manager = this.frame._selection,
+                                filtered;
+
+                        if ( ! this.get('syncSelection') || ! manager || ! selection )
</ins><span class="cx">                                 return;
</span><span class="cx">
</span><del>-                        selection.reset( lastSelection.toArray() ).single( lastSelection.single() );
</del><ins>+                        // Record the currently active attachments, which is a combination
+                        // of the selection's attachments and the set of selected
+                        // attachments that this specific selection considered invalid.
+                        // Reset the difference and record the single attachment.
+                        if ( selection.multiple ) {
+                                manager.attachments.reset( selection.toArray().concat( manager.difference ) );
+                                manager.difference = [];
+                        } else {
+                                manager.attachments.add( selection.toArray() );
+                        }
+
+                        manager.single = selection._single;
</ins><span class="cx">                 },
</span><span class="cx">
</span><span class="cx">                 refreshContent: function() {
</span><span class="lines">@@ -527,7 +567,11 @@
</span><span class="cx">                         content: 'browse',
</span><span class="cx">                         title: l10n.editGalleryTitle,
</span><span class="cx">                         priority: 60,
</span><del>-                        dragInfo: true
</del><ins>+                        dragInfo: true,
+
+                        // Don't sync the selection, as the Edit Gallery library
+                        // *is* the selection.
+                        syncSelection: false
</ins><span class="cx">                 },
</span><span class="cx">
</span><span class="cx">                 initialize: function() {
</span><span class="lines">@@ -601,7 +645,11 @@
</span><span class="cx">                         menu: 'gallery',
</span><span class="cx">                         toolbar: 'gallery-add',
</span><span class="cx">                         title: l10n.addToGalleryTitle,
</span><del>-                        priority: 100
</del><ins>+                        priority: 100,
+
+                        // Don't sync the selection, as the Edit Gallery library
+                        // *is* the selection.
+                        syncSelection: false
</ins><span class="cx">                 }, media.controller.Library.prototype.defaults ),
</span><span class="cx">
</span><span class="cx">                 initialize: function() {
</span><span class="lines">@@ -641,7 +689,9 @@
</span><span class="cx">                         multiple: false,
</span><span class="cx">                         toolbar: 'featured-image',
</span><span class="cx">                         title: l10n.setFeaturedImageTitle,
</span><del>-                        priority: 60
</del><ins>+                        priority: 60,
+
+                        syncSelection: false
</ins><span class="cx">                 }, media.controller.Library.prototype.defaults ),
</span><span class="cx">
</span><span class="cx">                 initialize: function() {
</span><span class="lines">@@ -1429,6 +1479,11 @@
</span><span class="cx">                                         multiple: this.options.multiple
</span><span class="cx">                                 });
</span><span class="cx">                         }
</span><ins>+
+                        this._selection = {
+                                attachments: new Attachments(),
+                                difference: []
+                        };
</ins><span class="cx">                 },
</span><span class="cx">
</span><span class="cx">                 createStates: function() {
</span><span class="lines">@@ -1441,7 +1496,6 @@
</span><span class="cx">                         this.states.add([
</span><span class="cx">                                 // Main states.
</span><span class="cx">                                 new media.controller.Library({
</span><del>-                                        selection: options.selection,
</del><span class="cx">                                         library: media.query( options.library ),
</span><span class="cx">                                         multiple: options.multiple,
</span><span class="cx">                                         title: options.title,
</span><span class="lines">@@ -1526,8 +1580,7 @@
</span><span class="cx">                 },
</span><span class="cx">
</span><span class="cx">                 createStates: function() {
</span><del>-                        var options = this.options,
-                                selection = options.selection;
</del><ins>+                        var options = this.options;
</ins><span class="cx">
</span><span class="cx">                         // Add the default states.
</span><span class="cx">                         this.states.add([
</span><span class="lines">@@ -1539,7 +1592,6 @@
</span><span class="cx">                                         toolbar: 'main-insert',
</span><span class="cx">                                         filterable: 'all',
</span><span class="cx">                                         library: media.query( options.library ),
</span><del>-                                        selection: selection,
</del><span class="cx">                                         multiple: options.multiple ? 'reset' : false,
</span><span class="cx">                                         editable: true,
</span><span class="cx">
</span><span class="lines">@@ -1565,11 +1617,7 @@
</span><span class="cx">
</span><span class="cx">                                         library: media.query( _.defaults({
</span><span class="cx">                                                 type: 'image'
</span><del>-                                        }, options.library ) ),
-
-                                        selection: new media.model.Selection( selection.models, {
-                                                multiple: 'add'
-                                        })
</del><ins>+                                        }, options.library ) )
</ins><span class="cx">                                 }),
</span><span class="cx">
</span><span class="cx">                                 // Embed states.
</span></span></pre>
</div>
</div>
</body>
</html>