]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-admin/js/theme.js
WordPress 3.9
[autoinstalls/wordpress.git] / wp-admin / js / theme.js
index aba7d9b9469c1ff776eb743d49e056aa0c1bb938..77054b5cbf24983aaed1c9cb80ad7d7c076ecc74 100644 (file)
@@ -12,10 +12,37 @@ themes = wp.themes = wp.themes || {};
 themes.data = _wpThemeSettings;
 l10n = themes.data.l10n;
 
+// Shortcut for isInstall check
+themes.isInstall = !! themes.data.settings.isInstall;
+
 // Setup app structure
 _.extend( themes, { model: {}, view: {}, routes: {}, router: {}, template: wp.template });
 
-themes.model = Backbone.Model.extend({});
+themes.Model = Backbone.Model.extend({
+       // Adds attributes to the default data coming through the .org themes api
+       // Map `id` to `slug` for shared code
+       initialize: function() {
+               var description;
+
+               // If theme is already installed, set an attribute.
+               if ( _.indexOf( themes.data.installedThemes, this.get( 'slug' ) ) !== -1 ) {
+                       this.set({ installed: true });
+               }
+
+               // Set the attributes
+               this.set({
+                       // slug is for installation, id is for existing.
+                       id: this.get( 'slug' ) || this.get( 'id' )
+               });
+
+               // Map `section.description` to `description`
+               // as the API sometimes returns it differently
+               if ( this.has( 'sections' ) ) {
+                       description = this.get( 'sections' ).description;
+                       this.set({ description: description });
+               }
+       }
+});
 
 // Main view controller for themes.php
 // Unifies and renders all available views
