WordPress/wp-includes/js/media/utils/selection-sync.js

67 lines
1.8 KiB
JavaScript
Raw Normal View History

/*globals _ */
/**
* wp.media.selectionSync
*
* Sync an attachments selection in a state with another state.
*
* Allows for selecting multiple images in the Insert Media workflow, and then
* switching to the Insert Gallery workflow while preserving the attachments selection.
*
* @mixin
*/
var selectionSync = {
/**
* @since 3.5.0
*/
syncSelection: function() {
var selection = this.get('selection'),
manager = this.frame._selection;
if ( ! this.get('syncSelection') || ! manager || ! selection ) {
return;
}
// If the selection supports multiple items, validate the stored
// attachments based on the new selection's conditions. Record
// the attachments that are not included; we'll maintain a
// reference to those. Other attachments are considered in flux.
if ( selection.multiple ) {
selection.reset( [], { silent: true });
selection.validateAll( manager.attachments );
manager.difference = _.difference( manager.attachments.models, selection.models );
}
// Sync the selection's single item with the master.
selection.single( manager.single );
},
/**
* Record the currently active attachments, which is a combination
* of the selection's attachments and the set of selected
* attachments that this specific selection considered invalid.
* Reset the difference and record the single attachment.
*
* @since 3.5.0
*/
recordSelection: function() {
var selection = this.get('selection'),
manager = this.frame._selection;
if ( ! this.get('syncSelection') || ! manager || ! selection ) {
return;
}
if ( selection.multiple ) {
manager.attachments.reset( selection.toArray().concat( manager.difference ) );
manager.difference = [];
} else {
manager.attachments.add( selection.toArray() );
}
manager.single = selection._single;
}
};
module.exports = selectionSync;