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