Make WordPress Core

Ticket #31846: 31846.diff

File 31846.diff, 12.8 KB (added by kucrut, 10 years ago)

Fix media grid's router

  • src/wp-includes/js/media-grid.js

    diff --git src/wp-includes/js/media-grid.js src/wp-includes/js/media-grid.js
    index 01db38b..77323aa 100644
    media.view.DeleteSelectedPermanentlyButton = require( './views/button/delete-sel 
    5151var Router = Backbone.Router.extend({
    5252        routes: {
    5353                'upload.php?item=:slug':    'showItem',
    54                 'upload.php?search=:query': 'search'
     54                'upload.php?search=:query': 'search',
     55                'upload.php':               'reset'
    5556        },
    5657
    5758        // Map routes against the page URL
    var Router = Backbone.Router.extend({ 
    5960                return 'upload.php' + url;
    6061        },
    6162
     63        reset: function( query ) {
     64                var frame = wp.media.frames.edit;
     65
     66                if ( frame ) {
     67                        frame.close();
     68                }
     69        },
     70
    6271        // Respond to the search route by filling the search field and trigggering the input event
    6372        search: function( query ) {
    6473                jQuery( '#media-search-input' ).val( query ).trigger( 'input' );
    var Router = Backbone.Router.extend({ 
    6776        // Show the modal with a specific item
    6877        showItem: function( query ) {
    6978                var media = wp.media,
    70                         library = media.frame.state().get('library'),
     79                        frame = media.frames.browse,
     80                        library = frame.state().get('library'),
    7181                        item;
    7282
    7383                // Trigger the media frame to open the correct item
    7484                item = library.findWhere( { id: parseInt( query, 10 ) } );
     85
    7586                if ( item ) {
    76                         media.frame.trigger( 'edit:attachment', item );
     87                        frame.trigger( 'edit:attachment', item );
    7788                } else {
    7889                        item = media.attachment( query );
    79                         media.frame.listenTo( item, 'change', function( model ) {
    80                                 media.frame.stopListening( item );
    81                                 media.frame.trigger( 'edit:attachment', model );
     90                        frame.listenTo( item, 'change', function( model ) {
     91                                frame.stopListening( item );
     92                                frame.trigger( 'edit:attachment', model );
    8293                        } );
    8394                        item.fetch();
    8495                }
    EditAttachments = MediaFrame.extend({ 
    401412                // Bind default title creation.
    402413                this.on( 'title:create:default', this.createTitle, this );
    403414
    404                 // Close the modal if the attachment is deleted.
    405                 this.listenTo( this.model, 'change:status destroy', this.close, this );
    406 
    407415                this.on( 'content:create:edit-metadata', this.editMetadataMode, this );
    408416                this.on( 'content:create:edit-image', this.editImageMode, this );
    409417                this.on( 'content:render:edit-image', this.editImageModeRender, this );
     418                this.on( 'refresh', this.rerender, this );
    410419                this.on( 'close', this.detach );
     420
     421                this.bindModelHandlers();
     422        },
     423
     424        bindModelHandlers: function() {
     425                // Close the modal if the attachment is deleted.
     426                this.listenTo( this.model, 'change:status destroy', this.close, this );
    411427        },
    412428
    413429        createModal: function() {
    EditAttachments = MediaFrame.extend({ 
    424440
    425441                        // Completely destroy the modal DOM element when closing it.
    426442                        this.modal.on( 'close', _.bind( function() {
    427                                 this.modal.remove();
    428443                                $( 'body' ).off( 'keydown.media-modal' ); /* remove the keydown event */
    429444                                // Restore the original focus item if possible
    430445                                $( 'li.attachment[data-id="' + this.model.get( 'id' ) +'"]' ).focus();
    EditAttachments = MediaFrame.extend({ 
    442457         */
    443458        createStates: function() {
    444459                this.states.add([
    445                         new wp.media.controller.EditAttachmentMetadata( { model: this.model } )
     460                        new wp.media.controller.EditAttachmentMetadata({
     461                                model:   this.model,
     462                                library: this.library
     463                        })
    446464                ]);
    447465        },
    448466
    EditAttachments = MediaFrame.extend({ 
    508526        /**
    509527         * Rerender the view.
    510528         */
    511         rerender: function() {
     529        rerender: function( model ) {
     530                this.stopListening( this.model );
     531
     532                this.model = model;
     533
     534                this.bindModelHandlers();
     535
    512536                // Only rerender the `content` region.
    513537                if ( this.content.mode() !== 'edit-metadata' ) {
    514538                        this.content.mode( 'edit-metadata' );
    EditAttachments = MediaFrame.extend({ 
    527551                        this.$( '.left' ).blur();
    528552                        return;
    529553                }
    530                 this.model = this.library.at( this.getCurrentIndex() - 1 );
    531                 this.rerender();
     554                this.trigger( 'refresh', this.library.at( this.getCurrentIndex() - 1 ) );
    532555                this.$( '.left' ).focus();
    533556        },
    534557
    EditAttachments = MediaFrame.extend({ 
    540563                        this.$( '.right' ).blur();
    541564                        return;
    542565                }
    543                 this.model = this.library.at( this.getCurrentIndex() + 1 );
    544                 this.rerender();
     566                this.trigger( 'refresh', this.library.at( this.getCurrentIndex() + 1 ) );
    545567                this.$( '.right' ).focus();
    546568        },
    547569
    Manage = MediaFrame.extend({ 
    662684                this.bindRegionModeHandlers();
    663685                this.render();
    664686                this.bindSearchHandler();
     687
     688                wp.media.frames.browse = this;
    665689        },
    666690
    667691        bindSearchHandler: function() {
    Manage = MediaFrame.extend({ 
    682706
    683707                // Update the URL when entering search string (at most once per second)
    684708                search.on( 'input', _.bind( input, this ) );
    685                 searchView.val( currentSearch ).trigger( 'input' );
    686 
    687                 this.gridRouter.on( 'route:search', function () {
    688                         var href = window.location.href;
    689                         if ( href.indexOf( 'mode=' ) > -1 ) {
    690                                 href = href.replace( /mode=[^&]+/g, 'mode=list' );
    691                         } else {
    692                                 href += href.indexOf( '?' ) > -1 ? '&mode=list' : '?mode=list';
    693                         }
    694                         href = href.replace( 'search=', 's=' );
    695                         listMode.prop( 'href', href );
    696                 } );
     709
     710                this.gridRouter
     711                        .on( 'route:search', function () {
     712                                var href = window.location.href;
     713                                if ( href.indexOf( 'mode=' ) > -1 ) {
     714                                        href = href.replace( /mode=[^&]+/g, 'mode=list' );
     715                                } else {
     716                                        href += href.indexOf( '?' ) > -1 ? '&mode=list' : '?mode=list';
     717                                }
     718                                href = href.replace( 'search=', 's=' );
     719                                listMode.prop( 'href', href );
     720                        })
     721                        .on( 'route:reset', function() {
     722                                searchView.val( '' ).trigger( 'input' );
     723                        });
    697724        },
    698725
    699726        /**
    Manage = MediaFrame.extend({ 
    781808         */
    782809        openEditAttachmentModal: function( model ) {
    783810                // Create a new EditAttachment frame, passing along the library and the attachment model.
    784                 wp.media( {
    785                         frame:       'edit-attachments',
    786                         controller:  this,
    787                         library:     this.state().get('library'),
    788                         model:       model
    789                 } );
     811                if ( wp.media.frames.edit ) {
     812                        wp.media.frames.edit.open().trigger( 'refresh', model );
     813                } else {
     814                        wp.media.frames.edit = wp.media( {
     815                                frame:       'edit-attachments',
     816                                controller:  this,
     817                                library:     this.state().get('library'),
     818                                model:       model
     819                        } );
     820                }
    790821        },
    791822
    792823        /**
  • src/wp-includes/js/media/routers/manage.js

    diff --git src/wp-includes/js/media/routers/manage.js src/wp-includes/js/media/routers/manage.js
    index 7395969..1b6f483 100644
     
    99var Router = Backbone.Router.extend({
    1010        routes: {
    1111                'upload.php?item=:slug':    'showItem',
    12                 'upload.php?search=:query': 'search'
     12                'upload.php?search=:query': 'search',
     13                'upload.php':               'reset'
    1314        },
    1415
    1516        // Map routes against the page URL
    var Router = Backbone.Router.extend({ 
    1718                return 'upload.php' + url;
    1819        },
    1920
     21        reset: function( query ) {
     22                var frame = wp.media.frames.edit;
     23
     24                if ( frame ) {
     25                        frame.close();
     26                }
     27        },
     28
    2029        // Respond to the search route by filling the search field and trigggering the input event
    2130        search: function( query ) {
    2231                jQuery( '#media-search-input' ).val( query ).trigger( 'input' );
    var Router = Backbone.Router.extend({ 
    2534        // Show the modal with a specific item
    2635        showItem: function( query ) {
    2736                var media = wp.media,
    28                         library = media.frame.state().get('library'),
     37                        frame = media.frames.browse,
     38                        library = frame.state().get('library'),
    2939                        item;
    3040
    3141                // Trigger the media frame to open the correct item
    3242                item = library.findWhere( { id: parseInt( query, 10 ) } );
     43
    3344                if ( item ) {
    34                         media.frame.trigger( 'edit:attachment', item );
     45                        frame.trigger( 'edit:attachment', item );
    3546                } else {
    3647                        item = media.attachment( query );
    37                         media.frame.listenTo( item, 'change', function( model ) {
    38                                 media.frame.stopListening( item );
    39                                 media.frame.trigger( 'edit:attachment', model );
     48                        frame.listenTo( item, 'change', function( model ) {
     49                                frame.stopListening( item );
     50                                frame.trigger( 'edit:attachment', model );
    4051                        } );
    4152                        item.fetch();
    4253                }
  • src/wp-includes/js/media/views/frame/edit-attachments.js

    diff --git src/wp-includes/js/media/views/frame/edit-attachments.js src/wp-includes/js/media/views/frame/edit-attachments.js
    index 3d29ff5..d97eafe 100644
    EditAttachments = MediaFrame.extend({ 
    5959                // Bind default title creation.
    6060                this.on( 'title:create:default', this.createTitle, this );
    6161
    62                 // Close the modal if the attachment is deleted.
    63                 this.listenTo( this.model, 'change:status destroy', this.close, this );
    64 
    6562                this.on( 'content:create:edit-metadata', this.editMetadataMode, this );
    6663                this.on( 'content:create:edit-image', this.editImageMode, this );
    6764                this.on( 'content:render:edit-image', this.editImageModeRender, this );
     65                this.on( 'refresh', this.rerender, this );
    6866                this.on( 'close', this.detach );
     67
     68                this.bindModelHandlers();
     69        },
     70
     71        bindModelHandlers: function() {
     72                // Close the modal if the attachment is deleted.
     73                this.listenTo( this.model, 'change:status destroy', this.close, this );
    6974        },
    7075
    7176        createModal: function() {
    EditAttachments = MediaFrame.extend({ 
    8287
    8388                        // Completely destroy the modal DOM element when closing it.
    8489                        this.modal.on( 'close', _.bind( function() {
    85                                 this.modal.remove();
    8690                                $( 'body' ).off( 'keydown.media-modal' ); /* remove the keydown event */
    8791                                // Restore the original focus item if possible
    8892                                $( 'li.attachment[data-id="' + this.model.get( 'id' ) +'"]' ).focus();
    EditAttachments = MediaFrame.extend({ 
    100104         */
    101105        createStates: function() {
    102106                this.states.add([
    103                         new wp.media.controller.EditAttachmentMetadata( { model: this.model } )
     107                        new wp.media.controller.EditAttachmentMetadata({
     108                                model:   this.model,
     109                                library: this.library
     110                        })
    104111                ]);
    105112        },
    106113
    EditAttachments = MediaFrame.extend({ 
    166173        /**
    167174         * Rerender the view.
    168175         */
    169         rerender: function() {
     176        rerender: function( model ) {
     177                this.stopListening( this.model );
     178
     179                this.model = model;
     180
     181                this.bindModelHandlers();
     182
    170183                // Only rerender the `content` region.
    171184                if ( this.content.mode() !== 'edit-metadata' ) {
    172185                        this.content.mode( 'edit-metadata' );
    EditAttachments = MediaFrame.extend({ 
    185198                        this.$( '.left' ).blur();
    186199                        return;
    187200                }
    188                 this.model = this.library.at( this.getCurrentIndex() - 1 );
    189                 this.rerender();
     201                this.trigger( 'refresh', this.library.at( this.getCurrentIndex() - 1 ) );
    190202                this.$( '.left' ).focus();
    191203        },
    192204
    EditAttachments = MediaFrame.extend({ 
    198210                        this.$( '.right' ).blur();
    199211                        return;
    200212                }
    201                 this.model = this.library.at( this.getCurrentIndex() + 1 );
    202                 this.rerender();
     213                this.trigger( 'refresh', this.library.at( this.getCurrentIndex() + 1 ) );
    203214                this.$( '.right' ).focus();
    204215        },
    205216
  • src/wp-includes/js/media/views/frame/manage.js

    diff --git src/wp-includes/js/media/views/frame/manage.js src/wp-includes/js/media/views/frame/manage.js
    index 46d758b..26a5da5 100644
    Manage = MediaFrame.extend({ 
    7777                this.bindRegionModeHandlers();
    7878                this.render();
    7979                this.bindSearchHandler();
     80
     81                wp.media.frames.browse = this;
    8082        },
    8183
    8284        bindSearchHandler: function() {
    Manage = MediaFrame.extend({ 
    9799
    98100                // Update the URL when entering search string (at most once per second)
    99101                search.on( 'input', _.bind( input, this ) );
    100                 searchView.val( currentSearch ).trigger( 'input' );
    101 
    102                 this.gridRouter.on( 'route:search', function () {
    103                         var href = window.location.href;
    104                         if ( href.indexOf( 'mode=' ) > -1 ) {
    105                                 href = href.replace( /mode=[^&]+/g, 'mode=list' );
    106                         } else {
    107                                 href += href.indexOf( '?' ) > -1 ? '&mode=list' : '?mode=list';
    108                         }
    109                         href = href.replace( 'search=', 's=' );
    110                         listMode.prop( 'href', href );
    111                 } );
     102
     103                this.gridRouter
     104                        .on( 'route:search', function () {
     105                                var href = window.location.href;
     106                                if ( href.indexOf( 'mode=' ) > -1 ) {
     107                                        href = href.replace( /mode=[^&]+/g, 'mode=list' );
     108                                } else {
     109                                        href += href.indexOf( '?' ) > -1 ? '&mode=list' : '?mode=list';
     110                                }
     111                                href = href.replace( 'search=', 's=' );
     112                                listMode.prop( 'href', href );
     113                        })
     114                        .on( 'route:reset', function() {
     115                                searchView.val( '' ).trigger( 'input' );
     116                        });
    112117        },
    113118
    114119        /**
    Manage = MediaFrame.extend({ 
    196201         */
    197202        openEditAttachmentModal: function( model ) {
    198203                // Create a new EditAttachment frame, passing along the library and the attachment model.
    199                 wp.media( {
    200                         frame:       'edit-attachments',
    201                         controller:  this,
    202                         library:     this.state().get('library'),
    203                         model:       model
    204                 } );
     204                if ( wp.media.frames.edit ) {
     205                        wp.media.frames.edit.open().trigger( 'refresh', model );
     206                } else {
     207                        wp.media.frames.edit = wp.media( {
     208                                frame:       'edit-attachments',
     209                                controller:  this,
     210                                library:     this.state().get('library'),
     211                                model:       model
     212                        } );
     213                }
    205214        },
    206215
    207216        /**