Make WordPress Core

Changeset 23089


Ignore:
Timestamp:
12/06/2012 03:55:37 AM (12 years ago)
Author:
markjaquith
Message:

New method of managing media selections. There is now a "master" selection in the background. props koopersmith. fixes #22725

  • 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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/js/media-views.js

    r23085 r23089  
    368368
    369369            // Sync the selection from the last state when 'multiple' matches.
    370             syncLastSelection: true
     370            syncSelection: true
    371371        },
    372372
    373373        initialize: function() {
    374             if ( ! this.get('selection') ) {
     374            var selection = this.get('selection'),
     375                props;
     376
     377            // If a library isn't provided, query all media items.
     378            if ( ! this.get('library') )
     379                this.set( 'library', media.query() );
     380
     381            // If a selection instance isn't provided, create one.
     382            if ( ! (selection instanceof media.model.Selection) ) {
     383                props = selection;
     384
     385                if ( ! props ) {
     386                    props = this.get('library').props.toJSON();
     387                    props = _.omit( props, 'orderby', 'query' );
     388                }
     389
     390                // If the `selection` attribute is set to an object,
     391                // it will use those values as the selection instance's
     392                // `props` model. Otherwise, it will copy the library's
     393                // `props` model.
    375394                this.set( 'selection', new media.model.Selection( null, {
    376                     multiple: this.get('multiple')
     395                    multiple: this.get('multiple'),
     396                    props: props
    377397                }) );
    378398            }
    379399
    380             if ( ! this.get('library') )
    381                 this.set( 'library', media.query() );
    382 
    383400            if ( ! this.get('edge') )
    384401                this.set( 'edge', 120 );
     
    391408
    392409        activate: function() {
    393             if ( this.get('syncLastSelection') )
    394                 this.getLastSelection();
     410            this.syncSelection();
    395411
    396412            wp.Uploader.queue.on( 'add', this.uploading, this );
     
    407423
    408424        deactivate: function() {
     425            this.recordSelection();
     426
    409427            this.frame.off( 'content:activate', this.saveContentMode, this );
    410428
     
    456474        },
    457475
    458         getLastSelection: function() {
     476        syncSelection: function() {
    459477            var selection = this.get('selection'),
    460                 lastState = this.frame.lastState(),
    461                 lastSelection = lastState && lastState.get('selection'),
    462                 lastMultiple, thisMultiple;
    463 
    464             if ( ! lastSelection )
     478                manager = this.frame._selection;
     479
     480            if ( ! this.get('syncSelection') || ! manager || ! selection )
    465481                return;
    466482
    467             // We don't care about the method of multiple selection the
    468             // selections use, just that they both support (or don't support)
    469             // multiple selection.
    470             lastMultiple = !! lastSelection.multiple;
    471             thisMultiple = !! selection.multiple;
    472 
    473             if ( lastMultiple !== thisMultiple )
     483            // If the selection supports multiple items, validate the stored
     484            // attachments based on the new selection's conditions. Record
     485            // the attachments that are not included; we'll maintain a
     486            // reference to those. Other attachments are considered in flux.
     487            if ( selection.multiple ) {
     488                selection.reset( [], { silent: true });
     489                selection.validateAll( manager.attachments );
     490                manager.difference = _.difference( manager.attachments.models, selection.models );
     491            }
     492
     493            // Sync the selection's single item with the master.
     494            selection.single( manager.single );
     495        },
     496
     497        recordSelection: function() {
     498            var selection = this.get('selection'),
     499                manager = this.frame._selection,
     500                filtered;
     501
     502            if ( ! this.get('syncSelection') || ! manager || ! selection )
    474503                return;
    475504
    476             selection.reset( lastSelection.toArray() ).single( lastSelection.single() );
     505            // Record the currently active attachments, which is a combination
     506            // of the selection's attachments and the set of selected
     507            // attachments that this specific selection considered invalid.
     508            // Reset the difference and record the single attachment.
     509            if ( selection.multiple ) {
     510                manager.attachments.reset( selection.toArray().concat( manager.difference ) );
     511                manager.difference = [];
     512            } else {
     513                manager.attachments.add( selection.toArray() );
     514            }
     515
     516            manager.single = selection._single;
    477517        },
    478518
     
    528568            title:      l10n.editGalleryTitle,
    529569            priority:   60,
    530             dragInfo:   true
     570            dragInfo:   true,
     571
     572            // Don't sync the selection, as the Edit Gallery library
     573            // *is* the selection.
     574            syncSelection: false
    531575        },
    532576
     
    602646            toolbar:      'gallery-add',
    603647            title:        l10n.addToGalleryTitle,
    604             priority:     100
     648            priority:     100,
     649
     650            // Don't sync the selection, as the Edit Gallery library
     651            // *is* the selection.
     652            syncSelection: false
    605653        }, media.controller.Library.prototype.defaults ),
    606654
     
    642690            toolbar:    'featured-image',
    643691            title:      l10n.setFeaturedImageTitle,
    644             priority:   60
     692            priority:   60,
     693
     694            syncSelection: false
    645695        }, media.controller.Library.prototype.defaults ),
    646696
     
    14301480                });
    14311481            }
     1482
     1483            this._selection = {
     1484                attachments: new Attachments(),
     1485                difference: []
     1486            };
    14321487        },
    14331488
     
    14421497                // Main states.
    14431498                new media.controller.Library({
    1444                     selection: options.selection,
    14451499                    library:   media.query( options.library ),
    14461500                    multiple:  options.multiple,
     
    15271581
    15281582        createStates: function() {
    1529             var options = this.options,
    1530                 selection = options.selection;
     1583            var options = this.options;
    15311584
    15321585            // Add the default states.
     
    15401593                    filterable: 'all',
    15411594                    library:    media.query( options.library ),
    1542                     selection:  selection,
    15431595                    multiple:   options.multiple ? 'reset' : false,
    15441596                    editable:   true,
     
    15661618                    library:  media.query( _.defaults({
    15671619                        type: 'image'
    1568                     }, options.library ) ),
    1569 
    1570                     selection: new media.model.Selection( selection.models, {
    1571                         multiple: 'add'
    1572                     })
     1620                    }, options.library ) )
    15731621                }),
    15741622
Note: See TracChangeset for help on using the changeset viewer.