<!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>[22952] trunk: Refine media state machine methods.</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/22952">22952</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2012-11-30 15:19:11 +0000 (Fri, 30 Nov 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Refine media state machine methods.

* state( id ) becomes setState( id )
* get( id ) becomes state( id )
* state() stays the same
* previous() becomes lastState()

Props koopersmith
fixes <a href="http://core.trac.wordpress.org/ticket/22652">#22652</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludesmetaboxesphp">trunk/wp-admin/includes/meta-boxes.php</a></li>
<li><a href="#trunkwpadminjscustombackgroundjs">trunk/wp-admin/js/custom-background.js</a></li>
<li><a href="#trunkwpadminjscustomheaderjs">trunk/wp-admin/js/custom-header.js</a></li>
<li><a href="#trunkwpincludesjsmediaeditorjs">trunk/wp-includes/js/media-editor.js</a></li>
<li><a href="#trunkwpincludesjsmediamodelsjs">trunk/wp-includes/js/media-models.js</a></li>
<li><a href="#trunkwpincludesjsmediaviewsjs">trunk/wp-includes/js/media-views.js</a></li>
<li><a href="#trunkwpincludesjstinymcepluginswpgalleryeditor_plugin_srcjs">trunk/wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludesmetaboxesphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/meta-boxes.php (22951 => 22952)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/meta-boxes.php        2012-11-30 15:11:44 UTC (rev 22951)
+++ trunk/wp-admin/includes/meta-boxes.php        2012-11-30 15:19:11 UTC (rev 22952)
</span><span class="lines">@@ -1020,7 +1020,7 @@
</span><span class="cx">                         $thumbnailId.val( thumbnailId );
</span><span class="cx"> 
</span><span class="cx">                         if ( frame ) {
</span><del>-                                selection = frame.get('library').get('selection');
</del><ins>+                                selection = frame.state('library').get('selection');
</ins><span class="cx"> 
</span><span class="cx">                                 if ( -1 === thumbnailId )
</span><span class="cx">                                         selection.clear();
</span><span class="lines">@@ -1055,7 +1055,7 @@
</span><span class="cx"> 
</span><span class="cx">                         frame = wp.media( options );
</span><span class="cx"> 
</span><del>-                        frame.get('library').set( 'filterable', 'uploaded' );
</del><ins>+                        frame.state('library').set( 'filterable', 'uploaded' );
</ins><span class="cx"> 
</span><span class="cx">                         frame.toolbar.on( 'activate:select', function() {
</span><span class="cx">                                 frame.toolbar.view().set({
</span></span></pre></div>
<a id="trunkwpadminjscustombackgroundjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/js/custom-background.js (22951 => 22952)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/js/custom-background.js        2012-11-30 15:11:44 UTC (rev 22951)
+++ trunk/wp-admin/js/custom-background.js        2012-11-30 15:19:11 UTC (rev 22952)
</span><span class="lines">@@ -57,7 +57,7 @@
</span><span class="cx">                                 });
</span><span class="cx">                         });
</span><span class="cx"> 
</span><del>-                        frame.state('library');
</del><ins>+                        frame.setState('library');
</ins><span class="cx">                 });
</span><span class="cx">         });
</span><span class="cx"> })(jQuery);
</span><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkwpadminjscustomheaderjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/js/custom-header.js (22951 => 22952)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/js/custom-header.js        2012-11-30 15:11:44 UTC (rev 22951)
+++ trunk/wp-admin/js/custom-header.js        2012-11-30 15:19:11 UTC (rev 22952)
</span><span class="lines">@@ -40,7 +40,7 @@
</span><span class="cx">                                 });
</span><span class="cx">                         });
</span><span class="cx"> 
</span><del>-                        frame.state('library');
</del><ins>+                        frame.setState('library');
</ins><span class="cx">                 });
</span><span class="cx">         });
</span><span class="cx"> }(jQuery));
</span></span></pre></div>
<a id="trunkwpincludesjsmediaeditorjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-editor.js (22951 => 22952)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-editor.js        2012-11-30 15:11:44 UTC (rev 22951)
+++ trunk/wp-includes/js/media-editor.js        2012-11-30 15:19:11 UTC (rev 22952)
</span><span class="lines">@@ -393,11 +393,11 @@
</span><span class="cx">                                 }, this );
</span><span class="cx">                         }, this );
</span><span class="cx"> 
</span><del>-                        workflow.get('gallery-edit').on( 'update', function( selection ) {
</del><ins>+                        workflow.state('gallery-edit').on( 'update', function( selection ) {
</ins><span class="cx">                                 this.insert( wp.media.gallery.shortcode( selection ).string() );
</span><span class="cx">                         }, this );
</span><span class="cx"> 
</span><del>-                        workflow.get('embed').on( 'select', function() {
</del><ins>+                        workflow.state('embed').on( 'select', function() {
</ins><span class="cx">                                 var embed = workflow.state().toJSON();
</span><span class="cx"> 
</span><span class="cx">                                 embed.url = embed.url || '';
</span></span></pre></div>
<a id="trunkwpincludesjsmediamodelsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-models.js (22951 => 22952)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-models.js        2012-11-30 15:11:44 UTC (rev 22951)
+++ trunk/wp-includes/js/media-models.js        2012-11-30 15:19:11 UTC (rev 22952)
</span><span class="lines">@@ -31,7 +31,7 @@
</span><span class="cx"> 
</span><span class="cx">                 delete attributes.frame;
</span><span class="cx">                 // Set the default state.
</span><del>-                frame.state( frame.options.state );
</del><ins>+                frame.setState( frame.options.state );
</ins><span class="cx">                 // Render, attach, and open the frame.
</span><span class="cx">                 return frame.render().attach().open();
</span><span class="cx">         };
</span></span></pre></div>
<a id="trunkwpincludesjsmediaviewsjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/media-views.js (22951 => 22952)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/media-views.js        2012-11-30 15:11:44 UTC (rev 22951)
+++ trunk/wp-includes/js/media-views.js        2012-11-30 15:19:11 UTC (rev 22952)
</span><span class="lines">@@ -143,31 +143,28 @@
</span><span class="cx">         // Add events to the `StateMachine`.
</span><span class="cx">         _.extend( media.controller.StateMachine.prototype, Backbone.Events, {
</span><span class="cx"> 
</span><del>-                // Fetch a state model.
</del><ins>+                // Fetch a state.
</ins><span class="cx">                 //
</span><ins>+                // If no `id` is provided, returns the active state.
+                //
</ins><span class="cx">                 // Implicitly creates states.
</span><del>-                get: function( id ) {
</del><ins>+                state: function( id ) {
</ins><span class="cx">                         // Ensure that the `states` collection exists so the `StateMachine`
</span><span class="cx">                         // can be used as a mixin.
</span><span class="cx">                         this.states = this.states || new Backbone.Collection();
</span><span class="cx"> 
</span><del>-                        if ( ! this.states.get( id ) )
</del><ins>+                        // Default to the active state.
+                        id = id || this._state;
+
+                        if ( id &amp;&amp; ! this.states.get( id ) )
</ins><span class="cx">                                 this.states.add({ id: id });
</span><span class="cx">                         return this.states.get( id );
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                // Selects or returns the active state.
-                //
-                // If a `id` is provided, sets that as the current state.
-                // If no parameters are provided, returns the current state object.
-                state: function( id ) {
-                        var previous;
</del><ins>+                // Sets the active state.
+                setState: function( id ) {
+                        var previous = this.state();
</ins><span class="cx"> 
</span><del>-                        if ( ! id )
-                                return this._state ? this.get( this._state ) : null;
-
-                        previous = this.state();
-
</del><span class="cx">                         // Bail if we're trying to select the current state, if we haven't
</span><span class="cx">                         // created the `states` collection, or are trying to select a state
</span><span class="cx">                         // that does not exist.
</span><span class="lines">@@ -176,15 +173,20 @@
</span><span class="cx"> 
</span><span class="cx">                         if ( previous ) {
</span><span class="cx">                                 previous.trigger('deactivate');
</span><del>-                                this._previous = previous.id;
</del><ins>+                                this._lastState = previous.id;
</ins><span class="cx">                         }
</span><span class="cx"> 
</span><span class="cx">                         this._state = id;
</span><span class="cx">                         this.state().trigger('activate');
</span><span class="cx">                 },
</span><span class="cx"> 
</span><del>-                previous: function() {
-                        return this._previous;
</del><ins>+                // Returns the previous active state.
+                //
+                // Call the `state()` method with no parameters to retrieve the current
+                // active state.
+                lastState: function() {
+                        if ( this._lastState )
+                                return this.state( this._lastState );
</ins><span class="cx">                 }
</span><span class="cx">         });
</span><span class="cx"> 
</span><span class="lines">@@ -427,10 +429,10 @@
</span><span class="cx">                                 previous = this.previous('excludeState');
</span><span class="cx"> 
</span><span class="cx">                         if ( previous )
</span><del>-                                this.frame.get( previous ).off( 'change:library', this._excludeStateLibrary, this );
</del><ins>+                                this.frame.state( previous ).off( 'change:library', this._excludeStateLibrary, this );
</ins><span class="cx"> 
</span><span class="cx">                         if ( current )
</span><del>-                                this.frame.get( current ).on( 'change:library', this._excludeStateLibrary, this );
</del><ins>+                                this.frame.state( current ).on( 'change:library', this._excludeStateLibrary, this );
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 _excludeStateLibrary: function() {
</span><span class="lines">@@ -439,7 +441,7 @@
</span><span class="cx">                         if ( ! current )
</span><span class="cx">                                 return;
</span><span class="cx"> 
</span><del>-                        this.set( 'exclude', this.frame.get( current ).get('library') );
</del><ins>+                        this.set( 'exclude', this.frame.state( current ).get('library') );
</ins><span class="cx">                 }
</span><span class="cx">         });
</span><span class="cx"> 
</span><span class="lines">@@ -474,8 +476,8 @@
</span><span class="cx">                 uploading: function( attachment ) {
</span><span class="cx">                         var library = this.get('libraryState');
</span><span class="cx"> 
</span><del>-                        this.frame.get( library ).get('selection').add( attachment );
-                        this.frame.state( library );
</del><ins>+                        this.frame.state( library ).get('selection').add( attachment );
+                        this.frame.setState( library );
</ins><span class="cx">                 }
</span><span class="cx">         });
</span><span class="cx"> 
</span><span class="lines">@@ -1122,7 +1124,7 @@
</span><span class="cx"> 
</span><span class="cx">                         // Generate the tab states.
</span><span class="cx">                         _.each( tabs, function( title, id ) {
</span><del>-                                var frame = this.get( 'iframe:' + id ).set( _.defaults({
</del><ins>+                                var frame = this.state( 'iframe:' + id ).set( _.defaults({
</ins><span class="cx">                                         tab:     id,
</span><span class="cx">                                         src:     tabUrl + '&amp;tab=' + id,
</span><span class="cx">                                         title:   title,
</span><span class="lines">@@ -1149,7 +1151,7 @@
</span><span class="cx"> 
</span><span class="cx">                         _.each( media.view.settings.tabs, function( title, id ) {
</span><span class="cx">                                 views[ 'iframe:' + id ] = {
</span><del>-                                        text: this.get( 'iframe:' + id ).get('title'),
</del><ins>+                                        text: this.state( 'iframe:' + id ).get('title'),
</ins><span class="cx">                                         priority: 200
</span><span class="cx">                                 };
</span><span class="cx">                         }, this );
</span><span class="lines">@@ -1167,7 +1169,7 @@
</span><span class="cx">                         window.tb_remove = function() {
</span><span class="cx">                                 frame.close();
</span><span class="cx">                                 frame.reset();
</span><del>-                                frame.state( frame.options.state );
</del><ins>+                                frame.setState( frame.options.state );
</ins><span class="cx">                                 frame._tb_remove.call( window );
</span><span class="cx">                         };
</span><span class="cx">                 },
</span><span class="lines">@@ -1322,7 +1324,7 @@
</span><span class="cx">                                                         controller.state().trigger( options.event );
</span><span class="cx">                                                         controller.reset();
</span><span class="cx">                                                         if ( options.state )
</span><del>-                                                                controller.state( options.state );
</del><ins>+                                                                controller.setState( options.state );
</ins><span class="cx">                                                 }
</span><span class="cx">                                         }
</span><span class="cx">                                 }
</span><span class="lines">@@ -1452,7 +1454,8 @@
</span><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 galleryMenu: function() {
</span><del>-                        var previous = this.previous(),
</del><ins>+                        var lastState = this.lastState(),
+                                previous = lastState &amp;&amp; lastState.id,
</ins><span class="cx">                                 frame = this;
</span><span class="cx"> 
</span><span class="cx">                         this.menu.view( new media.view.Menu({
</span><span class="lines">@@ -1463,7 +1466,7 @@
</span><span class="cx">                                                 priority: 20,
</span><span class="cx">                                                 click:    function() {
</span><span class="cx">                                                         if ( previous )
</span><del>-                                                                frame.state( previous );
</del><ins>+                                                                frame.setState( previous );
</ins><span class="cx">                                                         else
</span><span class="cx">                                                                 frame.close();
</span><span class="cx">                                                 }
</span><span class="lines">@@ -1581,7 +1584,7 @@
</span><span class="cx"> 
</span><span class="cx">                                                         controller.reset();
</span><span class="cx">                                                         // @todo: Make the state activated dynamic (instead of hardcoded).
</span><del>-                                                        controller.state('upload');
</del><ins>+                                                        controller.setState('upload');
</ins><span class="cx">                                                 }
</span><span class="cx">                                         }
</span><span class="cx">                                 }
</span><span class="lines">@@ -1600,7 +1603,7 @@
</span><span class="cx">                                                 click: function() {
</span><span class="cx">                                                         var controller = this.controller,
</span><span class="cx">                                                                 state = controller.state(),
</span><del>-                                                                edit = controller.get('gallery-edit');
</del><ins>+                                                                edit = controller.state('gallery-edit');
</ins><span class="cx"> 
</span><span class="cx">                                                         edit.get('library').add( state.get('selection').models );
</span><span class="cx">                                                         state.trigger('reset');
</span><span class="lines">@@ -2069,7 +2072,7 @@
</span><span class="cx">                                 controller.reset();
</span><span class="cx"> 
</span><span class="cx">                         if ( options.state )
</span><del>-                                controller.state( options.state );
</del><ins>+                                controller.setState( options.state );
</ins><span class="cx">                 }
</span><span class="cx">         });
</span><span class="cx"> 
</span><span class="lines">@@ -2105,7 +2108,7 @@
</span><span class="cx">                                 return function() {
</span><span class="cx">                                         var controller = this.controller,
</span><span class="cx">                                                 selection = controller.state().get('selection'),
</span><del>-                                                edit = controller.get( state ),
</del><ins>+                                                edit = controller.state( state ),
</ins><span class="cx">                                                 models = filter ? filter( selection ) : selection.models;
</span><span class="cx"> 
</span><span class="cx">                                         edit.set( 'library', new media.model.Selection( models, {
</span><span class="lines">@@ -2113,7 +2116,7 @@
</span><span class="cx">                                                 multiple: true
</span><span class="cx">                                         }) );
</span><span class="cx"> 
</span><del>-                                        this.controller.state( state );
</del><ins>+                                        this.controller.setState( state );
</ins><span class="cx">                                 };
</span><span class="cx">                         };
</span><span class="cx"> 
</span><span class="lines">@@ -2378,7 +2381,7 @@
</span><span class="cx">                         if ( options.click )
</span><span class="cx">                                 options.click.call( this );
</span><span class="cx">                         else if ( options.state )
</span><del>-                                this.controller.state( options.state );
</del><ins>+                                this.controller.setState( options.state );
</ins><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 render: function() {
</span></span></pre></div>
<a id="trunkwpincludesjstinymcepluginswpgalleryeditor_plugin_srcjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js (22951 => 22952)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js        2012-11-30 15:11:44 UTC (rev 22951)
+++ trunk/wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js        2012-11-30 15:19:11 UTC (rev 22952)
</span><span class="lines">@@ -28,7 +28,7 @@
</span><span class="cx"> 
</span><span class="cx">                                 frame = gallery.edit( '[' + ed.dom.getAttrib( el, 'title' ) + ']' );
</span><span class="cx"> 
</span><del>-                                frame.get('gallery-edit').on( 'update', function( selection ) {
</del><ins>+                                frame.state('gallery-edit').on( 'update', function( selection ) {
</ins><span class="cx">                                         var shortcode = gallery.shortcode( selection ).string().slice( 1, -1 );
</span><span class="cx">                                         ed.dom.setAttrib( el, 'title', shortcode );
</span><span class="cx">                                 });
</span><span class="lines">@@ -49,7 +49,7 @@
</span><span class="cx">                                         });
</span><span class="cx">                                 }
</span><span class="cx">                         });
</span><del>-                        
</del><ins>+
</ins><span class="cx">                         ed.onMouseDown.add(function(ed, e) {
</span><span class="cx">                                 if ( e.target.nodeName == 'IMG' &amp;&amp; ed.dom.hasClass(e.target, 'wpGallery') ) {
</span><span class="cx">                                         ed.plugins.wordpress._hideButtons();
</span></span></pre>
</div>
</div>

</body>
</html>