X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/a5871ab5512d87a33e7e95451d26ee7d3e789742..16365557b20c7efc689f3c6bf8e66882015df76b:/modules/tabsManager.js diff --git a/modules/tabsManager.js b/modules/tabsManager.js index a57a47e..4103887 100644 --- a/modules/tabsManager.js +++ b/modules/tabsManager.js @@ -3,19 +3,33 @@ rng.modules.tabsManager = function(sandbox) { var $ = sandbox.$; var view = $(sandbox.getTemplate('main')()); - + var currentSlug; var tabContent = {}; - function selectTab(tabCode) { - var tabBar = $(view, '#rng-main-tabs'); - var prevTabCode = tabBar.find('li.active a').attr('href').substr(1); + function selectTab(slug) { + var tabBar = view.find('#rng-tabsManager-tabBar'); + + var prevActive = tabBar.find('li.active'); + var prevSlug; + if(prevActive.length) + prevSlug = prevActive.find('a').attr('href').substr(1); + + if(prevSlug == slug) + return; + if(prevSlug) + sandbox.publish('leaving', prevSlug); + tabBar.find('li').removeClass('active'); - tabBar.find('a[href=#' + tabCode + ']').parent().addClass('active'); - $(view, '.rng-tab-content').hide(); - $(view, '#rng-tab-content-' + tabCode).show(); + tabBar.find('a[href=#' + slug + ']').parent().addClass('active'); + + if(prevSlug) + tabContent[prevSlug].detach(); + tabContent[slug].appendTo(view.find('#rng-tabsManager-content')); + currentSlug = slug; } + - $('#rng-main-tabs li a').click(function(e) { + view.on('click', 'li a', function(e) { selectTab($(e.target).attr('href').substr(1)); }); @@ -28,8 +42,14 @@ rng.modules.tabsManager = function(sandbox) { return view; }, - addTab: function(title, view) { - tabContent[title] = view; + addTab: function(title, slug, contentView) { + tabContent[slug] = contentView; + view.find('#rng-tabsManager-tabBar').append(sandbox.getTemplate('tabHandle')({title: title, slug: slug})); + if(_.values(tabContent).length === 1) + selectTab(slug); + }, + getCurrentSlug: function() { + return currentSlug; } }