Skip to content

Commit

Permalink
Added resetWizard utility method to help support dynamic steps
Browse files Browse the repository at this point in the history
The method updates the appropriate variables based on the current state
of the DOM and reattaches to the necessary events.
  • Loading branch information
Andy committed Oct 11, 2013
1 parent 9bd82a2 commit 9098773
Showing 1 changed file with 44 additions and 26 deletions.
70 changes: 44 additions & 26 deletions jquery.bootstrap.wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,48 @@ var bootstrapWizardCreate = function(element, options) {
// Remove menu item
$item.remove();
};

var innerTabClick = function (e) {
// Get the index of the clicked tab
var clickedIndex = $navigation.find(baseItemSelector).index($(e.currentTarget).parent(baseItemSelector));
if($settings.onTabClick && typeof $settings.onTabClick === 'function' && $settings.onTabClick($activeTab, $navigation, obj.currentIndex(), clickedIndex)===false){
return false;
}
};

var innerTabShown = function (e) { // use shown instead of show to help prevent double firing
$element = $(e.target).parent();
var nextTab = $navigation.find(baseItemSelector).index($element);

// If it's disabled then do not change
if($element.hasClass('disabled')) {
return false;
}

if($settings.onTabChange && typeof $settings.onTabChange === 'function' && $settings.onTabChange($activeTab, $navigation, obj.currentIndex(), nextTab)===false){
return false;
}

$activeTab = $element; // activated tab
obj.fixNavigationButtons();
};

this.resetWizard = function() {

// remove the existing handlers
$('a[data-toggle="tab"]', $navigation).off('click', innerTabClick);
$('a[data-toggle="tab"]', $navigation).off('shown shown.bs.tab', innerTabShown);

// reset elements based on current state of the DOM
$navigation = element.find('ul:first', element);
$activeTab = $navigation.find(baseItemSelector + '.active', element);

// re-add handlers
$('a[data-toggle="tab"]', $navigation).on('click', innerTabClick);
$('a[data-toggle="tab"]', $navigation).on('shown shown.bs.tab', innerTabShown);

obj.fixNavigationButtons();
};

$navigation = element.find('ul:first', element);
$activeTab = $navigation.find(baseItemSelector + '.active', element);
Expand All @@ -190,34 +232,10 @@ var bootstrapWizardCreate = function(element, options) {
$settings.onShow($activeTab, $navigation, obj.nextIndex());
}

// Work the next/previous buttons
obj.fixNavigationButtons();

$('a[data-toggle="tab"]', $navigation).on('click', function (e) {
// Get the index of the clicked tab
var clickedIndex = $navigation.find(baseItemSelector).index($(e.currentTarget).parent(baseItemSelector));
if($settings.onTabClick && typeof $settings.onTabClick === 'function' && $settings.onTabClick($activeTab, $navigation, obj.currentIndex(), clickedIndex)===false){
return false;
}
});
$('a[data-toggle="tab"]', $navigation).on('click', innerTabClick);

// attach to both shown and shown.bs.tab to support Bootstrap versions 2.3.2 and 3.0.0
$('a[data-toggle="tab"]', $navigation).on('shown shown.bs.tab', function (e) { // use shown instead of show to help prevent double firing
$element = $(e.target).parent();
var nextTab = $navigation.find(baseItemSelector).index($element);

// If it's disabled then do not change
if($element.hasClass('disabled')) {
return false;
}

if($settings.onTabChange && typeof $settings.onTabChange === 'function' && $settings.onTabChange($activeTab, $navigation, obj.currentIndex(), nextTab)===false){
return false;
}

$activeTab = $element; // activated tab
obj.fixNavigationButtons();
});
$('a[data-toggle="tab"]', $navigation).on('shown shown.bs.tab', innerTabShown);
};
$.fn.bootstrapWizard = function(options) {
//expose methods
Expand Down

0 comments on commit 9098773

Please sign in to comment.