From 78bcbde5e31a313f5c90833a2a1f9c6a1959197e Mon Sep 17 00:00:00 2001
From: Gregory Cornelius <gcorne@gmail.com>
Date: Fri, 28 Feb 2014 21:19:10 -0600
Subject: [PATCH 2/3] Refactor CollectionAdd and CollectionEdit
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Instead of generating new constructors use initialize method to set attributes based on the collectionType
* Use reference to the SettingsView instead of grabbing it from the namespace. This better matches with existing AttachmentDisplay attribute
* Switch from using ‘tag’ to ‘collectionType’ to better communicate what the purpose of the attribute is.
---
 src/wp-includes/js/media-views.js | 326 ++++++++++++++++++--------------------
 1 file changed, 153 insertions(+), 173 deletions(-)

diff --git src/wp-includes/js/media-views.js src/wp-includes/js/media-views.js
index 3ace225..d1fbc89 100644
--- src/wp-includes/js/media-views.js
+++ src/wp-includes/js/media-views.js
@@ -762,107 +762,107 @@
 	/**
 	 * wp.media.controller.CollectionEdit
 	 *
-	 * @static
-	 * @param {string} prop The shortcode slug
-	 * @param {object} args
-	 * @returns {wp.media.controller.Library}
+	 * @constructor
+	 * @augments wp.media.controller.Library
+	 * @augments wp.media.controller.State
+	 * @augments Backbone.Model
 	 */
