jquery.smartWizard.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*!
  2. * SmartWizard v4.2.2
  3. * The awesome jQuery step wizard plugin with Bootstrap support
  4. * http://www.techlaboratory.net/smartwizard
  5. *
  6. * Created by Dipu Raj
  7. * http://dipuraj.me
  8. *
  9. * Licensed under the terms of the MIT License
  10. * https://github.com/techlab/SmartWizard/blob/master/LICENSE
  11. */
  12. ;(function ($, window, document, undefined) {
  13. "use strict";
  14. // Default options
  15. var defaults = {
  16. selected: 0, // Initial selected step, 0 = first step
  17. keyNavigation: true, // Enable/Disable keyboard navigation(left and right keys are used if enabled)
  18. autoAdjustHeight: true, // Automatically adjust content height
  19. cycleSteps: false, // Allows to cycle the navigation of steps
  20. backButtonSupport: true, // Enable the back button support
  21. useURLhash: true, // Enable selection of the step based on url hash
  22. showStepURLhash: true, // Show url hash based on step
  23. lang: { // Language variables for button
  24. next: 'Next',
  25. previous: 'Previous'
  26. },
  27. toolbarSettings: {
  28. toolbarPosition: 'bottom', // none, top, bottom, both
  29. toolbarButtonPosition: 'right', // left, right
  30. showNextButton: true, // show/hide a Next button
  31. showPreviousButton: true, // show/hide a Previous button
  32. toolbarExtraButtons: [] // Extra buttons to show on toolbar, array of jQuery input/buttons elements
  33. },
  34. anchorSettings: {
  35. anchorClickable: true, // Enable/Disable anchor navigation
  36. enableAllAnchors: false, // Activates all anchors clickable all times
  37. markDoneStep: true, // Add done css
  38. markAllPreviousStepsAsDone: true, // When a step selected by url hash, all previous steps are marked done
  39. removeDoneStepOnNavigateBack: false, // While navigate back done step after active step will be cleared
  40. enableAnchorOnDoneStep: true // Enable/Disable the done steps navigation
  41. },
  42. contentURL: null, // content url, Enables Ajax content loading. Can also set as data data-content-url on anchor
  43. contentCache: true, // cache step contents, if false content is fetched always from ajax url
  44. ajaxSettings: {}, // Ajax extra settings
  45. disabledSteps: [], // Array Steps disabled
  46. errorSteps: [], // Highlight step with errors
  47. hiddenSteps: [], // Hidden steps
  48. theme: 'default', // theme for the wizard, related css need to include for other than default theme
  49. transitionEffect: 'none', // Effect on navigation, none/slide/fade
  50. transitionSpeed: '400'
  51. };
  52. // The plugin constructor
  53. function SmartWizard(element, options) {
  54. // Merge user settings with default, recursively
  55. this.options = $.extend(true, {}, defaults, options);
  56. // Main container element
  57. this.main = $(element);
  58. // Navigation bar element
  59. this.nav = this.main.children('ul');
  60. // Step anchor elements
  61. this.steps = $("li > a", this.nav);
  62. // Content container
  63. this.container = this.main.children('div');
  64. // Content pages
  65. this.pages = this.container.children('div');
  66. // Active step index
  67. this.current_index = null;
  68. // Call initial method
  69. this.init();
  70. }
  71. $.extend(SmartWizard.prototype, {
  72. init: function () {
  73. // Set the elements
  74. this._setElements();
  75. // Add toolbar
  76. this._setToolbar();
  77. // Assign plugin events
  78. this._setEvents();
  79. var idx = this.options.selected;
  80. // Get selected step from the url
  81. if (this.options.useURLhash) {
  82. // Get step number from url hash if available
  83. var hash = window.location.hash;
  84. if (hash && hash.length > 0) {
  85. var elm = $("a[href*='" + hash + "']", this.nav);
  86. if (elm.length > 0) {
  87. var id = this.steps.index(elm);
  88. idx = id >= 0 ? id : idx;
  89. }
  90. }
  91. }
  92. if (idx > 0 && this.options.anchorSettings.markDoneStep && this.options.anchorSettings.markAllPreviousStepsAsDone) {
  93. // Mark previous steps of the active step as done
  94. this.steps.eq(idx).parent('li').prevAll().addClass("done");
  95. }
  96. // Show the initial step
  97. this._showStep(idx);
  98. },
  99. // PRIVATE FUNCTIONS
  100. _setElements: function () {
  101. // Set the main element
  102. this.main.addClass('sw-main sw-theme-' + this.options.theme);
  103. // Set anchor elements
  104. this.nav.addClass('nav nav-tabs step-anchor'); // nav-justified nav-pills
  105. // Make the anchor clickable
  106. if (this.options.anchorSettings.enableAllAnchors !== false && this.options.anchorSettings.anchorClickable !== false) {
  107. this.steps.parent('li').addClass('clickable');
  108. }
  109. // Set content container
  110. this.container.addClass('sw-container tab-content');
  111. // Set content pages
  112. this.pages.addClass('step-content');
  113. // Disabled steps
  114. var mi = this;
  115. if (this.options.disabledSteps && this.options.disabledSteps.length > 0) {
  116. $.each(this.options.disabledSteps, function (i, n) {
  117. mi.steps.eq(n).parent('li').addClass('disabled');
  118. });
  119. }
  120. // Error steps
  121. if (this.options.errorSteps && this.options.errorSteps.length > 0) {
  122. $.each(this.options.errorSteps, function (i, n) {
  123. mi.steps.eq(n).parent('li').addClass('danger');
  124. });
  125. }
  126. // Hidden steps
  127. if (this.options.hiddenSteps && this.options.hiddenSteps.length > 0) {
  128. $.each(this.options.hiddenSteps, function (i, n) {
  129. mi.steps.eq(n).parent('li').addClass('hidden');
  130. });
  131. }
  132. return true;
  133. },
  134. _setToolbar: function () {
  135. // Skip right away if the toolbar is not enabled
  136. if (this.options.toolbarSettings.toolbarPosition === 'none') {
  137. return true;
  138. }
  139. // Create the toolbar buttons
  140. var btnNext = this.options.toolbarSettings.showNextButton !== false ? $('<button></button>').text(this.options.lang.next).addClass('btn btn-default sw-btn-next').attr('type', 'button') : null;
  141. var btnPrevious = this.options.toolbarSettings.showPreviousButton !== false ? $('<button></button>').text(this.options.lang.previous).addClass('btn btn-default sw-btn-prev').attr('type', 'button') : null;
  142. var btnGroup = $('<div></div>').addClass('btn-group navbar-btn sw-btn-group pull-' + this.options.toolbarSettings.toolbarButtonPosition).attr('role', 'group').append(btnPrevious, btnNext);
  143. // Add extra toolbar buttons
  144. var btnGroupExtra = null;
  145. if (this.options.toolbarSettings.toolbarExtraButtons && this.options.toolbarSettings.toolbarExtraButtons.length > 0) {
  146. btnGroupExtra = $('<div></div>').addClass('btn-group navbar-btn sw-btn-group-extra pull-' + this.options.toolbarSettings.toolbarButtonPosition).attr('role', 'group');
  147. $.each(this.options.toolbarSettings.toolbarExtraButtons, function (i, n) {
  148. btnGroupExtra.append(n.clone(true));
  149. });
  150. }
  151. var toolbarTop, toolbarBottom;
  152. // Append toolbar based on the position
  153. switch (this.options.toolbarSettings.toolbarPosition) {
  154. case 'top':
  155. toolbarTop = $('<nav></nav>').addClass('navbar btn-toolbar sw-toolbar sw-toolbar-top');
  156. toolbarTop.append(btnGroup);
  157. if (this.options.toolbarSettings.toolbarButtonPosition === 'left') {
  158. toolbarTop.append(btnGroupExtra);
  159. } else {
  160. toolbarTop.prepend(btnGroupExtra);
  161. }
  162. this.container.before(toolbarTop);
  163. break;
  164. case 'bottom':
  165. toolbarBottom = $('<nav></nav>').addClass('navbar btn-toolbar sw-toolbar sw-toolbar-bottom');
  166. toolbarBottom.append(btnGroup);
  167. if (this.options.toolbarSettings.toolbarButtonPosition === 'left') {
  168. toolbarBottom.append(btnGroupExtra);
  169. } else {
  170. toolbarBottom.prepend(btnGroupExtra);
  171. }
  172. this.container.after(toolbarBottom);
  173. break;
  174. case 'both':
  175. toolbarTop = $('<nav></nav>').addClass('navbar btn-toolbar sw-toolbar sw-toolbar-top');
  176. toolbarTop.append(btnGroup);
  177. if (this.options.toolbarSettings.toolbarButtonPosition === 'left') {
  178. toolbarTop.append(btnGroupExtra);
  179. } else {
  180. toolbarTop.prepend(btnGroupExtra);
  181. }
  182. this.container.before(toolbarTop);
  183. toolbarBottom = $('<nav></nav>').addClass('navbar btn-toolbar sw-toolbar sw-toolbar-bottom');
  184. toolbarBottom.append(btnGroup.clone(true));
  185. if (this.options.toolbarSettings.toolbarButtonPosition === 'left') {
  186. toolbarBottom.append(btnGroupExtra.clone(true));
  187. } else {
  188. toolbarBottom.prepend(btnGroupExtra.clone(true));
  189. }
  190. this.container.after(toolbarBottom);
  191. break;
  192. default:
  193. toolbarBottom = $('<nav></nav>').addClass('navbar btn-toolbar sw-toolbar sw-toolbar-bottom');
  194. toolbarBottom.append(btnGroup);
  195. if (this.options.toolbarSettings.toolbarButtonPosition === 'left') {
  196. toolbarBottom.append(btnGroupExtra);
  197. } else {
  198. toolbarBottom.prepend(btnGroupExtra);
  199. }
  200. this.container.after(toolbarBottom);
  201. break;
  202. }
  203. return true;
  204. },
  205. _setEvents: function () {
  206. // Anchor click event
  207. var mi = this;
  208. $(this.steps).on("click", function (e) {
  209. e.preventDefault();
  210. if (mi.options.anchorSettings.anchorClickable === false) {
  211. return true;
  212. }
  213. var idx = mi.steps.index(this);
  214. if (mi.options.anchorSettings.enableAnchorOnDoneStep === false && mi.steps.eq(idx).parent('li').hasClass('done')) {
  215. return true;
  216. }
  217. if (idx !== mi.current_index) {
  218. if (mi.options.anchorSettings.enableAllAnchors !== false && mi.options.anchorSettings.anchorClickable !== false) {
  219. mi._showStep(idx);
  220. } else {
  221. if (mi.steps.eq(idx).parent('li').hasClass('done')) {
  222. mi._showStep(idx);
  223. }
  224. }
  225. }
  226. });
  227. // Next button event
  228. $('.sw-btn-next', this.main).on("click", function (e) {
  229. e.preventDefault();
  230. if (mi.steps.index(this) !== mi.current_index) {
  231. mi._showNext();
  232. }
  233. });
  234. // Previous button event
  235. $('.sw-btn-prev', this.main).on("click", function (e) {
  236. e.preventDefault();
  237. if (mi.steps.index(this) !== mi.current_index) {
  238. mi._showPrevious();
  239. }
  240. });
  241. // Keyboard navigation event
  242. if (this.options.keyNavigation) {
  243. $(document).keyup(function (e) {
  244. mi._keyNav(e);
  245. });
  246. }
  247. // Back/forward browser button event
  248. if (this.options.backButtonSupport) {
  249. $(window).on('hashchange', function (e) {
  250. if (!mi.options.useURLhash) {
  251. return true;
  252. }
  253. if (window.location.hash) {
  254. var elm = $("a[href*='" + window.location.hash + "']", mi.nav);
  255. if (elm && elm.length > 0) {
  256. e.preventDefault();
  257. mi._showStep(mi.steps.index(elm));
  258. }
  259. }
  260. });
  261. }
  262. return true;
  263. },
  264. _showNext: function () {
  265. var si = this.current_index + 1;
  266. // Find the next not disabled step
  267. for (var i = si; i < this.steps.length; i++) {
  268. if (!this.steps.eq(i).parent('li').hasClass('disabled') && !this.steps.eq(i).parent('li').hasClass('hidden')) {
  269. si = i;
  270. break;
  271. }
  272. }
  273. if (this.steps.length <= si) {
  274. if (!this.options.cycleSteps) {
  275. return false;
  276. }
  277. si = 0;
  278. }
  279. this._showStep(si);
  280. return true;
  281. },
  282. _showPrevious: function () {
  283. var si = this.current_index - 1;
  284. // Find the previous not disabled step
  285. for (var i = si; i >= 0; i--) {
  286. if (!this.steps.eq(i).parent('li').hasClass('disabled') && !this.steps.eq(i).parent('li').hasClass('hidden')) {
  287. si = i;
  288. break;
  289. }
  290. }
  291. if (0 > si) {
  292. if (!this.options.cycleSteps) {
  293. return false;
  294. }
  295. si = this.steps.length - 1;
  296. }
  297. this._showStep(si);
  298. return true;
  299. },
  300. _showStep: function (idx) {
  301. // If step not found, skip
  302. if (!this.steps.eq(idx)) {
  303. return false;
  304. }
  305. // If current step is requested again, skip
  306. if (idx == this.current_index) {
  307. return false;
  308. }
  309. // If it is a disabled step, skip
  310. if (this.steps.eq(idx).parent('li').hasClass('disabled') || this.steps.eq(idx).parent('li').hasClass('hidden')) {
  311. return false;
  312. }
  313. // Load step content
  314. this._loadStepContent(idx);
  315. return true;
  316. },
  317. _loadStepContent: function (idx) {
  318. var mi = this;
  319. // Get current step elements
  320. var curTab = this.steps.eq(this.current_index);
  321. // Get the direction of step navigation
  322. var stepDirection = '';
  323. var elm = this.steps.eq(idx);
  324. var contentURL = elm.data('content-url') && elm.data('content-url').length > 0 ? elm.data('content-url') : this.options.contentURL;
  325. if (this.current_index !== null && this.current_index !== idx) {
  326. stepDirection = this.current_index < idx ? "forward" : "backward";
  327. }
  328. // Trigger "leaveStep" event
  329. if (this.current_index !== null && this._triggerEvent("leaveStep", [curTab, this.current_index, stepDirection]) === false) {
  330. return false;
  331. }
  332. if (contentURL && contentURL.length > 0 && (!elm.data('has-content') || !this.options.contentCache)) {
  333. // Get ajax content and then show step
  334. var selPage = elm.length > 0 ? $(elm.attr("href"), this.main) : null;
  335. var ajaxSettings = $.extend(true, {}, {
  336. url: contentURL,
  337. type: "POST",
  338. data: { step_number: idx },
  339. dataType: "text",
  340. beforeSend: function () {
  341. elm.parent('li').addClass('loading');
  342. },
  343. error: function (jqXHR, status, message) {
  344. elm.parent('li').removeClass('loading');
  345. $.error(message);
  346. },
  347. success: function (res) {
  348. if (res && res.length > 0) {
  349. elm.data('has-content', true);
  350. selPage.html(res);
  351. }
  352. elm.parent('li').removeClass('loading');
  353. mi._transitPage(idx);
  354. }
  355. }, this.options.ajaxSettings);
  356. $.ajax(ajaxSettings);
  357. } else {
  358. // Show step
  359. this._transitPage(idx);
  360. }
  361. return true;
  362. },
  363. _transitPage: function (idx) {
  364. var mi = this;
  365. // Get current step elements
  366. var curTab = this.steps.eq(this.current_index);
  367. var curPage = curTab.length > 0 ? $(curTab.attr("href"), this.main) : null;
  368. // Get step to show elements
  369. var selTab = this.steps.eq(idx);
  370. var selPage = selTab.length > 0 ? $(selTab.attr("href"), this.main) : null;
  371. // Get the direction of step navigation
  372. var stepDirection = '';
  373. if (this.current_index !== null && this.current_index !== idx) {
  374. stepDirection = this.current_index < idx ? "forward" : "backward";
  375. }
  376. var stepPosition = 'middle';
  377. if (idx === 0) {
  378. stepPosition = 'first';
  379. } else if (idx === this.steps.length - 1) {
  380. stepPosition = 'final';
  381. }
  382. this.options.transitionEffect = this.options.transitionEffect.toLowerCase();
  383. this.pages.finish();
  384. if (this.options.transitionEffect === 'slide') {
  385. // normal slide
  386. if (curPage && curPage.length > 0) {
  387. curPage.slideUp('fast', this.options.transitionEasing, function () {
  388. selPage.slideDown(mi.options.transitionSpeed, mi.options.transitionEasing);
  389. });
  390. } else {
  391. selPage.slideDown(this.options.transitionSpeed, this.options.transitionEasing);
  392. }
  393. } else if (this.options.transitionEffect === 'fade') {
  394. // normal fade
  395. if (curPage && curPage.length > 0) {
  396. curPage.fadeOut('fast', this.options.transitionEasing, function () {
  397. selPage.fadeIn('fast', mi.options.transitionEasing, function () {
  398. $(this).show();
  399. });
  400. });
  401. } else {
  402. selPage.fadeIn(this.options.transitionSpeed, this.options.transitionEasing, function () {
  403. $(this).show();
  404. });
  405. }
  406. } else {
  407. if (curPage && curPage.length > 0) {
  408. curPage.hide();
  409. }
  410. selPage.show();
  411. }
  412. // Change the url hash to new step
  413. this._setURLHash(selTab.attr("href"));
  414. // Update controls
  415. this._setAnchor(idx);
  416. // Set the buttons based on the step
  417. this._setButtons(idx);
  418. // Fix height with content
  419. this._fixHeight(idx);
  420. // Update the current index
  421. this.current_index = idx;
  422. // Trigger "showStep" event
  423. this._triggerEvent("showStep", [selTab, this.current_index, stepDirection, stepPosition]);
  424. return true;
  425. },
  426. _setAnchor: function (idx) {
  427. // Current step anchor > Remove other classes and add done class
  428. this.steps.eq(this.current_index).parent('li').removeClass("active danger loading");
  429. if (this.options.anchorSettings.markDoneStep !== false && this.current_index !== null) {
  430. this.steps.eq(this.current_index).parent('li').addClass("done");
  431. if (this.options.anchorSettings.removeDoneStepOnNavigateBack !== false) {
  432. this.steps.eq(idx).parent('li').nextAll().removeClass("done");
  433. }
  434. }
  435. // Next step anchor > Remove other classes and add active class
  436. this.steps.eq(idx).parent('li').removeClass("done danger loading").addClass("active");
  437. return true;
  438. },
  439. _setButtons: function (idx) {
  440. // Previous/Next Button enable/disable based on step
  441. if (!this.options.cycleSteps) {
  442. if (0 >= idx) {
  443. $('.sw-btn-prev', this.main).addClass("disabled");
  444. } else {
  445. $('.sw-btn-prev', this.main).removeClass("disabled");
  446. }
  447. if (this.steps.length - 1 <= idx) {
  448. $('.sw-btn-next', this.main).addClass("disabled");
  449. } else {
  450. $('.sw-btn-next', this.main).removeClass("disabled");
  451. }
  452. }
  453. return true;
  454. },
  455. // HELPER FUNCTIONS
  456. _keyNav: function (e) {
  457. var mi = this;
  458. // Keyboard navigation
  459. switch (e.which) {
  460. case 37:
  461. // left
  462. mi._showPrevious();
  463. e.preventDefault();
  464. break;
  465. case 39:
  466. // right
  467. mi._showNext();
  468. e.preventDefault();
  469. break;
  470. default:
  471. return; // exit this handler for other keys
  472. }
  473. },
  474. _fixHeight: function (idx) {
  475. // Auto adjust height of the container
  476. if (this.options.autoAdjustHeight) {
  477. var selPage = this.steps.eq(idx).length > 0 ? $(this.steps.eq(idx).attr("href"), this.main) : null;
  478. this.container.finish().animate({ minHeight: selPage.outerHeight() }, this.options.transitionSpeed, function () {});
  479. }
  480. return true;
  481. },
  482. _triggerEvent: function (name, params) {
  483. // Trigger an event
  484. var e = $.Event(name);
  485. this.main.trigger(e, params);
  486. if (e.isDefaultPrevented()) {
  487. return false;
  488. }
  489. return e.result;
  490. },
  491. _setURLHash: function (hash) {
  492. if (this.options.showStepURLhash && window.location.hash !== hash) {
  493. window.location.hash = hash;
  494. }
  495. },
  496. // PUBLIC FUNCTIONS
  497. theme: function (v) {
  498. if (this.options.theme === v) {
  499. return false;
  500. }
  501. this.main.removeClass('sw-theme-' + this.options.theme);
  502. this.options.theme = v;
  503. this.main.addClass('sw-theme-' + this.options.theme);
  504. // Trigger "themeChanged" event
  505. this._triggerEvent("themeChanged", [this.options.theme]);
  506. },
  507. next: function () {
  508. this._showNext();
  509. },
  510. prev: function () {
  511. this._showPrevious();
  512. },
  513. reset: function () {
  514. // Trigger "beginReset" event
  515. if (this._triggerEvent("beginReset") === false) {
  516. return false;
  517. }
  518. // Reset all elements and classes
  519. this.container.stop(true);
  520. this.pages.stop(true);
  521. this.pages.hide();
  522. this.current_index = null;
  523. this._setURLHash(this.steps.eq(this.options.selected).attr("href"));
  524. $(".sw-toolbar", this.main).remove();
  525. this.steps.removeClass();
  526. this.steps.parents('li').removeClass();
  527. this.steps.data('has-content', false);
  528. this.init();
  529. // Trigger "endReset" event
  530. this._triggerEvent("endReset");
  531. },
  532. stepState: function (stepArray, state) {
  533. var mi = this;
  534. stepArray = $.isArray(stepArray) ? stepArray : [stepArray];
  535. var selSteps = $.grep(this.steps, function (n, i) {
  536. return $.inArray(i, stepArray) !== -1 && i !== mi.current_index;
  537. });
  538. if (selSteps && selSteps.length > 0) {
  539. switch (state) {
  540. case 'disable':
  541. $(selSteps).parents('li').addClass('disabled');
  542. break;
  543. case 'enable':
  544. $(selSteps).parents('li').removeClass('disabled');
  545. break;
  546. case 'hide':
  547. $(selSteps).parents('li').addClass('hidden');
  548. break;
  549. case 'show':
  550. $(selSteps).parents('li').removeClass('hidden');
  551. break;
  552. }
  553. }
  554. }
  555. });
  556. // Wrapper for the plugin
  557. $.fn.smartWizard = function (options) {
  558. var args = arguments;
  559. var instance;
  560. if (options === undefined || typeof options === 'object') {
  561. return this.each(function () {
  562. if (!$.data(this, "smartWizard")) {
  563. $.data(this, "smartWizard", new SmartWizard(this, options));
  564. }
  565. });
  566. } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
  567. instance = $.data(this[0], 'smartWizard');
  568. if (options === 'destroy') {
  569. $.data(this, 'smartWizard', null);
  570. }
  571. if (instance instanceof SmartWizard && typeof instance[options] === 'function') {
  572. return instance[options].apply(instance, Array.prototype.slice.call(args, 1));
  573. } else {
  574. return this;
  575. }
  576. }
  577. };
  578. })(jQuery, window, document);