diff --git a/src/wp-includes/js/media-views.js b/src/wp-includes/js/media-views.js index 0051bef32c..5713cf1fad 100644 --- a/src/wp-includes/js/media-views.js +++ b/src/wp-includes/js/media-views.js @@ -768,6 +768,150 @@ } }); + /** + * wp.media.controller.GalleryEdit + * + * @constructor + * @augments wp.media.controller.Library + * @augments wp.media.controller.State + * @augments Backbone.Model + */ + media.controller.GalleryEdit = media.controller.Library.extend({ + defaults: { + id: 'gallery-edit', + multiple: false, + describe: true, + edge: 199, + editing: false, + sortable: true, + searchable: false, + toolbar: 'gallery-edit', + content: 'browse', + title: l10n.editGalleryTitle, + priority: 60, + dragInfo: true, + + // Don't sync the selection, as the Edit Gallery library + // *is* the selection. + syncSelection: false + }, + + initialize: function() { + // If we haven't been provided a `library`, create a `Selection`. + if ( ! this.get('library') ) + this.set( 'library', new media.model.Selection() ); + + // The single `Attachment` view to be used in the `Attachments` view. + if ( ! this.get('AttachmentView') ) + this.set( 'AttachmentView', media.view.Attachment.EditLibrary ); + media.controller.Library.prototype.initialize.apply( this, arguments ); + }, + + activate: function() { + var library = this.get('library'); + + // Limit the library to images only. + library.props.set( 'type', 'image' ); + + // Watch for uploaded attachments. + this.get('library').observe( wp.Uploader.queue ); + + this.frame.on( 'content:render:browse', this.gallerySettings, this ); + + media.controller.Library.prototype.activate.apply( this, arguments ); + }, + + deactivate: function() { + // Stop watching for uploaded attachments. + this.get('library').unobserve( wp.Uploader.queue ); + + this.frame.off( 'content:render:browse', this.gallerySettings, this ); + + media.controller.Library.prototype.deactivate.apply( this, arguments ); + }, + + gallerySettings: function( browser ) { + var library = this.get('library'); + + if ( ! library || ! browser ) + return; + + library.gallery = library.gallery || new Backbone.Model(); + + browser.sidebar.set({ + gallery: new media.view.Settings.Gallery({ + controller: this, + model: library.gallery, + priority: 40 + }) + }); + + browser.toolbar.set( 'reverse', { + text: l10n.reverseOrder, + priority: 80, + + click: function() { + library.reset( library.toArray().reverse() ); + } + }); + } + }); + + /** + * wp.media.controller.GalleryAdd + * + * @constructor + * @augments wp.media.controller.Library + * @augments wp.media.controller.State + * @augments Backbone.Model + */ + media.controller.GalleryAdd = media.controller.Library.extend({ + defaults: _.defaults({ + id: 'gallery-library', + filterable: 'uploaded', + multiple: 'add', + menu: 'gallery', + toolbar: 'gallery-add', + title: l10n.addToGalleryTitle, + priority: 100, + + // Don't sync the selection, as the Edit Gallery library + // *is* the selection. + syncSelection: false + }, media.controller.Library.prototype.defaults ), + + initialize: function() { + // If we haven't been provided a `library`, create a `Selection`. + if ( ! this.get('library') ) + this.set( 'library', media.query({ type: 'image' }) ); + + media.controller.Library.prototype.initialize.apply( this, arguments ); + }, + + activate: function() { + var library = this.get('library'), + edit = this.frame.state('gallery-edit').get('library'); + + if ( this.editLibrary && this.editLibrary !== edit ) + library.unobserve( this.editLibrary ); + + // Accepts attachments that exist in the original library and + // that do not exist in gallery's library. + library.validator = function( attachment ) { + return !! this.mirroring.get( attachment.cid ) && ! edit.get( attachment.cid ) && media.model.Selection.prototype.validator.apply( this, arguments ); + }; + + // Reset the library to ensure that all attachments are re-added + // to the collection. Do so silently, as calling `observe` will + // trigger the `reset` event. + library.reset( library.mirroring.models, { silent: true }); + library.observe( edit ); + this.editLibrary = edit; + + media.controller.Library.prototype.activate.apply( this, arguments ); + } + }); + /** * wp.media.controller.CollectionEdit * @@ -1961,21 +2105,13 @@ new media.controller.EditImage( { model: options.editImage } ), // Gallery states. - new media.controller.CollectionEdit({ - type: 'image', - collectionType: 'gallery', - title: l10n.editGalleryTitle, - SettingsView: media.view.Settings.Gallery, - library: options.selection, - editing: options.editing, - menu: 'gallery' + new media.controller.GalleryEdit({ + library: options.selection, + editing: options.editing, + menu: 'gallery' }), - new media.controller.CollectionAdd({ - type: 'image', - collectionType: 'gallery', - title: l10n.addToGalleryTitle - }), + new media.controller.GalleryAdd(), new media.controller.Library({ id: 'playlist',