1 /* global _wpThemeSettings, confirm */
2 window.wp = window.wp || {};
6 // Set up our namespace...
8 themes = wp.themes = wp.themes || {};
10 // Store the theme data and settings for organized and quick access
11 // themes.data.settings, themes.data.themes, themes.data.l10n
12 themes.data = _wpThemeSettings;
13 l10n = themes.data.l10n;
15 // Setup app structure
16 _.extend( themes, { model: {}, view: {}, routes: {}, router: {}, template: wp.template });
18 themes.model = Backbone.Model.extend({});
20 // Main view controller for themes.php
21 // Unifies and renders all available views
22 themes.view.Appearance = wp.Backbone.View.extend({
24 el: '#wpbody-content .wrap .theme-browser',
27 // Pagination instance
30 // Sets up a throttler for binding to 'scroll'
31 initialize: function() {
32 // Scroller checks how far the scroll position is
33 _.bindAll( this, 'scroller' );
35 // Bind to the scroll event and throttle
36 // the results from this.scroller
37 this.window.bind( 'scroll', _.throttle( this.scroller, 300 ) );
40 // Main render control
42 // Setup the main theme view
43 // with the current theme collection
44 this.view = new themes.view.Themes({
45 collection: this.collection,
49 // Render search form.
54 this.$el.empty().append( this.view.el ).addClass('rendered');
55 this.$el.append( '<br class="clear"/>' );
58 // Search input and view
59 // for current theme collection
64 // Don't render the search if there is only one theme
65 if ( themes.data.themes.length === 1 ) {
69 view = new themes.view.Search({ collection: self.collection });
71 // Render and append after screen title
74 .append( $.parseHTML( '<label class="screen-reader-text" for="theme-search-input">' + l10n.search + '</label>' ) )
78 // Checks when the user gets close to the bottom
79 // of the mage and triggers a theme:scroll event
80 scroller: function() {
84 bottom = this.window.scrollTop() + self.window.height();
85 threshold = self.$el.offset().top + self.$el.outerHeight( false ) - self.window.height();
86 threshold = Math.round( threshold * 0.9 );
88 if ( bottom > threshold ) {
89 this.trigger( 'theme:scroll' );
94 // Set up the Collection for our theme data
95 // @has 'id' 'name' 'screenshot' 'author' 'authorURI' 'version' 'active' ...
96 themes.Collection = Backbone.Collection.extend({
103 // Controls searching on the current theme collection
104 // and triggers an update event
105 doSearch: function( value ) {
107 // Don't do anything if we've already done this search
108 // Useful because the Search handler fires multiple times per keystroke
109 if ( this.terms === value ) {
113 // Updates terms with the value passed
116 // If we have terms, run a search...
117 if ( this.terms.length > 0 ) {
118 this.search( this.terms );
121 // If search is blank, show all themes
122 // Useful for resetting the views when you clean the input
123 if ( this.terms === '' ) {
124 this.reset( themes.data.themes );
127 // Trigger an 'update' event
128 this.trigger( 'update' );
131 // Performs a search within the collection
133 search: function( term ) {
134 var match, results, haystack;
136 // Start with a full collection
137 this.reset( themes.data.themes, { silent: true } );
139 // The RegExp object to match
141 // Consider spaces as word delimiters and match the whole string
142 // so matching terms can be combined
143 term = term.replace( ' ', ')(?=.*' );
144 match = new RegExp( '^(?=.*' + term + ').+', 'i' );
147 // _.filter and .test
148 results = this.filter( function( data ) {
149 haystack = _.union( data.get( 'name' ), data.get( 'id' ), data.get( 'description' ), data.get( 'author' ), data.get( 'tags' ) );
151 if ( match.test( data.get( 'author' ) ) && term.length > 2 ) {
152 data.set( 'displayAuthor', true );
155 return match.test( haystack );
158 this.reset( results );
161 // Paginates the collection with a helper method
162 // that slices the collection
163 paginate: function( instance ) {
164 var collection = this;
165 instance = instance || 0;
167 // Themes per instance are set at 15
168 collection = _( collection.rest( 15 * instance ) );
169 collection = _( collection.first( 15 ) );
175 // This is the view that controls each theme item
176 // that will be displayed on the screen
177 themes.view.Theme = wp.Backbone.View.extend({
179 // Wrap theme data on a div.theme element
182 // Reflects which theme view we have
183 // 'grid' (default) or 'detail'
186 // The HTML template for each element to be rendered
187 html: themes.template( 'theme' ),
192 'touchend': 'expand',
193 'touchmove': 'preventExpand'
199 var data = this.model.toJSON();
200 // Render themes using the html template
201 this.$el.html( this.html( data ) ).attr({
203 'aria-describedby' : data.id + '-action ' + data.id + '-name'
206 // Renders active theme styles
209 if ( this.model.get( 'displayAuthor' ) ) {
210 this.$el.addClass( 'display-author' );
214 // Adds a class to the currently active theme
215 // and to the overlay in detailed view mode
216 activeTheme: function() {
217 if ( this.model.get( 'active' ) ) {
218 this.$el.addClass( 'active' );
222 // Single theme overlay screen
223 // It's shown when clicking a theme
224 expand: function( event ) {
227 event = event || window.event;
229 // 'enter' and 'space' keys expand the details view when a theme is :focused
230 if ( event.type === 'keydown' && ( event.which !== 13 && event.which !== 32 ) ) {
234 // Bail if the user scrolled on a touch device
235 if ( this.touchDrag === true ) {
236 return this.touchDrag = false;
239 // Prevent the modal from showing when the user clicks
240 // one of the direct action buttons
241 if ( $( event.target ).is( '.theme-actions a' ) ) {
245 // Set focused theme to current element
246 themes.focusedTheme = this.$el;
248 this.trigger( 'theme:expand', self.model.cid );
251 preventExpand: function() {
252 this.touchDrag = true;
256 // Theme Details view
257 // Set ups a modal overlay with the expanded theme data
258 themes.view.Details = wp.Backbone.View.extend({
260 // Wrap theme data on a div.theme element
261 className: 'theme-overlay',
265 'click .delete-theme': 'deleteTheme',
266 'click .left': 'previousTheme',
267 'click .right': 'nextTheme'
270 // The HTML template for the theme overlay
271 html: themes.template( 'theme-single' ),
274 var data = this.model.toJSON();
275 this.$el.html( this.html( data ) );
276 // Renders active theme styles
278 // Set up navigation events
280 // Checks screenshot size
281 this.screenshotCheck( this.$el );
282 // Contain "tabbing" inside the overlay
283 this.containFocus( this.$el );
286 // Adds a class to the currently active theme
287 // and to the overlay in detailed view mode
288 activeTheme: function() {
289 // Check the model has the active property
290 this.$el.toggleClass( 'active', this.model.get( 'active' ) );
293 // Keeps :focus within the theme details elements
294 containFocus: function( $el ) {
297 // Move focus to the primary action
298 _.delay( function() {
299 $( '.theme-wrap a.button-primary:visible' ).focus();
302 $el.on( 'keydown.wp-themes', function( event ) {
305 if ( event.which === 9 ) {
306 $target = $( event.target );
308 // Keep focus within the overlay by making the last link on theme actions
309 // switch focus to button.left on tabbing and vice versa
310 if ( $target.is( 'button.left' ) && event.shiftKey ) {
311 $el.find( '.theme-actions a:last-child' ).focus();
312 event.preventDefault();
313 } else if ( $target.is( '.theme-actions a:last-child' ) ) {
314 $el.find( 'button.left' ).focus();
315 event.preventDefault();
321 // Single theme overlay screen
322 // It's shown when clicking a theme
323 collapse: function( event ) {
327 event = event || window.event;
329 // Prevent collapsing detailed view when there is only one theme available
330 if ( themes.data.themes.length === 1 ) {
334 // Detect if the click is inside the overlay
335 // and don't close it unless the target was
336 // the div.back button
337 if ( $( event.target ).is( '.theme-backdrop' ) || $( event.target ).is( '.close' ) || event.keyCode === 27 ) {
339 // Add a temporary closing class while overlay fades out
340 $( 'body' ).addClass( 'closing-overlay' );
342 // With a quick fade out animation
343 this.$el.fadeOut( 130, function() {
344 // Clicking outside the modal box closes the overlay
345 $( 'body' ).removeClass( 'theme-overlay-open closing-overlay' );
346 // Handle event cleanup
349 // Get scroll position to avoid jumping to the top
350 scroll = document.body.scrollTop;
352 // Clean the url structure
353 themes.router.navigate( themes.router.baseUrl( '' ), { replace: true } );
355 // Restore scroll position
356 document.body.scrollTop = scroll;
358 // Return focus to the theme div
359 if ( themes.focusedTheme ) {
360 themes.focusedTheme.focus();
366 // Handles .disabled classes for next/previous buttons
367 navigation: function() {
369 // Disable Left/Right when at the start or end of the collection
370 if ( this.model.cid === this.model.collection.at(0).cid ) {
371 this.$el.find( '.left' ).addClass( 'disabled' );
373 if ( this.model.cid === this.model.collection.at( this.model.collection.length - 1 ).cid ) {
374 this.$el.find( '.right' ).addClass( 'disabled' );
378 // Performs the actions to effectively close
379 // the theme details overlay
380 closeOverlay: function() {
383 this.trigger( 'theme:collapse' );
386 // Confirmation dialoge for deleting a theme
387 deleteTheme: function() {
388 return confirm( themes.data.settings.confirmDelete );
391 nextTheme: function() {
393 self.trigger( 'theme:next', self.model.cid );
396 previousTheme: function() {
398 self.trigger( 'theme:previous', self.model.cid );
401 // Checks if the theme screenshot is the old 300px width version
402 // and adds a corresponding class if it's true
403 screenshotCheck: function( el ) {
404 var screenshot, image;
406 screenshot = el.find( '.screenshot img' );
408 image.src = screenshot.attr( 'src' );
411 if ( image.width && image.width <= 300 ) {
412 el.addClass( 'small-screenshot' );
417 // Controls the rendering of div.themes,
418 // a wrapper that will hold all the theme elements
419 themes.view.Themes = wp.Backbone.View.extend({
422 $overlay: $( 'div.theme-overlay' ),
424 // Number to keep track of scroll position
425 // while in theme-overlay mode
428 // The theme count element
429 count: $( '.theme-count' ),
431 initialize: function( options ) {
435 this.parent = options.parent;
437 // Set current view to [grid]
438 this.setView( 'grid' );
440 // Move the active theme to the beginning of the collection
443 // When the collection is updated by user input...
444 this.listenTo( self.collection, 'update', function() {
445 self.parent.page = 0;
450 this.listenTo( this.parent, 'theme:scroll', function() {
451 self.renderThemes( self.parent.page );
454 // Bind keyboard events.
455 $('body').on( 'keyup', function( event ) {
456 if ( ! self.overlay ) {
460 // Pressing the right arrow key fires a theme:next event
461 if ( event.keyCode === 39 ) {
462 self.overlay.nextTheme();
465 // Pressing the left arrow key fires a theme:previous event
466 if ( event.keyCode === 37 ) {
467 self.overlay.previousTheme();
470 // Pressing the escape key fires a theme:collapse event
471 if ( event.keyCode === 27 ) {
472 self.overlay.collapse( event );
477 // Manages rendering of theme pages
478 // and keeping theme count in sync
480 // Clear the DOM, please
483 // If the user doesn't have switch capabilities
484 // or there is only one theme in the collection
485 // render the detailed view of the active theme
486 if ( themes.data.themes.length === 1 ) {
488 // Constructs the view
489 this.singleTheme = new themes.view.Details({
490 model: this.collection.models[0]
493 // Render and apply a 'single-theme' class to our container
494 this.singleTheme.render();
495 this.$el.addClass( 'single-theme' );
496 this.$el.append( this.singleTheme.el );
499 // Generate the themes
500 // Using page instance
501 this.renderThemes( this.parent.page );
503 // Display a live theme count for the collection
504 this.count.text( this.collection.length );
507 // Iterates through each instance of the collection
508 // and renders each theme module
509 renderThemes: function( page ) {
512 self.instance = self.collection.paginate( page );
514 // If we have no more themes bail
515 if ( self.instance.length === 0 ) {
519 // Make sure the add-new stays at the end
521 $( '.add-new-theme' ).remove();
524 // Loop through the themes and setup each theme view
525 self.instance.each( function( theme ) {
526 self.theme = new themes.view.Theme({
530 // Render the views...
532 // and append them to div.themes
533 self.$el.append( self.theme.el );
535 // Binds to theme:expand to show the modal box
536 // with the theme details
537 self.listenTo( self.theme, 'theme:expand', self.expand, self );
540 // 'Add new theme' element shown at the end of the grid
541 if ( themes.data.settings.canInstall ) {
542 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>' );
548 // Grabs current theme and puts it at the beginning of the collection
549 currentTheme: function() {
553 current = self.collection.findWhere({ active: true });
555 // Move the active theme to the beginning of the collection
557 self.collection.remove( current );
558 self.collection.add( current, { at:0 } );
563 setView: function( view ) {
567 // Renders the overlay with the ThemeDetails view
568 // Uses the current model data
569 expand: function( id ) {
572 // Set the current theme model
573 this.model = self.collection.get( id );
575 // Trigger a route update for the current model
576 themes.router.navigate( themes.router.baseUrl( '?theme=' + this.model.id ), { replace: true } );
578 // Sets this.view to 'detail'
579 this.setView( 'detail' );
580 $( 'body' ).addClass( 'theme-overlay-open' );
582 // Set up the theme details view
583 this.overlay = new themes.view.Details({
587 this.overlay.render();
588 this.$overlay.html( this.overlay.el );
590 // Bind to theme:next and theme:previous
591 // triggered by the arrow keys
593 // Keep track of the current model so we
594 // can infer an index position
595 this.listenTo( this.overlay, 'theme:next', function() {
596 // Renders the next theme on the overlay
597 self.next( [ self.model.cid ] );
600 .listenTo( this.overlay, 'theme:previous', function() {
601 // Renders the previous theme on the overlay
602 self.previous( [ self.model.cid ] );
606 // This method renders the next theme on the overlay modal
607 // based on the current position in the collection
608 // @params [model cid]
609 next: function( args ) {
613 // Get the current theme
614 model = self.collection.get( args[0] );
615 // Find the next model within the collection
616 nextModel = self.collection.at( self.collection.indexOf( model ) + 1 );
618 // Sanity check which also serves as a boundary test
619 if ( nextModel !== undefined ) {
621 // We have a new theme...
623 this.overlay.closeOverlay();
625 // Trigger a route update for the current model
626 self.theme.trigger( 'theme:expand', nextModel.cid );
631 // This method renders the previous theme on the overlay modal
632 // based on the current position in the collection
633 // @params [model cid]
634 previous: function( args ) {
636 model, previousModel;
638 // Get the current theme
639 model = self.collection.get( args[0] );
640 // Find the previous model within the collection
641 previousModel = self.collection.at( self.collection.indexOf( model ) - 1 );
643 if ( previousModel !== undefined ) {
645 // We have a new theme...
647 this.overlay.closeOverlay();
649 // Trigger a route update for the current model
650 self.theme.trigger( 'theme:expand', previousModel.cid );
656 // Search input view controller.
657 themes.view.Search = wp.Backbone.View.extend({
660 className: 'theme-search',
661 id: 'theme-search-input',
664 placeholder: l10n.searchPlaceholder,
675 // Runs a search on the theme collection.
676 search: function( event ) {
678 if ( event.type === 'keyup' && event.which === 27 ) {
679 event.target.value = '';
682 this.collection.doSearch( event.target.value );
684 // Update the URL hash
685 if ( event.target.value ) {
686 themes.router.navigate( themes.router.baseUrl( '?search=' + event.target.value ), { replace: true } );
688 themes.router.navigate( themes.router.baseUrl( '' ), { replace: true } );
693 // Sets up the routes events for relevant url queries
694 // Listens to [theme] and [search] params
695 themes.routes = Backbone.Router.extend({
697 initialize: function() {
698 this.routes = _.object([
702 baseUrl: function( url ) {
703 return themes.data.settings.root + url;
707 // Execute and setup the application
710 // Initializes the blog's theme library view
711 // Create a new collection with data
712 this.themes = new themes.Collection( themes.data.themes );
715 this.view = new themes.view.Appearance({
716 collection: this.themes
727 // Set the initial theme
728 if ( 'undefined' !== typeof themes.data.settings.theme && '' !== themes.data.settings.theme ){
729 this.view.view.theme.trigger( 'theme:expand', this.view.collection.findWhere( { id: themes.data.settings.theme } ) );
732 // Set the initial search
733 if ( 'undefined' !== typeof themes.data.settings.search && '' !== themes.data.settings.search ){
734 $( '.theme-search' ).val( themes.data.settings.search );
735 this.themes.doSearch( themes.data.settings.search );
738 // Start the router if browser supports History API
739 if ( window.history && window.history.pushState ) {
740 // Calls the routes functionality
741 Backbone.history.start({ pushState: true, silent: true });
746 // Bind to our global thx object
747 // so that the object is available to sub-views
748 themes.router = new themes.routes();
753 jQuery( document ).ready(
755 // Bring on the themes
756 _.bind( themes.Run.init, themes.Run )
762 // Align theme browser thickbox
764 jQuery(document).ready( function($) {
765 tb_position = function() {
766 var tbWindow = $('#TB_window'),
767 width = $(window).width(),
768 H = $(window).height(),
769 W = ( 1040 < width ) ? 1040 : width,
772 if ( $('body.admin-bar').length ) {
773 adminbar_height = parseInt( jQuery('#wpadminbar').css('height'), 10 );
776 if ( tbWindow.size() ) {
777 tbWindow.width( W - 50 ).height( H - 45 - adminbar_height );
778 $('#TB_iframeContent').width( W - 50 ).height( H - 75 - adminbar_height );
779 tbWindow.css({'margin-left': '-' + parseInt( ( ( W - 50 ) / 2 ), 10 ) + 'px'});
780 if ( typeof document.body.style.maxWidth !== 'undefined' ) {
781 tbWindow.css({'top': 20 + adminbar_height + 'px', 'margin-top': '0'});
786 $(window).resize(function(){ tb_position(); });