Skip to content

Commit

Permalink
Add a bulk edit mode.
Browse files Browse the repository at this point in the history
Add an option to wp.media.view.AttachmentsBrowser `bulkEdit`,
which defaults to false. When set to true, conditionally create
a SelectionBulkEdit view when multiple items are selected.

Multiple delete now possible.
  • Loading branch information
ericandrewlewis committed May 13, 2014
1 parent cb64848 commit dda70ad
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
52 changes: 52 additions & 0 deletions core-js-overrides/media-views.js
Expand Up @@ -2145,6 +2145,7 @@
filters: state.get('filterable'),
display: state.get('displaySettings'),
dragInfo: state.get('dragInfo'),
bulkEdit: true,

suggestedWidth: state.get('suggestedWidth'),
suggestedHeight: state.get('suggestedHeight'),
Expand Down Expand Up @@ -5521,6 +5522,7 @@
filters: false,
search: true,
display: false,
bulkEdit: false,

AttachmentView: media.view.Attachment.Library
});
Expand Down Expand Up @@ -5675,17 +5677,42 @@
}

selection.on( 'selection:single', this.createSingle, this );
if ( this.options.bulkEdit ) {
selection.on( 'selection:single', this.maybeCreateMultiple, this );
}
selection.on( 'selection:unsingle', this.disposeSingle, this );

if ( selection.single() ) {
this.createSingle();
}
},

maybeCreateMultiple: function() {
var sidebar = this.sidebar,
single = this.options.selection.single();

// Bail early if there isn't a multi-selection.
if ( this.options.selection.length <=1 ) {
sidebar.unset( 'selection' );
return;
}
sidebar.set( 'selection', new media.view.SelectionBulkEdit({
controller: this.controller,
collection: this.options.selection,
priority: -40
}).render() );
},

createSingle: function() {
var sidebar = this.sidebar,
single = this.options.selection.single();

// If bulk edit is enabled and more than one attachment has been
// selected, bail early.
if ( this.options.bulkEdit && this.options.selection.length > 1 ) {
return;
}

sidebar.set( 'details', new media.view.Attachment.Details({
controller: this.controller,
model: single,
Expand Down Expand Up @@ -5793,6 +5820,31 @@
}
});

/**
* wp.media.view.SelectionBulkEdit
*
* @constructor
* @augments wp.media.view.Selection
* @augments wp.media.View
* @augments wp.Backbone.View
* @augments Backbone.View
*/
media.view.SelectionBulkEdit = media.view.Selection.extend({
template: media.template('media-selection-bulk-edit'),
events: {
'click .clear-selection': 'clear',
'click .delete-selection': 'delete'
},

// Delete click handler.
delete: function( event ) {
event.preventDefault();
// Fold this string into the l10n object later.
if ( confirm( "You are about to permanently delete these items.\n'Cancel' to stop, 'OK' to delete." ) ) {
this.collection.invoke( 'destroy' );
}
}
});

/**
* wp.media.view.Attachment.Selection
Expand Down
10 changes: 10 additions & 0 deletions media-template.php
@@ -0,0 +1,10 @@
<script type="text/html" id="tmpl-media-selection-bulk-edit">
<div class="selection-info">
<span class="count"></span>
<# if ( data.clearable ) { #>
<a class="clear-selection" href="#"><?php _e('Clear'); ?></a>
<# } #>
<a class="delete-selection" href="#"><?php _e('Delete'); ?></a>
</div>
<div class="selection-view"></div>
</script>
14 changes: 14 additions & 0 deletions styles.css
Expand Up @@ -41,4 +41,18 @@
background: rgba(0,0,0,0.6);
color: #fff;
cursor: default;
}

#media-library .media-selection {
position: static;
}
#media-library .attachments-browser .media-selection {
height: auto;
}
#media-library .attachments-browser .media-selection .selection-view {
display: block;
}
#media-library .attachments-browser .media-selection .attachments {
position: static;
height: auto;
}
6 changes: 6 additions & 0 deletions wp-media-grid.php
Expand Up @@ -17,6 +17,8 @@ function __construct() {

add_action( 'load-upload.php', array( $this, 'media_grid' ) );
add_action( 'admin_init', array( $this, 'enqueue' ) );
add_action( 'wp_enqueue_media', array( $this, 'wp_enqueue_media' ) );

}


Expand Down Expand Up @@ -84,6 +86,10 @@ public function enqueue() {
wp_enqueue_style( 'wp-media-grid', plugins_url( 'styles.css', __FILE__ ) );
wp_enqueue_script( 'live-filter', plugins_url( 'libs/jquery.liveFilter.js', __FILE__ ) );
}

public function wp_enqueue_media() {
require_once( plugin_dir_path( __FILE__ ) . 'media-template.php' );
}
}

/**
Expand Down

0 comments on commit dda70ad

Please sign in to comment.