Skip to content

Commit cbc92a6

Browse files
author
Rafael J. Staib
committed
Fix problem with dynamic adding, inserting and removing
1 parent 5da5194 commit cbc92a6

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

demo/add-remove.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ <h1>Basic Demo</h1>
5252
title: "Second Step",
5353
content: "New!"
5454
});
55-
//$("#wizard").steps("remove", 2);
55+
$("#wizard").steps("remove", 2);
5656
});
5757
</script>
5858

jquery.steps.js

+39-10
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616
* - Loading Animation (Spinner)
1717
* - Implement slideLeft animation
1818
* - Fix bugs in insert and remove methods (add works fine)
19-
* a. Refresh ids higher than new index
20-
* b. The step button is not inserted right (still added at the end)
21-
* c. Add tests for add, insert and remove
19+
* a. Add tests for add, insert and remove
2220
*
2321
* Planed Features:
2422
* - Progress bar
2523
* - Advanced Accessibility support (WAI-ARIA)
2624
* - Implement preloadContent for async and iframe content types.
2725
* - Implement functionality to skip a certain amount of steps
2826
* - Dynamic settings change
27+
* - Dynamic step update
2928
*
3029
*/
3130

@@ -362,28 +361,31 @@
362361
options = wizard.data("options"),
363362
state = wizard.data("state");
364363

365-
if (index < 0 || index > state.Count || state.currentIndex === index)
364+
// Index out of range and try deleting current item will return false.
365+
if (index < 0 || index > state.stepCount || state.currentIndex === index)
366366
{
367367
return false;
368368
}
369369

370370
var contentContainer = wizard.children(".content");
371-
contentContainer.children(".title:eq(" + index + ")").remove();
372-
contentContainer.children(".body:eq(" + index + ")").remove();
371+
$(".title:eq(" + index + ")", contentContainer).remove();
372+
$(".body:eq(" + index + ")", contentContainer).remove();
373+
$(".steps > ol > li:eq(" + index + ")", wizard).remove();
373374

374375
if (index === 0)
375376
{
376-
contentContainer.children(".title:first").addClass("first");
377+
$(".title:first", contentContainer).addClass("first");
377378
}
378379

379380
// Reset state values
380381
if (state.currentIndex >= index)
381382
{
382383
state.currentIndex = state.currentIndex + 1;
383384
}
384-
state.stepCount = contentContainer.children(".body").length;
385+
state.stepCount = $(".body", contentContainer).length;
385386
state.currentStep = getStepProperties(wizard, state.currentIndex);
386387

388+
updateIdentifiers(wizard, index);
387389
refreshActionState(wizard);
388390

389391
return true;
@@ -467,6 +469,7 @@
467469
state.stepCount = contentContainer.children(".body").length;
468470
state.currentStep = getStepProperties(wizard, state.currentIndex);
469471

472+
updateIdentifiers(wizard, index);
470473
refreshActionState(wizard);
471474

472475
return wizard;
@@ -762,8 +765,16 @@
762765
{
763766
stepItem.addClass("disabled");
764767
}
765-
766-
$(".steps > ol", wizard).append(stepItem);
768+
769+
var stepCollection = $(".steps > ol", wizard);
770+
if (index === 0)
771+
{
772+
stepCollection.prepend(stepItem);
773+
}
774+
else
775+
{
776+
$("li:eq(" + (index - 1) + ")", stepCollection).after(stepItem);
777+
}
767778
}
768779

769780
/**
@@ -802,11 +813,29 @@
802813
}
803814
}
804815

816+
/**
817+
* Updates identifiers for step buttons and their related titles.
818+
*
819+
* @private
820+
* @method updateIdentifiers
821+
* @param wizard A jQuery wizard object
822+
* @param index The start point for updating ids
823+
*/
824+
function updateIdentifiers(wizard, index)
825+
{
826+
for (var i = index; i < wizard.data("state").stepCount; i++)
827+
{
828+
$(".steps > ol > li:eq(" + i + ") > a", wizard).attr("href", "#" + getUniqueId(wizard) + "-" + i);
829+
$(".content > .title:eq(" + i + ")", wizard).attr("id", getUniqueId(wizard) + "-" + i);
830+
}
831+
}
832+
805833
/**
806834
* Refreshs the action navigation.
807835
*
808836
* @private
809837
* @method refreshActionState
838+
* @param wizard A jQuery wizard object
810839
*/
811840
function refreshActionState(wizard)
812841
{

0 commit comments

Comments
 (0)