jquery.bootstrap.wizard.js 7.7 KB

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