From f07c7f51638bc6c61973157a9ed5be41affbcbbd Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Fri, 30 Nov 2012 15:19:11 +0000 Subject: [PATCH] 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: http://core.svn.wordpress.org/trunk@22952 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/meta-boxes.php | 4 +- wp-admin/js/custom-background.js | 2 +- wp-admin/js/custom-header.js | 2 +- wp-includes/js/media-editor.js | 4 +- wp-includes/js/media-models.js | 2 +- wp-includes/js/media-views.js | 71 ++++++++++--------- .../plugins/wpgallery/editor_plugin_src.js | 4 +- 7 files changed, 46 insertions(+), 43 deletions(-) diff --git a/wp-admin/includes/meta-boxes.php b/wp-admin/includes/meta-boxes.php index b9b92f5c04..ded66c2cc3 100644 --- a/wp-admin/includes/meta-boxes.php +++ b/wp-admin/includes/meta-boxes.php @@ -1020,7 +1020,7 @@ function post_thumbnail_meta_box( $post ) { $thumbnailId.val( thumbnailId ); if ( frame ) { - selection = frame.get('library').get('selection'); + selection = frame.state('library').get('selection'); if ( -1 === thumbnailId ) selection.clear(); @@ -1055,7 +1055,7 @@ function post_thumbnail_meta_box( $post ) { frame = wp.media( options ); - frame.get('library').set( 'filterable', 'uploaded' ); + frame.state('library').set( 'filterable', 'uploaded' ); frame.toolbar.on( 'activate:select', function() { frame.toolbar.view().set({ diff --git a/wp-admin/js/custom-background.js b/wp-admin/js/custom-background.js index 7b0c7090ca..5409b64991 100644 --- a/wp-admin/js/custom-background.js +++ b/wp-admin/js/custom-background.js @@ -57,7 +57,7 @@ }); }); - frame.state('library'); + frame.setState('library'); }); }); })(jQuery); \ No newline at end of file diff --git a/wp-admin/js/custom-header.js b/wp-admin/js/custom-header.js index 4cc55a71c0..9a806141eb 100644 --- a/wp-admin/js/custom-header.js +++ b/wp-admin/js/custom-header.js @@ -40,7 +40,7 @@ }); }); - frame.state('library'); + frame.setState('library'); }); }); }(jQuery)); diff --git a/wp-includes/js/media-editor.js b/wp-includes/js/media-editor.js index 657943df86..ea254e8ee8 100644 --- a/wp-includes/js/media-editor.js +++ b/wp-includes/js/media-editor.js @@ -393,11 +393,11 @@ }, 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 ); - workflow.get('embed').on( 'select', function() { + workflow.state('embed').on( 'select', function() { var embed = workflow.state().toJSON(); embed.url = embed.url || ''; diff --git a/wp-includes/js/media-models.js b/wp-includes/js/media-models.js index e5ecfab9bf..c3b6dc47d1 100644 --- a/wp-includes/js/media-models.js +++ b/wp-includes/js/media-models.js @@ -31,7 +31,7 @@ window.wp = window.wp || {}; delete attributes.frame; // Set the default state. - frame.state( frame.options.state ); + frame.setState( frame.options.state ); // Render, attach, and open the frame. return frame.render().attach().open(); }; diff --git a/wp-includes/js/media-views.js b/wp-includes/js/media-views.js index c408e5413e..c532e266dc 100644 --- a/wp-includes/js/media-views.js +++ b/wp-includes/js/media-views.js @@ -143,30 +143,27 @@ // Add events to the `StateMachine`. _.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. - get: function( id ) { + state: function( id ) { // Ensure that the `states` collection exists so the `StateMachine` // can be used as a mixin. 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 }); return this.states.get( id ); }, - // 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; - - if ( ! id ) - return this._state ? this.get( this._state ) : null; - - previous = this.state(); + // Sets the active state. + setState: function( id ) { + var previous = this.state(); // 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 @@ -176,15 +173,20 @@ if ( previous ) { previous.trigger('deactivate'); - this._previous = previous.id; + this._lastState = previous.id; } this._state = id; this.state().trigger('activate'); }, - previous: function() { - return this._previous; + // 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 ); } }); @@ -427,10 +429,10 @@ previous = this.previous('excludeState'); if ( previous ) - this.frame.get( previous ).off( 'change:library', this._excludeStateLibrary, this ); + this.frame.state( previous ).off( 'change:library', this._excludeStateLibrary, this ); if ( current ) - this.frame.get( current ).on( 'change:library', this._excludeStateLibrary, this ); + this.frame.state( current ).on( 'change:library', this._excludeStateLibrary, this ); }, _excludeStateLibrary: function() { @@ -439,7 +441,7 @@ if ( ! current ) 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 ) { var library = this.get('libraryState'); - this.frame.get( library ).get('selection').add( attachment ); - this.frame.state( library ); + this.frame.state( library ).get('selection').add( attachment ); + this.frame.setState( library ); } }); @@ -1122,7 +1124,7 @@ // Generate the tab states. _.each( tabs, function( title, id ) { - var frame = this.get( 'iframe:' + id ).set( _.defaults({ + var frame = this.state( 'iframe:' + id ).set( _.defaults({ tab: id, src: tabUrl + '&tab=' + id, title: title, @@ -1149,7 +1151,7 @@ _.each( media.view.settings.tabs, function( title, id ) { views[ 'iframe:' + id ] = { - text: this.get( 'iframe:' + id ).get('title'), + text: this.state( 'iframe:' + id ).get('title'), priority: 200 }; }, this ); @@ -1167,7 +1169,7 @@ window.tb_remove = function() { frame.close(); frame.reset(); - frame.state( frame.options.state ); + frame.setState( frame.options.state ); frame._tb_remove.call( window ); }; }, @@ -1322,7 +1324,7 @@ controller.state().trigger( options.event ); controller.reset(); if ( options.state ) - controller.state( options.state ); + controller.setState( options.state ); } } } @@ -1452,7 +1454,8 @@ }, galleryMenu: function() { - var previous = this.previous(), + var lastState = this.lastState(), + previous = lastState && lastState.id, frame = this; this.menu.view( new media.view.Menu({ @@ -1463,7 +1466,7 @@ priority: 20, click: function() { if ( previous ) - frame.state( previous ); + frame.setState( previous ); else frame.close(); } @@ -1581,7 +1584,7 @@ controller.reset(); // @todo: Make the state activated dynamic (instead of hardcoded). - controller.state('upload'); + controller.setState('upload'); } } } @@ -1600,7 +1603,7 @@ click: function() { var controller = this.controller, state = controller.state(), - edit = controller.get('gallery-edit'); + edit = controller.state('gallery-edit'); edit.get('library').add( state.get('selection').models ); state.trigger('reset'); @@ -2069,7 +2072,7 @@ controller.reset(); if ( options.state ) - controller.state( options.state ); + controller.setState( options.state ); } }); @@ -2105,7 +2108,7 @@ return function() { var controller = this.controller, selection = controller.state().get('selection'), - edit = controller.get( state ), + edit = controller.state( state ), models = filter ? filter( selection ) : selection.models; edit.set( 'library', new media.model.Selection( models, { @@ -2113,7 +2116,7 @@ multiple: true }) ); - this.controller.state( state ); + this.controller.setState( state ); }; }; @@ -2378,7 +2381,7 @@ if ( options.click ) options.click.call( this ); else if ( options.state ) - this.controller.state( options.state ); + this.controller.setState( options.state ); }, render: function() { diff --git a/wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js b/wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js index 49658d9fff..99434628e5 100644 --- a/wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js +++ b/wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js @@ -28,7 +28,7 @@ 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 ); ed.dom.setAttrib( el, 'title', shortcode ); }); @@ -49,7 +49,7 @@ }); } }); - + ed.onMouseDown.add(function(ed, e) { if ( e.target.nodeName == 'IMG' && ed.dom.hasClass(e.target, 'wpGallery') ) { ed.plugins.wordpress._hideButtons();