neha4no
2/5/2019 - 1:02 PM

collapse flexible layouts and admin meta boxes on click of F10 key


/**
 * Add quick-collapse feature to ACF Flexible Content fields On click of Collapse All link OR F10 key
 */
function vwo_acf_flexible_collapse_all_layout() {
    $screen = get_current_screen();
    if ( is_admin() ) {
        if($screen->id == 'recipes'){
            $style = 'font-size: 12px; cursor: pointer;';    
        } else{
            $style = 'position: absolute; top: 0; right: 0; cursor: pointer;';           
        }
    ?>
        <script type="text/javascript">
            (function($) {
                $(document).ready(function() {
                    var collapseButtonClass = 'collapse-all';
                    var style = '<?php echo $style; ?>';

                    // Add a clickable link to the label line of flexible content fields
                    $('.acf-field-flexible-content > .acf-label')
                        .append('<a class="' + collapseButtonClass + '" style="'+ style +'">Collapse All (F10)</a>');

                    // Simulate a click on each flexible content item's "collapse" button when clicking the new link
                    $('.' + collapseButtonClass).on('click', function() {
                        $('.acf-flexible-content .layout:not(.-collapsed) .acf-fc-layout-controls .-collapse').click();
                    });
                });
                $(document).keydown(function(event){ 
                      var collapseButtonClass = 'collapse-all';
                        var key =  event.which; 
                        if(key == 121 && $('.' + collapseButtonClass).length > 0){
                            $('.' + collapseButtonClass).trigger('click');
                        }
                });
            })(jQuery);
        </script>
    <?php
    }
}
add_action('acf/input/admin_head', 'vwo_acf_flexible_collapse_all_layout');

/***
 * Collapse all admin meta boxes which appears below page editor on page load and on F10 Key. 
 */
function vwo_admin_meta_boxes_collapse() {
    if ( is_admin() ) {
    ?>
	<script type='text/javascript'>
	(function ($) {
            $(document).ready(function(){				
                $('#postbox-container-2 .postbox').addClass('closed');	
            });
            $(document).keydown(function(event){ 
                        var key =  event.which; 
                        if(key == 121){
                            $('#postbox-container-2 .postbox').addClass('closed');
                        }
            });
	})(jQuery);
	</script>
    <?php 
	}   
}

add_action( 'admin_notices', 'vwo_admin_meta_boxes_collapse' );