wizard.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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);//remember, steps index is 1 based...
  125. var isFirstStep = (this.currentStep === 1);
  126. var isLastStep = (this.currentStep === this.numSteps);
  127. // disable buttons based on current step
  128. if (!this.options.disablePreviousStep) {
  129. this.$prevBtn.attr('disabled', (isFirstStep === 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 (isLastStep === true) {
  138. text = this.lastText;
  139. // add status class to wizard
  140. this.$element.addClass('complete');
  141. } else {
  142. this.$element.removeClass('complete');
  143. }
  144. var kids = this.$nextBtn.children().detach();
  145. this.$nextBtn.text(text).append(kids);
  146. }
  147. // reset classes for all steps
  148. var $steps = this.$element.find('.steps li');
  149. $steps.removeClass('active').removeClass('complete');
  150. $steps.find('span.badge').removeClass('badge-info').removeClass('badge-success');
  151. // set class for all previous steps
  152. var prevSelector = '.steps li:lt(' + (this.currentStep - 1) + ')';
  153. var $prevSteps = this.$element.find(prevSelector);
  154. $prevSteps.addClass('complete');
  155. $prevSteps.find('span.badge').addClass('badge-success');
  156. // set class for current step
  157. var currentSelector = '.steps li:eq(' + (this.currentStep - 1) + ')';
  158. var $currentStep = this.$element.find(currentSelector);
  159. $currentStep.addClass('active');
  160. $currentStep.find('span.badge').addClass('badge-info');
  161. // set display of target element
  162. var $stepContent = this.$element.find('.step-content');
  163. var target = $currentStep.attr('data-step');
  164. $stepContent.find('.step-pane').removeClass('active');
  165. $stepContent.find('.step-pane[data-step="' + target + '"]:first').addClass('active');
  166. // reset the wizard position to the left
  167. this.$element.find('.steps').first().attr('style', 'margin-left: 0');
  168. // check if the steps are wider than the container div
  169. var totalWidth = 0;
  170. this.$element.find('.steps > li').each(function () {
  171. totalWidth += $(this).outerWidth();
  172. });
  173. var containerWidth = 0;
  174. if (this.$element.find('.actions').length) {
  175. containerWidth = this.$element.width() - this.$element.find('.actions').first().outerWidth();
  176. } else {
  177. containerWidth = this.$element.width();
  178. }
  179. if (totalWidth > containerWidth) {
  180. // set the position so that the last step is on the right
  181. var newMargin = totalWidth - containerWidth;
  182. this.$element.find('.steps').first().attr('style', 'margin-left: -' + newMargin + 'px');
  183. // set the position so that the active step is in a good
  184. // position if it has been moved out of view
  185. if (this.$element.find('li.active').first().position().left < 200) {
  186. newMargin += this.$element.find('li.active').first().position().left - 200;
  187. if (newMargin < 1) {
  188. this.$element.find('.steps').first().attr('style', 'margin-left: 0');
  189. } else {
  190. this.$element.find('.steps').first().attr('style', 'margin-left: -' + newMargin + 'px');
  191. }
  192. }
  193. }
  194. // only fire changed event after initializing
  195. if (typeof (this.initialized) !== 'undefined') {
  196. var e = $.Event('changed.fu.wizard');
  197. this.$element.trigger(e, {
  198. step: this.currentStep
  199. });
  200. }
  201. this.initialized = true;
  202. },
  203. stepclicked: function (e) {
  204. var li = $(e.currentTarget);
  205. var index = this.$element.find('.steps li').index(li);
  206. if (index < this.currentStep && this.options.disablePreviousStep) {//enforce restrictions
  207. return;
  208. } else {
  209. var evt = $.Event('stepclicked.fu.wizard');
  210. this.$element.trigger(evt, {
  211. step: index + 1
  212. });
  213. if (evt.isDefaultPrevented()) {
  214. return;
  215. }
  216. this.currentStep = (index + 1);
  217. this.setState();
  218. }
  219. },
  220. syncSteps: function () {
  221. var i = 1;
  222. var $steps = this.$element.find('.steps');
  223. var $stepContent = this.$element.find('.step-content');
  224. $steps.children().each(function () {
  225. var item = $(this);
  226. var badge = item.find('.badge');
  227. var step = item.attr('data-step');
  228. if (!isNaN(parseInt(badge.html(), 10))) {
  229. badge.html(i);
  230. }
  231. item.attr('data-step', i);
  232. $stepContent.find('.step-pane[data-step="' + step + '"]:last').attr('data-step', i);
  233. i++;
  234. });
  235. },
  236. previous: function () {
  237. if (this.options.disablePreviousStep || this.currentStep === 1) {
  238. return;
  239. }
  240. var e = $.Event('actionclicked.fu.wizard');
  241. this.$element.trigger(e, {
  242. step: this.currentStep,
  243. direction: 'previous'
  244. });
  245. if (e.isDefaultPrevented()) {
  246. return;
  247. }// don't increment ...what? Why?
  248. this.currentStep -= 1;
  249. this.setState();
  250. // only set focus if focus is still on the $nextBtn (avoid stomping on a focus set programmatically in actionclicked callback)
  251. if (this.$prevBtn.is(':focus')) {
  252. var firstFormField = this.$element.find('.active').find('input, select, textarea')[0];
  253. if (typeof firstFormField !== 'undefined') {
  254. // allow user to start typing immediately instead of having to click on the form field.
  255. $(firstFormField).focus();
  256. } else if (this.$element.find('.active input:first').length === 0 && this.$prevBtn.is(':disabled')) {
  257. //only set focus on a button as the last resort if no form fields exist and the just clicked button is now disabled
  258. this.$nextBtn.focus();
  259. }
  260. }
  261. },
  262. next: function () {
  263. if (this.currentStep < this.numSteps) {
  264. var e = $.Event('actionclicked.fu.wizard');
  265. this.$element.trigger(e, {
  266. step: this.currentStep,
  267. direction: 'next'
  268. });
  269. if (e.isDefaultPrevented()) {
  270. return;
  271. }// respect preventDefault in case dev has attached validation to step and wants to stop propagation based on it.
  272. this.currentStep += 1;
  273. this.setState();
  274. } else {//is last step
  275. this.$element.trigger('finished.fu.wizard');
  276. }
  277. // only set focus if focus is still on the $nextBtn (avoid stomping on a focus set programmatically in actionclicked callback)
  278. if (this.$nextBtn.is(':focus')) {
  279. var firstFormField = this.$element.find('.active').find('input, select, textarea')[0];
  280. if (typeof firstFormField !== 'undefined') {
  281. // allow user to start typing immediately instead of having to click on the form field.
  282. $(firstFormField).focus();
  283. } else if (this.$element.find('.active input:first').length === 0 && this.$nextBtn.is(':disabled')) {
  284. //only set focus on a button as the last resort if no form fields exist and the just clicked button is now disabled
  285. this.$prevBtn.focus();
  286. }
  287. }
  288. },
  289. selectedItem: function (selectedItem) {
  290. var retVal, step;
  291. if (selectedItem) {
  292. step = selectedItem.step || -1;
  293. //allow selection of step by data-name
  294. step = isNaN(step) && this.$element.find('.steps li[data-name="' + step + '"]').first().attr('data-step') || step;
  295. if (1 <= step && step <= this.numSteps) {
  296. this.currentStep = step;
  297. this.setState();
  298. } else {
  299. step = this.$element.find('.steps li.active:first').attr('data-step');
  300. if (!isNaN(step)) {
  301. this.currentStep = parseInt(step, 10);
  302. this.setState();
  303. }
  304. }
  305. retVal = this;
  306. } else {
  307. retVal = {
  308. step: this.currentStep
  309. };
  310. if (this.$element.find('.steps li.active:first[data-name]').length) {
  311. retVal.stepname = this.$element.find('.steps li.active:first').attr('data-name');
  312. }
  313. }
  314. return retVal;
  315. }
  316. };
  317. // WIZARD PLUGIN DEFINITION
  318. $.fn.wizard = function (option) {
  319. var args = Array.prototype.slice.call(arguments, 1);
  320. var methodReturn;
  321. var $set = this.each(function () {
  322. var $this = $(this);
  323. var data = $this.data('fu.wizard');
  324. var options = typeof option === 'object' && option;
  325. if (!data) {
  326. $this.data('fu.wizard', (data = new Wizard(this, options)));
  327. }
  328. if (typeof option === 'string') {
  329. methodReturn = data[option].apply(data, args);
  330. }
  331. });
  332. return (methodReturn === undefined) ? $set : methodReturn;
  333. };
  334. $.fn.wizard.defaults = {
  335. disablePreviousStep: false,
  336. selectedItem: {
  337. step: -1
  338. }//-1 means it will attempt to look for "active" class in order to set the step
  339. };
  340. $.fn.wizard.Constructor = Wizard;
  341. $.fn.wizard.noConflict = function () {
  342. $.fn.wizard = old;
  343. return this;
  344. };
  345. // DATA-API
  346. $(document).on('mouseover.fu.wizard.data-api', '[data-initialize=wizard]', function (e) {
  347. var $control = $(e.target).closest('.wizard');
  348. if (!$control.data('fu.wizard')) {
  349. $control.wizard($control.data());
  350. }
  351. });
  352. // Must be domReady for AMD compatibility
  353. $(function () {
  354. $('[data-initialize=wizard]').each(function () {
  355. var $this = $(this);
  356. if ($this.data('fu.wizard')) return;
  357. $this.wizard($this.data());
  358. });
  359. });
  360. // -- BEGIN UMD WRAPPER AFTERWORD --
  361. }));
  362. // -- END UMD WRAPPER AFTERWORD --