]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-admin/js/theme.js
Wordpress 4.5.3-scripts
[autoinstalls/wordpress.git] / wp-admin / js / theme.js
index 0e83f4e04beac38df3a90881d64bd48a632ad32b..9cd738a1c32af144b83ee91432ba9a0e4817242a 100644 (file)
@@ -80,7 +80,6 @@ themes.view.Appearance = wp.Backbone.View.extend({
                // Render and append
                this.view.render();
                this.$el.empty().append( this.view.el ).addClass( 'rendered' );
-               this.$el.append( '<br class="clear"/>' );
        },
 
        // Defines search element container
@@ -159,8 +158,8 @@ themes.Collection = Backbone.Collection.extend({
                        $( 'body' ).removeClass( 'no-results' );
                }
 
-               // Trigger an 'update' event
-               this.trigger( 'update' );
+               // Trigger a 'themes:update' event
+               this.trigger( 'themes:update' );
        },
 
        // Performs a search within the collection
@@ -186,7 +185,7 @@ themes.Collection = Backbone.Collection.extend({
                        description = data.get( 'description' ).replace( /(<([^>]+)>)/ig, '' );
                        author      = data.get( 'author' ).replace( /(<([^>]+)>)/ig, '' );
 
-                       haystack = _.union( name, data.get( 'id' ), description, author, data.get( 'tags' ) );
+                       haystack = _.union( [ name, data.get( 'id' ), description, author, data.get( 'tags' ) ] );
 
                        if ( match.test( data.get( 'author' ) ) && term.length > 2 ) {
                                data.set( 'displayAuthor', true );
@@ -265,7 +264,7 @@ themes.Collection = Backbone.Collection.extend({
 
                                // Trigger a collection refresh event
                                // and a `query:success` event with a `count` argument.
-                               self.trigger( 'update' );
+                               self.trigger( 'themes:update' );
                                self.trigger( 'query:success', count );
 
                                if ( data.themes && data.themes.length === 0 ) {
@@ -309,7 +308,7 @@ themes.Collection = Backbone.Collection.extend({
                                this.count = this.length;
                        }
 
-                       this.trigger( 'update' );
+                       this.trigger( 'themes:update' );
                        this.trigger( 'query:success', this.count );
                }
        },
@@ -454,6 +453,8 @@ themes.view.Theme = wp.Backbone.View.extend({
                var self = this,
                        current, preview;
 
+               event = event || window.event;
+
                // Bail if the user scrolled on a touch device
                if ( this.touchDrag === true ) {
                        return this.touchDrag = false;
@@ -755,6 +756,7 @@ themes.view.Preview = themes.view.Details.extend({
        events: {
                'click .close-full-overlay': 'close',
                'click .collapse-sidebar': 'collapse',
+               'click .devices button': 'previewDevice',
                'click .previous-theme': 'previousTheme',
                'click .next-theme': 'nextTheme',
                'keyup': 'keyEvent'
@@ -764,9 +766,15 @@ themes.view.Preview = themes.view.Details.extend({
        html: themes.template( 'theme-preview' ),
 
        render: function() {
-               var data = this.model.toJSON();
+               var self = this, currentPreviewDevice,
+                       data = this.model.toJSON();
 
-               this.$el.html( this.html( data ) );
+               this.$el.removeClass( 'iframe-ready' ).html( this.html( data ) );
+
+               currentPreviewDevice = this.$el.data( 'current-preview-device' );
+               if ( currentPreviewDevice ) {
+                       self.tooglePreviewDeviceButtons( currentPreviewDevice );
+               }
 
                themes.router.navigate( themes.router.baseUrl( themes.router.themePath + this.model.get( 'id' ) ), { replace: true } );
 
@@ -774,6 +782,14 @@ themes.view.Preview = themes.view.Details.extend({
                        $( 'body' ).addClass( 'theme-installer-active full-overlay-active' );
                        $( '.close-full-overlay' ).focus();
                });
+
+               this.$el.find( 'iframe' ).one( 'load', function() {
+                       self.iframeLoaded();
+               });
+       },
+
+       iframeLoaded: function() {
+               this.$el.addClass( 'iframe-ready' );
        },
 
        close: function() {
@@ -784,7 +800,7 @@ themes.view.Preview = themes.view.Details.extend({
                        if ( themes.focusedTheme ) {
                                themes.focusedTheme.focus();
                        }
-               });
+               }).removeClass( 'iframe-ready' );
 
                themes.router.navigate( themes.router.baseUrl( '' ) );
                this.trigger( 'preview:close' );
@@ -805,6 +821,29 @@ themes.view.Preview = themes.view.Details.extend({
                return false;
        },
 
+       previewDevice: function( event ) {
+               var device = $( event.currentTarget ).data( 'device' );
+
+               this.$el
+                       .removeClass( 'preview-desktop preview-tablet preview-mobile' )
+                       .addClass( 'preview-' + device )
+                       .data( 'current-preview-device', device );
+
+               this.tooglePreviewDeviceButtons( device );
+       },
+
+       tooglePreviewDeviceButtons: function( newDevice ) {
+               var $devices = $( '.wp-full-overlay-footer .devices' );
+
+               $devices.find( 'button' )
+                       .removeClass( 'active' )
+                       .attr( 'aria-pressed', false );
+
+               $devices.find( 'button.preview-' + newDevice )
+                       .addClass( 'active' )
+                       .attr( 'aria-pressed', true );
+       },
+
        keyEvent: function( event ) {
                // The escape key closes the preview
                if ( event.keyCode === 27 ) {
@@ -827,7 +866,7 @@ themes.view.Preview = themes.view.Details.extend({
 // a wrapper that will hold all the theme elements
 themes.view.Themes = wp.Backbone.View.extend({
 
-       className: 'themes',
+       className: 'themes wp-clearfix',
        $overlay: $( 'div.theme-overlay' ),
 
        // Number to keep track of scroll position
@@ -835,7 +874,7 @@ themes.view.Themes = wp.Backbone.View.extend({
        index: 0,
 
        // The theme count element
-       count: $( '.wp-core-ui .theme-count' ),
+       count: $( '.wrap .theme-count' ),
 
        // The live themes count
        liveThemeCount: 0,
@@ -853,11 +892,11 @@ themes.view.Themes = wp.Backbone.View.extend({
                self.currentTheme();
 
                // When the collection is updated by user input...
-               this.listenTo( self.collection, 'update', function() {
+               this.listenTo( self.collection, 'themes:update', function() {
                        self.parent.page = 0;
                        self.currentTheme();
                        self.render( this );
-               });
+               } );
 
                // Update theme count to full result set when available.
                this.listenTo( self.collection, 'query:success', function( count ) {
@@ -958,7 +997,7 @@ themes.view.Themes = wp.Backbone.View.extend({
                }
 
                // Make sure the add-new stays at the end
-               if ( page >= 1 ) {
+               if ( ! themes.isInstall && page >= 1 ) {
                        $( '.add-new-theme' ).remove();
                }
 
@@ -980,8 +1019,8 @@ themes.view.Themes = wp.Backbone.View.extend({
                });
 
                // 'Add new theme' element shown at the end of the grid
-               if ( themes.data.settings.canInstall ) {
-                       this.$el.append( '<div class="theme add-new-theme"><a href="' + themes.data.settings.installURI + '"><div class="theme-screenshot"><span></span></div><h3 class="theme-name">' + l10n.addNew + '</h3></a></div>' );
+               if ( ! themes.isInstall && themes.data.settings.canInstall ) {
+                       this.$el.append( '<div class="theme add-new-theme"><a href="' + themes.data.settings.installURI + '"><div class="theme-screenshot"><span></span></div><h2 class="theme-name">' + l10n.addNew + '</h2></a></div>' );
                }
 
                this.parent.page++;
@@ -1322,7 +1361,7 @@ themes.view.InstallerSearch =  themes.view.Search.extend({
                }
 
                $( '.filter-links li > a.current' ).removeClass( 'current' );
-               $( 'body' ).removeClass( 'show-filters filters-applied' );
+               $( 'body' ).removeClass( 'show-filters filters-applied show-favorites-form' );
 
                // Get the themes by sending Ajax POST request to api.wordpress.org/themes
                // or searching the local cache
@@ -1345,7 +1384,9 @@ themes.view.Installer = themes.view.Appearance.extend({
                'click .filter-drawer .apply-filters': 'applyFilters',
                'click .filter-group [type="checkbox"]': 'addFilter',
                'click .filter-drawer .clear-filters': 'clearFilters',
-               'click .filtered-by': 'backToFilters'
+               'click .filtered-by': 'backToFilters',
+               'click .favorites-form-submit' : 'saveUsername',
+               'keyup #wporg-username-input': 'saveUsername'
        },
 
        // Initial render method
@@ -1437,6 +1478,12 @@ themes.view.Installer = themes.view.Appearance.extend({
                $( '.filter-links li > a, .theme-filter' ).removeClass( this.activeClass );
                $( '[data-sort="' + sort + '"]' ).addClass( this.activeClass );
 
+               if ( 'favorites' === sort ) {
+                       $ ( 'body' ).addClass( 'show-favorites-form' );
+               } else {
+                       $ ( 'body' ).removeClass( 'show-favorites-form' );
+               }
+
                this.browse( sort );
        },
 
@@ -1460,7 +1507,7 @@ themes.view.Installer = themes.view.Appearance.extend({
 
                // Construct the filter request
                // using the default values
-               filter = _.union( filter, this.filtersChecked() );
+               filter = _.union( [ filter, this.filtersChecked() ] );
                request = { tag: [ filter ] };
 
                // Get the themes by sending Ajax POST request to api.wordpress.org/themes
@@ -1498,6 +1545,35 @@ themes.view.Installer = themes.view.Appearance.extend({
                this.collection.query( request );
        },
 
+       // Save the user's WordPress.org username and get his favorite themes.
+       saveUsername: function ( event ) {
+               var username = $( '#wporg-username-input' ).val(),
+                       nonce = $( '#wporg-username-nonce' ).val(),
+                       request = { browse: 'favorites', user: username },
+                       that = this;
+
+               if ( event ) {
+                       event.preventDefault();
+               }
+
+               // save username on enter
+               if ( event.type === 'keyup' && event.which !== 13 ) {
+                       return;
+               }
+
+               return wp.ajax.send( 'save-wporg-username', {
+                       data: {
+                               _wpnonce: nonce,
+                               username: username
+                       },
+                       success: function () {
+                               // Get the themes by sending Ajax POST request to api.wordpress.org/themes
+                               // or searching the local cache
+                               that.collection.query( request );
+                       }
+               } );
+       },
+
        // Get the checked filters
        // @return {array} of tags or false
        filtersChecked: function() {
@@ -1658,6 +1734,9 @@ themes.RunInstaller = {
                themes.router.on( 'route:preview', function( slug ) {
                        request.theme = slug;
                        self.view.collection.query( request );
+                       self.view.collection.once( 'update', function() {
+                               self.view.view.theme.preview();
+                       });
                });
 
                // Handles sorting / browsing routes