@@ -492,7 +492,7 @@ themes.view.Theme = wp.Backbone.View.extend({
492492 themes . focusedTheme = this . $el ;
493493
494494 // Construct a new Preview view.
495- preview = new themes . view . Preview ( {
495+ themes . currentPreview = preview = new themes . view . Preview ( {
496496 model : this . model
497497 } ) ;
498498
@@ -572,6 +572,11 @@ themes.view.Theme = wp.Backbone.View.extend({
572572 this . listenTo ( preview , 'preview:close' , function ( ) {
573573 self . current = self . model ;
574574 } ) ;
575+
576+ // Listen for closepreview events, closing the preview.
577+ this . listenTo ( preview , 'closepreview' , function ( ) {
578+ preview . close ( ) ;
579+ } ) ;
575580 } ,
576581
577582 // Handles .disabled classes for previous/next buttons in theme installer preview
@@ -885,7 +890,7 @@ themes.view.Preview = themes.view.Details.extend({
885890 self . tooglePreviewDeviceButtons ( currentPreviewDevice ) ;
886891 }
887892
888- themes . router . navigate ( themes . router . baseUrl ( themes . router . themePath + this . model . get ( 'id' ) ) , { replace : true } ) ;
893+ themes . router . navigate ( themes . router . baseUrl ( themes . router . themePath + this . model . get ( 'id' ) ) , { replace : false } ) ;
889894
890895 this . $el . fadeIn ( 200 , function ( ) {
891896 $body . addClass ( 'theme-installer-active full-overlay-active' ) ;
@@ -1431,6 +1436,9 @@ themes.Run = {
14311436 this . view . render ( ) ;
14321437 this . routes ( ) ;
14331438
1439+ if ( Backbone . History . started ) {
1440+ Backbone . history . stop ( ) ;
1441+ }
14341442 Backbone . history . start ( {
14351443 root : themes . data . settings . adminUrl ,
14361444 pushState : true ,
@@ -1884,6 +1892,9 @@ themes.RunInstaller = {
18841892 this . view . render ( ) ;
18851893 this . routes ( ) ;
18861894
1895+ if ( Backbone . History . started ) {
1896+ Backbone . history . stop ( ) ;
1897+ }
18871898 Backbone . history . start ( {
18881899 root : themes . data . settings . adminUrl ,
18891900 pushState : true ,
@@ -1902,11 +1913,24 @@ themes.RunInstaller = {
19021913 // Handles `theme` route event
19031914 // Queries the API for the passed theme slug
19041915 themes . router . on ( 'route:preview' , function ( slug ) {
1905- request . theme = slug ;
1906- self . view . collection . query ( request ) ;
1907- self . view . collection . once ( 'update' , function ( ) {
1916+
1917+ // If the theme preview is active, set the current theme.
1918+ if ( self . view . view . theme && self . view . view . theme . preview ) {
1919+ self . view . view . theme . model = self . view . collection . findWhere ( { 'slug' : slug } ) ;
19081920 self . view . view . theme . preview ( ) ;
1909- } ) ;
1921+ } else {
1922+
1923+ // Select the theme by slug.
1924+ request . theme = slug ;
1925+ self . view . collection . query ( request ) ;
1926+ self . view . collection . trigger ( 'update' ) ;
1927+
1928+ // Open the theme preview.
1929+ self . view . collection . once ( 'query:success' , function ( ) {
1930+ $ ( 'div[data-slug="' + slug + '"]' ) . trigger ( 'click' ) ;
1931+ } ) ;
1932+
1933+ }
19101934 } ) ;
19111935
19121936 // Handles sorting / browsing routes
@@ -1917,7 +1941,11 @@ themes.RunInstaller = {
19171941 sort = 'featured' ;
19181942 }
19191943 self . view . sort ( sort ) ;
1920- self . view . trigger ( 'theme:close' ) ;
1944+
1945+ // Close the preview if open.
1946+ if ( themes . currentPreview ) {
1947+ themes . currentPreview . trigger ( 'closepreview' ) ;
1948+ }
19211949 } ) ;
19221950
19231951 // The `search` route event. The router populates the input field.
0 commit comments