<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[22027] trunk/wp-includes/js/media-views.js: Media JS: Improve UX for which buttons show when inserting media into a post.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/22027">22027</a></dd>
<dt>Author</dt> <dd>koopersmith</dd>
<dt>Date</dt> <dd>2012-09-27 00:59:04 +0000 (Thu, 27 Sep 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Media JS: Improve UX for which buttons show when inserting media into a post.

'Insert into post' is the primary action when either one item is selected, or any number of non-image items are selected. If multiple images are selected, 'Insert into post' becomes secondary, and 'Create a new gallery' takes the primary spot.

Adds a method to `get` Button views from the Toolbar. Adds a model for certain button attributes to allow size, style, and text to be easily changed.

see <a href="http://core.trac.wordpress.org/ticket/21390">#21390</a>, <a href="http://core.trac.wordpress.org/ticket/21808">#21808</a>, <a href="http://core.trac.wordpress.org/ticket/21809">#21809</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesjsmediaviewsjs">trunk/wp-includes/js/media-views.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesjsmediaviewsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-views.js (22026 => 22027)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-views.js        2012-09-27 00:47:01 UTC (rev 22026)
+++ trunk/wp-includes/js/media-views.js        2012-09-27 00:59:04 UTC (rev 22027)
</span><span class="lines">@@ -289,6 +289,10 @@
</span><span class="cx">                         return this;
</span><span class="cx">                 },
</span><span class="cx"> 
</span><ins>+                get: function( id ) {
+                        return this._views[ id ];
+                },
+
</ins><span class="cx">                 remove: function( id, options ) {
</span><span class="cx">                         delete this._views[ id ];
</span><span class="cx">                         if ( ! options || ! options.silent )
</span><span class="lines">@@ -310,25 +314,43 @@
</span><span class="cx">                         'click': 'click'
</span><span class="cx">                 },
</span><span class="cx"> 
</span><ins>+                defaults: {
+                        text:  '',
+                        style: '',
+                        size:  'large'
+                },
+
</ins><span class="cx">                 initialize: function() {
</span><del>-                        _.defaults( this.options, {
-                                text:    '',
-                                classes: []
-                        });
</del><ins>+                        // Create a model with the provided `defaults`.
+                        this.model = new Backbone.Model( this.defaults );
+
+                        // If any of the `options` have a key from `defaults`, apply its
+                        // value to the `model` and remove it from the `options object.
+                        _.each( this.defaults, function( def, key ) {
+                                var value = this.options[ key ];
+                                if ( _.isUndefined( value ) )
+                                        return;
+
+                                this.model.set( key, value );
+                                delete this.options[ key ];
+                        }, this );
+
+                        this.model.on( 'change', this.render, this );
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 render: function() {
</span><span class="cx">                         var classes = [ 'button', this.className ];
</span><span class="cx"> 
</span><del>-                        if ( this.options.style )
-                                classes.push( 'button-' + this.options.style );
</del><ins>+                        if ( this.model.get('style') )
+                                classes.push( 'button-' + this.model.get('style') );
</ins><span class="cx"> 
</span><del>-                        if ( this.options.size )
-                                classes.push( 'button-' + this.options.size );
</del><ins>+                        if ( this.model.get('size') )
+                                classes.push( 'button-' + this.model.get('size') );
</ins><span class="cx"> 
</span><span class="cx">                         classes = classes.concat( this.options.classes );
</span><span class="cx">                         this.el.className = classes.join(' ');
</span><del>-                        this.$el.text( this.options.text );
</del><ins>+
+                        this.$el.text( this.model.get('text') );
</ins><span class="cx">                         return this;
</span><span class="cx">                 },
</span><span class="cx"> 
</span><span class="lines">@@ -441,19 +463,22 @@
</span><span class="cx">                                                 collection: this.controller.selection,
</span><span class="cx">                                                 priority: -40
</span><span class="cx">                                         }),
</span><del>-                                        'insert-into-post': {
-                                                style: 'primary',
-                                                text:  'Insert into post',
-                                                priority: 40
-                                        },
</del><ins>+
</ins><span class="cx">                                         'create-new-gallery': {
</span><span class="cx">                                                 style: 'primary',
</span><span class="cx">                                                 text:  'Create a new gallery',
</span><del>-                                                priority: 30,
</del><ins>+                                                priority: 40,
</ins><span class="cx">                                                 click:  function() {
</span><span class="cx">                                                         controller.render('gallery');
</span><span class="cx">                                                 }
</span><span class="cx">                                         },
</span><ins>+
+                                        'insert-into-post': {
+                                                // style: 'primary',
+                                                text:  'Insert into post',
+                                                priority: 30
+                                        },
+
</ins><span class="cx">                                         'add-to-gallery': {
</span><span class="cx">                                                 text:  'Add to gallery',
</span><span class="cx">                                                 priority: 20
</span><span class="lines">@@ -462,7 +487,19 @@
</span><span class="cx">                         });
</span><span class="cx"> 
</span><span class="cx">                         this.controller.selection.on( 'add remove', function() {
</span><del>-                                this.$el.toggleClass( 'with-toolbar', !! this.controller.selection.length );
</del><ins>+                                var count = this.controller.selection.length,
+                                        showGallery;
+
+                                this.$el.toggleClass( 'with-toolbar', !! count );
+
+                                // Check if every attachment in the selection is an image.
+                                showGallery = count &gt; 1 &amp;&amp; this.controller.selection.all( function( attachment ) {
+                                        return 'image' === attachment.get('type');
+                                });
+
+                                this.toolbarView.get('create-new-gallery').$el.toggle( showGallery );
+                                insert = this.toolbarView.get('insert-into-post');
+                                insert.model.set( 'style', showGallery ? '' : 'primary' );
</ins><span class="cx">                         }, this );
</span><span class="cx"> 
</span><span class="cx">                         this.$content.append( this.toolbarView.$el );
</span></span></pre>
</div>
</div>

</body>
</html>