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 #22652


git-svn-id: https://develop.svn.wordpress.org/trunk@22952 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2012-11-30 15:19:11 +00:00
parent d7ec10376b
commit 06b9663f58
7 changed files with 46 additions and 43 deletions

View File

@ -1020,7 +1020,7 @@ function post_thumbnail_meta_box( $post ) {
$thumbnailId.val( thumbnailId ); $thumbnailId.val( thumbnailId );
if ( frame ) { if ( frame ) {
selection = frame.get('library').get('selection'); selection = frame.state('library').get('selection');
if ( -1 === thumbnailId ) if ( -1 === thumbnailId )
selection.clear(); selection.clear();
@ -1055,7 +1055,7 @@ function post_thumbnail_meta_box( $post ) {
frame = wp.media( options ); frame = wp.media( options );
frame.get('library').set( 'filterable', 'uploaded' ); frame.state('library').set( 'filterable', 'uploaded' );
frame.toolbar.on( 'activate:select', function() { frame.toolbar.on( 'activate:select', function() {
frame.toolbar.view().set({ frame.toolbar.view().set({

View File

@ -57,7 +57,7 @@
}); });
}); });
frame.state('library'); frame.setState('library');
}); });
}); });
})(jQuery); })(jQuery);

View File

@ -40,7 +40,7 @@
}); });
}); });
frame.state('library'); frame.setState('library');
}); });
}); });
}(jQuery)); }(jQuery));

View File

