/** * Asset Queue Manager * * Generate a management panel to view assets, dequeue assets and re- * queue them. */ var aqmPanel; jQuery(document).ready(function ($) { // Bail early if we're missing the aqm or aqmData vars. We won't get // very far without them. if ( typeof aqm === undefined || typeof aqmData === undefined ) { return; } aqmPanel = { // Management panel element el : $( '#aqm-panel' ), // Menu element in the admin bar menu_el : $( '#wp-admin-bar-asset-queue-manager' ), // Add the management panel init : function() { // Build the initial assets panel this.el.html( '
' + aqm.strings.deps + ' ' + asset.deps.join( ', ' ) + '
'; } // Add action links html += '' + aqmData.notices[notice].msg + '
'; } } } if ( asset.src === false ) { notices += '' + aqm.strings.no_src + '
'; } return notices; }, // Try to get a good URL for this asset. This is just kind of // guessing, really. getAssetURL : function( asset ) { var url = asset.src.toString(); if ( url.substring( 0, 2 ) === '//' ) { link = 'http:' + asset.src; } else if ( url.substring( 0, 1 ) === '/' ) { url = aqm.siteurl + asset.src; } else if ( url.substring( 0, 4 ) === 'http' ) { url = asset.src; } return url; }, // Open/close the panel toggle : function() { if ( this.menu_el.hasClass( 'open' ) ) { this.el.slideUp(); this.el.removeClass( 'open' ); this.menu_el.removeClass( 'open' ); } else { this.el.addClass( 'open' ); this.menu_el.addClass( 'open' ); this.el.slideDown(); } }, // Open/close an asset panel toggleAsset : function( asset ) { if ( asset.hasClass( 'open' ) ) { asset.removeClass( 'open' ); } else { asset.addClass( 'open' ); } }, // Enable the auto-selection in the source field enableSrcSelect : function( el ) { // Don't bubble up to the open/close asset toggle el.click( function(e) { e.stopPropagation(); }); el.click( function() { this.select(); }); }, // Send an Ajax request to dequeue or re-enqueue an asset toggleQueueState : function( handle, location, type, dequeue ) { var asset = this.el.find( '.asset.handle-' + handle + '.' + type ); asset.find( '.body .notice.request' ).remove(); asset.find( '.body' ).append( '' + aqm.strings.sending + '
' ); var data = $.param({ action: 'aqm-modify-asset', nonce: aqm.nonce, handle: handle, type: type, dequeue: dequeue, asset_data: aqmData.assets[location][type][handle] }); var jqxhr = $.post( aqm.ajaxurl, data, function( r ) { var notice = asset.find( '.notice.request' ); if ( r.success ) { // If we got a successful return but no data, // something's gone wonky. if ( typeof r.data == 'undefined' ) { notice.addClass( 'error' ).text( aqm.strings.unknown_error ); console.log( r ); return; } notice.slideUp( null, function() { $(this).remove(); }); if ( r.data.dequeue ) { asset.fadeOut( null, function() { $(this).remove(); }); aqmPanel.appendAsset( r.data.option[r.data.type][r.data.handle], 'dequeued', r.data.type ); // Add this the array of dequeued assets so // the data can be retrieved if they want to // stop dequeuing it. Ideally we'd also remove // the asset data from the enqueued asset arrays // but this will do for now. if ( aqmData.assets.dequeued === false ) { aqmData.assets.dequeued = []; } if ( typeof aqmData.assets.dequeued[type] === 'undefined' ) { aqmData.assets.dequeued[type] = []; } aqmData.assets.dequeued[type][r.data.handle] = aqmData.assets[location][type][handle]; } else { asset.addClass( 'requeued' ).find( '.body' ).empty().append( '' + aqm.strings.requeued + '
' ); } } else { if ( typeof r.data == 'undefined' || typeof r.data.msg == 'undefined' ) { notice.addClass( 'error' ).text( aqm.strings.unknown_error ); } else { notice.addClass( 'error' ).text( r.data.msg ); } } asset.find( '.links .sending' ).removeClass( 'sending' ); }); } }; aqmPanel.init(); });