Make WordPress Core

Changeset 21820


Ignore:
Timestamp:
09/11/2012 09:13:07 PM (11 years ago)
Author:
koopersmith
Message:

Media JS: Add support for multiple views to the media workflow controller. see #21390.

Location:
trunk/wp-includes/js
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/js/media-models.js

    r21692 r21820  
    1717    media = wp.media = function( attributes ) {
    1818        if ( media.controller.Workflow )
    19             return new media.controller.Workflow( attributes ).render();
     19            return new media.controller.Workflow( attributes ).attach().render();
    2020    };
    2121
  • trunk/wp-includes/js/media-views.js

    r21814 r21820  
    1717    media.controller.Workflow = Backbone.Model.extend({
    1818        defaults: {
    19             multiple: false
     19            multiple: false,
     20            view:     'library'
    2021        },
    2122
     
    2324            this.createSelection();
    2425
    25             // Initialize views.
    26             this.modal     = new media.view.Modal({ controller: this });
    27             this.workspace = new media.view.Workspace({ controller: this });
     26            // Initialize view storage.
     27            this._views   = {};
     28            this._pending = {};
     29
     30            // Initialize modal container view.
     31            this.modal = new media.view.Modal({ controller: this });
     32
     33            // Add default views.
     34            this.add( 'library', {
     35                view: media.view.Workspace
     36            });
     37        },
     38
     39
     40        // Accepts an `id` and `options` for a view. Options is an object that
     41        // contains two keys: the `view` key is a `Backbone.View` constructor,
     42        // and the `options` key are the options to be passed when the view is
     43        // initialized.
     44        //
     45        // Triggers the `add` and `add:VIEW_ID` events.
     46        add: function( id, options ) {
     47            this.remove( id );
     48            this._pending[ id ] = options;
     49            this.trigger( 'add add:' + id, options );
     50            return this;
     51        },
     52
     53        // Returns a registered view instance. If an `id` is not provided,
     54        // it will return the active view.
     55        //
     56        // Lazily instantiates a registered view.
     57        //
     58        // Triggers the `init` and `init:VIEW_ID` events.
     59        view: function( id ) {
     60            var pending;
     61
     62            id = id || this.get('view');
     63            pending = this._pending[ id ];
     64
     65            if ( ! this._views[ id ] && pending ) {
     66                this._views[ id ] = new pending.view( _.extend({ controller: this }, pending.options || {} ) );
     67                delete this._pending[ id ];
     68                this.trigger( 'init init:' + id, this._views[ id ] );
     69            }
     70
     71            return this._views[ id ];
     72        },
     73
     74        // Unregisters a view from the workflow.
     75        //
     76        // Triggers the `remove` and `remove:VIEW_ID` events.
     77        remove: function( id ) {
     78            delete this._views[ id ];
     79            delete this._pending[ id ];
     80            this.trigger( 'remove remove:' + id );
     81            return this;
     82        },
     83
     84        // Renders a view and places it within the modal window.
     85        // Automatically adds a view if `options` are provided.
     86        render: function( id, options ) {
     87            var view;
     88            id = id || this.get('view');
     89
     90            if ( options )
     91                this.add( id, options );
     92
     93            view = this.view( id );
     94
     95            if ( ! view )
     96                return;
     97
     98            view.render();
     99            this.modal.content( view );
     100            return this;
    28101        },
    29102
     
    66139                }
    67140            });
    68         },
    69 
    70         render: function() {
    71             this.workspace.render();
    72             this.modal.content( this.workspace ).attach();
    73             return this;
    74         }
     141        }
     142    });
     143
     144    // Map modal methods to the workflow.
     145    _.each(['attach','detach','open','close'], function( method ) {
     146        media.controller.Workflow.prototype[ method ] = function() {
     147            this.modal[ method ].apply( this.modal, arguments );
     148            return this;
     149        };
    75150    });
    76151
Note: See TracChangeset for help on using the changeset viewer.