Skip to content

Commit

Permalink
Merge pull request #4 from seanhuber/reload_tabs
Browse files Browse the repository at this point in the history
Added markForReload function
  • Loading branch information
seanhuber authored Aug 24, 2016
2 parents 4dc5abc + c12030b commit fa75e56
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ $('#tab_modal_anchor').tabModal({
// show() displays the tabbed modal. It often makes sense to put this in a click handler.
$('#tab_modal_anchor').tabModal('show');
// markForReload(tab_id) - Marks a tab to be reloaded. This means the next time the tab is selected, it will load
// content even if the tab has been previously loaded.
$('#tab_modal_anchor').tabModal('markForReload', 'fourth');
</script>
```

Expand Down
12 changes: 9 additions & 3 deletions bootstrap-tab-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@
$( "a[data-target='#"+tab_id+"']" ).addClass('hidden');
},

markForReload: function(tab_id) {
$('#' + tab_id).addClass('load-content');
},

setDefaultTab: function( tab_id ) {
$.each( this.options.tabs, function(k, v) { delete v.active; });
this.options.tabs[tab_id].active = true;
Expand All @@ -104,17 +108,19 @@
var $modal = this._buildModal();

$initial_tabpanel = $modal.find('.tab-pane.active');
if ( $initial_tabpanel.text() == 'use-throbber' ) {
if ($initial_tabpanel.hasClass('load-content')) {
this._addThrobber( $initial_tabpanel );
this.options.tabs[$initial_tabpanel.prop('id')].showTab();
$initial_tabpanel.removeClass('load-content');
}

$modal.find("a[data-toggle='tab']").on('show.bs.tab', function (e) {
that.hideAlert();
var $tabpanel = $($(e.target).data('target'));
if ( $tabpanel.text() == 'use-throbber' ) {
if ($tabpanel.hasClass('load-content')) {
that._addThrobber( $tabpanel );
that.options.tabs[$tabpanel.prop('id')].showTab();
$tabpanel.removeClass('load-content');
}
});

Expand Down Expand Up @@ -201,7 +207,7 @@

t_html.t_links += "<li role='presentation' class='"+tab_class+"'><a data-target='#"+k+"' aria-controls='"+k+"' role='tab' data-toggle='tab'>"+v.label+"</a></li>";
var tab_content = v.hasOwnProperty( 'content' ) ? v.content : that.options.default_tab_content;
t_html.t_panels += "<div role='tabpanel' class='tab-pane "+tab_class+"' id='"+k+"'>"+tab_content+"</div>";
t_html.t_panels += "<div role='tabpanel' class='load-content tab-pane "+tab_class+"' id='"+k+"'>"+tab_content+"</div>";
});
return t_html;
},
Expand Down
4 changes: 3 additions & 1 deletion demo/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ <h4>Click the button to display the tabbed modal:</h4>
label: 'Fourth Tab',
showTab: function() {
setTimeout( function() {
$('#demo').tabModal('setTabContent', 'fourth', '<h3>Fourth!</h3>');
$demo = $('#demo');
$demo.tabModal('setTabContent', 'fourth', '<h3>Fourth!</h3>');
$demo.tabModal('markForReload', 'fourth');
}, 500); // just a delay for demonstrating the built-in throbber
}
},
Expand Down
15 changes: 15 additions & 0 deletions qunit/qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,18 @@ QUnit.test( "add tabs", function( assert ) {
});
$anchor.tabModal('show');
});

QUnit.test("reload tab", function(assert) {
expect(1);
var done = assert.async();
var $anchor = $("<div id='test5'></div>");
$anchor.appendTo('body');
$anchor.tabModal(_modal_opts).on( 'sh.tabModal.shown', function() {
tabLink($anchor, 'fourth').trigger('click');
$anchor.tabModal('markForReload', 'fourth');
assert.ok($('#fourth').hasClass('load-content'));
$anchor.tabModal('close');
done();
});
$anchor.tabModal('show');
});

0 comments on commit fa75e56

Please sign in to comment.