+ var library, comparator;
+
+ // 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 );
+
+ library = this.get('library');
+ comparator = library.comparator;
+
+ // Overload the library's comparator to push items that are not in
+ // the mirrored query to the front of the aggregate collection.
+ library.comparator = function( a, b ) {
+ var aInQuery = !! this.mirroring.get( a.cid ),
+ bInQuery = !! this.mirroring.get( b.cid );
+
+ if ( ! aInQuery && bInQuery ) {
+ return -1;
+ } else if ( aInQuery && ! bInQuery ) {
+ return 1;
+ } else {
+ return comparator.apply( this, arguments );
+ }
+ };
+
+ // Add all items in the selection to the library, so any featured
+ // images that are not initially loaded still appear.
+ library.observe( this.get('selection') );
+ },
+
+ /**
+ * @since 3.5.0
+ */
+ activate: function() {
+ this.updateSelection();
+ this.frame.on( 'open', this.updateSelection, this );
+
+ media.controller.Library.prototype.activate.apply( this, arguments );
+ },
+
+ /**
+ * @since 3.5.0
+ */
+ deactivate: function() {
+ this.frame.off( 'open', this.updateSelection, this );
+
+ media.controller.Library.prototype.deactivate.apply( this, arguments );
+ },
+
+ /**
+ * @since 3.5.0
+ */
+ updateSelection: function() {
+ var selection = this.get('selection'),
+ id = media.view.settings.post.featuredImageId,
+ attachment;
+
+ if ( '' !== id && -1 !== id ) {
+ attachment = media.model.Attachment.get( id );
+ attachment.fetch();
+ }
+
+ selection.reset( attachment ? [ attachment ] : [] );
+ }
+ });
+
+ /**
+ * wp.media.controller.ReplaceImage
+ *
+ * A state for replacing an image.
+ *
+ * @class
+ * @augments wp.media.controller.Library
+ * @augments wp.media.controller.State
+ * @augments Backbone.Model
+ *
+ * @param {object} [attributes] The attributes hash passed to the state.
+ * @param {string} [attributes.id=replace-image] Unique identifier.
+ * @param {string} [attributes.title=Replace Image] Title for the state. Displays in the media menu and the frame's title region.
+ * @param {wp.media.model.Attachments} [attributes.library] The attachments collection to browse.
+ * If one is not supplied, a collection of all images will be created.
+ * @param {boolean} [attributes.multiple=false] Whether multi-select is enabled.
+ * @param {string} [attributes.content=upload] Initial mode for the content region.
+ * Overridden by persistent user setting if 'contentUserSetting' is true.
+ * @param {string} [attributes.menu=default] Initial mode for the menu region.
+ * @param {string} [attributes.router=browse] Initial mode for the router region.
+ * @param {string} [attributes.toolbar=replace] Initial mode for the toolbar region.
+ * @param {int} [attributes.priority=60] The priority for the state link in the media menu.
+ * @param {boolean} [attributes.searchable=true] Whether the library is searchable.
+ * @param {boolean|string} [attributes.filterable=uploaded] Whether the library is filterable, and if so what filters should be shown.
+ * Accepts 'all', 'uploaded', or 'unattached'.
+ * @param {boolean} [attributes.sortable=true] Whether the Attachments should be sortable. Depends on the orderby property being set to menuOrder on the attachments collection.
+ * @param {boolean} [attributes.autoSelect=true] Whether an uploaded attachment should be automatically added to the selection.
+ * @param {boolean} [attributes.describe=false] Whether to offer UI to describe attachments - e.g. captioning images in a gallery.
+ * @param {boolean} [attributes.contentUserSetting=true] Whether the content region's mode should be set and persisted per user.
+ * @param {boolean} [attributes.syncSelection=true] Whether the Attachments selection should be persisted from the last state.
+ */
+ media.controller.ReplaceImage = media.controller.Library.extend({
+ defaults: _.defaults({
+ id: 'replace-image',
+ title: l10n.replaceImageTitle,
+ multiple: false,
+ filterable: 'uploaded',
+ toolbar: 'replace',
+ menu: false,
+ priority: 60,
+ syncSelection: true
+ }, media.controller.Library.prototype.defaults ),
+
+ /**
+ * @since 3.9.0
+ *
+ * @param options
+ */
+ initialize: function( options ) {
+ var library, comparator;
+
+ this.image = options.image;
+ // 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 );
+
+ library = this.get('library');
+ comparator = library.comparator;
+
+ // Overload the library's comparator to push items that are not in
+ // the mirrored query to the front of the aggregate collection.
+ library.comparator = function( a, b ) {
+ var aInQuery = !! this.mirroring.get( a.cid ),
+ bInQuery = !! this.mirroring.get( b.cid );
+
+ if ( ! aInQuery && bInQuery ) {
+ return -1;
+ } else if ( aInQuery && ! bInQuery ) {
+ return 1;
+ } else {
+ return comparator.apply( this, arguments );
+ }
+ };
+
+ // Add all items in the selection to the library, so any featured
+ // images that are not initially loaded still appear.
+ library.observe( this.get('selection') );
+ },
+
+ /**
+ * @since 3.9.0
+ */
+ activate: function() {
+ this.updateSelection();
+ media.controller.Library.prototype.activate.apply( this, arguments );
+ },
+
+ /**
+ * @since 3.9.0
+ */
+ updateSelection: function() {
+ var selection = this.get('selection'),
+ attachment = this.image.attachment;
+
+ selection.reset( attachment ? [ attachment ] : [] );
+ }
+ });
+
+ /**
+ * wp.media.controller.EditImage
+ *
+ * A state for editing (cropping, etc.) an image.
+ *
+ * @class
+ * @augments wp.media.controller.State
+ * @augments Backbone.Model
+ *
+ * @param {object} attributes The attributes hash passed to the state.
+ * @param {wp.media.model.Attachment} attributes.model The attachment.
+ * @param {string} [attributes.id=edit-image] Unique identifier.
+ * @param {string} [attributes.title=Edit Image] Title for the state. Displays in the media menu and the frame's title region.
+ * @param {string} [attributes.content=edit-image] Initial mode for the content region.
+ * @param {string} [attributes.toolbar=edit-image] Initial mode for the toolbar region.
+ * @param {string} [attributes.menu=false] Initial mode for the menu region.
+ * @param {string} [attributes.url] Unused. @todo Consider removal.
+ */
+ media.controller.EditImage = media.controller.State.extend({
+ defaults: {
+ id: 'edit-image',
+ title: l10n.editImage,
+ menu: false,
+ toolbar: 'edit-image',
+ content: 'edit-image',
+ url: ''
+ },
+
+ /**
+ * @since 3.9.0
+ */
+ activate: function() {
+ this.listenTo( this.frame, 'toolbar:render:edit-image', this.toolbar );
+ },
+
+ /**
+ * @since 3.9.0
+ */
+ deactivate: function() {
+ this.stopListening( this.frame );
+ },
+
+ /**
+ * @since 3.9.0
+ */
+ toolbar: function() {
+ var frame = this.frame,
+ lastState = frame.lastState(),
+ previous = lastState && lastState.id;
+
+ frame.toolbar.set( new media.view.Toolbar({
+ controller: frame,
+ items: {
+ back: {
+ style: 'primary',
+ text: l10n.back,
+ priority: 20,
+ click: function() {
+ if ( previous ) {
+ frame.setState( previous );
+ } else {
+ frame.close();
+ }
+ }
+ }
+ }
+ }) );
+ }
+ });
+
+ /**
+ * wp.media.controller.MediaLibrary
+ *
+ * @class
+ * @augments wp.media.controller.Library
+ * @augments wp.media.controller.State
+ * @augments Backbone.Model
+ */
+ media.controller.MediaLibrary = media.controller.Library.extend({
+ defaults: _.defaults({
+ // Attachments browser defaults. @see media.view.AttachmentsBrowser
+ filterable: 'uploaded',
+
+ displaySettings: false,
+ priority: 80,
+ syncSelection: false
+ }, media.controller.Library.prototype.defaults ),
+
+ /**
+ * @since 3.9.0
+ *
+ * @param options
+ */
+ initialize: function( options ) {
+ this.media = options.media;
+ this.type = options.type;
+ this.set( 'library', media.query({ type: this.type }) );
+
+ media.controller.Library.prototype.initialize.apply( this, arguments );
+ },
+
+ /**
+ * @since 3.9.0
+ */
+ activate: function() {
+ // @todo this should use this.frame.
+ if ( media.frame.lastMime ) {
+ this.set( 'library', media.query({ type: media.frame.lastMime }) );
+ delete media.frame.lastMime;
+ }
+ media.controller.Library.prototype.activate.apply( this, arguments );
+ }
+ });
+
+ /**
+ * wp.media.controller.Embed
+ *
+ * A state for embedding media from a URL.
+ *
+ * @class
+ * @augments wp.media.controller.State
+ * @augments Backbone.Model
+ *
+ * @param {object} attributes The attributes hash passed to the state.
+ * @param {string} [attributes.id=embed] Unique identifier.
+ * @param {string} [attributes.title=Insert From URL] Title for the state. Displays in the media menu and the frame's title region.
+ * @param {string} [attributes.content=embed] Initial mode for the content region.
+ * @param {string} [attributes.menu=default] Initial mode for the menu region.
+ * @param {string} [attributes.toolbar=main-embed] Initial mode for the toolbar region.
+ * @param {string} [attributes.menu=false] Initial mode for the menu region.
+ * @param {int} [attributes.priority=120] The priority for the state link in the media menu.
+ * @param {string} [attributes.type=link] The type of embed. Currently only link is supported.
+ * @param {string} [attributes.url] The embed URL.
+ * @param {object} [attributes.metadata={}] Properties of the embed, which will override attributes.url if set.
+ */
+ media.controller.Embed = media.controller.State.extend({
+ defaults: {
+ id: 'embed',
+ title: l10n.insertFromUrlTitle,
+ content: 'embed',
+ menu: 'default',
+ toolbar: 'main-embed',
+ priority: 120,
+ type: 'link',
+ url: '',
+ metadata: {}
+ },
+
+ // The amount of time used when debouncing the scan.
+ sensitivity: 200,
+
+ initialize: function(options) {
+ this.metadata = options.metadata;