|
1 | 1 | /*! |
2 | | - * jQuery Steps Plugin v0.9.1 - A powerful jQuery wizard plugin that supports accessibility and HTML5 |
| 2 | + * jQuery Steps Plugin v0.9.2 - A powerful jQuery wizard plugin that supports accessibility and HTML5 |
3 | 3 | * https://github.com/rstaib/jquery-steps |
4 | 4 | * |
5 | 5 | * Copyright (c) 2013 Rafael J. Staib |
|
13 | 13 |
|
14 | 14 | /* |
15 | 15 | * TODOs: |
16 | | - * - Loading Animation (Spinner) |
| 16 | + * - Add tests and styles for loading animation (Spinner) |
17 | 17 | * - Add tests for add, insert and remove |
18 | 18 | * - Shrink the comprehensive code |
19 | 19 | * |
|
246 | 246 | current: "current step:", /* For Accessability reasons */ |
247 | 247 | finish: "Finish", |
248 | 248 | next: "Next", |
249 | | - previous: "Previous" |
| 249 | + previous: "Previous", |
| 250 | + loading: "Loading ..." |
250 | 251 | } |
251 | 252 | }; |
252 | 253 |
|
|
286 | 287 | **/ |
287 | 288 | $.fn.steps.getStep = function (index) |
288 | 289 | { |
289 | | - var $this = $(this); |
290 | | - var state = $this.data("state"); |
| 290 | + var wizard = $(this), |
| 291 | + state = wizard.data("state"); |
291 | 292 |
|
292 | | - return (index === state.currentStep) ? state.currentStep : getStepProperties($this, index); |
| 293 | + return (index === state.currentStep) ? state.currentStep : getStepProperties(wizard, index); |
293 | 294 | }; |
294 | 295 |
|
295 | 296 | /** |
|
300 | 301 | **/ |
301 | 302 | $.fn.steps.next = function () |
302 | 303 | { |
303 | | - var $this = $(this); |
304 | | - return actionClick($this, $this.data("state").currentIndex + 1); |
| 304 | + var wizard = $(this); |
| 305 | + |
| 306 | + return actionClick(wizard, wizard.data("state").currentIndex + 1); |
305 | 307 | }; |
306 | 308 |
|
307 | 309 | /** |
|
312 | 314 | **/ |
313 | 315 | $.fn.steps.previous = function () |
314 | 316 | { |
315 | | - var $this = $(this); |
316 | | - return actionClick($this, $this.data("state").currentIndex - 1); |
| 317 | + var wizard = $(this); |
| 318 | + |
| 319 | + return actionClick(wizard, wizard.data("state").currentIndex - 1); |
317 | 320 | }; |
318 | 321 |
|
319 | 322 | /** |
|
335 | 338 | **/ |
336 | 339 | $.fn.steps.finish = function () |
337 | 340 | { |
338 | | - var $this = $(this), |
339 | | - options = $this.data("options"), |
340 | | - state = $this.data("state"), |
341 | | - currentStep = $(".steps li:eq(" + state.currentIndex + ")", $this); |
| 341 | + var wizard = $(this), |
| 342 | + options = wizard.data("options"), |
| 343 | + state = wizard.data("state"), |
| 344 | + currentStep = $(".steps li:eq(" + state.currentIndex + ")", wizard); |
342 | 345 |
|
343 | | - if ($this.triggerHandler("finishing", [state.currentIndex])) |
| 346 | + if (wizard.triggerHandler("finishing", [state.currentIndex])) |
344 | 347 | { |
345 | 348 | currentStep.addClass("done").removeClass("error"); |
346 | | - |
347 | | - $this.triggerHandler("finished", [state.currentIndex]); |
| 349 | + wizard.triggerHandler("finished", [state.currentIndex]); |
348 | 350 | } |
349 | 351 | else |
350 | 352 | { |
|
587 | 589 | var oldIndex = state.currentIndex; |
588 | 590 | if (wizard.triggerHandler("stepChanging", [state.currentIndex, index])) |
589 | 591 | { |
| 592 | + if (state.asyncRequest != null) |
| 593 | + { |
| 594 | + state.asyncRequest.abort(); |
| 595 | + } |
| 596 | + |
590 | 597 | // Save new state |
591 | 598 | state.currentIndex = index; |
592 | 599 | state.currentStep = getStepProperties(wizard, index); |
|
672 | 679 | **/ |
673 | 680 | function transform(wizard) |
674 | 681 | { |
675 | | - var options = wizard.data("options"); |
676 | | - var state = wizard.data("state"); |
677 | | - |
678 | | - wizard.addClass("wizard"); |
| 682 | + var options = wizard.data("options"), |
| 683 | + state = wizard.data("state"), |
| 684 | + contentWrapper = $(document.createElement(options.contentContainerTag)).addClass("content"); |
679 | 685 |
|
680 | | - var contentWrapper = $(document.createElement(options.contentContainerTag)).addClass("content"); |
681 | 686 | contentWrapper.html(wizard.html()); |
682 | | - wizard.empty(); |
683 | | - wizard.append(contentWrapper); |
684 | | - |
685 | | - var stepTitles = contentWrapper.children(options.headerTag); |
| 687 | + wizard.addClass("wizard").empty().append(contentWrapper); |
686 | 688 |
|
687 | | - var stepContents = contentWrapper.children(options.bodyTag).addClass("body"); |
| 689 | + var stepTitles = contentWrapper.children(options.headerTag), |
| 690 | + stepContents = contentWrapper.children(options.bodyTag).addClass("body"); |
688 | 691 |
|
689 | 692 | // hides all contents except the defined start content |
690 | 693 | stepContents.not(":eq(" + options.startIndex + ")").hide(); |
|
699 | 702 | throw new Error("One or more corresponding step titles are missing."); |
700 | 703 | } |
701 | 704 |
|
702 | | - var stepsWrapper = $(document.createElement(options.stepsContainerTag)).addClass("steps"); |
| 705 | + var stepsWrapper = $(document.createElement(options.stepsContainerTag)) |
| 706 | + .addClass("steps").append($(document.createElement("ol"))); |
703 | 707 | wizard.prepend(stepsWrapper); |
704 | 708 |
|
705 | | - stepsWrapper.append($(document.createElement("ol"))); |
706 | | - |
707 | 709 | stepTitles.each(function (index) |
708 | 710 | { |
709 | 711 | transformTitle(wizard, $(this), index); |
|
715 | 717 |
|
716 | 718 | if (options.enablePagination) |
717 | 719 | { |
718 | | - var actionWrapper = $(document.createElement(options.actionContainerTag)).addClass("actions"); |
| 720 | + var actionCollection = $(document.createElement("ul")), |
| 721 | + actionWrapper = $(document.createElement(options.actionContainerTag)) |
| 722 | + .addClass("actions").append(actionCollection); |
719 | 723 | wizard.append(actionWrapper); |
720 | 724 |
|
721 | | - var actionCollection = $(document.createElement("ul")); |
722 | | - actionWrapper.append(actionCollection); |
723 | | - |
724 | 725 | if (!options.forceMoveForward) |
725 | 726 | { |
726 | 727 | actionCollection.append($("<li><a href=\"#previous\">" + options.labels.previous + "</a></li>")); |
|
805 | 806 |
|
806 | 807 | if (!options.enableContentCache || !state.currentStep.contentLoaded) |
807 | 808 | { |
808 | | - var currentStepContent; |
809 | 809 | switch (getValidEnumValue($.fn.steps.contentMode, state.currentStep.contentMode)) |
810 | 810 | { |
811 | 811 | case $.fn.steps.contentMode.iframe: |
812 | | - currentStepContent = $(".content > .body", wizard).eq(state.currentIndex); |
813 | | - currentStepContent.html($("<iframe src=\"" + state.currentStep.contentUrl + "\" />")); |
814 | | - currentStepContent.data("loaded", "1"); |
| 812 | + $(".content > .body", wizard).eq(state.currentIndex) |
| 813 | + .html($("<iframe src=\"" + state.currentStep.contentUrl + "\" />")) |
| 814 | + .data("loaded", "1"); |
815 | 815 | break; |
816 | 816 |
|
817 | 817 | case $.fn.steps.contentMode.async: |
818 | | - currentStepContent = $(".content > .body", wizard).eq(state.currentIndex); |
819 | | - currentStepContent.empty(); |
820 | | - $.ajax({ url: state.currentStep.contentUrl, cache: false }).done(function (data) |
821 | | - { |
822 | | - currentStepContent.html(data); |
823 | | - currentStepContent.data("loaded", "1"); |
824 | | - }); |
| 818 | + var currentStepContent = $(".content > .body", wizard).eq(state.currentIndex) |
| 819 | + .empty().append(renderTemplate(options.loadingTemplate, { text: options.labels.loading })); |
| 820 | + $.ajax({ url: state.currentStep.contentUrl, cache: false }) |
| 821 | + .done(function (data) |
| 822 | + { |
| 823 | + currentStepContent.empty().html(data).data("loaded", "1"); |
| 824 | + }); |
825 | 825 | break; |
826 | 826 | } |
827 | 827 | } |
|
853 | 853 | */ |
854 | 854 | function refreshActionState(wizard) |
855 | 855 | { |
856 | | - var options = wizard.data("options"); |
857 | | - var state = wizard.data("state"); |
| 856 | + var options = wizard.data("options"), |
| 857 | + state = wizard.data("state"); |
858 | 858 |
|
859 | 859 | if (options.enablePagination) |
860 | 860 | { |
861 | | - var finish = $(".actions a[href$='#finish']", wizard).parent(); |
862 | | - var next = $(".actions a[href$='#next']", wizard).parent(); |
| 861 | + var finish = $(".actions a[href$='#finish']", wizard).parent(), |
| 862 | + next = $(".actions a[href$='#next']", wizard).parent(); |
863 | 863 |
|
864 | 864 | if (!options.forceMoveForward) |
865 | 865 | { |
|
929 | 929 | function renderTemplate(template, substitutes) |
930 | 930 | { |
931 | 931 | var matches = template.match(/#([a-z]*)#/gi); |
| 932 | + |
932 | 933 | for (var i = 0; i < matches.length; i++) |
933 | 934 | { |
934 | | - var match = matches[i], key = match.substring(1, match.length - 1); |
| 935 | + var match = matches[i], |
| 936 | + key = match.substring(1, match.length - 1); |
935 | 937 | template = template.replace(match, getSubstitute(substitutes, key)); |
936 | 938 | } |
937 | 939 |
|
|
968 | 970 | */ |
969 | 971 | function getStepProperties(wizard, index) |
970 | 972 | { |
971 | | - var options = wizard.data("options"); |
972 | | - var $header = $(".content > .title:eq(" + index + ")", wizard); |
973 | | - var $content = $header.next(".body"); |
974 | | - var mode = ($content.data("mode") == null) ? $.fn.steps.contentMode.html : |
975 | | - getValidEnumValue($.fn.steps.contentMode, (/^\s*$/.test($content.data("mode")) || isNaN($content.data("mode"))) ? |
976 | | - $content.data("mode") : Number($content.data("mode"))); |
977 | | - var contentUrl = (mode === $.fn.steps.contentMode.html || $content.data("url") === undefined) ? |
978 | | - "" : $content.data("url"); |
979 | | - var contentLoaded = (mode !== $.fn.steps.contentMode.html && $content.data("loaded") === "1"); |
| 973 | + var options = wizard.data("options"), |
| 974 | + header = $(".content > .title:eq(" + index + ")", wizard), |
| 975 | + content = header.next(".body"), |
| 976 | + mode = (content.data("mode") == null) ? $.fn.steps.contentMode.html : |
| 977 | + getValidEnumValue($.fn.steps.contentMode, (/^\s*$/.test(content.data("mode")) || isNaN(content.data("mode"))) ? |
| 978 | + content.data("mode") : Number(content.data("mode"))), |
| 979 | + contentUrl = (mode === $.fn.steps.contentMode.html || content.data("url") === undefined) ? |
| 980 | + "" : content.data("url"), |
| 981 | + contentLoaded = (mode !== $.fn.steps.contentMode.html && content.data("loaded") === "1"); |
980 | 982 |
|
981 | 983 | return { |
982 | | - title: $header.html(), |
983 | | - content: (mode === $.fn.steps.contentMode.html) ? $content.html() : "", |
| 984 | + title: header.html(), |
| 985 | + content: (mode === $.fn.steps.contentMode.html) ? content.html() : "", |
984 | 986 | contentUrl: contentUrl, |
985 | 987 | contentMode: mode, |
986 | 988 | contentLoaded: contentLoaded |
|
1115 | 1117 | { |
1116 | 1118 | event.preventDefault(); |
1117 | 1119 |
|
1118 | | - var anchor = $(this); |
1119 | | - var wizard = anchor.parents(".wizard"); |
| 1120 | + var anchor = $(this), |
| 1121 | + wizard = anchor.parents(".wizard"); |
1120 | 1122 | switch (anchor.attr("href").substring(anchor.attr("href").lastIndexOf("#"))) |
1121 | 1123 | { |
1122 | 1124 | case "#finish": |
|
1144 | 1146 | { |
1145 | 1147 | event.preventDefault(); |
1146 | 1148 |
|
1147 | | - var anchor = $(this); |
1148 | | - var wizard = anchor.parents(".wizard"); |
1149 | | - var state = wizard.data("state"); |
1150 | | - var oldIndex = state.currentIndex; |
| 1149 | + var anchor = $(this), |
| 1150 | + wizard = anchor.parents(".wizard"), |
| 1151 | + state = wizard.data("state"), |
| 1152 | + oldIndex = state.currentIndex; |
1151 | 1153 |
|
1152 | 1154 | if (anchor.parent().is(":not(.disabled):not(.current)")) |
1153 | 1155 | { |
|
0 commit comments