@@ -28,10 +55,11 @@ themes.view.Appearance = wp.Backbone.View.extend({
        page: 0,
 
        // Sets up a throttler for binding to 'scroll'
-       initialize: function() {
+       initialize: function( options ) {
                // Scroller checks how far the scroll position is
                _.bindAll( this, 'scroller' );
 
+               this.SearchView = options.SearchView ? options.SearchView : themes.view.Search;
                // Bind to the scroll event and throttle
                // the results from this.scroller
                this.window.bind( 'scroll', _.throttle( this.scroller, 300 ) );
@@ -55,6 +83,9 @@ themes.view.Appearance = wp.Backbone.View.extend({
                this.$el.append( '<br class="clear"/>' );
        },
 
+       // Defines search element container
+       searchContainer: $( '#wpbody h2:first' ),
+
        // Search input and view
        // for current theme collection
        search: function() {
@@ -66,11 +97,14 @@ themes.view.Appearance = wp.Backbone.View.extend({
                        return;
                }
 
-               view = new themes.view.Search({ collection: self.collection });
+               view = new this.SearchView({
+                       collection: self.collection,
+                       parent: this
+               });
 
                // Render and append after screen title
                view.render();
-               $('#wpbody h2:first')
+               this.searchContainer
                        .append( $.parseHTML( '<label class="screen-reader-text" for="theme-search-input">' + l10n.search + '</label>' ) )
                        .append( view.el );
        },
@@ -95,7 +129,7 @@ themes.view.Appearance = wp.Backbone.View.extend({
 // @has 'id' 'name' 'screenshot' 'author' 'authorURI' 'version' 'active' ...
 themes.Collection = Backbone.Collection.extend({
 
-       model: themes.model,
+       model: themes.Model,
 
        // Search terms
        terms: '',
@@ -136,11 +170,12 @@ themes.Collection = Backbone.Collection.extend({
                // Start with a full collection
                this.reset( themes.data.themes, { silent: true } );
 
-               // The RegExp object to match
-               //
+               // Escape the term string for RegExp meta characters
+               term = term.replace( /[-\/\\^$*+?.()|[\]{}]/g, '\\$&' );
+
                // Consider spaces as word delimiters and match the whole string
                // so matching terms can be combined
-               term = term.replace( ' ', ')(?=.*' );
+               term = term.replace( / /g, ')(?=.*' );
                match = new RegExp( '^(?=.*' + term + ').+', 'i' );
 
                // Find results
@@ -164,12 +199,151 @@ themes.Collection = Backbone.Collection.extend({
                var collection = this;
                instance = instance || 0;
 
-               // Themes per instance are set at 15
-               collection = _( collection.rest( 15 * instance ) );
-               collection = _( collection.first( 15 ) );
+               // Themes per instance are set at 20
+               collection = _( collection.rest( 20 * instance ) );
+               collection = _( collection.first( 20 ) );
 
                return collection;
-       }
+       },
+
+       count: false,
+
+       // Handles requests for more themes
+       // and caches results
+       //
+       // When we are missing a cache object we fire an apiCall()
+       // which triggers events of `query:success` or `query:fail`
+       query: function( request ) {
+               /**
+                * @static
+                * @type Array
+                */
+               var queries = this.queries,
+                       self = this,
+                       query, isPaginated, count;
+
+               // Store current query request args
+               // for later use with the event `theme:end`
+               this.currentQuery.request = request;
+
+               // Search the query cache for matches.
+               query = _.find( queries, function( query ) {
+                       return _.isEqual( query.request, request );
+               });
+
+               // If the request matches the stored currentQuery.request
+               // it means we have a paginated request.
+               isPaginated = _.has( request, 'page' );
+
+               // Reset the internal api page counter for non paginated queries.
+               if ( ! isPaginated ) {
+                       this.currentQuery.page = 1;
+               }
+
+               // Otherwise, send a new API call and add it to the cache.
+               if ( ! query && ! isPaginated ) {
+                       query = this.apiCall( request ).done( function( data ) {
+
+                               // Update the collection with the queried data.
+                               if ( data.themes ) {
+                                       self.reset( data.themes );
+                                       count = data.info.results;
+                                       // Store the results and the query request
+                                       queries.push( { themes: data.themes, request: request, total: count } );
+                               }
+
+                               // Trigger a collection refresh event
+                               // and a `query:success` event with a `count` argument.
+                               self.trigger( 'update' );
+                               self.trigger( 'query:success', count );
+
+                               if ( data.themes && data.themes.length === 0 ) {
+                                       self.trigger( 'query:empty' );
+                               }
+
+                       }).fail( function() {
+                               self.trigger( 'query:fail' );
+                       });
+               } else {
+                       // If it's a paginated request we need to fetch more themes...
+                       if ( isPaginated ) {
+                               return this.apiCall( request, isPaginated ).done( function( data ) {
+                                       // Add the new themes to the current collection
+                                       // @todo update counter
+                                       self.add( data.themes );
+                                       self.trigger( 'query:success' );
+
+                                       // We are done loading themes for now.
+                                       self.loadingThemes = false;
+
+                               }).fail( function() {
+                                       self.trigger( 'query:fail' );
+                               });
+                       }
+
+                       if ( query.themes.length === 0 ) {
+                               self.trigger( 'query:empty' );
+                       } else {
+                               $( 'body' ).removeClass( 'no-results' );
+                       }
+
+                       // Only trigger an update event since we already have the themes
+                       // on our cached object
+                       if ( _.isNumber( query.total ) ) {
+                               this.count = query.total;
+                       }
+
+                       this.reset( query.themes );
+                       if ( ! query.total ) {
+                               this.count = this.length;
+                       }
+
+                       this.trigger( 'update' );
+                       this.trigger( 'query:success', this.count );
+               }
+       },
+
+       // Local cache array for API queries
+       queries: [],
+
+       // Keep track of current query so we can handle pagination
+       currentQuery: {
+               page: 1,
+               request: {}
+       },
+
+       // Send request to api.wordpress.org/themes
+       apiCall: function( request, paginated ) {
+               return wp.ajax.send( 'query-themes', {
+                       data: {
+                       // Request data
+                               request: _.extend({
+                                       per_page: 100,
+                                       fields: {
+                                               description: true,
+                                               tested: true,
+                                               requires: true,
+                                               rating: true,
+                                               downloaded: true,
+                                               downloadLink: true,
+                                               last_updated: true,
+                                               homepage: true,
+                                               num_ratings: true
+                                       }
+                               }, request)
+                       },
+
+                       beforeSend: function() {
+                               if ( ! paginated ) {
+                                       // Spin it
+                                       $( 'body' ).addClass( 'loading-themes' ).removeClass( 'no-results' );
+                               }
+                       }
+               });
+       },
+
+       // Static status controller for when we are loading themes.
+       loadingThemes: false
 });
 
 // This is the view that controls each theme item
@@ -187,9 +361,11 @@ themes.view.Theme = wp.Backbone.View.extend({
        html: themes.template( 'theme' ),
 
        events: {
-               'click': 'expand',
-               'keydown': 'expand',
-               'touchend': 'expand',
+               'click': themes.isInstall ? 'preview': 'expand',
+               'click .preview': 'preview',
+               'keydown': themes.isInstall ? 'preview': 'expand',
+               'touchend': themes.isInstall ? 'preview': 'expand',
+               'keyup': 'addFocus',
                'touchmove': 'preventExpand'
        },
 
@@ -209,6 +385,10 @@ themes.view.Theme = wp.Backbone.View.extend({
                if ( this.model.get( 'displayAuthor' ) ) {
                        this.$el.addClass( 'display-author' );
                }
+
+               if ( this.model.get( 'installed' ) ) {
+                       this.$el.addClass( 'is-installed' );
+               }
        },
 
        // Adds a class to the currently active theme
@@ -219,6 +399,14 @@ themes.view.Theme = wp.Backbone.View.extend({
                }
        },
 
+       // Add class of focus to the theme we are focused on.
+       addFocus: function() {
+               var $themeToFocus = ( $( ':focus' ).hasClass( 'theme' ) ) ? $( ':focus' ) : $(':focus').parents('.theme');
+
+               $('.theme.focus').removeClass('focus');
+               $themeToFocus.addClass('focus');
+       },
+
        // Single theme overlay screen
        // It's shown when clicking a theme
        expand: function( event ) {
@@ -250,6 +438,144 @@ themes.view.Theme = wp.Backbone.View.extend({
 
        preventExpand: function() {
                this.touchDrag = true;
+       },
+
+       preview: function( event ) {
+               var self = this,
+                       current, preview;
+
+               // Bail if the user scrolled on a touch device
+               if ( this.touchDrag === true ) {
+                       return this.touchDrag = false;
+               }
+
+               // Allow direct link path to installing a theme.
+               if ( $( event.target ).hasClass( 'button-primary' ) ) {
+                       return;
+               }
+
+               // 'enter' and 'space' keys expand the details view when a theme is :focused
+               if ( event.type === 'keydown' && ( event.which !== 13 && event.which !== 32 ) ) {
+                       return;
+               }
+
+               // pressing enter while focused on the buttons shouldn't open the preview
+               if ( event.type === 'keydown' && event.which !== 13 && $( ':focus' ).hasClass( 'button' ) ) {
+                       return;
+               }
+
+               event.preventDefault();
+
+               event = event || window.event;
+
+               // Set focus to current theme.
+               themes.focusedTheme = this.$el;
+
+               // Construct a new Preview view.
+               preview = new themes.view.Preview({
+                       model: this.model
+               });
+
+               // Render the view and append it.
+               preview.render();
+               this.setNavButtonsState();
+
+               // Hide previous/next navigation if there is only one theme
+               if ( this.model.collection.length === 1 ) {
+                       preview.$el.addClass( 'no-navigation' );
+               } else {
+                       preview.$el.removeClass( 'no-navigation' );
+               }
+
+               // Apend preview
+               $( 'div.wrap' ).append( preview.el );
+
+               // Listen to our preview object
+               // for `theme:next` and `theme:previous` events.
+               this.listenTo( preview, 'theme:next', function() {
+
+                       // Keep local track of current theme model.
+                       current = self.model;
+
+                       // If we have ventured away from current model update the current model position.
+                       if ( ! _.isUndefined( self.current ) ) {
+                               current = self.current;
+                       }
+
+                       // Get previous theme model.
+                       self.current = self.model.collection.at( self.model.collection.indexOf( current ) + 1 );
+
+                       // If we have no more themes, bail.
+                       if ( _.isUndefined( self.current ) ) {
+                               self.options.parent.parent.trigger( 'theme:end' );
+                               return self.current = current;
+                       }
+
+                       // Construct a new Preview view.
+                       preview = new themes.view.Preview({
+                               model: self.current
+                       });
+
+                       // Render and append.
+                       preview.render();
+                       this.setNavButtonsState();
+                       $( 'div.wrap' ).append( preview.el );
+                       $( '.next-theme' ).focus();
+               })
+               .listenTo( preview, 'theme:previous', function() {
+
+                       // Keep track of current theme model.
+                       current = self.model;
+
+                       // Bail early if we are at the beginning of the collection
+                       if ( self.model.collection.indexOf( self.current ) === 0 ) {
+                               return;
+                       }
+
+                       // If we have ventured away from current model update the current model position.
+                       if ( ! _.isUndefined( self.current ) ) {
+                               current = self.current;
+                       }
+
+                       // Get previous theme model.
+                       self.current = self.model.collection.at( self.model.collection.indexOf( current ) - 1 );
+
+                       // If we have no more themes, bail.
+                       if ( _.isUndefined( self.current ) ) {
+                               return;
+                       }
+
+                       // Construct a new Preview view.
+                       preview = new themes.view.Preview({
+                               model: self.current
+                       });
+
+                       // Render and append.
+                       preview.render();
+                       this.setNavButtonsState();
+                       $( 'div.wrap' ).append( preview.el );
+                       $( '.previous-theme' ).focus();
+               });
+
+               this.listenTo( preview, 'preview:close', function() {
+                       self.current = self.model;
+               });
+       },
+
+       // Handles .disabled classes for previous/next buttons in theme installer preview
+       setNavButtonsState: function() {
+               var $themeInstaller = $( '.theme-install-overlay' ),
+                       current = _.isUndefined( this.current ) ? this.model : this.current;
+
+               // Disable previous at the zero position
+               if ( 0 === this.model.collection.indexOf( current ) ) {
+                       $themeInstaller.find( '.previous-theme' ).addClass( 'disabled' );
+               }
+
+               // Disable next if the next model is undefined
+               if ( _.isUndefined( this.model.collection.at( this.model.collection.indexOf( current ) + 1 ) ) ) {
+                       $themeInstaller.find( '.next-theme' ).addClass( 'disabled' );
+               }
        }
 });
 
@@ -342,7 +668,7 @@ themes.view.Details = wp.Backbone.View.extend({
                        // With a quick fade out animation
                        this.$el.fadeOut( 130, function() {
                                // Clicking outside the modal box closes the overlay
-                               $( 'body' ).removeClass( 'theme-overlay-open closing-overlay' );
+                               $( 'body' ).removeClass( 'closing-overlay' );
                                // Handle event cleanup
                                self.closeOverlay();
 
@@ -350,7 +676,7 @@ themes.view.Details = wp.Backbone.View.extend({
                                scroll = document.body.scrollTop;
 
                                // Clean the url structure
-                               themes.router.navigate( themes.router.baseUrl( '' ), { replace: true } );
+                               themes.router.navigate( themes.router.baseUrl( '' ) );
 
                                // Restore scroll position
                                document.body.scrollTop = scroll;
@@ -378,12 +704,13 @@ themes.view.Details = wp.Backbone.View.extend({
        // Performs the actions to effectively close
        // the theme details overlay
        closeOverlay: function() {
+               $( 'body' ).removeClass( 'theme-overlay-open' );
                this.remove();
                this.unbind();
                this.trigger( 'theme:collapse' );
        },
 
-       // Confirmation dialoge for deleting a theme
+       // Confirmation dialog for deleting a theme
        deleteTheme: function() {
                return confirm( themes.data.settings.confirmDelete );
        },
@@ -391,11 +718,13 @@ themes.view.Details = wp.Backbone.View.extend({
        nextTheme: function() {
                var self = this;
                self.trigger( 'theme:next', self.model.cid );
+               return false;
        },
 
        previousTheme: function() {
                var self = this;
                self.trigger( 'theme:previous', self.model.cid );
+               return false;
        },
 
        // Checks if the theme screenshot is the old 300px width version
@@ -414,6 +743,77 @@ themes.view.Details = wp.Backbone.View.extend({
        }
 });
 
+// Theme Preview view
+// Set ups a modal overlay with the expanded theme data
+themes.view.Preview = themes.view.Details.extend({
+
+       className: 'wp-full-overlay expanded',
+       el: '.theme-install-overlay',
+
+       events: {
+               'click .close-full-overlay': 'close',
+               'click .collapse-sidebar': 'collapse',
+               'click .previous-theme': 'previousTheme',
+               'click .next-theme': 'nextTheme',
+               'keyup': 'keyEvent'
+       },
+
+       // The HTML template for the theme preview
+       html: themes.template( 'theme-preview' ),
+
+       render: function() {
+               var data = this.model.toJSON();
+
+               this.$el.html( this.html( data ) );
+
+               themes.router.navigate( themes.router.baseUrl( '?theme=' + this.model.get( 'id' ) ), { replace: true } );
+
+               this.$el.fadeIn( 200, function() {
+                       $( 'body' ).addClass( 'theme-installer-active full-overlay-active' );
+                       $( '.close-full-overlay' ).focus();
+               });
+       },
+
+       close: function() {
+               this.$el.fadeOut( 200, function() {
+                       $( 'body' ).removeClass( 'theme-installer-active full-overlay-active' );
+
+                       // Return focus to the theme div
+                       if ( themes.focusedTheme ) {
+                               themes.focusedTheme.focus();
+                       }
+               });
+
+               themes.router.navigate( themes.router.baseUrl( '' ) );
+               this.trigger( 'preview:close' );
+               this.unbind();
+               return false;
+       },
+
+       collapse: function() {
+
+               this.$el.toggleClass( 'collapsed' ).toggleClass( 'expanded' );
+               return false;
+       },
+
+       keyEvent: function( event ) {
+               // The escape key closes the preview
+               if ( event.keyCode === 27 ) {
+                       this.undelegateEvents();
+                       this.close();
+               }
+               // The right arrow key, next theme
+               if ( event.keyCode === 39 ) {
+                       _.once( this.nextTheme() );
+               }
+
+               // The left arrow key, previous theme
+               if ( event.keyCode === 37 ) {
+                       this.previousTheme();
+               }
+       }
+});
+
 // Controls the rendering of div.themes,
 // a wrapper that will hold all the theme elements
 themes.view.Themes = wp.Backbone.View.extend({
@@ -447,12 +847,31 @@ themes.view.Themes = wp.Backbone.View.extend({
                        self.render( this );
                });
 
+               // Update theme count to full result set when available.
+               this.listenTo( self.collection, 'query:success', function( count ) {
+                       if ( _.isNumber( count ) ) {
+                               self.count.text( count );
+                       } else {
+                               self.count.text( self.collection.length );
+                       }
+               });
+
+               this.listenTo( self.collection, 'query:empty', function() {
+                       $( 'body' ).addClass( 'no-results' );
+               });
+
                this.listenTo( this.parent, 'theme:scroll', function() {
                        self.renderThemes( self.parent.page );
                });
 
+               this.listenTo( this.parent, 'theme:close', function() {
+                       if ( self.overlay ) {
+                               self.overlay.closeOverlay();
+                       }
+               } );
+
                // Bind keyboard events.
-               $('body').on( 'keyup', function( event ) {
+               $( 'body' ).on( 'keyup', function( event ) {
                        if ( ! self.overlay ) {
                                return;
                        }
@@ -498,10 +917,13 @@ themes.view.Themes = wp.Backbone.View.extend({
 
                // Generate the themes
                // Using page instance
-               this.renderThemes( this.parent.page );
+               // While checking the collection has items
+               if ( this.options.collection.size() > 0 ) {
+                       this.renderThemes( this.parent.page );
+               }
 
                // Display a live theme count for the collection
-               this.count.text( this.collection.length );
+               this.count.text( this.collection.count ? this.collection.count : this.collection.length );
        },
 
        // Iterates through each instance of the collection
@@ -512,7 +934,9 @@ themes.view.Themes = wp.Backbone.View.extend({
                self.instance = self.collection.paginate( page );
 
                // If we have no more themes bail
-               if ( self.instance.length === 0 ) {
+               if ( self.instance.size() === 0 ) {
+                       // Fire a no-more-themes event.
+                       this.parent.trigger( 'theme:end' );
                        return;
                }
 
@@ -524,7 +948,8 @@ themes.view.Themes = wp.Backbone.View.extend({
                // Loop through the themes and setup each theme view
                self.instance.each( function( theme ) {
                        self.theme = new themes.view.Theme({
-                               model: theme
+                               model: theme,
+                               parent: self
                        });
 
                        // Render the views...
@@ -573,7 +998,7 @@ themes.view.Themes = wp.Backbone.View.extend({
                this.model = self.collection.get( id );
 
                // Trigger a route update for the current model
-               themes.router.navigate( themes.router.baseUrl( '?theme=' + this.model.id ), { replace: true } );
+               themes.router.navigate( themes.router.baseUrl( '?theme=' + this.model.id ) );
 
                // Sets this.view to 'detail'
                this.setView( 'detail' );
@@ -659,6 +1084,7 @@ themes.view.Search = wp.Backbone.View.extend({
        tagName: 'input',
        className: 'theme-search',
        id: 'theme-search-input',
+       searching: false,
 
        attributes: {
                placeholder: l10n.searchPlaceholder,
@@ -669,39 +1095,94 @@ themes.view.Search = wp.Backbone.View.extend({
                'input':  'search',
                'keyup':  'search',
                'change': 'search',
-               'search': 'search'
+               'search': 'search',
+               'blur':   'pushState'
+       },
+
+       initialize: function( options ) {
+
+               this.parent = options.parent;
+
+               this.listenTo( this.parent, 'theme:close', function() {
+                       this.searching = false;
+               } );
+
        },
 
        // Runs a search on the theme collection.
        search: function( event ) {
+               var options = {};
+
                // Clear on escape.
                if ( event.type === 'keyup' && event.which === 27 ) {
                        event.target.value = '';
                }
 
+               // Lose input focus when pressing enter
+               if ( event.which === 13 ) {
+                       this.$el.trigger( 'blur' );
+               }
+
                this.collection.doSearch( event.target.value );
 
+               // if search is initiated and key is not return
+               if ( this.searching && event.which !== 13 ) {
+                       options.replace = true;
+               } else {
+                       this.searching = true;
+               }
+
                // Update the URL hash
                if ( event.target.value ) {
-                       themes.router.navigate( themes.router.baseUrl( '?search=' + event.target.value ), { replace: true } );
+                       themes.router.navigate( themes.router.baseUrl( '?search=' + event.target.value ), options );
                } else {
-                       themes.router.navigate( themes.router.baseUrl( '' ), { replace: true } );
+                       themes.router.navigate( themes.router.baseUrl( '' ) );
+               }
+       },
+
+       pushState: function( event ) {
+               var url = themes.router.baseUrl( '' );
+
+               if ( event.target.value ) {
+                       url = themes.router.baseUrl( '?search=' + event.target.value );
                }
+
+               this.searching = false;
+               themes.router.navigate( url );
+
        }
 });
 
 // Sets up the routes events for relevant url queries
 // Listens to [theme] and [search] params
-themes.routes = Backbone.Router.extend({
-
-       initialize: function() {
-               this.routes = _.object([
-               ]);
+themes.Router = Backbone.Router.extend({
+
+       routes: {
+               'themes.php?theme=:slug': 'theme',
+               'themes.php?search=:query': 'search',
+               'themes.php?s=:query': 'search',
+               'themes.php': 'themes',
+               '': 'themes'
        },
 
        baseUrl: function( url ) {
-               return themes.data.settings.root + url;
+               return 'themes.php' + url;
+       },
+
+       search: function( query ) {
+               $( '.theme-search' ).val( query );
+       },
+
+       themes: function() {
+               $( '.theme-search' ).val( '' );
+       },
+
+       navigate: function() {
+               if ( Backbone.history._hasPushState ) {
+                       Backbone.Router.prototype.navigate.apply( this, arguments );
+               }
        }
+
 });
 
 // Execute and setup the application
@@ -720,42 +1201,473 @@ themes.Run = {
        },
 
        render: function() {
+
                // Render results
                this.view.render();
                this.routes();
 
-               // Set the initial theme
-               if ( 'undefined' !== typeof themes.data.settings.theme && '' !== themes.data.settings.theme ){
-                       this.view.view.theme.trigger( 'theme:expand', this.view.collection.findWhere( { id: themes.data.settings.theme } ) );
+               Backbone.history.start({
+                       root: themes.data.settings.adminUrl,
+                       pushState: true,
+                       hashChange: false
+               });
+       },
+
+       routes: function() {
+               var self = this;
+               // Bind to our global thx object
+               // so that the object is available to sub-views
+               themes.router = new themes.Router();
+
+               // Handles theme details route event
+               themes.router.on( 'route:theme', function( slug ) {
+                       self.view.view.expand( slug );
+               });
+
+               themes.router.on( 'route:themes', function() {
+                       self.themes.doSearch( '' );
+                       self.view.trigger( 'theme:close' );
+               });
+
+               // Handles search route event
+               themes.router.on( 'route:search', function() {
+                       $( '.theme-search' ).trigger( 'keyup' );
+               });
+
+               this.extraRoutes();
+       },
+
+       extraRoutes: function() {
+               return false;
+       }
+};
+
+// Extend the main Search view
+themes.view.InstallerSearch =  themes.view.Search.extend({
+
+       events: {
+               'keyup': 'search'
+       },
+
+       // Handles Ajax request for searching through themes in public repo
+       search: function( event ) {
+
+               // Tabbing or reverse tabbing into the search input shouldn't trigger a search
+               if ( event.type === 'keyup' && ( event.which === 9 || event.which === 16 ) ) {
+                       return;
+               }
+
+               this.collection = this.options.parent.view.collection;
+
+               // Clear on escape.
+               if ( event.type === 'keyup' && event.which === 27 ) {
+                       event.target.value = '';
                }
 
-               // Set the initial search
-               if ( 'undefined' !== typeof themes.data.settings.search && '' !== themes.data.settings.search ){
-                       $( '.theme-search' ).val( themes.data.settings.search );
-                       this.themes.doSearch( themes.data.settings.search );
+               _.debounce( _.bind( this.doSearch, this ), 300 )( event.target.value );
+       },
+
+       doSearch: _.debounce( function( value ) {
+               var request = {};
+
+               request.search = value;
+
+               // Intercept an [author] search.
+               //
+               // If input value starts with `author:` send a request
+               // for `author` instead of a regular `search`
+               if ( value.substring( 0, 7 ) === 'author:' ) {
+                       request.search = '';
+                       request.author = value.slice( 7 );
                }
 
-               // Start the router if browser supports History API
-               if ( window.history && window.history.pushState ) {
-                       // Calls the routes functionality
-                       Backbone.history.start({ pushState: true, silent: true });
+               // Intercept a [tag] search.
+               //
+               // If input value starts with `tag:` send a request
+               // for `tag` instead of a regular `search`
+               if ( value.substring( 0, 4 ) === 'tag:' ) {
+                       request.search = '';
+                       request.tag = [ value.slice( 4 ) ];
                }
+
+               $( '.theme-section.current' ).removeClass( 'current' );
+               $( 'body' ).removeClass( 'more-filters-opened filters-applied' );
+
+               // Get the themes by sending Ajax POST request to api.wordpress.org/themes
+               // or searching the local cache
+               this.collection.query( request );
+
+               // Set route
+               themes.router.navigate( themes.router.baseUrl( '?search=' + value ), { replace: true } );
+       }, 300 )
+});
+
+themes.view.Installer = themes.view.Appearance.extend({
+
+       el: '#wpbody-content .wrap',
+
+       // Register events for sorting and filters in theme-navigation
+       events: {
+               'click .theme-section': 'onSort',
+               'click .theme-filter': 'onFilter',
+               'click .more-filters': 'moreFilters',
+               'click .apply-filters': 'applyFilters',
+               'click [type="checkbox"]': 'addFilter',
+               'click .clear-filters': 'clearFilters',
+               'click .feature-name': 'filterSection',
+               'click .filtering-by a': 'backToFilters'
+       },
+
+       // Initial render method
+       render: function() {
+               var self = this;
+
+               this.search();
+               this.uploader();
+
+               this.collection = new themes.Collection();
+
+               // Bump `collection.currentQuery.page` and request more themes if we hit the end of the page.
+               this.listenTo( this, 'theme:end', function() {
+
+                       // Make sure we are not already loading
+                       if ( self.collection.loadingThemes ) {
+                               return;
+                       }
+
+                       // Set loadingThemes to true and bump page instance of currentQuery.
+                       self.collection.loadingThemes = true;
+                       self.collection.currentQuery.page++;
+
+                       // Use currentQuery.page to build the themes request.
+                       _.extend( self.collection.currentQuery.request, { page: self.collection.currentQuery.page } );
+                       self.collection.query( self.collection.currentQuery.request );
+               });
+
+               this.listenTo( this.collection, 'query:success', function() {
+                       $( 'body' ).removeClass( 'loading-themes' );
+                       $( '.theme-browser' ).find( 'div.error' ).remove();
+               });
+
+               this.listenTo( this.collection, 'query:fail', function() {
+                       $( 'body' ).removeClass( 'loading-themes' );
+                       $( '.theme-browser' ).find( 'div.error' ).remove();
+                       $( '.theme-browser' ).find( 'div.themes' ).before( '<div class="error"><p>' + l10n.error + '</p></div>' );
+               });
+
+               if ( this.view ) {
+                       this.view.remove();
+               }
+
+               // Set ups the view and passes the section argument
+               this.view = new themes.view.Themes({
+                       collection: this.collection,
+                       parent: this
+               });
+
+               // Reset pagination every time the install view handler is run
+               this.page = 0;
+
+               // Render and append
+               this.$el.find( '.themes' ).remove();
+               this.view.render();
+               this.$el.find( '.theme-browser' ).append( this.view.el ).addClass( 'rendered' );
+       },
+
+       // Handles all the rendering of the public theme directory
+       browse: function( section ) {
+               // Create a new collection with the proper theme data
+               // for each section
+               this.collection.query( { browse: section } );
+       },
+
+       // Sorting navigation
+       onSort: function( event ) {
+               var $el = $( event.target ),
+                       sort = $el.data( 'sort' );
+
+               event.preventDefault();
+
+               $( 'body' ).removeClass( 'filters-applied more-filters-opened' );
+
+               // Bail if this is already active
+               if ( $el.hasClass( this.activeClass ) ) {
+                       return;
+               }
+
+               this.sort( sort );
+
+               // Trigger a router.naviagte update
+               themes.router.navigate( themes.router.baseUrl( '?browse=' + sort ) );
+       },
+
+       sort: function( sort ) {
+               this.clearSearch();
+
+               $( '.theme-section, .theme-filter' ).removeClass( this.activeClass );
+               $( '[data-sort="' + sort + '"]' ).addClass( this.activeClass );
+
+               this.browse( sort );
+       },
+
+       // Filters and Tags
+       onFilter: function( event ) {
+               var request,
+                       $el = $( event.target ),
+                       filter = $el.data( 'filter' );
+
+               // Bail if this is already active
+               if ( $el.hasClass( this.activeClass ) ) {
+                       return;
+               }
+
+               $( '.theme-filter, .theme-section' ).removeClass( this.activeClass );
+               $el.addClass( this.activeClass );
+
+               if ( ! filter ) {
+                       return;
+               }
+
+               // Construct the filter request
+               // using the default values
+               filter = _.union( filter, this.filtersChecked() );
+               request = { tag: [ filter ] };
+
+               // Get the themes by sending Ajax POST request to api.wordpress.org/themes
+               // or searching the local cache
+               this.collection.query( request );
+       },
+
+       // Clicking on a checkbox to add another filter to the request
+       addFilter: function() {
+               this.filtersChecked();
+       },
+
+       // Applying filters triggers a tag request
+       applyFilters: function( event ) {
+               var name,
+                       tags = this.filtersChecked(),
+                       request = { tag: tags },
+                       filteringBy = $( '.filtering-by .tags' );
+
+               if ( event ) {
+                       event.preventDefault();
+               }
+
+               $( 'body' ).addClass( 'filters-applied' );
+               $( '.theme-section.current' ).removeClass( 'current' );
+               filteringBy.empty();
+
+               _.each( tags, function( tag ) {
+                       name = $( 'label[for="feature-id-' + tag + '"]' ).text();
+                       filteringBy.append( '<span class="tag">' + name + '</span>' );
+               });
+
+               // Get the themes by sending Ajax POST request to api.wordpress.org/themes
+               // or searching the local cache
+               this.collection.query( request );
+       },
+
+       // Get the checked filters
+       // @return {array} of tags or false
+       filtersChecked: function() {
+               var items = $( '.feature-group' ).find( ':checkbox' ),
+                       tags = [];
+
+               _.each( items.filter( ':checked' ), function( item ) {
+                       tags.push( $( item ).prop( 'value' ) );
+               });
+
+               // When no filters are checked, restore initial state and return
+               if ( tags.length === 0 ) {
+                       $( '.apply-filters' ).find( 'span' ).text( '' );
+                       $( '.clear-filters' ).hide();
+                       $( 'body' ).removeClass( 'filters-applied' );
+                       return false;
+               }
+
+               $( '.apply-filters' ).find( 'span' ).text( tags.length );
+               $( '.clear-filters' ).css( 'display', 'inline-block' );
+
+               return tags;
+       },
+
+       activeClass: 'current',
+
+       // Overwrite search container class to append search
+       // in new location
+       searchContainer: $( '.theme-navigation' ),
+
+       uploader: function() {
+               $( 'a.upload' ).on( 'click', function( event ) {
+                       event.preventDefault();
+                       $( 'body' ).addClass( 'show-upload-theme' );
+                       themes.router.navigate( themes.router.baseUrl( '?upload' ), { replace: true } );
+               });
+               $( 'a.browse-themes' ).on( 'click', function( event ) {
+                       event.preventDefault();
+                       $( 'body' ).removeClass( 'show-upload-theme' );
+                       themes.router.navigate( themes.router.baseUrl( '' ), { replace: true } );
+               });
+       },
+
+       // Toggle the full filters navigation
+       moreFilters: function( event ) {
+               event.preventDefault();
+
+               if ( $( 'body' ).hasClass( 'filters-applied' ) ) {
+                       return this.backToFilters();
+               }
+
+               // If the filters section is opened and filters are checked
+               // run the relevant query collapsing to filtered-by state
+               if ( $( 'body' ).hasClass( 'more-filters-opened' ) && this.filtersChecked() ) {
+                       return this.addFilter();
+               }
+
+               this.clearSearch();
+
+               themes.router.navigate( themes.router.baseUrl( '' ) );
+               $( 'body' ).toggleClass( 'more-filters-opened' );
+       },
+
+       // Expand/collapse each individual filter section
+       filterSection: function() {
+               $( event.target ).parent().toggleClass( 'open' );
+       },
+
+       // Clears all the checked filters
+       // @uses filtersChecked()
+       clearFilters: function( event ) {
+               var items = $( '.feature-group' ).find( ':checkbox' ),
+                       self = this;
+
+               event.preventDefault();
+
+               _.each( items.filter( ':checked' ), function( item ) {
+                       $( item ).prop( 'checked', false );
+                       return self.filtersChecked();
+               });
+       },
+
+       backToFilters: function( event ) {
+               if ( event ) {
+                       event.preventDefault();
+               }
+
+               $( 'body' ).removeClass( 'filters-applied' );
+       },
+
+       clearSearch: function() {
+               $( '#theme-search-input').val( '' );
+       }
+});
+
+themes.InstallerRouter = Backbone.Router.extend({
+       routes: {
+               'theme-install.php?theme=:slug': 'preview',
+               'theme-install.php?browse=:sort': 'sort',
+               'theme-install.php?upload': 'upload',
+               'theme-install.php?search=:query': 'search',
+               'theme-install.php': 'sort'
+       },
+
+       baseUrl: function( url ) {
+               return 'theme-install.php' + url;
+       },
+
+       search: function( query ) {
+               $( '.theme-search' ).val( query );
+       },
+
+       navigate: function() {
+               if ( Backbone.history._hasPushState ) {
+                       Backbone.Router.prototype.navigate.apply( this, arguments );
+               }
+       }
+});
+
+
+themes.RunInstaller = {
+
+       init: function() {
+               // Set up the view
+               // Passes the default 'section' as an option
+               this.view = new themes.view.Installer({
+                       section: 'featured',
+                       SearchView: themes.view.InstallerSearch
+               });
+
+               // Render results
+               this.render();
+
+       },
+
+       render: function() {
+
+               // Render results
+               this.view.render();
+               this.routes();
+
+               Backbone.history.start({
+                       root: themes.data.settings.adminUrl,
+                       pushState: true,
+                       hashChange: false
+               });
        },
 
        routes: function() {
-               // Bind to our global thx object
-               // so that the object is available to sub-views
-               themes.router = new themes.routes();
+               var self = this,
+                       request = {};
+
+               // Bind to our global `wp.themes` object
+               // so that the router is available to sub-views
+               themes.router = new themes.InstallerRouter();
+
+               // Handles `theme` route event
+               // Queries the API for the passed theme slug
+               themes.router.on( 'route:preview', function( slug ) {
+                       request.theme = slug;
+                       self.view.collection.query( request );
+               });
+
+               // Handles sorting / browsing routes
+               // Also handles the root URL triggering a sort request
+               // for `featured`, the default view
+               themes.router.on( 'route:sort', function( sort ) {
+                       if ( ! sort ) {
+                               sort = 'featured';
+                       }
+                       self.view.sort( sort );
+                       self.view.trigger( 'theme:close' );
+               });
+
+               // Support the `upload` route by going straight to upload section
+               themes.router.on( 'route:upload', function() {
+                       $( 'a.upload' ).trigger( 'click' );
+               });
+
+               // The `search` route event. The router populates the input field.
+               themes.router.on( 'route:search', function() {
+                       $( '.theme-search' ).focus().trigger( 'keyup' );
+               });
+
+               this.extraRoutes();
+       },
+
+       extraRoutes: function() {
+               return false;
        }
 };
 
 // Ready...
-jQuery( document ).ready(
-
-       // Bring on the themes
-       _.bind( themes.Run.init, themes.Run )
-
-);
+$( document ).ready(function() {
+       if ( themes.isInstall ) {
+               themes.RunInstaller.init();
+       } else {
+               themes.Run.init();
+       }
+});
 
 })( jQuery );
 
@@ -769,8 +1681,8 @@ jQuery(document).ready( function($) {
                        W = ( 1040 < width ) ? 1040 : width,
                        adminbar_height = 0;
 
-               if ( $('body.admin-bar').length ) {
-                       adminbar_height = parseInt( jQuery('#wpadminbar').css('height'), 10 );
+               if ( $('#wpadminbar').length ) {
+                       adminbar_height = parseInt( $('#wpadminbar').css('height'), 10 );
                }
 
                if ( tbWindow.size() ) {