2013-11-13 15:58:05 -05:00
|
|
|
/* global _wpThemeSettings, confirm */
|
|
|
|
window.wp = window.wp || {};
|
|
|
|
|
|
|
|
( function($) {
|
|
|
|
|
|
|
|
// Set up our namespace...
|
2013-11-20 14:44:12 -05:00
|
|
|
var themes, l10n;
|
|
|
|
themes = wp.themes = wp.themes || {};
|
2013-11-13 15:58:05 -05:00
|
|
|
|
|
|
|
// Store the theme data and settings for organized and quick access
|
2013-11-20 14:44:12 -05:00
|
|
|
// themes.data.settings, themes.data.themes, themes.data.l10n
|
2013-11-13 15:58:05 -05:00
|
|
|
themes.data = _wpThemeSettings;
|
2013-11-20 14:44:12 -05:00
|
|
|
l10n = themes.data.l10n;
|
2013-11-13 15:58:05 -05:00
|
|
|
|
|
|
|
// Setup app structure
|
|
|
|
_.extend( themes, { model: {}, view: {}, routes: {}, router: {}, template: wp.template });
|
|
|
|
|
|
|
|
themes.model = Backbone.Model.extend({});
|
|
|
|
|
|
|
|
// Main view controller for themes.php
|
|
|
|
// Unifies and renders all available views
|
|
|
|
themes.view.Appearance = wp.Backbone.View.extend({
|
|
|
|
|
2013-11-20 14:44:12 -05:00
|
|
|
el: '#wpbody-content .wrap .theme-browser',
|
2013-11-13 15:58:05 -05:00
|
|
|
|
|
|
|
window: $( window ),
|
|
|
|
// Pagination instance
|
|
|
|
page: 0,
|
|
|
|
|
|
|
|
// Sets up a throttler for binding to 'scroll'
|
|
|
|
initialize: function() {
|
2013-11-29 00:14:09 -05:00
|
|
|
// Scroller checks how far the scroll position is
|
|
|
|
_.bindAll( this, 'scroller' );
|
2013-11-13 15:58:05 -05:00
|
|
|
|
2013-11-29 00:14:09 -05:00
|
|
|
// Bind to the scroll event and throttle
|
|
|
|
// the results from this.scroller
|
|
|
|
this.window.bind( 'scroll', _.throttle( this.scroller, 300 ) );
|
2013-11-13 15:58:05 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// Main render control
|
|
|
|
render: function() {
|
|
|
|
// Setup the main theme view
|
|
|
|
// with the current theme collection
|
|
|
|
this.view = new themes.view.Themes({
|
|
|
|
collection: this.collection,
|
|
|
|
parent: this
|
|
|
|
});
|
2013-11-20 14:44:12 -05:00
|
|
|
|
|
|
|
// Render search form.
|
|
|
|
this.search();
|
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Render and append
|
|
|
|
this.view.render();
|
No-JavaScript and no-Customizer support for the new Themes screen.
JavaScript is rarely disabled, but graceful degradation is still important. For example, syntax errors can occur, usually with major WP updates that overhaul entire experiences and update external libraries combined with themes or plugins doing weird or old things. If this error is due to their current theme, a user needs to be able to access the themes screen to switch away from the theme. A more subtle issue could make things painful to diagnose.
This commit renders the grid in PHP (the template is duplicated, but it lightweight, fairly mundane, and easy to sync). On Backbone render, the grid is then re-rendered from JavaScript so searches can occur. Customize and Live Preview is disabled if JS fails to kick in. If JS is disabled, old-school "Preview" links are displayed.
No-Customizer support: The customizer is only supported when the browser supports postMessage (IE8+), and if the frontend is a different domain, CORS (IE10+). We use the .hide-if-no-customize class for this. Pre-customize "Preview" links should use .hide-if-customize.
The .load-customize class should be used to declare a link that opens the customizer. This enables customize-loader.js to intercept this link and load the customizer on top of the current window, making for a smoother experience.
fixes #25964.
Built from https://develop.svn.wordpress.org/trunk@26726
git-svn-id: http://core.svn.wordpress.org/trunk@26615 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2013-12-06 11:11:10 -05:00
|
|
|
this.$el.empty().append( this.view.el ).addClass('rendered');
|
2013-11-29 00:17:11 -05:00
|
|
|
this.$el.append( '<br class="clear"/>' );
|
2013-11-13 15:58:05 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// Search input and view
|
|
|
|
// for current theme collection
|
|
|
|
search: function() {
|
|
|
|
var view,
|
|
|
|
self = this;
|
|
|
|
|
2013-12-04 19:16:10 -05:00
|
|
|
// Don't render the search if there is only one theme
|
|
|
|
if ( themes.data.themes.length === 1 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
view = new themes.view.Search({ collection: self.collection });
|
|
|
|
|
|
|
|
// Render and append after screen title
|
|
|
|
view.render();
|
2013-11-28 21:40:09 -05:00
|
|
|
$('#wpbody h2:first').append( view.el );
|
2013-11-13 15:58:05 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// Checks when the user gets close to the bottom
|
|
|
|
// of the mage and triggers a theme:scroll event
|
|
|
|
scroller: function() {
|
|
|
|
var self = this,
|
|
|
|
bottom, threshold;
|
|
|
|
|
|
|
|
bottom = this.window.scrollTop() + self.window.height();
|
|
|
|
threshold = self.$el.offset().top + self.$el.outerHeight( false ) - self.window.height();
|
|
|
|
threshold = Math.round( threshold * 0.9 );
|
|
|
|
|
|
|
|
if ( bottom > threshold ) {
|
|
|
|
this.trigger( 'theme:scroll' );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2013-11-13 15:58:05 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Set up the Collection for our theme data
|
|
|
|
// @has 'id' 'name' 'screenshot' 'author' 'authorURI' 'version' 'active' ...
|
|
|
|
themes.Collection = Backbone.Collection.extend({
|
|
|
|
|
|
|
|
model: themes.model,
|
|
|
|
|
|
|
|
// Search terms
|
|
|
|
terms: '',
|
|
|
|
|
|
|
|
// Controls searching on the current theme collection
|
|
|
|
// and triggers an update event
|
|
|
|
doSearch: function( value ) {
|
|
|
|
|
2013-12-02 02:16:10 -05:00
|
|
|
// Don't do anything if we've already done this search
|
|
|
|
// Useful because the Search handler fires multiple times per keystroke
|
|
|
|
if ( this.terms === value ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Updates terms with the value passed
|
|
|
|
this.terms = value;
|
|
|
|
|
|
|
|
// If we have terms, run a search...
|
|
|
|
if ( this.terms.length > 0 ) {
|
|
|
|
this.search( this.terms );
|
|
|
|
}
|
|
|
|
|
|
|
|
// If search is blank, show all themes
|
|
|
|
// Useful for resetting the views when you clean the input
|
|
|
|
if ( this.terms === '' ) {
|
|
|
|
this.reset( themes.data.themes );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger an 'update' event
|
|
|
|
this.trigger( 'update' );
|
|
|
|
},
|
2013-12-01 19:55:09 -05:00
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Performs a search within the collection
|
|
|
|
// @uses RegExp
|
|
|
|
search: function( term ) {
|
2013-12-05 14:15:11 -05:00
|
|
|
var match, results, haystack;
|
2013-11-13 15:58:05 -05:00
|
|
|
|
|
|
|
// Start with a full collection
|
2013-12-05 14:15:11 -05:00
|
|
|
this.reset( themes.data.themes, { silent: true } );
|
2013-11-13 15:58:05 -05:00
|
|
|
|
|
|
|
// The RegExp object to match
|
|
|
|
//
|
|
|
|
// Consider spaces as word delimiters and match the whole string
|
|
|
|
// so matching terms can be combined
|
|
|
|
term = term.replace( ' ', ')(?=.*' );
|
|
|
|
match = new RegExp( '^(?=.*' + term + ').+', 'i' );
|
|
|
|
|
|
|
|
// Find results
|
|
|
|
// _.filter and .test
|
2013-12-05 14:15:11 -05:00
|
|
|
results = this.filter( function( data ) {
|
2013-11-29 17:18:10 -05:00
|
|
|
haystack = _.union( data.get( 'name' ), data.get( 'description' ), data.get( 'author' ), data.get( 'tags' ) );
|
2013-11-13 15:58:05 -05:00
|
|
|
|
2013-12-05 14:57:11 -05:00
|
|
|
if ( match.test( data.get( 'author' ) ) && term.length > 2 ) {
|
2013-11-13 15:58:05 -05:00
|
|
|
data.set( 'displayAuthor', true );
|
|
|
|
}
|
|
|
|
|
|
|
|
return match.test( haystack );
|
|
|
|
});
|
|
|
|
|
2013-12-05 14:15:11 -05:00
|
|
|
this.reset( results );
|
2013-11-13 15:58:05 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// Paginates the collection with a helper method
|
|
|
|
// that slices the collection
|
|
|
|
paginate: function( instance ) {
|
|
|
|
var collection = this;
|
|
|
|
instance = instance || 0;
|
|
|
|
|
|
|
|
// Themes per instance are set at 15
|
|
|
|
collection = _( collection.rest( 15 * instance ) );
|
|
|
|
collection = _( collection.first( 15 ) );
|
|
|
|
|
|
|
|
return collection;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// This is the view that controls each theme item
|
|
|
|
// that will be displayed on the screen
|
|
|
|
themes.view.Theme = wp.Backbone.View.extend({
|
|
|
|
|
|
|
|
// Wrap theme data on a div.theme element
|
|
|
|
className: 'theme',
|
|
|
|
|
|
|
|
// Reflects which theme view we have
|
|
|
|
// 'grid' (default) or 'detail'
|
|
|
|
state: 'grid',
|
|
|
|
|
|
|
|
// The HTML template for each element to be rendered
|
|
|
|
html: themes.template( 'theme' ),
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'click': 'expand'
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var data = this.model.toJSON();
|
|
|
|
// Render themes using the html template
|
|
|
|
this.$el.html( this.html( data ) );
|
|
|
|
// Renders active theme styles
|
|
|
|
this.activeTheme();
|
|
|
|
|
|
|
|
if ( this.model.get( 'displayAuthor' ) ) {
|
|
|
|
this.$el.addClass( 'display-author' );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Adds a class to the currently active theme
|
|
|
|
// and to the overlay in detailed view mode
|
|
|
|
activeTheme: function() {
|
|
|
|
if ( this.model.get( 'active' ) ) {
|
|
|
|
this.$el.addClass( 'active' );
|
2013-11-20 14:44:12 -05:00
|
|
|
$( '.theme-overlay' ).addClass( 'active' );
|
2013-11-13 15:58:05 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Single theme overlay screen
|
|
|
|
// It's shown when clicking a theme
|
|
|
|
expand: function( event ) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
event = event || window.event;
|
|
|
|
|
|
|
|
// Prevent the modal from showing when the user clicks
|
|
|
|
// one of the direct action buttons
|
2013-12-01 19:55:09 -05:00
|
|
|
if ( $( event.target ).is( '.theme-actions a' ) ) {
|
2013-11-13 15:58:05 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.trigger( 'theme:expand', self.model.cid );
|
|
|
|
}
|
2013-07-05 11:15:58 -04:00
|
|
|
});
|
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Theme Details view
|
|
|
|
// Set ups a modal overlay with the expanded theme data
|
|
|
|
themes.view.Details = wp.Backbone.View.extend({
|
|
|
|
|
|
|
|
// Wrap theme data on a div.theme element
|
2013-11-20 14:44:12 -05:00
|
|
|
className: 'theme-overlay',
|
2013-11-13 15:58:05 -05:00
|
|
|
|
|
|
|
events: {
|
|
|
|
'click': 'collapse',
|
|
|
|
'click .delete-theme': 'deleteTheme',
|
|
|
|
'click .left': 'previousTheme',
|
|
|
|
'click .right': 'nextTheme'
|
|
|
|
},
|
|
|
|
|
|
|
|
// The HTML template for the theme overlay
|
|
|
|
html: themes.template( 'theme-single' ),
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var data = this.model.toJSON();
|
|
|
|
this.$el.html( this.html( data ) );
|
|
|
|
// Renders active theme styles
|
|
|
|
this.activeTheme();
|
|
|
|
// Set up navigation events
|
|
|
|
this.navigation();
|
2013-12-06 14:42:11 -05:00
|
|
|
// Checks screenshot size
|
|
|
|
this.screenshotCheck( this.$el );
|
2013-11-13 15:58:05 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// Adds a class to the currently active theme
|
|
|
|
// and to the overlay in detailed view mode
|
|
|
|
activeTheme: function() {
|
|
|
|
// Check the model has the active property
|
2013-11-20 14:44:12 -05:00
|
|
|
this.$el.toggleClass( 'active', this.model.get( 'active' ) );
|
2013-11-13 15:58:05 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// Single theme overlay screen
|
|
|
|
// It's shown when clicking a theme
|
|
|
|
collapse: function( event ) {
|
|
|
|
var self = this,
|
|
|
|
scroll;
|
|
|
|
|
|
|
|
event = event || window.event;
|
|
|
|
|
2013-12-04 23:57:10 -05:00
|
|
|
// Prevent collapsing detailed view when there is only one theme available
|
|
|
|
if ( themes.data.themes.length === 1 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Detect if the click is inside the overlay
|
|
|
|
// and don't close it unless the target was
|
|
|
|
// the div.back button
|
2013-12-02 02:12:09 -05:00
|
|
|
if ( $( event.target ).is( '.theme-backdrop' ) || $( event.target ).is( 'div.close' ) || event.keyCode === 27 ) {
|
2013-11-13 15:58:05 -05:00
|
|
|
|
|
|
|
// Add a temporary closing class while overlay fades out
|
|
|
|
$( 'body' ).addClass( 'closing-overlay' );
|
|
|
|
|
|
|
|
// With a quick fade out animation
|
|
|
|
this.$el.fadeOut( 130, function() {
|
|
|
|
// Clicking outside the modal box closes the overlay
|
2013-11-20 14:44:12 -05:00
|
|
|
$( 'body' ).removeClass( 'theme-overlay-open closing-overlay' );
|
2013-11-13 15:58:05 -05:00
|
|
|
// Handle event cleanup
|
|
|
|
self.closeOverlay();
|
|
|
|
|
|
|
|
// Get scroll position to avoid jumping to the top
|
|
|
|
scroll = document.body.scrollTop;
|
|
|
|
|
|
|
|
// Clean the url structure
|
|
|
|
themes.router.navigate( '' );
|
|
|
|
|
|
|
|
// Restore scroll position
|
|
|
|
document.body.scrollTop = scroll;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Handles arrow keys navigation for the overlay
|
|
|
|
// Triggers theme:next and theme:previous events
|
|
|
|
navigation: function() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
$( 'body' ).on( 'keyup', function( event ) {
|
|
|
|
|
|
|
|
// Pressing the right arrow key fires a theme:next event
|
|
|
|
if ( event.keyCode === 39 ) {
|
|
|
|
self.trigger( 'theme:next', self.model.cid );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pressing the left arrow key fires a theme:previous event
|
|
|
|
if ( event.keyCode === 37 ) {
|
|
|
|
self.trigger( 'theme:previous', self.model.cid );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pressing the escape key closes the theme details panel
|
|
|
|
if ( event.keyCode === 27 ) {
|
|
|
|
self.collapse();
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
2013-12-01 00:59:11 -05:00
|
|
|
|
|
|
|
// Disable Left/Right when at the start or end of the collection
|
|
|
|
if ( this.model.cid === this.model.collection.at(0).cid ) {
|
|
|
|
this.$el.find( '.left' ).addClass( 'disabled' );
|
|
|
|
}
|
|
|
|
if ( this.model.cid === this.model.collection.at( this.model.collection.length - 1 ).cid ) {
|
|
|
|
this.$el.find( '.right' ).addClass( 'disabled' );
|
|
|
|
}
|
2013-11-13 15:58:05 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// Performs the actions to effectively close
|
|
|
|
// the theme details overlay
|
|
|
|
closeOverlay: function() {
|
|
|
|
this.remove();
|
|
|
|
this.unbind();
|
|
|
|
this.trigger( 'theme:collapse' );
|
|
|
|
},
|
|
|
|
|
|
|
|
// Confirmation dialoge for deleting a theme
|
|
|
|
deleteTheme: function() {
|
|
|
|
return confirm( themes.data.settings.confirmDelete );
|
|
|
|
},
|
|
|
|
|
|
|
|
nextTheme: function() {
|
|
|
|
var self = this;
|
|
|
|
self.trigger( 'theme:next', self.model.cid );
|
|
|
|
},
|
|
|
|
|
|
|
|
previousTheme: function() {
|
|
|
|
var self = this;
|
|
|
|
self.trigger( 'theme:previous', self.model.cid );
|
2013-12-06 14:42:11 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// Checks if the theme screenshot is the old 300px width version
|
|
|
|
// and adds a corresponding class if it's true
|
|
|
|
screenshotCheck: function( el ) {
|
|
|
|
var screenshot, image, width;
|
|
|
|
|
|
|
|
screenshot = el.find( '.screenshot img' );
|
|
|
|
image = new Image();
|
|
|
|
image.src = screenshot.attr( 'src' );
|
|
|
|
|
|
|
|
// Width check
|
|
|
|
if ( image.width <= 300 ) {
|
|
|
|
el.addClass( 'small-screenshot' );
|
|
|
|
}
|
2013-11-13 15:58:05 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
2013-11-20 14:44:12 -05:00
|
|
|
// Controls the rendering of div.themes,
|
2013-11-13 15:58:05 -05:00
|
|
|
// a wrapper that will hold all the theme elements
|
|
|
|
themes.view.Themes = wp.Backbone.View.extend({
|
|
|
|
|
2013-11-20 14:44:12 -05:00
|
|
|
className: 'themes',
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Number to keep track of scroll position
|
|
|
|
// while in theme-overlay mode
|
|
|
|
index: 0,
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// The theme count element
|
2013-11-20 14:44:12 -05:00
|
|
|
count: $( '.theme-count' ),
|
2013-11-13 15:58:05 -05:00
|
|
|
|
|
|
|
initialize: function( options ) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Set up parent
|
|
|
|
this.parent = options.parent;
|
|
|
|
|
|
|
|
// Set current view to [grid]
|
|
|
|
this.setView( 'grid' );
|
|
|
|
|
|
|
|
// Move the active theme to the beginning of the collection
|
|
|
|
self.currentTheme();
|
|
|
|
|
|
|
|
// When the collection is updated by user input...
|
|
|
|
this.listenTo( self.collection, 'update', function() {
|
|
|
|
self.parent.page = 0;
|
|
|
|
self.currentTheme();
|
|
|
|
self.render( this );
|
|
|
|
});
|
|
|
|
|
|
|
|
this.listenTo( this.parent, 'theme:scroll', function() {
|
|
|
|
self.renderThemes( self.parent.page );
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Manages rendering of theme pages
|
|
|
|
// and keeping theme count in sync
|
|
|
|
render: function() {
|
|
|
|
// Clear the DOM, please
|
|
|
|
this.$el.html( '' );
|
|
|
|
|
2013-12-04 19:16:10 -05:00
|
|
|
// If the user doesn't have switch capabilities
|
|
|
|
// or there is only one theme in the collection
|
|
|
|
// render the detailed view of the active theme
|
|
|
|
if ( themes.data.themes.length === 1 ) {
|
|
|
|
|
|
|
|
// Constructs the view
|
|
|
|
this.singleTheme = new themes.view.Details({
|
|
|
|
model: this.collection.models[0]
|
|
|
|
});
|
|
|
|
|
|
|
|
// Render and apply a 'single-theme' class to our container
|
|
|
|
this.singleTheme.render();
|
|
|
|
this.$el.addClass( 'single-theme' );
|
|
|
|
this.$el.append( this.singleTheme.el );
|
|
|
|
}
|
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Generate the themes
|
|
|
|
// Using page instance
|
|
|
|
this.renderThemes( this.parent.page );
|
|
|
|
|
|
|
|
// Display a live theme count for the collection
|
|
|
|
this.count.text( this.collection.length );
|
|
|
|
},
|
|
|
|
|
|
|
|
// Iterates through each instance of the collection
|
|
|
|
// and renders each theme module
|
|
|
|
renderThemes: function( page ) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.instance = self.collection.paginate( page );
|
|
|
|
|
|
|
|
// If we have no more themes bail
|
|
|
|
if ( self.instance.length === 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the add-new stays at the end
|
|
|
|
if ( page >= 1 ) {
|
2013-11-20 14:44:12 -05:00
|
|
|
$( '.add-new-theme' ).remove();
|
2013-11-13 15:58:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through the themes and setup each theme view
|
|
|
|
self.instance.each( function( theme ) {
|
|
|
|
self.theme = new themes.view.Theme({
|
|
|
|
model: theme
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Render the views...
|
|
|
|
self.theme.render();
|
2013-11-20 14:44:12 -05:00
|
|
|
// and append them to div.themes
|
2013-11-13 15:58:05 -05:00
|
|
|
self.$el.append( self.theme.el );
|
|
|
|
|
|
|
|
// Binds to theme:expand to show the modal box
|
|
|
|
// with the theme details
|
|
|
|
self.listenTo( self.theme, 'theme:expand', self.expand, self );
|
|
|
|
});
|
|
|
|
|
|
|
|
// 'Add new theme' element shown at the end of the grid
|
2013-11-20 09:09:10 -05:00
|
|
|
if ( themes.data.settings.canInstall ) {
|
2013-11-21 04:46:10 -05:00
|
|
|
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>' );
|
2013-11-20 09:09:10 -05:00
|
|
|
}
|
2013-11-13 15:58:05 -05:00
|
|
|
|
|
|
|
this.parent.page++;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Grabs current theme and puts it at the beginning of the collection
|
|
|
|
currentTheme: function() {
|
|
|
|
var self = this,
|
|
|
|
current;
|
|
|
|
|
|
|
|
current = self.collection.findWhere({ active: true });
|
|
|
|
|
|
|
|
// Move the active theme to the beginning of the collection
|
|
|
|
if ( current ) {
|
|
|
|
self.collection.remove( current );
|
|
|
|
self.collection.add( current, { at:0 } );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Sets current view
|
|
|
|
setView: function( view ) {
|
|
|
|
return view;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Renders the overlay with the ThemeDetails view
|
|
|
|
// Uses the current model data
|
|
|
|
expand: function( id ) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Set the current theme model
|
|
|
|
this.model = self.collection.get( id );
|
|
|
|
|
|
|
|
// Trigger a route update for the current model
|
|
|
|
themes.router.navigate( 'theme/' + this.model.id );
|
|
|
|
|
|
|
|
// Sets this.view to 'detail'
|
|
|
|
this.setView( 'detail' );
|
2013-11-20 14:44:12 -05:00
|
|
|
$( 'body' ).addClass( 'theme-overlay-open' );
|
2013-11-13 15:58:05 -05:00
|
|
|
|
|
|
|
// Set up the theme details view
|
|
|
|
this.overlay = new themes.view.Details({
|
2013-11-20 14:44:12 -05:00
|
|
|
model: self.model
|
2013-11-13 15:58:05 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
this.overlay.render();
|
|
|
|
this.$el.append( this.overlay.el );
|
|
|
|
|
|
|
|
// Bind to theme:next and theme:previous
|
|
|
|
// triggered by the arrow keys
|
|
|
|
//
|
2013-11-20 14:44:12 -05:00
|
|
|
// Keep track of the current model so we
|
|
|
|
// can infer an index position
|
2013-11-13 15:58:05 -05:00
|
|
|
this.listenTo( this.overlay, 'theme:next', function() {
|
|
|
|
// Renders the next theme on the overlay
|
2013-11-20 14:44:12 -05:00
|
|
|
self.next( [ self.model.cid ] );
|
2013-11-13 15:58:05 -05:00
|
|
|
|
|
|
|
})
|
|
|
|
.listenTo( this.overlay, 'theme:previous', function() {
|
|
|
|
// Renders the previous theme on the overlay
|
2013-11-20 14:44:12 -05:00
|
|
|
self.previous( [ self.model.cid ] );
|
2013-11-13 15:58:05 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// This method renders the next theme on the overlay modal
|
|
|
|
// based on the current position in the collection
|
2013-11-20 14:44:12 -05:00
|
|
|
// @params [model cid]
|
2013-11-13 15:58:05 -05:00
|
|
|
next: function( args ) {
|
|
|
|
var self = this,
|
|
|
|
model, nextModel;
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Get the current theme
|
|
|
|
model = self.collection.get( args[0] );
|
2013-11-20 14:44:12 -05:00
|
|
|
// Find the next model within the collection
|
|
|
|
nextModel = self.collection.at( self.collection.indexOf( model ) + 1 );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Sanity check which also serves as a boundary test
|
|
|
|
if ( nextModel !== undefined ) {
|
|
|
|
|
|
|
|
// We have a new theme...
|
2013-11-20 14:44:12 -05:00
|
|
|
// Close the overlay
|
|
|
|
this.overlay.closeOverlay();
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Trigger a route update for the current model
|
2013-11-20 14:44:12 -05:00
|
|
|
// that renders the new theme's overlay
|
|
|
|
themes.router.navigate( 'theme/' + nextModel.id, { trigger: true } );
|
2013-11-13 15:58:05 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// This method renders the previous theme on the overlay modal
|
|
|
|
// based on the current position in the collection
|
2013-11-20 14:44:12 -05:00
|
|
|
// @params [model cid]
|
2013-11-13 15:58:05 -05:00
|
|
|
previous: function( args ) {
|
|
|
|
var self = this,
|
|
|
|
model, previousModel;
|
|
|
|
|
|
|
|
// Get the current theme
|
|
|
|
model = self.collection.get( args[0] );
|
2013-11-20 14:44:12 -05:00
|
|
|
// Find the previous model within the collection
|
|
|
|
previousModel = self.collection.at( self.collection.indexOf( model ) - 1 );
|
2013-11-13 15:58:05 -05:00
|
|
|
|
|
|
|
if ( previousModel !== undefined ) {
|
|
|
|
|
|
|
|
// We have a new theme...
|
2013-11-20 14:44:12 -05:00
|
|
|
// Close the overlay
|
|
|
|
this.overlay.closeOverlay();
|
2013-11-13 15:58:05 -05:00
|
|
|
|
|
|
|
// Trigger a route update for the current model
|
2013-11-20 14:44:12 -05:00
|
|
|
// that renders the new theme's overlay
|
|
|
|
themes.router.navigate( 'theme/' + previousModel.id, { trigger: true } );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2013-11-13 15:58:05 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-11-20 14:44:12 -05:00
|
|
|
// Search input view controller.
|
2013-11-13 15:58:05 -05:00
|
|
|
themes.view.Search = wp.Backbone.View.extend({
|
|
|
|
|
2013-11-20 14:44:12 -05:00
|
|
|
tagName: 'input',
|
|
|
|
className: 'theme-search',
|
2013-11-13 15:58:05 -05:00
|
|
|
|
2013-11-20 14:44:12 -05:00
|
|
|
attributes: {
|
2013-11-29 17:20:10 -05:00
|
|
|
placeholder: l10n.search,
|
|
|
|
type: 'search'
|
2013-11-13 15:58:05 -05:00
|
|
|
},
|
|
|
|
|
2013-11-20 14:44:12 -05:00
|
|
|
events: {
|
|
|
|
'input': 'search',
|
|
|
|
'keyup': 'search',
|
|
|
|
'change': 'search',
|
|
|
|
'search': 'search'
|
2013-11-13 15:58:05 -05:00
|
|
|
},
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-20 14:44:12 -05:00
|
|
|
// Runs a search on the theme collection.
|
|
|
|
search: function( event ) {
|
|
|
|
// Clear on escape.
|
|
|
|
if ( event.type === 'keyup' && event.which === 27 ) {
|
|
|
|
event.target.value = '';
|
|
|
|
}
|
2013-12-01 19:48:09 -05:00
|
|
|
|
2013-11-20 14:44:12 -05:00
|
|
|
this.collection.doSearch( event.target.value );
|
2013-12-01 19:48:09 -05:00
|
|
|
|
|
|
|
// Update the URL hash
|
2013-12-01 20:51:09 -05:00
|
|
|
if ( event.target.value ) {
|
|
|
|
themes.router.navigate( 'search/' + event.target.value, { replace: true } );
|
|
|
|
} else {
|
2013-12-01 19:48:09 -05:00
|
|
|
themes.router.navigate( '' );
|
2013-12-01 20:51:09 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2013-11-13 15:58:05 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// Sets up the routes events for relevant url queries
|
|
|
|
// Listens to [theme] and [search] params
|
|
|
|
themes.routes = Backbone.Router.extend({
|
|
|
|
|
|
|
|
routes: {
|
2013-12-01 19:48:09 -05:00
|
|
|
'search/*query': 'search',
|
|
|
|
'theme/*slug': 'theme'
|
2013-11-13 15:58:05 -05:00
|
|
|
},
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Set the search input value based on url
|
|
|
|
search: function( query ) {
|
2013-11-20 14:44:12 -05:00
|
|
|
$( '.theme-search' ).val( query );
|
2013-11-13 15:58:05 -05:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Make routes easily extendable
|
|
|
|
_.extend( themes.routes, themes.data.settings.extraRoutes );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Execute and setup the application
|
|
|
|
themes.Run = {
|
|
|
|
init: function() {
|
|
|
|
// Initializes the blog's theme library view
|
|
|
|
// Create a new collection with data
|
|
|
|
this.themes = new themes.Collection( themes.data.themes );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
// Set up the view
|
|
|
|
this.view = new themes.view.Appearance({
|
|
|
|
collection: this.themes
|
|
|
|
});
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
this.render();
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
// Render results
|
|
|
|
this.view.render();
|
|
|
|
|
|
|
|
// Calls the routes functionality
|
|
|
|
this.routes();
|
|
|
|
|
|
|
|
// Set ups history with pushState and our root
|
|
|
|
Backbone.history.start({ root: themes.data.settings.root });
|
|
|
|
},
|
|
|
|
|
|
|
|
routes: function() {
|
|
|
|
var self = this;
|
|
|
|
// Bind to our global thx object
|
|
|
|
// so that the object is available to sub-views
|
|
|
|
themes.router = new themes.routes();
|
|
|
|
|
|
|
|
// Handles theme details route event
|
|
|
|
themes.router.on( 'route:theme', function( slug ) {
|
|
|
|
self.view.view.expand( slug );
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handles search route event
|
|
|
|
themes.router.on( 'route:search', function( query ) {
|
|
|
|
self.themes.doSearch( query );
|
|
|
|
});
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2013-11-13 15:58:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Ready...
|
|
|
|
jQuery( document ).ready(
|
|
|
|
|
|
|
|
// Bring on the themes
|
|
|
|
_.bind( themes.Run.init, themes.Run )
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
);
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2013-11-13 15:58:05 -05:00
|
|
|
})( jQuery );
|
2013-11-22 03:06:09 -05:00
|
|
|
|
|
|
|
// Align theme browser thickbox
|
|
|
|
var tb_position;
|
|
|
|
jQuery(document).ready( function($) {
|
|
|
|
tb_position = function() {
|
|
|
|
var tbWindow = $('#TB_window'),
|
|
|
|
width = $(window).width(),
|
|
|
|
H = $(window).height(),
|
|
|
|
W = ( 1040 < width ) ? 1040 : width,
|
|
|
|
adminbar_height = 0;
|
|
|
|
|
|
|
|
if ( $('body.admin-bar').length ) {
|
|
|
|
adminbar_height = parseInt( jQuery('#wpadminbar').css('height'), 10 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( tbWindow.size() ) {
|
|
|
|
tbWindow.width( W - 50 ).height( H - 45 - adminbar_height );
|
|
|
|
$('#TB_iframeContent').width( W - 50 ).height( H - 75 - adminbar_height );
|
|
|
|
tbWindow.css({'margin-left': '-' + parseInt( ( ( W - 50 ) / 2 ), 10 ) + 'px'});
|
|
|
|
if ( typeof document.body.style.maxWidth !== 'undefined' ) {
|
|
|
|
tbWindow.css({'top': 20 + adminbar_height + 'px', 'margin-top': '0'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$(window).resize(function(){ tb_position(); });
|
|
|
|
});
|