Make WordPress Core

Changeset 22335


Ignore:
Timestamp:
10/30/2012 11:15:16 PM (12 years ago)
Author:
koopersmith
Message:

Media JS: Improve handling of single attachments in selections.

  • Adds wp.media.model.Selection.single() to specify a single item used in a multi-item selection.
  • Fixes a bug where the details class would not be removed when "Clear Selection" was clicked.

see #21390.

Location:
trunk/wp-includes
Files:
3 edited

Legend:

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

    r22332 r22335  
    245245    background: #777;
    246246    border: 1px solid #fff;
    247     /*border-width: 0 1px 1px 0;*/
    248247    border-width: 0 0 1px 1px;
    249248    box-shadow: -1px 1px 0 rgba( 0, 0, 0, 0.1 );
  • trunk/wp-includes/js/media-models.js

    r22323 r22335  
    623623            Attachments.prototype.initialize.apply( this, arguments );
    624624            this.multiple = options && options.multiple;
     625
     626            // Refresh the `single` model whenever the selection changes.
     627            // Binds `single` instead of using the context argument to ensure
     628            // it receives no parameters.
     629            this.on( 'add remove reset', _.bind( this.single, this ) );
    625630        },
    626631
     
    639644        // Removes all models from the selection.
    640645        clear: function( options ) {
    641             return this.remove( this.models, options );
     646            this.remove( this.models, options ).single();
     647            return this;
    642648        },
    643649
     
    646652        // as we need them to fire.
    647653        reset: function( models, options ) {
    648             return this.clear( options ).add( models, options );
     654            this.clear( options ).add( models, options ).single();
     655            return this;
    649656        },
    650657
     
    654661        has: function( attachment ) {
    655662            return !! ( this.getByCid( attachment.cid ) || this.get( attachment.id ) );
     663        },
     664
     665        single: function( model ) {
     666            var previous = this._single;
     667
     668            // If a `model` is provided, use it as the single model.
     669            if ( model )
     670                this._single = model;
     671
     672            // If the single model isn't in the selection, remove it.
     673            if ( this._single && ! this.has( this._single ) )
     674                delete this._single;
     675
     676            this._single = this._single || this.last();
     677
     678            // If single has changed, fire an event.
     679            if ( this._single !== previous ) {
     680                if ( this._single )
     681                    this._single.trigger( 'selection:single', this._single, this );
     682                if ( previous )
     683                    previous.trigger( 'selection:unsingle', previous, this );
     684            }
     685
     686            // Return the single model, or the last model as a fallback.
     687            return this._single;
    656688        }
    657689    });
  • trunk/wp-includes/js/media-views.js

    r22333 r22335  
    213213
    214214        details: function( options ) {
    215             var model = this.get('details'),
     215            var model = this.get('selection').single(),
    216216                view;
    217217
     
    233233
    234234        toggleSelection: function( model ) {
    235             var details = this.get('details'),
    236                 selection = this.get('selection'),
    237                 selected = selection.has( model );
    238 
    239             if ( ! selection )
    240                 return;
    241 
    242             if ( ! selected )
    243                 selection.add( model );
    244 
    245             // If the model is not the same as the details model,
    246             // it now becomes the details model. If the model is
    247             // in the selection, it is not removed.
    248             if ( details !== model ) {
    249                 this.set( 'details', model );
    250                 return;
    251             }
    252 
    253             // The model is the details model.
    254             // Removed it from the selection.
    255             selection.remove( model );
    256 
    257             // Show the last selected item, or clear the details view.
    258             if ( selection.length )
    259                 this.set( 'details', selection.last() );
    260             else
    261                 this.unset('details');
    262 
     235            var selection = this.get('selection');
     236
     237            if ( selection.has( model ) ) {
     238                // If the model is the single model, remove it.
     239                // If it is not the same as the single model,
     240                // it now becomes the single model.
     241                selection[ selection.single() === model ? 'remove' : 'single' ]( model );
     242            } else {
     243                selection.add( model ).single();
     244            }
     245
     246            return this;
    263247        }
    264248    });
     
    10881072
    10891073            // Update the model's details view.
    1090             this.controller.state().on( 'change:details', this.details, this );
     1074            this.model.on( 'selection:single selection:unsingle', this.details, this );
    10911075            this.details();
    10921076
     
    10951079
    10961080        destroy: function() {
    1097             this.controller.state().off( 'change:details', this.details, this );
     1081            this.model.off( 'single', this.details, this );
    10981082        },
    10991083
     
    11371121        },
    11381122
    1139         details: function() {
    1140             var details = this.controller.state().get('details');
     1123        details: function( model, collection ) {
     1124            var selection = this.controller.state().get('selection'),
     1125                details;
     1126
     1127            if ( selection !== collection )
     1128                return;
     1129
     1130            details = selection.single();
    11411131            this.$el.toggleClass( 'details', details === this.model );
    11421132        },
Note: See TracChangeset for help on using the changeset viewer.