Make WordPress Core

Changeset 22951


Ignore:
Timestamp:
11/30/2012 03:11:44 PM (14 years ago)
Author:
ryan
Message:

Update the "Uploaded to this post" filter view when uploading new files.

  • Add an Attachments collection filter for uploadedTo (post_parent).
  • Correct an erroneous map from the non-existent parent prop to post_parent.
  • Define default menuOrder and uploadedTo props for uploading attachments.
  • Unify filterable props into a single method (improving validation performance for calls to set with multiple properties).

Props koopersmith
fixes #22654

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

Legend:

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

    r22943 r22951  
    206206
    207207                                if ( ! args.post__in )
    208                                         args.parent = attrs.id;
     208                                        args.uploadedTo = attrs.id;
    209209
    210210                                // Collect the attributes that were not included in `args`.
     
    229229                                attrs.ids = attachments.pluck('id');
    230230
    231                                 // Copy the `parent` post ID.
    232                                 if ( props.parent )
    233                                         attrs.id = props.parent;
     231                                // Copy the `uploadedTo` post ID.
     232                                if ( props.uploadedTo )
     233                                        attrs.id = props.uploadedTo;
    234234
    235235                                // If the `ids` attribute is set and `orderby` attribute
  • trunk/wp-includes/js/media-models.js

    r22882 r22951  
    317317
    318318                        // Bind default `change` events to the `props` model.
     319                        this.props.on( 'change', this._changeFilteredProps, this );
     320
    319321                        this.props.on( 'change:order',   this._changeOrder,   this );
    320322                        this.props.on( 'change:orderby', this._changeOrderby, this );
    321323                        this.props.on( 'change:query',   this._changeQuery,   this );
    322                         this.props.on( 'change:search',  this._changeSearch,  this );
    323                         this.props.on( 'change:type',    this._changeType,    this );
    324324
    325325                        // Set the `props` model and fill the default property values.
     
    360360                },
    361361
    362                 _changeFilteredProp: function( prop, model, term ) {
     362                _changeFilteredProps: function( model, options ) {
    363363                        // If this is a query, updating the collection will be handled by
    364364                        // `this._requery()`.
     
    366366                                return;
    367367
    368                         if ( term && ! this.filters[ prop ] )
    369                                 this.filters[ prop ] = Attachments.filters[ prop ];
    370                         else if ( ! term && this.filters[ prop ] === Attachments.filters[ prop ] )
    371                                 delete this.filters[ prop ];
     368                        var changed = _.chain( options.changes ).map( function( t, prop ) {
     369                                var filter = Attachments.filters[ prop ],
     370                                        term = model.get( prop );
     371
     372                                if ( ! filter )
     373                                        return;
     374
     375                                if ( term && ! this.filters[ prop ] )
     376                                        this.filters[ prop ] = filter;
     377                                else if ( ! term && this.filters[ prop ] === filter )
     378                                        delete this.filters[ prop ];
     379                                else
     380                                        return;
     381
     382                                // Record the change.
     383                                return true;
     384                        }, this ).any().value();
     385
     386                        if ( ! changed )
     387                                return;
    372388
    373389                        // If no `Attachments` model is provided to source the searches
     
    378394
    379395                        this.reset( this._source.filter( this.validator, this ) );
    380                 },
    381 
    382                 _changeSearch: function( model, term ) {
    383                         return this._changeFilteredProp( 'search', model, term );
    384                 },
    385 
    386                 _changeType: function( model, term ) {
    387                         return this._changeFilteredProp( 'type', model, term );
    388396                },
    389397
     
    500508                }
    501509        }, {
    502                 comparator: function( a, b ) {
     510                comparator: function( a, b, options ) {
    503511                        var key   = this.props.get('orderby'),
    504512                                order = this.props.get('order') || 'DESC',
     
    513521                                b = b || new Date();
    514522                        }
     523
     524                        // If `options.ties` is set, don't enforce the `cid` tiebreaker.
     525                        if ( options && options.ties )
     526                                ac = bc = null;
    515527
    516528                        return ( 'DESC' === order ) ? compare( a, b, ac, bc ) : compare( b, a, bc, ac );
     
    532544                        type: function( attachment ) {
    533545                                var type = this.props.get('type');
    534                                 if ( ! type )
     546                                return ! type || -1 !== type.indexOf( attachment.get('type') );
     547                        },
     548
     549                        uploadedTo: function( attachment ) {
     550                                var uploadedTo = this.props.get('uploadedTo');
     551                                if ( _.isUndefined( uploadedTo ) )
    535552                                        return true;
    536553
    537                                 return -1 !== type.indexOf( attachment.get('type') );
     554                                return uploadedTo === attachment.get('uploadedTo');
    538555                        }
    539556                }
     
    572589
    573590                        this.filters.order = function( attachment ) {
     591                                var orderby = this.props.get('orderby'),
     592                                        order = this.props.get('order');
     593
    574594                                if ( ! this.comparator )
    575595                                        return true;
     
    579599                                // item, then we can't guarantee the set is complete.
    580600                                if ( this.length ) {
    581                                         return 1 !== this.comparator( attachment, this.last() );
     601                                        return 1 !== this.comparator( attachment, this.last(), { ties: true });
    582602
    583603                                // Handle the case where there are no items yet and
    584604                                // we're sorting for recent items. In that case, we want
    585605                                // changes that occurred after we created the query.
    586                                 } else if ( 'DESC' === this.args.order && ( 'date' === this.args.orderby || 'modified' === this.args.orderby ) ) {
    587                                         return attachment.get( this.args.orderby ) >= this.created;
     606                                } else if ( 'DESC' === order && ( 'date' === orderby || 'modified' === orderby ) ) {
     607                                        return attachment.get( orderby ) >= this.created;
     608
     609                                // If we're sorting by menu order and we have no items,
     610                                // accept any items that have the default menu order (0).
     611                                } else if ( 'ASC' === order && 'menuOrder' === orderby ) {
     612                                        return attachment.get( orderby ) === 0;
    588613                                }
    589614
     
    598623                        // are no filters for other properties, so observing will result in
    599624                        // false positives in those queries.
    600                         allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type' ];
     625                        allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type', 'post_parent' ];
    601626                        if ( wp.Uploader && _( this.args ).chain().keys().difference( allowed ).isEmpty().value() )
    602627                                this.observe( wp.Uploader.queue );
     
    671696                        'search':    's',
    672697                        'type':      'post_mime_type',
    673                         'parent':    'post_parent',
    674698                        'perPage':   'posts_per_page',
    675                         'menuOrder': 'menu_order'
     699                        'menuOrder': 'menu_order',
     700                        'uploadedTo': 'post_parent'
    676701                },
    677702
  • trunk/wp-includes/js/media-views.js

    r22948 r22951  
    29212921                                text:  l10n.allMediaItems,
    29222922                                props: {
    2923                                         parent: null,
     2923                                        uploadedTo: null,
    29242924                                        orderby: 'date',
    29252925                                        order:   'DESC'
     
    29312931                                text:  l10n.uploadedToThisPost,
    29322932                                props: {
    2933                                         parent: media.view.settings.postId,
     2933                                        uploadedTo: media.view.settings.postId,
    29342934                                        orderby: 'menuOrder',
    29352935                                        order:   'ASC'
     
    29492949                                        props: {
    29502950                                                type:    key,
    2951                                                 parent: null,
     2951                                                uploadedTo: null,
    29522952                                                orderby: 'date',
    29532953                                                order:   'DESC'
     
    29602960                                props: {
    29612961                                        type:    null,
    2962                                         parent: null,
     2962                                        uploadedTo: null,
    29632963                                        orderby: 'date',
    29642964                                        order:   'DESC'
     
    29712971                                props: {
    29722972                                        type:    null,
    2973                                         parent: media.view.settings.postId,
     2973                                        uploadedTo: media.view.settings.postId,
    29742974                                        orderby: 'menuOrder',
    29752975                                        order:   'ASC'
  • trunk/wp-includes/js/plupload/wp-plupload.js

    r22822 r22951  
    156156                                        uploading: true,
    157157                                        date:      new Date(),
    158                                         filename:  file.name
     158                                        filename:  file.name,
     159                                        menuOrder: 0,
     160                                        uploadedTo: media.model.settings.postId
    159161                                }, _.pick( file, 'loaded', 'size', 'percent' ) );
    160162
Note: See TracChangeset for help on using the changeset viewer.