jquery.smartWizard.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * SmartWizard 3.3.1 plugin
  3. * jQuery Wizard control Plugin
  4. * by Dipu
  5. *
  6. * Refactored and extended:
  7. * https://github.com/mstratman/jQuery-Smart-Wizard
  8. *
  9. * Original URLs:
  10. * http://www.techlaboratory.net
  11. * http://tech-laboratory.blogspot.com
  12. */
  13. function SmartWizard(target, options) {
  14. this.target = target;
  15. this.options = options;
  16. this.curStepIdx = options.selected;
  17. this.steps = $(target).children("ul").children("li").children("a"); // Get all anchors
  18. this.contentWidth = 0;
  19. this.msgBox = $('<div class="msgBox"><div class="content"></div><a href="#" class="close">X</a></div>');
  20. this.elmStepContainer = $('<div></div>').addClass("stepContainer");
  21. this.loader = $('<div>Loading</div>').addClass("loader");
  22. this.buttons = {
  23. next : $('<a>'+options.labelNext+'</a>').attr("href","#").addClass("buttonNext"),
  24. previous : $('<a>'+options.labelPrevious+'</a>').attr("href","#").addClass("buttonPrevious"),
  25. finish : $('<a>'+options.labelFinish+'</a>').attr("href","#").addClass("buttonFinish")
  26. };
  27. /*
  28. * Private functions
  29. */
  30. var _init = function($this) {
  31. var elmActionBar = $('<div></div>').addClass("actionBar");
  32. elmActionBar.append($this.msgBox);
  33. $('.close',$this.msgBox).click(function() {
  34. $this.msgBox.fadeOut("normal");
  35. return false;
  36. });
  37. var allDivs = $this.target.children('div');
  38. $this.target.children('ul').addClass("anchor");
  39. allDivs.addClass("content");
  40. // highlight steps with errors
  41. if($this.options.errorSteps && $this.options.errorSteps.length>0){
  42. $.each($this.options.errorSteps, function(i, n){
  43. $this.setError({ stepnum: n, iserror:true });
  44. });
  45. }
  46. $this.elmStepContainer.append(allDivs);
  47. elmActionBar.append($this.loader);
  48. $this.target.append($this.elmStepContainer);
  49. elmActionBar.append($this.buttons.finish)
  50. .append($this.buttons.next)
  51. .append($this.buttons.previous);
  52. $this.target.append(elmActionBar);
  53. this.contentWidth = $this.elmStepContainer.width();
  54. $($this.buttons.next).click(function() {
  55. $this.goForward();
  56. return false;
  57. });
  58. $($this.buttons.previous).click(function() {
  59. $this.goBackward();
  60. return false;
  61. });
  62. $($this.buttons.finish).click(function() {
  63. if(!$(this).hasClass('buttonDisabled')){
  64. if($.isFunction($this.options.onFinish)) {
  65. var context = { fromStep: $this.curStepIdx + 1 };
  66. if(!$this.options.onFinish.call(this,$($this.steps), context)){
  67. return false;
  68. }
  69. }else{
  70. var frm = $this.target.parents('form');
  71. if(frm && frm.length){
  72. frm.submit();
  73. }
  74. }
  75. }
  76. return false;
  77. });
  78. $($this.steps).bind("click", function(e){
  79. if($this.steps.index(this) == $this.curStepIdx){
  80. return false;
  81. }
  82. var nextStepIdx = $this.steps.index(this);
  83. var isDone = $this.steps.eq(nextStepIdx).attr("isDone") - 0;
  84. if(isDone == 1){
  85. _loadContent($this, nextStepIdx);
  86. }
  87. return false;
  88. });
  89. // Enable keyboard navigation
  90. if($this.options.keyNavigation){
  91. $(document).keyup(function(e){
  92. if(e.which==39){ // Right Arrow
  93. $this.goForward();
  94. }else if(e.which==37){ // Left Arrow
  95. $this.goBackward();
  96. }
  97. });
  98. }
  99. // Prepare the steps
  100. _prepareSteps($this);
  101. // Show the first slected step
  102. _loadContent($this, $this.curStepIdx);
  103. };
  104. var _prepareSteps = function($this) {
  105. if(! $this.options.enableAllSteps){
  106. $($this.steps, $this.target).removeClass("selected").removeClass("done").addClass("disabled");
  107. $($this.steps, $this.target).attr("isDone",0);
  108. }else{
  109. $($this.steps, $this.target).removeClass("selected").removeClass("disabled").addClass("done");
  110. $($this.steps, $this.target).attr("isDone",1);
  111. }
  112. $($this.steps, $this.target).each(function(i){
  113. $($(this).attr("href").replace(/^.+#/, '#'), $this.target).hide();
  114. $(this).attr("rel",i+1);
  115. });
  116. };
  117. var _step = function ($this, selStep) {
  118. return $(
  119. $(selStep, $this.target).attr("href").replace(/^.+#/, '#'),
  120. $this.target
  121. );
  122. };
  123. var _loadContent = function($this, stepIdx) {
  124. var selStep = $this.steps.eq(stepIdx);
  125. var ajaxurl = $this.options.contentURL;
  126. var ajaxurl_data = $this.options.contentURLData;
  127. var hasContent = selStep.data('hasContent');
  128. var stepNum = stepIdx+1;
  129. if (ajaxurl && ajaxurl.length>0) {
  130. if ($this.options.contentCache && hasContent) {
  131. _showStep($this, stepIdx);
  132. } else {
  133. var ajax_args = {
  134. url: ajaxurl,
  135. type: "POST",
  136. data: ({step_number : stepNum}),
  137. dataType: "text",
  138. beforeSend: function(){
  139. $this.loader.show();
  140. },
  141. error: function(){
  142. $this.loader.hide();
  143. },
  144. success: function(res){
  145. $this.loader.hide();
  146. if(res && res.length>0){
  147. selStep.data('hasContent',true);
  148. _step($this, selStep).html(res);
  149. _showStep($this, stepIdx);
  150. }
  151. }
  152. };
  153. if (ajaxurl_data) {
  154. ajax_args = $.extend(ajax_args, ajaxurl_data(stepNum));
  155. }
  156. $.ajax(ajax_args);
  157. }
  158. }else{
  159. _showStep($this,stepIdx);
  160. }
  161. };
  162. var _showStep = function($this, stepIdx) {
  163. var selStep = $this.steps.eq(stepIdx);
  164. var curStep = $this.steps.eq($this.curStepIdx);
  165. if(stepIdx != $this.curStepIdx){
  166. if($.isFunction($this.options.onLeaveStep)) {
  167. var context = { fromStep: $this.curStepIdx+1, toStep: stepIdx+1 };
  168. if (! $this.options.onLeaveStep.call($this,$(curStep), context)){
  169. return false;
  170. }
  171. }
  172. }
  173. $this.elmStepContainer.height(_step($this, selStep).outerHeight());
  174. var prevCurStepIdx = $this.curStepIdx;
  175. $this.curStepIdx = stepIdx;
  176. if ($this.options.transitionEffect == 'slide'){
  177. _step($this, curStep).slideUp("fast",function(e){
  178. _step($this, selStep).slideDown("fast");
  179. _setupStep($this,curStep,selStep);
  180. });
  181. } else if ($this.options.transitionEffect == 'fade'){
  182. _step($this, curStep).fadeOut("fast",function(e){
  183. _step($this, selStep).fadeIn("fast");
  184. _setupStep($this,curStep,selStep);
  185. });
  186. } else if ($this.options.transitionEffect == 'slideleft'){
  187. var nextElmLeft = 0;
  188. var nextElmLeft1 = null;
  189. var nextElmLeft = null;
  190. var curElementLeft = 0;
  191. if(stepIdx > prevCurStepIdx){
  192. nextElmLeft1 = $this.contentWidth + 10;
  193. nextElmLeft2 = 0;
  194. curElementLeft = 0 - _step($this, curStep).outerWidth();
  195. } else {
  196. nextElmLeft1 = 0 - _step($this, selStep).outerWidth() + 20;
  197. nextElmLeft2 = 0;
  198. curElementLeft = 10 + _step($this, curStep).outerWidth();
  199. }
  200. if (stepIdx == prevCurStepIdx) {
  201. nextElmLeft1 = $($(selStep, $this.target).attr("href"), $this.target).outerWidth() + 20;
  202. nextElmLeft2 = 0;
  203. curElementLeft = 0 - $($(curStep, $this.target).attr("href"), $this.target).outerWidth();
  204. } else {
  205. $($(curStep, $this.target).attr("href"), $this.target).animate({left:curElementLeft},"fast",function(e){
  206. $($(curStep, $this.target).attr("href"), $this.target).hide();
  207. });
  208. }
  209. _step($this, selStep).css("left",nextElmLeft1).show().animate({left:nextElmLeft2},"fast",function(e){
  210. _setupStep($this,curStep,selStep);
  211. });
  212. } else {
  213. _step($this, curStep).hide();
  214. _step($this, selStep).show();
  215. _setupStep($this,curStep,selStep);
  216. }
  217. return true;
  218. };
  219. var _setupStep = function($this, curStep, selStep) {
  220. $(curStep, $this.target).removeClass("selected");
  221. $(curStep, $this.target).addClass("done");
  222. $(selStep, $this.target).removeClass("disabled");
  223. $(selStep, $this.target).removeClass("done");
  224. $(selStep, $this.target).addClass("selected");
  225. $(selStep, $this.target).attr("isDone",1);
  226. _adjustButton($this);
  227. if($.isFunction($this.options.onShowStep)) {
  228. var context = { fromStep: parseInt($(curStep).attr('rel')), toStep: parseInt($(selStep).attr('rel')) };
  229. if(! $this.options.onShowStep.call(this,$(selStep),context)){
  230. return false;
  231. }
  232. }
  233. if ($this.options.noForwardJumping) {
  234. // +2 == +1 (for index to step num) +1 (for next step)
  235. for (var i = $this.curStepIdx + 2; i <= $this.steps.length; i++) {
  236. $this.disableStep(i);
  237. }
  238. }
  239. };
  240. var _adjustButton = function($this) {
  241. if (! $this.options.cycleSteps){
  242. if (0 >= $this.curStepIdx) {
  243. $($this.buttons.previous).addClass("buttonDisabled");
  244. if ($this.options.hideButtonsOnDisabled) {
  245. $($this.buttons.previous).hide();
  246. }
  247. }else{
  248. $($this.buttons.previous).removeClass("buttonDisabled");
  249. if ($this.options.hideButtonsOnDisabled) {
  250. $($this.buttons.previous).show();
  251. }
  252. }
  253. if (($this.steps.length-1) <= $this.curStepIdx){
  254. $($this.buttons.next).addClass("buttonDisabled");
  255. if ($this.options.hideButtonsOnDisabled) {
  256. $($this.buttons.next).hide();
  257. }
  258. }else{
  259. $($this.buttons.next).removeClass("buttonDisabled");
  260. if ($this.options.hideButtonsOnDisabled) {
  261. $($this.buttons.next).show();
  262. }
  263. }
  264. }
  265. // Finish Button
  266. if (! $this.steps.hasClass('disabled') || $this.options.enableFinishButton){
  267. $($this.buttons.finish).removeClass("buttonDisabled");
  268. if ($this.options.hideButtonsOnDisabled) {
  269. $($this.buttons.finish).show();
  270. }
  271. }else{
  272. $($this.buttons.finish).addClass("buttonDisabled");
  273. if ($this.options.hideButtonsOnDisabled) {
  274. $($this.buttons.finish).hide();
  275. }
  276. }
  277. };
  278. /*
  279. * Public methods
  280. */
  281. SmartWizard.prototype.goForward = function(){
  282. var nextStepIdx = this.curStepIdx + 1;
  283. if (this.steps.length <= nextStepIdx){
  284. if (! this.options.cycleSteps){
  285. return false;
  286. }
  287. nextStepIdx = 0;
  288. }
  289. _loadContent(this, nextStepIdx);
  290. };
  291. SmartWizard.prototype.goBackward = function(){
  292. var nextStepIdx = this.curStepIdx-1;
  293. if (0 > nextStepIdx){
  294. if (! this.options.cycleSteps){
  295. return false;
  296. }
  297. nextStepIdx = this.steps.length - 1;
  298. }
  299. _loadContent(this, nextStepIdx);
  300. };
  301. SmartWizard.prototype.goToStep = function(stepNum){
  302. var stepIdx = stepNum - 1;
  303. if (stepIdx >= 0 && stepIdx < this.steps.length) {
  304. _loadContent(this, stepIdx);
  305. }
  306. };
  307. SmartWizard.prototype.enableStep = function(stepNum) {
  308. var stepIdx = stepNum - 1;
  309. if (stepIdx == this.curStepIdx || stepIdx < 0 || stepIdx >= this.steps.length) {
  310. return false;
  311. }
  312. var step = this.steps.eq(stepIdx);
  313. $(step, this.target).attr("isDone",1);
  314. $(step, this.target).removeClass("disabled").removeClass("selected").addClass("done");
  315. }
  316. SmartWizard.prototype.disableStep = function(stepNum) {
  317. var stepIdx = stepNum - 1;
  318. if (stepIdx == this.curStepIdx || stepIdx < 0 || stepIdx >= this.steps.length) {
  319. return false;
  320. }
  321. var step = this.steps.eq(stepIdx);
  322. $(step, this.target).attr("isDone",0);
  323. $(step, this.target).removeClass("done").removeClass("selected").addClass("disabled");
  324. }
  325. SmartWizard.prototype.currentStep = function() {
  326. return this.curStepIdx + 1;
  327. }
  328. SmartWizard.prototype.showMessage = function (msg) {
  329. $('.content', this.msgBox).html(msg);
  330. this.msgBox.show();
  331. }
  332. SmartWizard.prototype.hideMessage = function () {
  333. this.msgBox.fadeOut("normal");
  334. }
  335. SmartWizard.prototype.showError = function(stepnum) {
  336. this.setError(stepnum, true);
  337. }
  338. SmartWizard.prototype.hideError = function(stepnum) {
  339. this.setError(stepnum, false);
  340. }
  341. SmartWizard.prototype.setError = function(stepnum,iserror) {
  342. if (typeof stepnum == "object") {
  343. iserror = stepnum.iserror;
  344. stepnum = stepnum.stepnum;
  345. }
  346. if (iserror){
  347. $(this.steps.eq(stepnum-1), this.target).addClass('error')
  348. }else{
  349. $(this.steps.eq(stepnum-1), this.target).removeClass("error");
  350. }
  351. }
  352. SmartWizard.prototype.fixHeight = function(){
  353. var height = 0;
  354. var selStep = this.steps.eq(this.curStepIdx);
  355. var stepContainer = _step(this, selStep);
  356. stepContainer.children().each(function() {
  357. height += $(this).outerHeight();
  358. });
  359. // These values (5 and 20) are experimentally chosen.
  360. stepContainer.height(height + 5);
  361. this.elmStepContainer.height(height + 20);
  362. }
  363. _init(this);
  364. };
  365. (function($){
  366. $.fn.smartWizard = function(method) {
  367. var args = arguments;
  368. var rv = undefined;
  369. var allObjs = this.each(function() {
  370. var wiz = $(this).data('smartWizard');
  371. if (typeof method == 'object' || ! method || ! wiz) {
  372. var options = $.extend({}, $.fn.smartWizard.defaults, method || {});
  373. if (! wiz) {
  374. wiz = new SmartWizard($(this), options);
  375. $(this).data('smartWizard', wiz);
  376. }
  377. } else {
  378. if (typeof SmartWizard.prototype[method] == "function") {
  379. rv = SmartWizard.prototype[method].apply(wiz, Array.prototype.slice.call(args, 1));
  380. return rv;
  381. } else {
  382. $.error('Method ' + method + ' does not exist on jQuery.smartWizard');
  383. }
  384. }
  385. });
  386. if (rv === undefined) {
  387. return allObjs;
  388. } else {
  389. return rv;
  390. }
  391. };
  392. // Default Properties and Events
  393. $.fn.smartWizard.defaults = {
  394. selected: 0, // Selected Step, 0 = first step
  395. keyNavigation: true, // Enable/Disable key navigation(left and right keys are used if enabled)
  396. enableAllSteps: false,
  397. transitionEffect: 'fade', // Effect on navigation, none/fade/slide/slideleft
  398. contentURL:null, // content url, Enables Ajax content loading
  399. contentCache:true, // cache step contents, if false content is fetched always from ajax url
  400. cycleSteps: false, // cycle step navigation
  401. enableFinishButton: false, // make finish button enabled always
  402. hideButtonsOnDisabled: false, // when the previous/next/finish buttons are disabled, hide them instead?
  403. errorSteps:[], // Array Steps with errors
  404. labelNext:'Next',
  405. labelPrevious:'Previous',
  406. labelFinish:'Finish',
  407. noForwardJumping: false,
  408. onLeaveStep: null, // triggers when leaving a step
  409. onShowStep: null, // triggers when showing a step
  410. onFinish: null // triggers when Finish button is clicked
  411. };
  412. })(jQuery);