X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/42aebe6945a3a60c8f73853bea2c8b202d64a20b..256a3b381f63716209b3527d0a14442ae570c283:/wp-admin/js/theme.js diff --git a/wp-admin/js/theme.js b/wp-admin/js/theme.js index 0899ab72..ac5e51d9 100644 --- a/wp-admin/js/theme.js +++ b/wp-admin/js/theme.js @@ -1,279 +1,738 @@ -/** - * Theme Browsing - * - * Controls visibility of theme details on manage and install themes pages. - */ -jQuery( function($) { - $('#availablethemes').on( 'click', '.theme-detail', function (event) { - var theme = $(this).closest('.available-theme'), - details = theme.find('.themedetaildiv'); - - if ( ! details.length ) { - details = theme.find('.install-theme-info .theme-details'); - details = details.clone().addClass('themedetaildiv').appendTo( theme ).hide(); +/* global _wpThemeSettings, confirm */ +window.wp = window.wp || {}; + +( function($) { + +// Set up our namespace... +var themes, l10n; +themes = wp.themes = wp.themes || {}; + +// Store the theme data and settings for organized and quick access +// themes.data.settings, themes.data.themes, themes.data.l10n +themes.data = _wpThemeSettings; +l10n = themes.data.l10n; + +// 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({ + + el: '#wpbody-content .wrap .theme-browser', + + window: $( window ), + // Pagination instance + page: 0, + + // Sets up a throttler for binding to 'scroll' + initialize: function() { + // Scroller checks how far the scroll position is + _.bindAll( this, 'scroller' ); + + // Bind to the scroll event and throttle + // the results from this.scroller + this.window.bind( 'scroll', _.throttle( this.scroller, 300 ) ); + }, + + // 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 + }); + + // Render search form. + this.search(); + + // Render and append + this.view.render(); + this.$el.empty().append( this.view.el ).addClass('rendered'); + this.$el.append( '
' ); + }, + + // Search input and view + // for current theme collection + search: function() { + var view, + self = this; + + // Don't render the search if there is only one theme + if ( themes.data.themes.length === 1 ) { + return; } - details.toggle(); - event.preventDefault(); - }); + view = new themes.view.Search({ collection: self.collection }); + + // Render and append after screen title + view.render(); + $('#wpbody h2:first') + .append( $.parseHTML( '' ) ) + .append( view.el ); + }, + + // 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' ); + } + } }); -/** - * Theme Browser Thickbox - * - * Aligns 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; +// Set up the Collection for our theme data +// @has 'id' 'name' 'screenshot' 'author' 'authorURI' 'version' 'active' ... +themes.Collection = Backbone.Collection.extend({ - if ( $('body.admin-bar').length ) - adminbar_height = 28; + model: themes.model, - 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'}); - }; - }; + // Search terms + terms: '', - $(window).resize(function(){ tb_position(); }); + // Controls searching on the current theme collection + // and triggers an update event + doSearch: function( value ) { + + // 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; + } + + // 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' ); + }, + + // Performs a search within the collection + // @uses RegExp + search: function( term ) { + var match, results, haystack; + + // Start with a full collection + this.reset( themes.data.themes, { silent: true } ); + + // 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 + results = this.filter( function( data ) { + haystack = _.union( data.get( 'name' ), data.get( 'description' ), data.get( 'author' ), data.get( 'tags' ) ); + + if ( match.test( data.get( 'author' ) ) && term.length > 2 ) { + data.set( 'displayAuthor', true ); + } + + return match.test( haystack ); + }); + + this.reset( results ); + }, + + // 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; + } }); -/** - * Theme Install - * - * Displays theme previews on theme install pages. - */ -jQuery( function($) { - if( ! window.postMessage ) - return; - - var preview = $('#theme-installer'), - info = preview.find('.install-theme-info'), - panel = preview.find('.wp-full-overlay-main'), - body = $( document.body ); - - preview.on( 'click', '.close-full-overlay', function( event ) { - preview.fadeOut( 200, function() { - panel.empty(); - body.removeClass('theme-installer-active full-overlay-active'); +// 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', + 'touchend': 'expand', + 'touchmove': 'preventExpand' + }, + + touchDrag: false, + + 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' ); + } + }, + + // Single theme overlay screen + // It's shown when clicking a theme + expand: function( event ) { + var self = this; + + // Bail if the user scrolled on a touch device + if ( this.touchDrag === true ) { + return this.touchDrag = false; + } + + event = event || window.event; + + // Prevent the modal from showing when the user clicks + // one of the direct action buttons + if ( $( event.target ).is( '.theme-actions a' ) ) { + return; + } + + this.trigger( 'theme:expand', self.model.cid ); + }, + + preventExpand: function() { + this.touchDrag = true; + } +}); + +// 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 + className: 'theme-overlay', + + 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(); + // Checks screenshot size + this.screenshotCheck( this.$el ); + }, + + // 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 + this.$el.toggleClass( 'active', this.model.get( 'active' ) ); + }, + + // Single theme overlay screen + // It's shown when clicking a theme + collapse: function( event ) { + var self = this, + scroll; + + event = event || window.event; + + // Prevent collapsing detailed view when there is only one theme available + if ( themes.data.themes.length === 1 ) { + return; + } + + // Detect if the click is inside the overlay + // and don't close it unless the target was + // the div.back button + if ( $( event.target ).is( '.theme-backdrop' ) || $( event.target ).is( 'div.close' ) || event.keyCode === 27 ) { + + // 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 + $( 'body' ).removeClass( 'theme-overlay-open closing-overlay' ); + // 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( themes.router.baseUrl( '' ), { replace: true } ); + + // Restore scroll position + document.body.scrollTop = scroll; + }); + } + }, + + // Handles .disabled classes for next/previous buttons + navigation: function() { + + // 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' ); + } + }, + + // 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 ); + }, + + // 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; + + screenshot = el.find( '.screenshot img' ); + image = new Image(); + image.src = screenshot.attr( 'src' ); + + // Width check + if ( image.width && image.width <= 300 ) { + el.addClass( 'small-screenshot' ); + } + } +}); + +// Controls the rendering of div.themes, +// a wrapper that will hold all the theme elements +themes.view.Themes = wp.Backbone.View.extend({ + + className: 'themes', + $overlay: $( 'div.theme-overlay' ), + + // Number to keep track of scroll position + // while in theme-overlay mode + index: 0, + + // The theme count element + count: $( '.theme-count' ), + + 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 ); }); - event.preventDefault(); - }); - - preview.on( 'click', '.collapse-sidebar', function( event ) { - preview.toggleClass( 'collapsed' ).toggleClass( 'expanded' ); - event.preventDefault(); - }); - - $('#availablethemes').on( 'click', '.install-theme-preview', function( event ) { - var src; - - info.html( $(this).closest('.installable-theme').find('.install-theme-info').html() ); - src = info.find( '.theme-preview-url' ).val(); - panel.html( '