xref: /template/sprintdoc/js/meta-box.js (revision c1ed1dae53eb95ae0decd30f39704799ec96f104)
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    $(function(){
68        var $metaBox = $('#spr__meta-box');
69        if (!$metaBox.length) return;
70
71        registerClickForTabsInMetaBox($metaBox);
72    });
73
74
75})(jQuery);
76