From b73c43e0e265d1e4b170f2daf7c56c4db30e9269 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Fri, 30 Nov 2012 15:11:44 +0000 Subject: [PATCH] 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 git-svn-id: http://core.svn.wordpress.org/trunk@22951 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/js/media-editor.js | 8 +-- wp-includes/js/media-models.js | 73 +++++++++++++++++--------- wp-includes/js/media-views.js | 10 ++-- wp-includes/js/plupload/wp-plupload.js | 4 +- 4 files changed, 61 insertions(+), 34 deletions(-) diff --git a/wp-includes/js/media-editor.js b/wp-includes/js/media-editor.js index 3d0daae7c0..657943df86 100644 --- a/wp-includes/js/media-editor.js +++ b/wp-includes/js/media-editor.js @@ -205,7 +205,7 @@ args.post__not_in = attrs.exclude.split(','); if ( ! args.post__in ) - args.parent = attrs.id; + args.uploadedTo = attrs.id; // Collect the attributes that were not included in `args`. others = _.omit( attrs, 'id', 'ids', 'include', 'exclude', 'orderby', 'order' ); @@ -228,9 +228,9 @@ // the collection will already reflect those properties. attrs.ids = attachments.pluck('id'); - // Copy the `parent` post ID. - if ( props.parent ) - attrs.id = props.parent; + // Copy the `uploadedTo` post ID. + if ( props.uploadedTo ) + attrs.id = props.uploadedTo; // If the `ids` attribute is set and `orderby` attribute // is the default value, clear it for cleaner output. diff --git a/wp-includes/js/media-models.js b/wp-includes/js/media-models.js index 8207645788..e5ecfab9bf 100644 --- a/wp-includes/js/media-models.js +++ b/wp-includes/js/media-models.js @@ -316,11 +316,11 @@ window.wp = window.wp || {}; this.filters = options.filters || {}; // Bind default `change` events to the `props` model. + this.props.on( 'change', this._changeFilteredProps, this ); + this.props.on( 'change:order', this._changeOrder, this ); this.props.on( 'change:orderby', this._changeOrderby, this ); this.props.on( 'change:query', this._changeQuery, this ); - this.props.on( 'change:search', this._changeSearch, this ); - this.props.on( 'change:type', this._changeType, this ); // Set the `props` model and fill the default property values. this.props.set( _.defaults( options.props || {} ) ); @@ -359,16 +359,32 @@ window.wp = window.wp || {}; } }, - _changeFilteredProp: function( prop, model, term ) { + _changeFilteredProps: function( model, options ) { // If this is a query, updating the collection will be handled by // `this._requery()`. if ( this.props.get('query') ) return; - if ( term && ! this.filters[ prop ] ) - this.filters[ prop ] = Attachments.filters[ prop ]; - else if ( ! term && this.filters[ prop ] === Attachments.filters[ prop ] ) - delete this.filters[ prop ]; + var changed = _.chain( options.changes ).map( function( t, prop ) { + var filter = Attachments.filters[ prop ], + term = model.get( prop ); + + if ( ! filter ) + return; + + if ( term && ! this.filters[ prop ] ) + this.filters[ prop ] = filter; + else if ( ! term && this.filters[ prop ] === filter ) + delete this.filters[ prop ]; + else + return; + + // Record the change. + return true; + }, this ).any().value(); + + if ( ! changed ) + return; // If no `Attachments` model is provided to source the searches // from, then automatically generate a source from the existing @@ -379,14 +395,6 @@ window.wp = window.wp || {}; this.reset( this._source.filter( this.validator, this ) ); }, - _changeSearch: function( model, term ) { - return this._changeFilteredProp( 'search', model, term ); - }, - - _changeType: function( model, term ) { - return this._changeFilteredProp( 'type', model, term ); - }, - validator: function( attachment ) { return _.all( this.filters, function( filter, key ) { return !! filter.call( this, attachment ); @@ -499,7 +507,7 @@ window.wp = window.wp || {}; this.mirror( Query.get( this.props.toJSON() ) ); } }, { - comparator: function( a, b ) { + comparator: function( a, b, options ) { var key = this.props.get('orderby'), order = this.props.get('order') || 'DESC', ac = a.cid, @@ -513,6 +521,10 @@ window.wp = window.wp || {}; b = b || new Date(); } + // If `options.ties` is set, don't enforce the `cid` tiebreaker. + if ( options && options.ties ) + ac = bc = null; + return ( 'DESC' === order ) ? compare( a, b, ac, bc ) : compare( b, a, bc, ac ); }, @@ -531,10 +543,15 @@ window.wp = window.wp || {}; type: function( attachment ) { var type = this.props.get('type'); - if ( ! type ) + return ! type || -1 !== type.indexOf( attachment.get('type') ); + }, + + uploadedTo: function( attachment ) { + var uploadedTo = this.props.get('uploadedTo'); + if ( _.isUndefined( uploadedTo ) ) return true; - return -1 !== type.indexOf( attachment.get('type') ); + return uploadedTo === attachment.get('uploadedTo'); } } }); @@ -571,6 +588,9 @@ window.wp = window.wp || {}; this.created = new Date(); this.filters.order = function( attachment ) { + var orderby = this.props.get('orderby'), + order = this.props.get('order'); + if ( ! this.comparator ) return true; @@ -578,13 +598,18 @@ window.wp = window.wp || {}; // item in the set. If we add any items after the last // item, then we can't guarantee the set is complete. if ( this.length ) { - return 1 !== this.comparator( attachment, this.last() ); + return 1 !== this.comparator( attachment, this.last(), { ties: true }); // Handle the case where there are no items yet and // we're sorting for recent items. In that case, we want // changes that occurred after we created the query. - } else if ( 'DESC' === this.args.order && ( 'date' === this.args.orderby || 'modified' === this.args.orderby ) ) { - return attachment.get( this.args.orderby ) >= this.created; + } else if ( 'DESC' === order && ( 'date' === orderby || 'modified' === orderby ) ) { + return attachment.get( orderby ) >= this.created; + + // If we're sorting by menu order and we have no items, + // accept any items that have the default menu order (0). + } else if ( 'ASC' === order && 'menuOrder' === orderby ) { + return attachment.get( orderby ) === 0; } // Otherwise, we don't want any items yet. @@ -597,7 +622,7 @@ window.wp = window.wp || {}; // Only observe when a limited number of query args are set. There // are no filters for other properties, so observing will result in // false positives in those queries. - allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type' ]; + allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type', 'post_parent' ]; if ( wp.Uploader && _( this.args ).chain().keys().difference( allowed ).isEmpty().value() ) this.observe( wp.Uploader.queue ); }, @@ -670,9 +695,9 @@ window.wp = window.wp || {}; propmap: { 'search': 's', 'type': 'post_mime_type', - 'parent': 'post_parent', 'perPage': 'posts_per_page', - 'menuOrder': 'menu_order' + 'menuOrder': 'menu_order', + 'uploadedTo': 'post_parent' }, // Caches query objects so queries can be easily reused. diff --git a/wp-includes/js/media-views.js b/wp-includes/js/media-views.js index 4994c3e6df..c408e5413e 100644 --- a/wp-includes/js/media-views.js +++ b/wp-includes/js/media-views.js @@ -2920,7 +2920,7 @@ all: { text: l10n.allMediaItems, props: { - parent: null, + uploadedTo: null, orderby: 'date', order: 'DESC' }, @@ -2930,7 +2930,7 @@ uploaded: { text: l10n.uploadedToThisPost, props: { - parent: media.view.settings.postId, + uploadedTo: media.view.settings.postId, orderby: 'menuOrder', order: 'ASC' }, @@ -2948,7 +2948,7 @@ text: text, props: { type: key, - parent: null, + uploadedTo: null, orderby: 'date', order: 'DESC' } @@ -2959,7 +2959,7 @@ text: l10n.allMediaItems, props: { type: null, - parent: null, + uploadedTo: null, orderby: 'date', order: 'DESC' }, @@ -2970,7 +2970,7 @@ text: l10n.uploadedToThisPost, props: { type: null, - parent: media.view.settings.postId, + uploadedTo: media.view.settings.postId, orderby: 'menuOrder', order: 'ASC' }, diff --git a/wp-includes/js/plupload/wp-plupload.js b/wp-includes/js/plupload/wp-plupload.js index 3b545a4d00..9c631c2728 100644 --- a/wp-includes/js/plupload/wp-plupload.js +++ b/wp-includes/js/plupload/wp-plupload.js @@ -155,7 +155,9 @@ window.wp = window.wp || {}; file: file, uploading: true, date: new Date(), - filename: file.name + filename: file.name, + menuOrder: 0, + uploadedTo: media.model.settings.postId }, _.pick( file, 'loaded', 'size', 'percent' ) ); // Handle early mime type scanning for images.