// Render and append
this.view.render();
- this.$el.empty().append( this.view.el ).addClass('rendered');
- this.$el.append( '<br class="clear"/>' );
+ this.$el.empty().append( this.view.el ).addClass( 'rendered' );
},
// Defines search element container
- searchContainer: $( '#wpbody h2:first' ),
+ searchContainer: $( '#wpbody h1:first' ),
// Search input and view
// for current theme collection
// Useful for resetting the views when you clean the input
if ( this.terms === '' ) {
this.reset( themes.data.themes );
+ $( '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
// @uses RegExp
search: function( term ) {
- var match, results, haystack;
+ var match, results, haystack, name, description, author;
// Start with a full collection
this.reset( themes.data.themes, { silent: true } );
// Find results
// _.filter and .test
results = this.filter( function( data ) {
- haystack = _.union( data.get( 'name' ), data.get( 'id' ), data.get( 'description' ), data.get( 'author' ), data.get( 'tags' ) );
+ name = data.get( 'name' ).replace( /(<([^>]+)>)/ig, '' );
+ description = data.get( 'description' ).replace( /(<([^>]+)>)/ig, '' );
+ author = data.get( 'author' ).replace( /(<([^>]+)>)/ig, '' );
+
+ haystack = _.union( [ name, data.get( 'id' ), description, author, data.get( 'tags' ) ] );
if ( match.test( data.get( 'author' ) ) && term.length > 2 ) {
data.set( 'displayAuthor', true );
// 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 ) {
this.count = this.length;
}
- this.trigger( 'update' );
+ this.trigger( 'themes:update' );
this.trigger( 'query:success', this.count );
}
},
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;
events: {
'click .close-full-overlay': 'close',
'click .collapse-sidebar': 'collapse',
+ 'click .devices button': 'previewDevice',
'click .previous-theme': 'previousTheme',
'click .next-theme': 'nextTheme',
'keyup': 'keyEvent'
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 } );
$( '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() {
if ( themes.focusedTheme ) {
themes.focusedTheme.focus();
}
- });
+ }).removeClass( 'iframe-ready' );
themes.router.navigate( themes.router.baseUrl( '' ) );
this.trigger( 'preview:close' );
return false;
},
- collapse: function() {
+ collapse: function( event ) {
+ var $button = $( event.currentTarget );
+ if ( 'true' === $button.attr( 'aria-expanded' ) ) {
+ $button.attr({ 'aria-expanded': 'false', 'aria-label': l10n.expandSidebar });
+ } else {
+ $button.attr({ 'aria-expanded': 'true', 'aria-label': l10n.collapseSidebar });
+ }
this.$el.toggleClass( 'collapsed' ).toggleClass( 'expanded' );
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 ) {
// 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
index: 0,
// The theme count element
- count: $( '.wp-filter .theme-count' ),
+ count: $( '.wrap .theme-count' ),
+
+ // The live themes count
+ liveThemeCount: 0,
initialize: function( options ) {
var self = this;
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 ) {
if ( _.isNumber( count ) ) {
self.count.text( count );
+ self.announceSearchResults( count );
} else {
self.count.text( self.collection.length );
+ self.announceSearchResults( self.collection.length );
}
});
// and keeping theme count in sync
render: function() {
// Clear the DOM, please
- this.$el.html( '' );
+ this.$el.empty();
// If the user doesn't have switch capabilities
// or there is only one theme in the collection
}
// Display a live theme count for the collection
- this.count.text( this.collection.count ? this.collection.count : this.collection.length );
+ this.liveThemeCount = this.collection.count ? this.collection.count : this.collection.length;
+ this.count.text( this.liveThemeCount );
+
+ this.announceSearchResults( this.liveThemeCount );
},
// Iterates through each instance of the collection
}
// Make sure the add-new stays at the end
- if ( page >= 1 ) {
+ if ( ! themes.isInstall && page >= 1 ) {
$( '.add-new-theme' ).remove();
}
});
// '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++;
self.theme.trigger( 'theme:expand', previousModel.cid );
}
+ },
+
+ // Dispatch audible search results feedback message
+ announceSearchResults: function( count ) {
+ if ( 0 === count ) {
+ wp.a11y.speak( l10n.noThemesFound );
+ } else {
+ wp.a11y.speak( l10n.themesFound.replace( '%d', count ) );
+ }
}
});
attributes: {
placeholder: l10n.searchPlaceholder,
- type: 'search'
+ type: 'search',
+ 'aria-describedby': 'live-search-desc'
},
events: {
- 'input': 'search',
- 'keyup': 'search',
- 'change': 'search',
- 'search': 'search',
- 'blur': 'pushState'
+ 'input': 'search',
+ 'keyup': 'search',
+ 'blur': 'pushState'
},
initialize: function( options ) {
},
- // 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' );
- }
+ /**
+ * Since doSearch is debounced, it will only run when user input comes to a rest
+ */
+ this.doSearch( event );
+ },
+
+ // Runs a search on the theme collection.
+ doSearch: _.debounce( function( event ) {
+ var options = {};
this.collection.doSearch( event.target.value );
} else {
themes.router.navigate( themes.router.baseUrl( '' ) );
}
- },
+ }, 500 ),
pushState: function( event ) {
var url = themes.router.baseUrl( '' );
themes.view.InstallerSearch = themes.view.Search.extend({
events: {
+ 'input': 'search',
'keyup': 'search'
},
event.target.value = '';
}
- _.debounce( _.bind( this.doSearch, this ), 300 )( event.target.value );
+ this.doSearch( event.target.value );
},
doSearch: _.debounce( function( value ) {
}
$( '.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
// Set route
themes.router.navigate( themes.router.baseUrl( themes.router.searchPath + value ), { replace: true } );
- }, 300 )
+ }, 500 )
});
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
$( '.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 );
},
// 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
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() {
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