1
2/**
3 * Sets up the behaviour of the meta box
4 *
5 * @author Andreas Gohr <gohr@cosmocode.de>
6 * @author Jana Deutschlaender <deutschlaender@cosmocode.de>
7 */
8(function($) {
9
10
11    /**
12     * Register the click handler for the tabs
13     *
14     * Tabs can be added dynamically later on and this handler will still
15     * provide the open/close functionality
16     */
17    var registerClickForTabsInMetaBox = function($metaBox) {
18
19            $metaBox.on('click', '.meta-tabs a', function (e) {
20                e.preventDefault();
21                var $tab = $(this),
22                    isopen = $tab.attr('aria-expanded') === 'true';
23
24                // disable all existing tabs
25                disableExistingTabs($metaBox);
26
27
28                if (isopen) return; // tab was open, we closed it. we're done
29
30                // enable current tab
31                $tab
32                    .attr('aria-expanded', 'true')
33                    .closest('li')
34                    .addClass('active');
35                $metaBox.find($tab.attr('href'))
36                    .addClass('active')
37                    .attr('aria-hidden', 'false');
38
39            }).find('.meta-content').on('click', 'a[href*="#"]', function (e) {
40                disableExistingTabs($metaBox);
41                /* uses custome event handler hide see spc.js */
42            }).find('#tagging__edit').on('hide', function(e){
43                disableExistingTabs($metaBox);
44            });
45
46            /**
47             * in admin templates show toc tab, if available
48             */
49            if($('body').hasClass('do-admin')) {
50                var $tocLink = $metaBox.find('a[href="#spr__tab-toc"]');
51                if($tocLink.length === 1) {
52                    $tocLink.trigger('click');
53                }
54            }
55        },
56        disableExistingTabs = function($metaBox) {
57            $metaBox.find('.meta-tabs li')
58                .removeClass('active')
59                .find('a')
60                .attr('aria-expanded', 'false');
61            $metaBox.find('.meta-content .tab-pane')
62                .removeClass('active')
63                .attr('aria-hidden', 'false');
64        };
65
66
67    var stickyBox = function ($metaBox, topOffset, leftOffset) {
68        if (window.pageYOffset >= topOffset) {
69            $metaBox.addClass("sticky").attr("style", "left: " + leftOffset + "px");
70        } else {
71            $metaBox.removeClass("sticky").removeAttr("style");
72        }
73    };
74
75
76    $(function(){
77        var $metaBox = $('#spr__meta-box');
78        if (!$metaBox.length) return;
79
80        registerClickForTabsInMetaBox($metaBox);
81
82        var topOffset = $metaBox.offset().top;
83        window.onscroll = function () {
84            // check while scrolling, or window resizing will break horizontal positioning
85            var leftOffset = $metaBox.offset().left;
86            stickyBox($metaBox, topOffset, leftOffset)
87        };
88    });
89
90
91})(jQuery);
92