-	media.controller.CollectionEdit = function ( prop, args ) {
-		/**
-		 * @constructor
-		 * @augments wp.media.controller.Library
-		 * @augments wp.media.controller.State
-		 * @augments Backbone.Model
-		 */
-		return media.controller.Library.extend({
-			defaults : _.defaults(args.defaults || {}, {
-				id:         prop + '-edit',
-				toolbar:    prop + '-edit',
-				multiple:   false,
-				describe:   true,
-				edge:       199,
-				editing:    false,
-				sortable:   true,
-				searchable: false,
-				content:    'browse',
-				priority:   60,
-				dragInfo:   true,
-
-				// Don't sync the selection, as the Edit {Collection} library
-				// *is* the selection.
-				syncSelection: false
-			}),
+	media.controller.CollectionEdit = media.controller.Library.extend({
+		defaults: {
+			multiple:     false,
+			describe:     true,
+			edge:         199,
+			editing:      false,
+			sortable:     true,
+			searchable:   false,
+			content:      'browse',
+			priority:     60,
+			dragInfo:     true,
+			SettingsView: false,
+
+			// Don't sync the selection, as the Edit {Collection} 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 );
-			},
+		initialize: function() {
+			var collectionType = this.get('collectionType');
 
-			activate: function() {
-				var library = this.get('library');
+			this.set( 'id', collectionType + '-edit' );
+			this.set( 'toolbar', collectionType + '-edit' );
 
-				// Limit the library to images only.
-				library.props.set( 'type', args.type );
+			// 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 );
+		},
 
-				// Watch for uploaded attachments.
-				this.get('library').observe( wp.Uploader.queue );
+		activate: function() {
+			var library = this.get('library');
 
-				this.frame.on( 'content:render:browse', this.settings, this );
+			// Limit the library to images only.
+			library.props.set( 'type', this.get( 'type' ) );
 
-				media.controller.Library.prototype.activate.apply( this, arguments );
-			},
+			// Watch for uploaded attachments.
+			this.get('library').observe( wp.Uploader.queue );
 
-			deactivate: function() {
-				// Stop watching for uploaded attachments.
-				this.get('library').unobserve( wp.Uploader.queue );
+			this.frame.on( 'content:render:browse', this.renderSettings, this );
 
-				this.frame.off( 'content:render:browse', this.settings, this );
+			media.controller.Library.prototype.activate.apply( this, arguments );
+		},
 
-				media.controller.Library.prototype.deactivate.apply( this, arguments );
-			},
+		deactivate: function() {
+			// Stop watching for uploaded attachments.
+			this.get('library').unobserve( wp.Uploader.queue );
 
-			settings: function( browser ) {
-				var library = this.get('library'), obj = {};
+			this.frame.off( 'content:render:browse', this.renderSettings, this );
 
-				if ( ! library || ! browser ) {
-					return;
-				}
+			media.controller.Library.prototype.deactivate.apply( this, arguments );
+		},
 
-				library[ prop ] = library[ prop ] || new Backbone.Model();
+		renderSettings: function( browser ) {
+			var library = this.get('library'),
+				collectionType = this.get('collectionType'),
+				dragInfoText = this.get('dragInfoText'),
+				SettingsView = this.get('SettingsView'),
+				obj = {};
 
-				obj[ prop ] = new media.view.Settings[ args.settings ]({
-					controller: this,
-					model:      library[ prop ],
-					priority:   40
-				});
+			if ( ! library || ! browser ) {
+				return;
+			}
 
-				browser.sidebar.set( obj );
+			library[ collectionType ] = library[ collectionType ] || new Backbone.Model();
 
-				if ( args.dragInfoText ) {
-					browser.toolbar.set( 'dragInfo', new media.View({
-						el: $( '<div class="instructions">' + args.dragInfoText + '</div>' )[0],
-						priority: -40
-					}) );
-				}
+			obj[ collectionType ] = new SettingsView({
+				controller: this,
+				model:      library[ collectionType ],
+				priority:   40
+			});
 
-				browser.toolbar.set( 'reverse', {
-					text:     l10n.reverseOrder,
-					priority: 80,
+			browser.sidebar.set( obj );
 
-					click: function() {
-						library.reset( library.toArray().reverse() );
-					}
-				});
+			if ( dragInfoText ) {
+				browser.toolbar.set( 'dragInfo', new media.View({
+					el: $( '<div class="instructions">' + dragInfoText + '</div>' )[0],
+					priority: -40
+				}) );
 			}
-		});
-	};
+
+			browser.toolbar.set( 'reverse', {
+				text:     l10n.reverseOrder,
+				priority: 80,
+
+				click: function() {
+					library.reset( library.toArray().reverse() );
+				}
+			});
+		}
+	});
 
 	/**
 	 * wp.media.controller.CollectionAdd
@@ -872,87 +872,51 @@
 	 * @augments wp.media.controller.State
 	 * @augments Backbone.Model
 	 */
-	media.controller.CollectionAdd = function (attributes) {
-		var ExtendedLibrary, extended = _.extend( attributes, {
-			defaults: _.defaults( {
-				id: attributes.tag + '-library',
-				title: attributes.title,
-				menu: attributes.tag,
-				toolbar: attributes.tag + '-add',
-				filterable: 'uploaded',
-				multiple:   'add',
-				priority:   100,
-				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: this.type }) );
-				}
-				media.controller.Library.prototype.initialize.apply( this, arguments );
-			},
+	media.controller.CollectionAdd = media.controller.Library.extend({
+		defaults: _.defaults( {
+			filterable:    'uploaded',
+			multiple:      'add',
+			priority:      100,
+			syncSelection: false
+		}, media.controller.Library.prototype.defaults ),
 
-			activate: function() {
-				var library = this.get('library'),
-					edit    = this.frame.state( this.tag + '-edit' ).get('library');
+		initialize: function() {
+			var collectionType = this.get('collectionType');
 
-				if ( this.editLibrary && this.editLibrary !== edit ) {
-					library.unobserve( this.editLibrary );
-				}
+			this.set( 'id', collectionType + '-library' );
+			this.set( 'toolbar', collectionType + '-add' );
+			this.set( 'menu', collectionType );
 
-				// 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 );
-				};
+			// If we haven't been provided a `library`, create a `Selection`.
+			if ( ! this.get('library') ) {
+				this.set( 'library', media.query({ type: this.get('type') }) );
+			}
+			media.controller.Library.prototype.initialize.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;
+		activate: function() {
+			var library = this.get('library'),
+				editLibrary = this.get('editLibrary'),
+				edit = this.frame.state( this.get('collectionType') + '-edit' ).get('library');
 
-				media.controller.Library.prototype.activate.apply( this, arguments );
+			if ( editLibrary && editLibrary !== edit ) {
+				library.unobserve( editLibrary );
 			}
-		} );
-		ExtendedLibrary = media.controller.Library.extend( extended );
-
-		return new ExtendedLibrary();
-	};
 
-	// wp.media.controller.GalleryEdit
-	// -------------------------------
-	media.controller.GalleryEdit = media.controller.CollectionEdit( 'gallery', {
-		type: 'image',
-		settings: 'Gallery',
-		defaults: {
-			title: l10n.editGalleryTitle
-		}
-	});
+			// 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 );
+			};
 
-	// wp.media.controller.PlaylistEdit
-	// -------------------------------
-	media.controller.PlaylistEdit = media.controller.CollectionEdit( 'playlist', {
-		type: 'audio',
-		settings: 'Playlist',
-		dragInfoText: l10n.playlistDragInfo,
-		defaults: {
-			title: l10n.editPlaylistTitle,
-			dragInfo : false
-		}
-	});
+			// 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.set('editLibrary', edit);
 
-	// wp.media.controller.VideoPlaylistEdit
-	// -------------------------------
-	media.controller.VideoPlaylistEdit = media.controller.CollectionEdit( 'video-playlist', {
-		type: 'video',
-		settings: 'Playlist',
-		dragInfoText: l10n.videoPlaylistDragInfo,
-		defaults: {
-			title: l10n.editVideoPlaylistTitle,
-			dragInfo : false
+			media.controller.Library.prototype.activate.apply( this, arguments );
 		}
 	});
 
@@ -1776,16 +1740,20 @@
 				new media.controller.Embed(),
 
 				// Gallery states.
-				new media.controller.GalleryEdit({
-					library: options.selection,
-					editing: options.editing,
-					menu:    'gallery'
+				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.CollectionAdd({
-					tag: 'gallery',
-					type: 'image',
-					title: l10n.addToGalleryTitle
+					type:           'image',
+					collectionType: 'gallery',
+					title:          l10n.addToGalleryTitle
 				}),
 
 				new media.controller.Library({
@@ -1803,15 +1771,21 @@
 				}),
 
 				// Playlist states.
-				new media.controller.PlaylistEdit({
-					library: options.selection,
-					editing: options.editing,
-					menu:    'playlist'
+				new media.controller.CollectionEdit({
+					type:           'audio',
+					collectionType: 'playlist',
+					title:          l10n.editPlaylistTitle,
+					SettingsView:   media.view.Settings.Playlist,
+					library:        options.selection,
+					editing:        options.editing,
+					menu:           'playlist',
+					dragInfoText:   l10n.playlistDragInfo,
+					dragInfo:       false
 				}),
 
 				new media.controller.CollectionAdd({
-					tag: 'playlist',
 					type: 'audio',
+					collectionType: 'playlist',
 					title: l10n.addToPlaylistTitle
 				}),
 
@@ -1830,16 +1804,22 @@
 				}),
 
 				// Video Playlist states.
-				new media.controller.VideoPlaylistEdit({
-					library: options.selection,
-					editing: options.editing,
-					menu:    'video-playlist'
+				new media.controller.CollectionEdit({
+					type:           'video',
+					collectionType: 'video-playlist',
+					title:          l10n.editVideoPlaylistTitle,
+					SettingsView:   media.view.Settings.Playlist,
+					library:        options.selection,
+					editing:        options.editing,
+					menu:           'video-playlist',
+					dragInfoText:   l10n.videoPlaylistDragInfo,
+					dragInfo:       false
 				}),
 
 				new media.controller.CollectionAdd({
-					tag: 'video-playlist',
-					type: 'video',
-					title: l10n.addToVideoPlaylistTitle
+					type:           'video',
+					collectionType: 'video-playlist',
+					title:          l10n.addToVideoPlaylistTitle
 				})
 			]);
 
-- 
1.8.5.4

