Make WordPress Core

Changeset 22477


Ignore:
Timestamp:
11/09/2012 01:23:20 AM (12 years ago)
Author:
koopersmith
Message:

Media JS: Add JIT composite library creation/destruction to states to allow for dynamically excluded Attachment models. see #21390.

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

Legend:

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

    r22467 r22477  
    742742        observe: function( attachments, options ) {
    743743            var silent = options && options.silent;
     744            this.observers = this.observers || [];
     745            this.observers.push( attachments );
    744746
    745747            attachments.on( 'add remove',  silent ? this._evaluateSilentHandler : this._evaluateHandler, this );
     
    747749
    748750            this.evaluateAll( attachments, options );
     751            return this;
    749752        },
    750753
    751754        unobserve: function( attachments ) {
    752             attachments.off( 'add remove', this._evaluateHandler, this );
    753             attachments.off( 'reset', this._evaluateAllHandler, this );
     755            if ( attachments ) {
     756                attachments.off( null, null, this );
     757                this.observers = _.without( this.observers, attachments );
     758
     759            } else {
     760                _.each( this.observers, function( attachments ) {
     761                    attachments.off( null, null, this );
     762                }, this );
     763                delete this.observers;
     764            }
     765
     766            return this;
    754767        },
    755768
  • trunk/wp-includes/js/media-views.js

    r22467 r22477  
    292292            var selection = this.get('selection');
    293293
     294            this._excludeStateLibrary();
     295            this.buildComposite();
     296            this.on( 'change:library change:exclude', this.buildComposite, this );
     297            this.on( 'change:excludeState', this._excludeState, this );
     298
    294299            // If we're in a workflow that supports multiple attachments,
    295300            // automatically select any uploading attachments.
     
    307312
    308313        deactivate: function() {
     314            this.off( 'change:library change:exclude', this.buildComposite, this );
     315            this.off( 'change:excludeState', this._excludeState, this );
     316            this.destroyComposite();
     317
    309318            wp.Uploader.queue.off( 'add', this.selectUpload, this );
    310319
     
    376385
    377386            return this;
     387        },
     388
     389        buildComposite: function() {
     390            var original = this.get('_library'),
     391                exclude = this.get('exclude'),
     392                composite;
     393
     394            this.destroyComposite();
     395            if ( ! this.get('exclude') )
     396                return;
     397
     398            // Remember the state's original library.
     399            if ( ! original )
     400                this.set( '_library', original = this.get('library') );
     401
     402            // Create a composite library in its place.
     403            composite = new media.model.Composite( null, {
     404                props: _.pick( original.props.toJSON(), 'order', 'orderby' )
     405            });
     406
     407            // Accepts attachments that exist in the original library and
     408            // that do not exist in the excluded library.
     409            composite.validator = function( attachment ) {
     410                return !! original.getByCid( attachment.cid ) && ! exclude.getByCid( attachment.cid );
     411            };
     412
     413            composite.observe( original ).observe( exclude );
     414
     415            // When `more()` is triggered on the composite collection,
     416            // pass the command over to the `original`, which will
     417            // populate the query.
     418            composite.more = _.bind( original.more, original );
     419
     420            this.set( 'library', composite );
     421        },
     422
     423        destroyComposite: function() {
     424            var composite = this.get('library'),
     425                original = this.get('_library');
     426
     427            if ( ! original )
     428                return;
     429
     430            composite.unobserve();
     431            this.set( 'library', original );
     432            this.unset('_library');
     433        },
     434
     435        _excludeState: function() {
     436            var current = this.get('excludeState'),
     437                previous = this.previous('excludeState');
     438
     439            if ( previous )
     440                this.frame.get( previous ).off( 'change:library', this._excludeStateLibrary, this );
     441
     442            if ( current )
     443                this.frame.get( previous ).on( 'change:library', this._excludeStateLibrary, this );
     444        },
     445
     446        _excludeStateLibrary: function() {
     447            var current = this.get('excludeState');
     448
     449            if ( ! current )
     450                return;
     451
     452            this.set( 'exclude', this.frame.get( current ).get('library') );
    378453        }
    379454    });
     
    660735                new media.controller.Library( _.defaults({
    661736                    id:      'gallery-library',
    662                     library: media.query({ type: 'image' })
     737                    library: media.query({ type: 'image' }),
     738                    excludeState: 'gallery-edit'
    663739                }, gallery ) ),
    664740
    665741                new media.controller.Upload( _.defaults({
    666                     id: 'gallery-upload'
     742                    id: 'gallery-upload',
     743                    excludeState: 'gallery-edit'
    667744                }, gallery ) )
    668745            ]);
    669746
    670             this.get('gallery-edit').on( 'change:library', this.updateGalleryLibraries, this ).set({
    671                 library: options.selection
    672             });
    673 
    674747            // Set the default state.
    675748            this.state( options.state );
    676         },
    677 
    678         updateGalleryLibraries: function() {
    679             var editLibrary = this.get('gallery-edit').get('library');
    680 
    681             _.each(['gallery-library','gallery-upload'], function( id ) {
    682                 var state = this.get( id ),
    683                     original = state.get('_library'),
    684                     composite;
    685 
    686                 // Remember the state's original library.
    687                 if ( ! original )
    688                     state.set( '_library', original = state.get('library') );
    689 
    690                 // Create a composite library in its place.
    691                 composite = new media.model.Composite( null, {
    692                     props: _.pick( original.props.toJSON(), 'order', 'orderby' )
    693                 });
    694 
    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 );
    699                 };
    700 
    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 );
    710             }, this );
    711749        },
    712750
Note: See TracChangeset for help on using the changeset viewer.