github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/js/wizard/jquery.bootstrap.wizard.js (about)

     1  /*!
     2   * jQuery twitter bootstrap wizard plugin
     3   * Examples and documentation at: http://github.com/VinceG/twitter-bootstrap-wizard
     4   * version 1.0
     5   * Requires jQuery v1.3.2 or later
     6   * Supports Bootstrap 2.2.x, 2.3.x, 3.0
     7   * Dual licensed under the MIT and GPL licenses:
     8   * http://www.opensource.org/licenses/mit-license.php
     9   * http://www.gnu.org/licenses/gpl.html
    10   * Authors: Vadim Vincent Gabriel (http://vadimg.com), Jason Gill (www.gilluminate.com)
    11   */
    12  ;(function($) {
    13  var bootstrapWizardCreate = function(element, options) {
    14  	var element = $(element);
    15  	var obj = this;
    16  	
    17  	// selector skips any 'li' elements that do not contain a child with a tab data-toggle
    18  	var baseItemSelector = 'li:has([data-toggle="tab"])';
    19  
    20  	// Merge options with defaults
    21  	var $settings = $.extend({}, $.fn.bootstrapWizard.defaults, options);
    22  	var $activeTab = null;
    23  	var $navigation = null;
    24  	
    25  	this.rebindClick = function(selector, fn)
    26  	{
    27  		selector.unbind('click', fn).bind('click', fn);
    28  	}
    29  
    30  	this.fixNavigationButtons = function() {
    31  		// Get the current active tab
    32  		if(!$activeTab.length) {
    33  			// Select first one
    34  			$navigation.find('a:first').tab('show');
    35  			$activeTab = $navigation.find(baseItemSelector + ':first');
    36  		}
    37  
    38  		// See if we're currently in the first/last then disable the previous and last buttons
    39  		$($settings.previousSelector, element).toggleClass('disabled', (obj.firstIndex() >= obj.currentIndex()));
    40  		$($settings.nextSelector, element).toggleClass('disabled', (obj.currentIndex() >= obj.navigationLength()));
    41  
    42  		// We are unbinding and rebinding to ensure single firing and no double-click errors
    43  		obj.rebindClick($($settings.nextSelector, element), obj.next);
    44  		obj.rebindClick($($settings.previousSelector, element), obj.previous);
    45  		obj.rebindClick($($settings.lastSelector, element), obj.last);
    46  		obj.rebindClick($($settings.firstSelector, element), obj.first);
    47  
    48  		if($settings.onTabShow && typeof $settings.onTabShow === 'function' && $settings.onTabShow($activeTab, $navigation, obj.currentIndex())===false){
    49  			return false;
    50  		}
    51  	};
    52  
    53  	this.next = function(e) {
    54  
    55  		// If we clicked the last then dont activate this
    56  		if(element.hasClass('last')) {
    57  			return false;
    58  		}
    59  
    60  		if($settings.onNext && typeof $settings.onNext === 'function' && $settings.onNext($activeTab, $navigation, obj.nextIndex())===false){
    61  			return false;
    62  		}
    63  
    64  		// Did we click the last button
    65  		$index = obj.nextIndex();
    66  		if($index > obj.navigationLength()) {
    67  		} else {
    68  			$navigation.find(baseItemSelector + ':eq('+$index+') a').tab('show');
    69  		}
    70  	};
    71  
    72  	this.previous = function(e) {
    73  
    74  		// If we clicked the first then dont activate this
    75  		if(element.hasClass('first')) {
    76  			return false;
    77  		}
    78  
    79  		if($settings.onPrevious && typeof $settings.onPrevious === 'function' && $settings.onPrevious($activeTab, $navigation, obj.previousIndex())===false){
    80  			return false;
    81  		}
    82  
    83  		$index = obj.previousIndex();
    84  		if($index < 0) {
    85  		} else {
    86  			$navigation.find(baseItemSelector + ':eq('+$index+') a').tab('show');
    87  		}
    88  	};
    89  
    90  	this.first = function(e) {
    91  		if($settings.onFirst && typeof $settings.onFirst === 'function' && $settings.onFirst($activeTab, $navigation, obj.firstIndex())===false){
    92  			return false;
    93  		}
    94  
    95  		// If the element is disabled then we won't do anything
    96  		if(element.hasClass('disabled')) {
    97  			return false;
    98  		}
    99  		$navigation.find(baseItemSelector + ':eq(0) a').tab('show');
   100  
   101  	};
   102  	this.last = function(e) {
   103  		if($settings.onLast && typeof $settings.onLast === 'function' && $settings.onLast($activeTab, $navigation, obj.lastIndex())===false){
   104  			return false;
   105  		}
   106  
   107  		// If the element is disabled then we won't do anything
   108  		if(element.hasClass('disabled')) {
   109  			return false;
   110  		}
   111  		$navigation.find(baseItemSelector + ':eq('+obj.navigationLength()+') a').tab('show');
   112  	};
   113  	this.currentIndex = function() {
   114  		return $navigation.find(baseItemSelector).index($activeTab);
   115  	};
   116  	this.firstIndex = function() {
   117  		return 0;
   118  	};
   119  	this.lastIndex = function() {
   120  		return obj.navigationLength();
   121  	};
   122  	this.getIndex = function(e) {
   123  		return $navigation.find(baseItemSelector).index(e);
   124  	};
   125  	this.nextIndex = function() {
   126  		return $navigation.find(baseItemSelector).index($activeTab) + 1;
   127  	};
   128  	this.previousIndex = function() {
   129  		return $navigation.find(baseItemSelector).index($activeTab) - 1;
   130  	};
   131  	this.navigationLength = function() {
   132  		return $navigation.find(baseItemSelector).length - 1;
   133  	};
   134  	this.activeTab = function() {
   135  		return $activeTab;
   136  	};
   137  	this.nextTab = function() {
   138  		return $navigation.find(baseItemSelector + ':eq('+(obj.currentIndex()+1)+')').length ? $navigation.find(baseItemSelector + ':eq('+(obj.currentIndex()+1)+')') : null;
   139  	};
   140  	this.previousTab = function() {
   141  		if(obj.currentIndex() <= 0) {
   142  			return null;
   143  		}
   144  		return $navigation.find(baseItemSelector + ':eq('+parseInt(obj.currentIndex()-1)+')');
   145  	};
   146  	this.show = function(index) {
   147  		return element.find(baseItemSelector + ':eq(' + index + ') a').tab('show');
   148  	};
   149  	this.disable = function(index) {
   150  		$navigation.find(baseItemSelector + ':eq('+index+')').addClass('disabled');
   151  	};
   152  	this.enable = function(index) {
   153  		$navigation.find(baseItemSelector + ':eq('+index+')').removeClass('disabled');
   154  	};
   155  	this.hide = function(index) {
   156  		$navigation.find(baseItemSelector + ':eq('+index+')').hide();
   157  	};
   158  	this.display = function(index) {
   159  		$navigation.find(baseItemSelector + ':eq('+index+')').show();
   160  	};
   161  	this.remove = function(args) {
   162  		var $index = args[0];
   163  		var $removeTabPane = typeof args[1] != 'undefined' ? args[1] : false;
   164  		var $item = $navigation.find(baseItemSelector + ':eq('+$index+')');
   165  
   166  		// Remove the tab pane first if needed
   167  		if($removeTabPane) {
   168  			var $href = $item.find('a').attr('href');
   169  			$($href).remove();
   170  		}
   171  
   172  		// Remove menu item
   173  		$item.remove();
   174  	};
   175  	
   176  	var innerTabClick = function (e) {
   177  		// Get the index of the clicked tab
   178  		var clickedIndex = $navigation.find(baseItemSelector).index($(e.currentTarget).parent(baseItemSelector));
   179  		if($settings.onTabClick && typeof $settings.onTabClick === 'function' && $settings.onTabClick($activeTab, $navigation, obj.currentIndex(), clickedIndex)===false){
   180  			return false;
   181  		}
   182  	};
   183  	
   184  	var innerTabShown = function (e) {  // use shown instead of show to help prevent double firing
   185  		$element = $(e.target).parent();
   186  		var nextTab = $navigation.find(baseItemSelector).index($element);
   187  
   188  		// If it's disabled then do not change
   189  		if($element.hasClass('disabled')) {
   190  			return false;
   191  		}
   192  
   193  		if($settings.onTabChange && typeof $settings.onTabChange === 'function' && $settings.onTabChange($activeTab, $navigation, obj.currentIndex(), nextTab)===false){
   194  				return false;
   195  		}
   196  
   197  		$activeTab = $element; // activated tab
   198  		obj.fixNavigationButtons();
   199  	};
   200  	
   201  	this.resetWizard = function() {
   202  		
   203  		// remove the existing handlers
   204  		$('a[data-toggle="tab"]', $navigation).off('click', innerTabClick);
   205  		$('a[data-toggle="tab"]', $navigation).off('shown shown.bs.tab', innerTabShown);
   206  		
   207  		// reset elements based on current state of the DOM
   208  		$navigation = element.find('ul:first', element);
   209  		$activeTab = $navigation.find(baseItemSelector + '.active', element);
   210  		
   211  		// re-add handlers
   212  		$('a[data-toggle="tab"]', $navigation).on('click', innerTabClick);
   213  		$('a[data-toggle="tab"]', $navigation).on('shown shown.bs.tab', innerTabShown);
   214  		
   215  		obj.fixNavigationButtons();
   216  	};
   217  
   218  	$navigation = element.find('ul:first', element);
   219  	$activeTab = $navigation.find(baseItemSelector + '.active', element);
   220  
   221  	if(!$navigation.hasClass($settings.tabClass)) {
   222  		$navigation.addClass($settings.tabClass);
   223  	}
   224  
   225  	// Load onInit
   226  	if($settings.onInit && typeof $settings.onInit === 'function'){
   227  		$settings.onInit($activeTab, $navigation, 0);
   228  	}
   229  
   230  	// Load onShow
   231  	if($settings.onShow && typeof $settings.onShow === 'function'){
   232  		$settings.onShow($activeTab, $navigation, obj.nextIndex());
   233  	}
   234  
   235  	$('a[data-toggle="tab"]', $navigation).on('click', innerTabClick);
   236  
   237  	// attach to both shown and shown.bs.tab to support Bootstrap versions 2.3.2 and 3.0.0
   238  	$('a[data-toggle="tab"]', $navigation).on('shown shown.bs.tab', innerTabShown);
   239  
   240  	this.fixNavigationButtons();
   241  };
   242  $.fn.bootstrapWizard = function(options) {
   243  	//expose methods
   244  	if (typeof options == 'string') {
   245  		var args = Array.prototype.slice.call(arguments, 1)
   246  		if(args.length === 1) {
   247  			args.toString();
   248  		}
   249  		return this.data('bootstrapWizard')[options](args);
   250  	}
   251  	return this.each(function(index){
   252  		var element = $(this);
   253  		// Return early if this element already has a plugin instance
   254  		if (element.data('bootstrapWizard')) return;
   255  		// pass options to plugin constructor
   256  		var wizard = new bootstrapWizardCreate(element, options);
   257  		// Store plugin object in this element's data
   258  		element.data('bootstrapWizard', wizard);
   259  	});
   260  };
   261  
   262  // expose options
   263  $.fn.bootstrapWizard.defaults = {
   264  	tabClass:         'nav nav-pills',
   265  	nextSelector:     '.wizard li.next',
   266  	previousSelector: '.wizard li.previous',
   267  	firstSelector:    '.wizard li.first',
   268  	lastSelector:     '.wizard li.last',
   269  	onShow:           null,
   270  	onInit:           null,
   271  	onNext:           null,
   272  	onPrevious:       null,
   273  	onLast:           null,
   274  	onFirst:          null,
   275  	onTabChange:      null, 
   276  	onTabClick:       null,
   277  	onTabShow:        null
   278  };
   279  
   280  })(jQuery);