WordPress.org

Make WordPress Core

Changeset 22467


Ignore:
Timestamp:
11/08/2012 03:32:40 PM (6 years ago)
Author:
koopersmith
Message:

Media JS: Add media.model.Composite, to aid in the representation of joint views. see #21390.

Location:
trunk/wp-includes/js
Files:
2 edited

Legend:

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

    r22437 r22467  
    698698    });
    699699
     700    /**
     701     * wp.media.model.Composite
     702     *
     703     * Creates a model that can simultaneously pull from two or more collections.
     704     */
     705    media.model.Composite = Attachments.extend({
     706        initialize: function( models, options ) {
     707            this.observe( this, { silent: true });
     708            Attachments.prototype.initialize.apply( this, arguments );
     709        },
     710
     711        evaluate: function( attachment, options ) {
     712            var valid = this.validator( attachment ),
     713                hasAttachment = !! this.getByCid( attachment.cid );
     714
     715            if ( ! valid && hasAttachment ) {
     716                this.remove( attachment, options );
     717            } else if ( valid && ! hasAttachment ) {
     718                this.add( attachment, options );
     719
     720                // If we haven't been silenced, resort the collection.
     721                if ( this.comparator && ( ! options || ! options.silent ) )
     722                    this.sort({ silent: true });
     723            }
     724
     725            return this;
     726        },
     727
     728        validator: function() {
     729            return true;
     730        },
     731
     732        evaluateAll: function( attachments, options ) {
     733            _.each( attachments.models, function( attachment ) {
     734                this.evaluate( attachment, { silent: true });
     735            }, this );
     736
     737            if ( this.comparator )
     738                this.sort( options );
     739            return this;
     740        },
     741
     742        observe: function( attachments, options ) {
     743            var silent = options && options.silent;
     744
     745            attachments.on( 'add remove',  silent ? this._evaluateSilentHandler : this._evaluateHandler, this );
     746            attachments.on( 'reset',  silent ? this._evaluateAllSilentHandler : this._evaluateAllHandler, this );
     747
     748            this.evaluateAll( attachments, options );
     749        },
     750
     751        unobserve: function( attachments ) {
     752            attachments.off( 'add remove', this._evaluateHandler, this );
     753            attachments.off( 'reset', this._evaluateAllHandler, this );
     754        },
     755
     756        _evaluateHandler: function( attachment, attachments, options ) {
     757            return this.evaluate( attachment, options );
     758        },
     759
     760        _evaluateAllHandler: function( attachments, options ) {
     761            return this.evaluateAll( attachments, options );
     762        },
     763
     764        _evaluateSilentHandler: function( attachment, attachments, options ) {
     765            return this.evaluate( attachment, _.defaults({ silent: true }, options ) );
     766        },
     767
     768        _evaluateAllSilentHandler: function( attachments, options ) {
     769            return this.evaluateAll( attachments, _.defaults({ silent: true }, options ) );
     770        }
     771    });
     772
    700773}(jQuery));
  • trunk/wp-includes/js/media-views.js

    r22466 r22467  
    682682                var state = this.get( id ),
    683683                    original = state.get('_library'),
    684                     skeleton;
     684                    composite;
    685685
    686686                // Remember the state's original library.
     
    688688                    state.set( '_library', original = state.get('library') );
    689689
    690                 // Create a skeleton library in its place.
    691                 skeleton = new Attachments( null, {
     690                // Create a composite library in its place.
     691                composite = new media.model.Composite( null, {
    692692                    props: _.pick( original.props.toJSON(), 'order', 'orderby' )
    693693                });
    694694
    695                 // Rejects attachments that do not exist in the original library
    696                 // or that do exist edit state's library.
    697                 skeleton.filters.difference = function( attachment ) {
    698                     return ! original.getByCid( attachment.cid ) || !! editLibrary.getByCid( attachment.cid );
     695                // Accepts attachments that exist in the original library and
     696                // that do not exist in the state's library.
     697                composite.validator = function( attachment ) {
     698                    return !! original.getByCid( attachment.cid ) && ! editLibrary.getByCid( attachment.cid );
    699699                };
    700700
    701                 skeleton.evaluate = function( attachment ) {
    702                     var valid = ! this.validator( attachment ),
    703                         inSkeleton = !! this.getByCid( attachment.cid );
    704 
    705                     if ( ! valid && inSkeleton )
    706                         this.remove( attachment );
    707                     else if ( valid && ! inSkeleton )
    708                         this.add( attachment ).sort();
    709 
    710                     return this;
    711                 };
    712 
    713                 skeleton.evaluateAll = function ( attachments ) {
    714                     _.each( attachments.models, this.evaluate, this );
    715                     return this;
    716                 };
    717 
    718                 skeleton.on( 'add remove', skeleton.evaluate, skeleton );
    719                 skeleton.on( 'reset', skeleton.evaluateAll, skeleton );
    720                 editLibrary.on( 'add remove', skeleton.evaluate, skeleton );
    721                 editLibrary.on( 'reset', skeleton.evaluateAll, skeleton );
    722 
    723                 // Mirror the original library.
    724                 skeleton.mirror( original );
    725 
    726                 // Ensure we've evaluated everything in the edit library.
    727                 skeleton.evaluateAll( editLibrary );
    728 
    729                 state.set( 'library', skeleton );
     701                composite.observe( original );
     702                composite.observe( editLibrary );
     703
     704                // When `more()` is triggered on the composite collection,
     705                // pass the command over to the `original`, which will
     706                // populate the query.
     707                composite.more = _.bind( original.more, original );
     708
     709                state.set( 'library', composite );
    730710            }, this );
    731711        },
Note: See TracChangeset for help on using the changeset viewer.