|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +jQuery(function($){ |
| 4 | + // metabox tab navigation |
| 5 | + var $active, |
| 6 | + $content, |
| 7 | + $tabNav = $('.nav-tab-wrapper'), |
| 8 | + $links = $tabNav.find('a'), |
| 9 | + singleTabHeight = $('.nav-tab').outerHeight(); |
| 10 | + |
| 11 | + $active = $($links.filter(':visible')[0]).addClass('nav-tab-active'); |
| 12 | + |
| 13 | + $content = $( $($active[0]).attr('href') ); |
| 14 | + |
| 15 | + // hide all metaboxes that are hidden in nav-tab as well |
| 16 | + $links.not($active).each(function() { |
| 17 | + // use specific class instead of show/hide so metaboxes don't appear during checkbox toggle in "Screen Options" |
| 18 | + $(this.hash).addClass('cmb2-exp-hidden'); |
| 19 | + }); |
| 20 | + |
| 21 | + // screen options metabox functionality |
| 22 | + $('.metabox-prefs input[type="checkbox"]').each(function(){ |
| 23 | + var $this = $(this), |
| 24 | + $tab = '#'+$this.val(); |
| 25 | + |
| 26 | + $this.on('click', function(e){ |
| 27 | + //on click, toggle appearance in .nav-tab-wrapper |
| 28 | + $tabNav.find('[href="'+ $tab +'"]').toggleClass('hidden'); |
| 29 | + responsiveTabs(); |
| 30 | + }); |
| 31 | + |
| 32 | + }); |
| 33 | + |
| 34 | + // click actions for tab nav links |
| 35 | + $tabNav.on('click', 'a', function(e){ |
| 36 | + $active.removeClass('nav-tab-active'); |
| 37 | + $content.addClass('cmb2-exp-hidden'); |
| 38 | + |
| 39 | + $active = $(this); |
| 40 | + $content = $(this.hash); |
| 41 | + |
| 42 | + $active.addClass('nav-tab-active'); |
| 43 | + $content.removeClass('closed cmb2-exp-hidden'); |
| 44 | + |
| 45 | + e.preventDefault(); |
| 46 | + //return false; |
| 47 | + }); |
| 48 | + |
| 49 | + //check if CMB2 admin-tabs are breaking into rows |
| 50 | + function responsiveTabs(){ |
| 51 | + var tabsDisplay = $tabNav.css('display'), |
| 52 | + tabsParentWidth = $tabNav.parent().width(), |
| 53 | + $sidebar_width = $('#side-sortables').css('width'), |
| 54 | + $metaBoxes = $(); |
| 55 | + |
| 56 | + // grab metaboxes based on class in the .nav-tab-wrapper |
| 57 | + $links.not('.hidden').each(function(){ |
| 58 | + var $div = $($(this).attr('href')); |
| 59 | + $metaBoxes = $metaBoxes.add( $div ); |
| 60 | + }); |
| 61 | + |
| 62 | + // collapse sidebar metaboxes only when in mobile view (when post content width == sidebar width) |
| 63 | + var contentWidth = $('#post-body-content').width(), |
| 64 | + sideBarWidth = $('#postbox-container-1').width(), |
| 65 | + $sideMbxs = $('#side-sortables .postbox'); |
| 66 | + |
| 67 | + if( contentWidth == sideBarWidth ){ |
| 68 | + $sideMbxs.addClass('alt-meta-display closed'); |
| 69 | + }else{ |
| 70 | + $sideMbxs.removeClass('alt-meta-display closed'); |
| 71 | + } |
| 72 | + |
| 73 | + // set width of .nav-tab-wrapper to its parent's width, THEN grab the height (flor fluid collapse of nav tabs) |
| 74 | + $tabNav.outerWidth(tabsParentWidth); |
| 75 | + var tabsHeight = $tabNav.height(); |
| 76 | + |
| 77 | + if(tabsDisplay == 'block' && tabsHeight > singleTabHeight){ |
| 78 | + $metaBoxes |
| 79 | + .addClass('alt-meta-display closed') |
| 80 | + .on('click', function(){ |
| 81 | + $metaBoxes.not($(this)).addClass('closed'); |
| 82 | + }); |
| 83 | + $tabNav.addClass('alt-tabs-display'); |
| 84 | + }else if(tabsHeight <= singleTabHeight){ |
| 85 | + $tabNav.removeClass('alt-tabs-display'); |
| 86 | + $metaBoxes.removeClass('alt-meta-display closed'); |
| 87 | + } |
| 88 | + } |
| 89 | + responsiveTabs(); |
| 90 | + $(window).on('resize', responsiveTabs); |
| 91 | + |
| 92 | +}); |
| 93 | + |
| 94 | + |
| 95 | +(function($) { |
| 96 | + // Override the WP heartbeat ajax-admin function that saves the hidden metaboxes, |
| 97 | + // and instead use the unchecked boxes in "Screen Options" |
| 98 | + // Original code in file /wp-admin/js/postbox.js |
| 99 | + if( 'undefined' !== typeof postboxes ) { |
| 100 | + postboxes.save_state = function( page ) { |
| 101 | + var closed = $('.postbox').filter('.closed').map(function() { return this.id; }).get().join(','), |
| 102 | + hidden = $('.hide-postbox-tog').not(':checked').map(function() { return this.value; }).get().join(','); |
| 103 | + |
| 104 | + $.post(ajaxurl, { |
| 105 | + action: 'closed-postboxes', |
| 106 | + closed: closed, |
| 107 | + hidden: hidden, |
| 108 | + closedpostboxesnonce: jQuery('#closedpostboxesnonce').val(), |
| 109 | + page: page |
| 110 | + }); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | +}(jQuery)); |
0 commit comments