fuelux.wizard.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Fuel UX Wizard
  3. * https://github.com/ExactTarget/fuelux
  4. *
  5. * Copyright (c) 2014 ExactTarget
  6. * Licensed under the BSD New license.
  7. */
  8. // -- BEGIN UMD WRAPPER PREFACE --
  9. // For more information on UMD visit:
  10. // https://github.com/umdjs/umd/blob/master/jqueryPlugin.js
  11. (function (factory) {
  12. if (typeof define === 'function' && define.amd) {
  13. // if AMD loader is available, register as an anonymous module.
  14. define(['jquery'], factory);
  15. } else {
  16. // OR use browser globals if AMD is not present
  17. factory(jQuery);
  18. }
  19. }(function ($) {
  20. // -- END UMD WRAPPER PREFACE --
  21. // -- BEGIN MODULE CODE HERE --
  22. var old = $.fn.wizard;
  23. // WIZARD CONSTRUCTOR AND PROTOTYPE
  24. var Wizard = function (element, options) {
  25. var kids;
  26. this.$element = $(element);
  27. this.options = $.extend({}, $.fn.wizard.defaults, options);
  28. this.options.disablePreviousStep = ( this.$element.attr('data-restrict') === "previous" ) ? true : this.options.disablePreviousStep;
  29. this.currentStep = this.options.selectedItem.step;
  30. this.numSteps = this.$element.find('.steps li').length;
  31. this.$prevBtn = this.$element.find('button.btn-prev');
  32. this.$nextBtn = this.$element.find('button.btn-next');
  33. kids = this.$nextBtn.children().detach();
  34. this.nextText = $.trim(this.$nextBtn.text());
  35. this.$nextBtn.append(kids);
  36. // handle events
  37. this.$prevBtn.on('click.fu.wizard', $.proxy(this.previous, this));
  38. this.$nextBtn.on('click.fu.wizard', $.proxy(this.next, this));
  39. this.$element.on('click.fu.wizard', 'li.complete', $.proxy(this.stepclicked, this));
  40. this.selectedItem(this.options.selectedItem);
  41. if( this.options.disablePreviousStep ) {
  42. this.$prevBtn.attr( 'disabled', true );
  43. this.$element.find( '.steps' ).addClass( 'previous-disabled' );
  44. }
  45. };
  46. Wizard.prototype = {
  47. constructor: Wizard,
  48. destroy: function() {
  49. this.$element.remove();
  50. // any external bindings [none]
  51. // empty elements to return to original markup [none]
  52. // returns string of markup
  53. return this.$element[0].outerHTML;
  54. },
  55. //index is 1 based
  56. //second parameter can be array of objects [{ ... }, { ... }] or you can pass n additional objects as args
  57. //object structure is as follows (all params are optional): { badge: '', label: '', pane: '' }
  58. addSteps: function(index){
  59. var items = [].slice.call(arguments).slice(1);
  60. var $steps = this.$element.find('.steps');
  61. var $stepContent = this.$element.find('.step-content');
  62. var i, l, $pane, $startPane, $startStep, $step;
  63. index = (index===-1 || (index>(this.numSteps+1))) ? this.numSteps+1 : index;
  64. if(items[0] instanceof Array){
  65. items = items[0];
  66. }
  67. $startStep = $steps.find('li:nth-child(' + index + ')');
  68. $startPane = $stepContent.find('.step-pane:nth-child(' + index + ')');
  69. if($startStep.length<1){
  70. $startStep = null;
  71. }
  72. for(i=0, l=items.length; i<l; i++){
  73. $step = $('<li data-step="' + index + '"><span class="badge badge-info"></span></li>');
  74. $step.append(items[i].label || '').append('<span class="chevron"></span>');
  75. $step.find('.badge').append(items[i].badge || index);
  76. $pane = $('<div class="step-pane" data-step="' + index + '"></div>');
  77. $pane.append(items[i].pane || '');
  78. if(!$startStep){
  79. $steps.append($step);
  80. $stepContent.append($pane);
  81. }else{
  82. $startStep.before($step);
  83. $startPane.before($pane);
  84. }
  85. index++;
  86. }
  87. this.syncSteps();
  88. this.numSteps = $steps.find('li').length;
  89. this.setState();
  90. },
  91. //index is 1 based, howMany is number to remove
  92. removeSteps: function(index, howMany){
  93. var action = 'nextAll';
  94. var i = 0;
  95. var $steps = this.$element.find('.steps');
  96. var $stepContent = this.$element.find('.step-content');
  97. var $start;
  98. howMany = (howMany!==undefined) ? howMany : 1;
  99. if(index>$steps.find('li').length){
  100. $start = $steps.find('li:last');
  101. }else{
  102. $start = $steps.find('li:nth-child(' + index + ')').prev();
  103. if($start.length<1){
  104. action = 'children';
  105. $start = $steps;
  106. }
  107. }
  108. $start[action]().each(function(){
  109. var item = $(this);
  110. var step = item.attr('data-step');
  111. if(i<howMany){
  112. item.remove();
  113. $stepContent.find('.step-pane[data-step="' + step + '"]:first').remove();
  114. }else{
  115. return false;
  116. }
  117. i++;
  118. });
  119. this.syncSteps();
  120. this.numSteps = $steps.find('li').length;
  121. this.setState();
  122. },
  123. setState: function () {
  124. var canMovePrev = (this.currentStep > 1);
  125. var firstStep = (this.currentStep === 1);
  126. var lastStep = (this.currentStep === this.numSteps);
  127. // disable buttons based on current step
  128. if( !this.options.disablePreviousStep ) {
  129. this.$prevBtn.attr('disabled', (firstStep === true || canMovePrev === false));
  130. }
  131. // change button text of last step, if specified
  132. var last = this.$nextBtn.attr('data-last');
  133. if (last) {
  134. this.lastText = last;
  135. // replace text
  136. var text = this.nextText;
  137. if ( lastStep === true ) {
  138. text = this.lastText;
  139. // add status class to wizard
  140. this.$element.addClass('complete');
  141. }
  142. else {
  143. this.$element.removeClass('complete');
  144. }
  145. var kids = this.$nextBtn.children().detach();
  146. this.$nextBtn.text(text).append(kids);
  147. }
  148. // reset classes for all steps
  149. var $steps = this.$element.find('.steps li');
  150. $steps.removeClass('active').removeClass('complete');
  151. $steps.find('span.badge').removeClass('badge-info').removeClass('badge-success');
  152. // set class for all previous steps
  153. var prevSelector = '.steps li:lt(' + (this.currentStep - 1) + ')';
  154. var $prevSteps = this.$element.find(prevSelector);
  155. $prevSteps.addClass('complete');
  156. $prevSteps.find('span.badge').addClass('badge-success');
  157. // set class for current step
  158. var currentSelector = '.steps li:eq(' + (this.currentStep - 1) + ')';
  159. var $currentStep = this.$element.find(currentSelector);
  160. $currentStep.addClass('active');
  161. $currentStep.find('span.badge').addClass('badge-info');
  162. // set display of target element
  163. var $stepContent = this.$element.find('.step-content');
  164. var target = $currentStep.attr('data-step');
  165. $stepContent.find('.step-pane').removeClass('active');
  166. $stepContent.find('.step-pane[data-step="' + target + '"]:first').addClass('active');
  167. //ACE
  168. /**
  169. // reset the wizard position to the left
  170. this.$element.find('.steps').first().attr('style','margin-left: 0');
  171. // check if the steps are wider than the container div
  172. var totalWidth = 0;
  173. this.$element.find('.steps > li').each(function () {
  174. totalWidth += $(this).outerWidth();
  175. });
  176. var containerWidth = 0;
  177. if (this.$element.find('.actions').length) {
  178. containerWidth = this.$element.width() - this.$element.find('.actions').first().outerWidth();
  179. } else {
  180. containerWidth = this.$element.width();
  181. }
  182. if (totalWidth > containerWidth) {
  183. // set the position so that the last step is on the right
  184. var newMargin = totalWidth - containerWidth;
  185. this.$element.find('.steps').first().attr('style','margin-left: -' + newMargin + 'px');
  186. // set the position so that the active step is in a good
  187. // position if it has been moved out of view
  188. if (this.$element.find('li.active').first().position().left < 200) {
  189. newMargin += this.$element.find('li.active').first().position().left - 200;
  190. if (newMargin < 1) {
  191. this.$element.find('.steps').first().attr('style','margin-left: 0');
  192. } else {
  193. this.$element.find('.steps').first().attr('style','margin-left: -' + newMargin + 'px');
  194. }
  195. }
  196. }
  197. */
  198. // only fire changed event after initializing
  199. if(typeof(this.initialized) !== 'undefined' ) {
  200. var e = $.Event('changed.fu.wizard');
  201. this.$element.trigger(e, {step: this.currentStep});
  202. }
  203. this.initialized = true;
  204. },
  205. stepclicked: function (e) {
  206. var li = $(e.currentTarget);
  207. var index = this.$element.find('.steps li').index(li);
  208. var canMovePrev = true;
  209. if( this.options.disablePreviousStep ) {
  210. if( index < this.currentStep ) {
  211. canMovePrev = false;
  212. }
  213. }
  214. if( canMovePrev ) {
  215. var evt = $.Event('stepclicked.fu.wizard');
  216. this.$element.trigger(evt, {step: index + 1});
  217. if (evt.isDefaultPrevented()) { return; }
  218. this.currentStep = (index + 1);
  219. this.setState();
  220. }
  221. },
  222. syncSteps: function(){
  223. var i = 1;
  224. var $steps = this.$element.find('.steps');
  225. var $stepContent = this.$element.find('.step-content');
  226. $steps.children().each(function(){
  227. var item = $(this);
  228. var badge = item.find('.badge');
  229. var step = item.attr('data-step');
  230. if(!isNaN(parseInt(badge.html(), 10))){
  231. badge.html(i);
  232. }
  233. item.attr('data-step', i);
  234. $stepContent.find('.step-pane[data-step="' + step + '"]:last').attr('data-step', i);
  235. i++;
  236. });
  237. },
  238. previous: function () {
  239. var canMovePrev = (this.currentStep > 1);
  240. if( this.options.disablePreviousStep ) {
  241. canMovePrev = false;
  242. }
  243. if (canMovePrev) {
  244. var e = $.Event('actionclicked.fu.wizard');
  245. this.$element.trigger(e, {step: this.currentStep, direction: 'previous'});
  246. if (e.isDefaultPrevented()) { return; } // don't increment
  247. this.currentStep -= 1;
  248. this.setState();
  249. }
  250. // return focus to control after selecting an option
  251. if( this.$prevBtn.is(':disabled') ) {
  252. this.$nextBtn.focus();
  253. }
  254. else {
  255. this.$prevBtn.focus();
  256. }
  257. },
  258. next: function () {
  259. var canMoveNext = (this.currentStep + 1 <= this.numSteps);
  260. var lastStep = (this.currentStep === this.numSteps);
  261. if (canMoveNext) {
  262. var e = $.Event('actionclicked.fu.wizard');
  263. this.$element.trigger(e, {step: this.currentStep, direction: 'next'});
  264. if (e.isDefaultPrevented()) { return; } // don't increment
  265. this.currentStep += 1;
  266. this.setState();
  267. }
  268. else if (lastStep) {
  269. this.$element.trigger('finished.fu.wizard');
  270. }
  271. // return focus to control after selecting an option
  272. if( this.$nextBtn.is(':disabled') ) {
  273. this.$prevBtn.focus();
  274. }
  275. else {
  276. this.$nextBtn.focus();
  277. }
  278. },
  279. selectedItem: function (selectedItem) {
  280. var retVal, step;
  281. if(selectedItem) {
  282. step = selectedItem.step || -1;
  283. if(step >= 1 && step <= this.numSteps) {
  284. this.currentStep = step;
  285. this.setState();
  286. }else{
  287. step = this.$element.find('.steps li.active:first').attr('data-step');
  288. if(!isNaN(step)){
  289. this.currentStep = parseInt(step, 10);
  290. this.setState();
  291. }
  292. }
  293. retVal = this;
  294. }
  295. else {
  296. retVal = { step: this.currentStep };
  297. }
  298. return retVal;
  299. }
  300. };
  301. // WIZARD PLUGIN DEFINITION
  302. $.fn.wizard = function (option) {
  303. var args = Array.prototype.slice.call( arguments, 1 );
  304. var methodReturn;
  305. var $set = this.each(function () {
  306. var $this = $( this );
  307. var data = $this.data('fu.wizard');
  308. var options = typeof option === 'object' && option;
  309. if( !data ) $this.data('fu.wizard', (data = new Wizard( this, options ) ) );
  310. if( typeof option === 'string' ) methodReturn = data[ option ].apply( data, args );
  311. });
  312. return ( methodReturn === undefined ) ? $set : methodReturn;
  313. };
  314. $.fn.wizard.defaults = {
  315. disablePreviousStep: false,
  316. selectedItem: { step: -1 } //-1 means it will attempt to look for "active" class in order to set the step
  317. };
  318. $.fn.wizard.Constructor = Wizard;
  319. $.fn.wizard.noConflict = function () {
  320. $.fn.wizard = old;
  321. return this;
  322. };
  323. // DATA-API
  324. $(document).on('mouseover.fu.wizard.data-api', '[data-initialize=wizard]', function (e) {
  325. var $control = $(e.target).closest('.wizard');
  326. if ( !$control.data('fu.wizard') ) {
  327. $control.wizard($control.data());
  328. }
  329. });
  330. // Must be domReady for AMD compatibility
  331. $(function () {
  332. $('[data-initialize=wizard]').each(function () {
  333. var $this = $(this);
  334. if ($this.data('fu.wizard')) return;
  335. $this.wizard($this.data());
  336. });
  337. });
  338. // -- BEGIN UMD WRAPPER AFTERWORD --
  339. }));
  340. // -- END UMD WRAPPER AFTERWORD --