simplify meta box tab handling RES-799

This simplifies the tab handling by attaching one click handler to
handle the open/close mechanism for all tabs. It should now allow to
dynamically add tabs even after the fact and the handler should still
work.

Support for the actual tab contents has been removed for now. This
should most probably be readded via PHP anyway.
This commit is contained in:
Andreas Gohr 2017-02-15 15:50:11 +01:00
commit c93c93a805

View file

@ -1,81 +1,38 @@
(function ($, spc) {
jQuery(function () {
const $metaBox = jQuery('#spr__meta-box');
if (!$metaBox.length) return;
var toggleTabs = function () {
var $metaBox = $('#spr__meta-box'),
$tabLinks = $metaBox.find('.meta-tabs').find('a'),
$tabPanels = $metaBox.find('.meta-content').find('.tab-pane');
try {
$tabLinks.each(function () {
$(this).on("click", function (e) {
/**
* Register the click handler for the tabs
*
* Tabs can be added dynamically later on and this handler will still
* provide the open/close functionality
*/
$metaBox.on('click', '.meta-tabs a', function (e) {
e.preventDefault();
var $link = $(this),
$li = $link.closest('li'),
$panel = $($link.attr('href'));
const $tab = jQuery(this);
/* close panel */
if($li.hasClass('active')){
//reset
resetTabs($tabLinks,$tabPanels);
// disable all existing tabs
$metaBox.find('.meta-tabs li')
.removeClass('active')
.find('a')
.attr('aria-expanded', false);
$metaBox.find('.meta-content .tab-pane')
.removeClass('active')
.attr('aria-hidden', false);
/* close panel */
}else{
//reset
resetTabs($tabLinks,$tabPanels);
//current state
$li.addClass('active');
$link.attr('aria-expanded','true');
$panel.addClass('active').attr('aria-hidden','false');
}
// enable current tab
$tab
.attr('aria-expanded', true)
.closest('li')
.addClass('active');
$metaBox.find($tab.attr('href'))
.addClass('active')
.attr('aria-hidden', false);
});
});
} catch (err) {
//alert('err');
}
},
resetTabs = function($tabLinks,$tabPanels){
$tabLinks.closest('li').removeClass('active');
$tabLinks.attr('aria-expanded','false');
$tabPanels.removeClass('active').attr('aria-hidden','true');
},
findJiraTickets = function(){
var $tickets = $('#dokuwiki__content').find('a.jiralink');
if($tickets.length >0){
var $panel = $('#spr__tab-jira'),
$num = $('a[href="#spr__tab-jira"]').find('.num');
if($panel.length > 0 && $num.length > 0){
$num.empty().append($tickets.length);
$panel.find('> div').empty().append('<ul></ul>');
var $ul = $panel.find('ul');
$tickets.each(function (){
var $ticket = $(this).clone();
$ul.prepend('<li></li>');
$ul.find('li:first-child').append($ticket);
});
}
}
},
findSitemap = function(){
var $panel = $('#spr__tab-toc'),
$toc = $panel.find('ul'),
$num = $('a[href="#spr__tab-toc"]').find('.num');
if($toc.length == 0){
$panel.append('<div><p>'+LANG.template.sprintdoc.meta_box_toc_none+'</p></div>');
}else{
$num.empty().append('1');
}
};
$(function () {
toggleTabs();
findJiraTickets();
findSitemap();
});
})(jQuery, spc);
});