← Back
Editing: themes-tab.js
/* global jQuery, gdThemesTab, ajaxurl, */ jQuery( document ).ready( function( $ ) { var themesTab = { init: function() { var $item = $( '<li></li>' ).prependTo( '.wp-filter > .filter-links' ); $( '<a href="" data-sort="gd-recommended" class="js-gd-recommended"></a>' ).text( gdThemesTab.tab ).appendTo( $item ); $( '.page-title-action' ).attr( 'aria-expanded', true ); }, shiftAddNewTheme: function() { $( '.add-new-theme' ).remove().prependTo( '.theme-browser .themes' ); } }; var coreThemeInstaller = wp.themes.view.Installer.prototype, $body = $( 'body' ); wp.themes.view.Installer = wp.themes.view.Installer.extend( { events: function() { var events = _.extend( {}, coreThemeInstaller.events, { 'click .filter-links li > a.js-gd-recommended': 'gdThemes' } ); delete events['click .filter-links li > a']; events['click .filter-links li > a:not( .js-gd-recommended )'] = 'onSort'; return events; }, browse: function( section ) { if ( 'gd-recommended' === section ) { this.loadThemes(); return; } this.collection.query( { browse: section } ); }, gdThemes: function( event ) { event.preventDefault(); this.clearSearch(); var target = event.target, sort = target.getAttribute( 'data-sort' ); if ( target.classList.contains( this.activeClass ) ) { return; } $body.removeClass( 'filters-applied show-filters' ); $( '.drawer-toggle' ).attr( 'aria-expanded', 'false' ); $body.removeClass( 'show-favorites-form' ); $( '.filter-links li > a, .theme-filter' ).removeClass( this.activeClass ); $( '[data-sort]' ).filter( function() { return $( this ).data( 'sort' ) === sort; } ).addClass( this.activeClass ); this.loadThemes(); wp.themes.router.navigate( wp.themes.router.baseUrl( wp.themes.router.browsePath + sort ) ); }, loadThemes: function() { $body.addClass( 'loading-content' ).removeClass( 'no-results' ); $.post( ajaxurl, { action: 'gd_render_themes' }, function( response ) { if ( ! response.success ) { return; } $body.removeClass( 'loading-content' ); $( '.theme-browser .themes' ).empty().append( response.data ); $( '.theme-count' ).text( $( '.theme-browser .themes > div.theme' ).length ); } ); if ( $( '#gd-theme-install' ).length ) { return; } $( '.theme-browser .themes' ).after( '<div id="gd-theme-install" style="display:none;"></div>' ); }, /** * Overrides wp.themes.view.Appearance.scroller */ scroller: function() { if ( $( '.js-gd-recommended' ).hasClass( 'current' ) ) { return; } coreThemeInstaller.scroller.apply( this, arguments ); } } ); var installTheme = { notice: function( classes, text ) { return $( '<div></div>' ).addClass( classes ).append( $( '<p></p>' ).text( text ) ); }, init: function( button ) { var data = { action: 'gd_install_theme', slug: button.data( 'slug' ), gd_package: button.data( 'package' ), _ajax_nonce: button.data( 'nonce' ) }; var $theme = button.closest( 'div.theme' ); $.post( ajaxurl, data, function( response ) { if ( ! response.success ) { $theme.append( installTheme.notice( 'notice notice-error notice-alt update-message', gdThemesTab.error ) ); button.removeClass( 'updating-message' ); return; } $theme .append( installTheme.notice( 'notice notice-success notice-alt', gdThemesTab.installed ) ) .removeClass( 'focus' ) .find( '.theme-actions > .theme-install' ) .removeClass( 'updating-message' ) .text( gdThemesTab.installed ) .addClass( 'disabled' ) .delay( 1500 ) .replaceWith( [ $( '<a class="button button-primary activate-theme"></a>' ).attr( 'href', response.data.activateUrl ).text( gdThemesTab.activate )[0], $( '<a class="button button-secondary live-preview-theme"></a>' ).attr( 'href', response.data.customizeUrl ).text( gdThemesTab.live_preview )[0] ] ); } ); } }; themesTab.init(); $( window ).on( 'load', themesTab.shiftAddNewTheme ); $( document ).on( 'click', '.theme.godaddy .theme-screenshot, .theme.godaddy .more-details', function() { var url = $( this ).closest( '.theme.godaddy' ).attr( 'data-demo-url' ); window.open( url, '_blank' ); } ); $( document ).on( 'click', '.theme.godaddy .button.theme-install', function( e ) { e.preventDefault(); e.stopPropagation(); $( this ).addClass( 'updating-message' ).parents( 'div.theme' ).addClass( 'focus' ); installTheme.init( $( this ) ); } ); } );
Save File
Cancel