| 172 | |
| 173 | // Set up the Backbone router after a brief delay |
| 174 | _.delay( function(){ |
| 175 | wp.media.mediarouter = new media.view.Frame.Router( self ); |
| 176 | // Verify pushState support and activate |
| 177 | if ( window.history && window.history.pushState ) { |
| 178 | Backbone.history.start({ |
| 179 | root: mediaGridSettings.adminUrl, |
| 180 | pushState: true |
| 181 | }); |
| 182 | } |
| 183 | }, 150); |
| 184 | |
| 185 | // Update the URL when entering search string (at most once per second) |
| 186 | $( '#media-search-input' ).on( 'input', _.debounce( function() { |
| 187 | if ( '' !== $( this ).val() ) { |
| 188 | wp.media.mediarouter.navigate( wp.media.mediarouter.baseUrl( '?search=' + $( this ).val() ), { trigger: false, replace: false } ); |
| 189 | } |
| 190 | }, 1000 ) ); |
| 347 | * A router for handling the browser history and application state |
| 348 | */ |
| 349 | media.view.Frame.Router = Backbone.Router.extend({ |
| 350 | |
| 351 | mediaFrame: '', |
| 352 | |
| 353 | initialize: function( mediaFrame ){ |
| 354 | this.mediaFrame = mediaFrame; |
| 355 | }, |
| 356 | |
| 357 | routes: { |
| 358 | 'upload.php?item=:slug': 'showitem', |
| 359 | 'upload.php?search=:query': 'search', |
| 360 | ':default': 'defaultRoute' |
| 361 | }, |
| 362 | |
| 363 | // Map routes against the page URL |
| 364 | baseUrl: function( url ) { |
| 365 | return 'upload.php' + url; |
| 366 | }, |
| 367 | |
| 368 | // Respond to the search route by filling the search field and trigggering the input event |
| 369 | search: function( query ) { |
| 370 | // Ensure modal closed, see back button |
| 371 | this.closeModal(); |
| 372 | $( '#media-search-input' ).val( query ).trigger( 'input' ); |
| 373 | }, |
| 374 | |
| 375 | // Show the modal with a specific item |
| 376 | showitem: function( query ) { |
| 377 | var library = this.mediaFrame.state().get('library'); |
| 378 | |
| 379 | // Remove existing modal if present |
| 380 | this.closeModal(); |
| 381 | // Trigger the media frame to open the correct item |
| 382 | this.mediaFrame.trigger( 'edit:attachment', library.findWhere( { id: parseInt( query, 10 ) } ) ); |
| 383 | }, |
| 384 | |
| 385 | // Close the modal if set up |
| 386 | closeModal: function() { |
| 387 | if ( 'undefined' !== typeof wp.media.editAttachmentFrame ) { |
| 388 | wp.media.editAttachmentFrame.modal.remove(); |
| 389 | } |
| 390 | }, |
| 391 | |
| 392 | // Default route: make sure the modal and search are reset |
| 393 | defaultRoute: function() { |
| 394 | this.closeModal(); |
| 395 | $( '#media-search-input' ).val( '' ).trigger( 'input' ); |
| 396 | } |
| 397 | }); |
| 398 | |
| 399 | /** |
| 569 | }, |
| 570 | /** |
| 571 | * Respond to the keyboard events: right arrow, left arrow, escape. |
| 572 | */ |
| 573 | keyEvent: function( event ) { |
| 574 | // The right arrow key |
| 575 | if ( event.keyCode === 39 ) { |
| 576 | if ( ! this.options.hasNext ) { return; } |
| 577 | _.once( this.nextMediaItem() ); |
| 578 | } |
| 579 | |
| 580 | // The left arrow key |
| 581 | if ( event.keyCode === 37 ) { |
| 582 | if ( ! this.options.hasPrevious ) { return; } |
| 583 | _.once( this.previousMediaItem() ); |
| 584 | } |
| 585 | |
| 586 | // Pressing the escape key routes back to main url |
| 587 | if ( event.keyCode === 27 ) { |
| 588 | this.resetRoute(); |
| 589 | return event; |
| 590 | } |
| 591 | }, |
| 592 | |
| 593 | resetRoute: function() { |
| 594 | wp.media.mediarouter.navigate( wp.media.mediarouter.baseUrl( '' ), { trigger: false, replace: false } ); |
| 595 | return; |