/** * Front-end functionality for Query Monitor. * * @package query-monitor */ var QM_i18n = { // http://core.trac.wordpress.org/ticket/20491 number_format : function( number, decimals ) { if ( isNaN( number ) ) { return; } if ( ! decimals ) { decimals = 0; } number = parseFloat( number ); var num_float = number.toFixed( decimals ), num_int = Math.floor( number ), num_str = num_int.toString(), fraction = num_float.substring( num_float.indexOf( '.' ) + 1, num_float.length ), o = ''; if ( num_str.length > 3 ) { for ( i = num_str.length; i > 3; i -= 3 ) { o = qm_number_format.thousands_sep + num_str.slice( i - 3, i ) + o; } o = num_str.slice( 0, i ) + o; } else { o = num_str; } if ( decimals ) { o = o + qm_number_format.decimal_point + fraction; } return o; } }; if ( window.jQuery ) { jQuery( function($) { var toolbarHeight = $('#wpadminbar').length ? $('#wpadminbar').outerHeight() : 0; var minheight = 100; var maxheight = ( $(window).height() - toolbarHeight ); var minwidth = 300; var maxwidth = $(window).width(); var container = $('#query-monitor-main'); var body = $('body'); var body_margin = body.css('margin-bottom'); var container_height_key = 'qm-container-height'; var container_pinned_key = 'qm-' + ( $('body').hasClass('wp-admin') ? 'admin' : 'front' ) + '-container-pinned'; var container_position_key = 'qm-container-position'; var container_width_key = 'qm-container-width'; if ( container.hasClass('qm-peek') ) { minheight = 27; } container.removeClass('qm-no-js').addClass('qm-js'); var theme = localStorage.getItem( 'qm-theme' ); if ( theme ) { container.attr('data-theme', theme); $('.qm-theme-toggle[value="' + theme + '"]').prop('checked', true); } if ( $('#qm-fatal').length ) { console.error(qm_l10n.fatal_error + ': ' + $('#qm-fatal').attr('data-qm-message') ); if ( $('#wp-admin-bar-query-monitor').length ) { $('#wp-admin-bar-query-monitor') .addClass('qm-error') .find('a').eq(0) .text(qm_l10n.fatal_error); var fatal_container = document.createDocumentFragment(); var fatal_message_menu = $('#wp-admin-bar-query-monitor-placeholder') .clone() .attr('id','wp-admin-bar-qm-fatal-message'); fatal_message_menu .find('a').eq(0) .text($('#qm-fatal').attr('data-qm-message')) .attr('href','#qm-fatal'); fatal_container.appendChild( fatal_message_menu.get(0) ); var fatal_file_menu = $('#wp-admin-bar-query-monitor-placeholder') .clone() .attr('id','wp-admin-bar-qm-fatal-file'); fatal_file_menu .find('a').eq(0) .text($('#qm-fatal').attr('data-qm-file') + ':' + $('#qm-fatal').attr('data-qm-line')) .attr('href','#qm-fatal'); fatal_container.appendChild( fatal_file_menu.get(0) ); $('#wp-admin-bar-query-monitor ul').append(fatal_container); } } var link_click = function(e){ var href = $( this ).attr('href') || $( this ).data('qm-href'); if ( '#qm-fatal' === href ) { return; } show_panel( href ); $(href).focus(); $('#wp-admin-bar-query-monitor').removeClass('hover'); e.preventDefault(); }; var stripes = function( table ) { table.each(function() { $(this).find('tbody tr').removeClass('qm-odd').not('[class*="qm-hide-"]').filter(':even').addClass('qm-odd'); } ); }; var show_panel = function( panel ) { container.addClass('qm-show').removeClass('qm-hide'); $( '.qm' ).removeClass('qm-panel-show'); $('#qm-panels').scrollTop(0); $( panel ).addClass('qm-panel-show'); if ( container.height() < minheight ) { container.height( minheight ); } if ( container.hasClass('qm-show-right') ) { body.css( 'margin-bottom', '' ); } else { body.css( 'margin-bottom', 'calc( ' + body_margin + ' + ' + container.height() + 'px )' ); } $('#qm-panel-menu').find('button').removeAttr('aria-selected'); $('#qm-panel-menu').find('li').removeClass('qm-current-menu'); var selected_menu = $('#qm-panel-menu').find('[data-qm-href="' + panel + '"]').attr('aria-selected',true); if ( selected_menu.length ) { var selected_menu_top = selected_menu.position().top - 27; var menu_height = $('#qm-panel-menu').height(); var menu_scroll = $('#qm-panel-menu').scrollTop(); selected_menu.closest('#qm-panel-menu > ul > li').addClass('qm-current-menu'); var selected_menu_off_bottom = ( selected_menu_top > ( menu_height ) ); var selected_menu_off_top = ( selected_menu_top < 0 ); if ( selected_menu_off_bottom || selected_menu_off_top ) { $('#qm-panel-menu').scrollTop( selected_menu_top + menu_scroll - ( menu_height / 2 ) + ( selected_menu.outerHeight() / 2 ) ); } } $('.qm-title-heading select').val(panel); localStorage.setItem( container_pinned_key, panel ); var filters = $( panel ).find('.qm-filter'); if ( filters.length ) { filters.trigger('change'); } else { stripes( $(panel).find('table') ); } }; if ( $('#wp-admin-bar-query-monitor').length ) { var admin_bar_menu_container = document.createDocumentFragment(); if ( window.qm && window.qm.menu ) { $('#wp-admin-bar-query-monitor') .addClass(qm.menu.top.classname) .attr('dir','ltr') .find('a').eq(0) .html(qm.menu.top.title); $.each( qm.menu.sub, function( i, el ) { var new_menu = $('#wp-admin-bar-query-monitor-placeholder') .clone() .attr('id','wp-admin-bar-' + el.id); new_menu .find('a').eq(0) .html(el.title) .attr('href',el.href); if ( ( typeof el.meta != 'undefined' ) && ( typeof el.meta.classname != 'undefined' ) ) { new_menu.addClass(el.meta.classname); } admin_bar_menu_container.appendChild( new_menu.get(0) ); } ); $('#wp-admin-bar-query-monitor ul').append(admin_bar_menu_container); } $('#wp-admin-bar-query-monitor').find('a').on('click',link_click); $('#wp-admin-bar-query-monitor,#wp-admin-bar-query-monitor-default').show(); } else { container.addClass('qm-peek').removeClass('qm-hide'); $('#qm-overview').addClass('qm-panel-show'); } $('#qm-panel-menu').find('button').on('click',link_click); container.find('.qm-filter').on('change',function(e){ var filter = $(this).attr('data-filter'), table = $(this).closest('table'), tr = table.find('tbody tr[data-qm-' + filter + ']'), // Escape the following chars with a backslash before passing into jQ selectors: [ ] ( ) ' " \ val = $(this).val().replace(/[[\]()'"\\]/g, "\\$&"), total = tr.removeClass('qm-hide-' + filter).length, hilite = $(this).attr('data-highlight'), time = 0; key = $(this).attr('id'); if ( val ) { sessionStorage.setItem( key, $(this).val() ); } else { sessionStorage.removeItem( key ); } if ( hilite ) { table.find('tr').removeClass('qm-highlight'); } if ( $(this).val() !== '' ) { if ( hilite ) { tr.filter('[data-qm-' + hilite + '*="' + val + '"]').addClass('qm-highlight'); } tr.not('[data-qm-' + filter + '*="' + val + '"]').addClass('qm-hide-' + filter); $(this).closest('th').addClass('qm-filtered'); } else { $(this).closest('th').removeClass('qm-filtered'); } var matches = tr.filter(':visible'); var filtered_count = 0; var total_count = 0; matches.each(function(i){ var row_time = $(this).attr('data-qm-time'); if ( row_time ) { time += parseFloat( row_time ); } var row_count = $(this).attr('data-qm-count'); if ( row_count ) { filtered_count += parseFloat( row_count ); } else { filtered_count++; } }); if ( time ) { time = QM_i18n.number_format( time, 4 ); } tr.each(function(i){ var row_count = $(this).attr('data-qm-count'); if ( row_count ) { total_count += parseFloat( row_count ); } else { total_count++; } }); if ( table.find('.qm-filtered').length ) { var count = filtered_count + ' / ' + total_count; } else { var count = filtered_count; } table.find('.qm-items-number').text(count); table.find('.qm-items-time').text(time); stripes(table); }); container.find('.qm-filter').each(function () { var key = $(this).attr('id'); var value = sessionStorage.getItem( key ); if ( value !== null ) { // Escape the following chars with a backslash before passing into jQ selectors: [ ] ( ) ' " \ var val = value.replace(/[[\]()'"\\]/g, "\\$&"); if ( ! $(this).find('option[value="' + val + '"]').length ) { $('