@ -393,11 +393,11 @@
}, this ); }, this );
}, this ); }, this );
workflow.get('gallery-edit').on( 'update', function( selection ) { workflow.state('gallery-edit').on( 'update', function( selection ) {
this.insert( wp.media.gallery.shortcode( selection ).string() ); this.insert( wp.media.gallery.shortcode( selection ).string() );
}, this ); }, this );
workflow.get('embed').on( 'select', function() { workflow.state('embed').on( 'select', function() {
var embed = workflow.state().toJSON(); var embed = workflow.state().toJSON();
embed.url = embed.url || ''; embed.url = embed.url || '';

View File

@ -31,7 +31,7 @@ window.wp = window.wp || {};
delete attributes.frame; delete attributes.frame;
// Set the default state. // Set the default state.
frame.state( frame.options.state ); frame.setState( frame.options.state );
// Render, attach, and open the frame. // Render, attach, and open the frame.
return frame.render().attach().open(); return frame.render().attach().open();
}; };

View File

@ -143,30 +143,27 @@
// Add events to the `StateMachine`. // Add events to the `StateMachine`.
_.extend( media.controller.StateMachine.prototype, Backbone.Events, { _.extend( media.controller.StateMachine.prototype, Backbone.Events, {
// Fetch a state model. // Fetch a state.
//
// If no `id` is provided, returns the active state.
// //
// Implicitly creates states. // Implicitly creates states.
get: function( id ) { state: function( id ) {
// Ensure that the `states` collection exists so the `StateMachine` // Ensure that the `states` collection exists so the `StateMachine`
// can be used as a mixin. // can be used as a mixin.
this.states = this.states || new Backbone.Collection(); this.states = this.states || new Backbone.Collection();
if ( ! this.states.get( id ) ) // Default to the active state.
id = id || this._state;
if ( id && ! this.states.get( id ) )
this.states.add({ id: id }); this.states.add({ id: id });
return this.states.get( id ); return this.states.get( id );
}, },
// Selects or returns the active state. // Sets the active state.
// setState: function( id ) {
// If a `id` is provided, sets that as the current state. var previous = this.state();
// If no parameters are provided, returns the current state object.
state: function( id ) {
var previous;
if ( ! id )
return this._state ? this.get( this._state ) : null;
previous = this.state();
// Bail if we're trying to select the current state, if we haven't // Bail if we're trying to select the current state, if we haven't
// created the `states` collection, or are trying to select a state // created the `states` collection, or are trying to select a state
@ -176,15 +173,20 @@
if ( previous ) { if ( previous ) {
previous.trigger('deactivate'); previous.trigger('deactivate');
this._previous = previous.id; this._lastState = previous.id;
} }
this._state = id; this._state = id;
this.state().trigger('activate'); this.state().trigger('activate');
}, },
previous: function() { // Returns the previous active state.
return this._previous; //
// Call the `state()` method with no parameters to retrieve the current
// active state.
lastState: function() {
if ( this._lastState )
return this.state( this._lastState );
} }
}); });
@ -427,10 +429,10 @@
previous = this.previous('excludeState'); previous = this.previous('excludeState');
if ( previous ) if ( previous )
this.frame.get( previous ).off( 'change:library', this._excludeStateLibrary, this ); this.frame.state( previous ).off( 'change:library', this._excludeStateLibrary, this );
if ( current ) if ( current )
this.frame.get( current ).on( 'change:library', this._excludeStateLibrary, this ); this.frame.state( current ).on( 'change:library', this._excludeStateLibrary, this );
}, },
_excludeStateLibrary: function() { _excludeStateLibrary: function() {
@ -439,7 +441,7 @@
if ( ! current ) if ( ! current )
return; return;
this.set( 'exclude', this.frame.get( current ).get('library') ); this.set( 'exclude', this.frame.state( current ).get('library') );
} }
}); });
@ -474,8 +476,8 @@
uploading: function( attachment ) { uploading: function( attachment ) {
var library = this.get('libraryState'); var library = this.get('libraryState');
this.frame.get( library ).get('selection').add( attachment ); this.frame.state( library ).get('selection').add( attachment );
this.frame.state( library ); this.frame.setState( library );
} }
}); });
@ -1122,7 +1124,7 @@
// Generate the tab states. // Generate the tab states.
_.each( tabs, function( title, id ) { _.each( tabs, function( title, id ) {
var frame = this.get( 'iframe:' + id ).set( _.defaults({ var frame = this.state( 'iframe:' + id ).set( _.defaults({
tab: id, tab: id,
src: tabUrl + '&tab=' + id, src: tabUrl + '&tab=' + id,
title: title, title: title,
@ -1149,7 +1151,7 @@
_.each( media.view.settings.tabs, function( title, id ) { _.each( media.view.settings.tabs, function( title, id ) {
views[ 'iframe:' + id ] = { views[ 'iframe:' + id ] = {
text: this.get( 'iframe:' + id ).get('title'), text: this.state( 'iframe:' + id ).get('title'),
priority: 200 priority: 200
}; };
}, this ); }, this );
@ -1167,7 +1169,7 @@
window.tb_remove = function() { window.tb_remove = function() {
frame.close(); frame.close();
frame.reset(); frame.reset();
frame.state( frame.options.state ); frame.setState( frame.options.state );
frame._tb_remove.call( window ); frame._tb_remove.call( window );
}; };
}, },
@ -1322,7 +1324,7 @@
controller.state().trigger( options.event ); controller.state().trigger( options.event );
controller.reset(); controller.reset();
if ( options.state ) if ( options.state )
controller.state( options.state ); controller.setState( options.state );
} }
} }
} }
@ -1452,7 +1454,8 @@
}, },
galleryMenu: function() { galleryMenu: function() {
var previous = this.previous(), var lastState = this.lastState(),
previous = lastState && lastState.id,
frame = this; frame = this;
this.menu.view( new media.view.Menu({ this.menu.view( new media.view.Menu({
@ -1463,7 +1466,7 @@
priority: 20, priority: 20,
click: function() { click: function() {
if ( previous ) if ( previous )
frame.state( previous ); frame.setState( previous );
else else
frame.close(); frame.close();
} }
@ -1581,7 +1584,7 @@
controller.reset(); controller.reset();
// @todo: Make the state activated dynamic (instead of hardcoded). // @todo: Make the state activated dynamic (instead of hardcoded).
controller.state('upload'); controller.setState('upload');
} }
} }
} }
@ -1600,7 +1603,7 @@
click: function() { click: function() {
var controller = this.controller, var controller = this.controller,
state = controller.state(), state = controller.state(),
edit = controller.get('gallery-edit'); edit = controller.state('gallery-edit');
edit.get('library').add( state.get('selection').models ); edit.get('library').add( state.get('selection').models );
state.trigger('reset'); state.trigger('reset');
@ -2069,7 +2072,7 @@
controller.reset(); controller.reset();
if ( options.state ) if ( options.state )
controller.state( options.state ); controller.setState( options.state );
} }
}); });
@ -2105,7 +2108,7 @@
return function() { return function() {
var controller = this.controller, var controller = this.controller,
selection = controller.state().get('selection'), selection = controller.state().get('selection'),
edit = controller.get( state ), edit = controller.state( state ),
models = filter ? filter( selection ) : selection.models; models = filter ? filter( selection ) : selection.models;
edit.set( 'library', new media.model.Selection( models, { edit.set( 'library', new media.model.Selection( models, {
@ -2113,7 +2116,7 @@
multiple: true multiple: true
}) ); }) );
this.controller.state( state ); this.controller.setState( state );
}; };
}; };
@ -2378,7 +2381,7 @@
if ( options.click ) if ( options.click )
options.click.call( this ); options.click.call( this );
else if ( options.state ) else if ( options.state )
this.controller.state( options.state ); this.controller.setState( options.state );
}, },
render: function() { render: function() {

View File

@ -28,7 +28,7 @@
frame = gallery.edit( '[' + ed.dom.getAttrib( el, 'title' ) + ']' ); frame = gallery.edit( '[' + ed.dom.getAttrib( el, 'title' ) + ']' );
frame.get('gallery-edit').on( 'update', function( selection ) { frame.state('gallery-edit').on( 'update', function( selection ) {
var shortcode = gallery.shortcode( selection ).string().slice( 1, -1 ); var shortcode = gallery.shortcode( selection ).string().slice( 1, -1 );
ed.dom.setAttrib( el, 'title', shortcode ); ed.dom.setAttrib( el, 'title', shortcode );
}); });