Media JS: Add support for filtering Attachment collections by mime type. see #21390, #21776, #21809.

git-svn-id: https://develop.svn.wordpress.org/trunk@22021 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Daryl Koopersmith 2012-09-26 20:50:13 +00:00
parent 5453d7d84d
commit 4a90e0541a
2 changed files with 26 additions and 8 deletions

View File

@ -1802,7 +1802,7 @@ function wp_ajax_get_attachment() {
*/
function wp_ajax_query_attachments() {
$query = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array();
$query = array_intersect_key( $query, array_flip( array( 's', 'order', 'orderby', 'posts_per_page', 'paged' ) ) );
$query = array_intersect_key( $query, array_flip( array( 's', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type' ) ) );
$query['post_type'] = 'attachment';
$query['post_status'] = 'inherit';

View File

@ -224,6 +224,7 @@ if ( typeof wp === 'undefined' )
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 || {}, {
@ -264,15 +265,15 @@ if ( typeof wp === 'undefined' )
}
},
_changeSearch: function( model, term ) {
_changeFilteredProp: function( prop, model, term ) {
// Bail if we're currently searching for the same term.
if ( this.props.get('search') === term )
if ( this.props.get( prop ) === term )
return;
if ( term && ! this.filters.search )
this.filters.search = Attachments.filters.search;
else if ( ! term && this.filters.search === Attachments.filters.search )
delete this.filters.search;
if ( term && ! this.filters[ prop ] )
this.filters[ prop ] = Attachments.filters[ prop ];
else if ( ! term && this.filters[ prop ] === Attachments.filters[ prop ] )
delete this.filters[ prop ];
// If no `Attachments` model is provided to source the searches
// from, then automatically generate a source from the existing
@ -283,6 +284,14 @@ if ( typeof wp === 'undefined' )
this.reset( this.props.get('source').filter( this.validator ) );
},
_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 );
@ -380,6 +389,14 @@ if ( typeof wp === 'undefined' )
var value = attachment.get( key );
return value && -1 !== value.search( this.props.get('search') );
}, this );
},
type: function( attachment ) {
var type = this.props.get('type');
if ( ! type )
return true;
return -1 !== type.indexOf( attachment.get('type') );
}
}
});
@ -497,7 +514,8 @@ if ( typeof wp === 'undefined' )
},
propmap: {
'search': 's'
'search': 's',
'type': 'post_mime_type'
},
// Caches query objects so queries can be easily reused.