11var privates = {
2- addStep : function ( wizard , step )
2+ /**
3+ * Adds a step to the cache.
4+ *
5+ * @static
6+ * @private
7+ * @method addStepToCache
8+ * @param wizard {Object} A jQuery wizard object
9+ * @param step {Object} The step object to add
10+ **/
11+ addStepToCache : function ( wizard , step )
312 {
413 wizard . data ( "steps" ) . push ( step ) ;
514 } ,
@@ -18,7 +27,7 @@ var privates = {
1827 {
1928 throw new Error ( "One or more corresponding step titles are missing." ) ;
2029 }
21-
30+
2231 var startIndex = options . startIndex ;
2332
2433 state . stepCount = stepTitles . length ;
@@ -55,7 +64,7 @@ var privates = {
5564 contentLoaded : contentLoaded
5665 } ) ;
5766
58- privates . addStep ( wizard , step ) ;
67+ privates . addStepToCache ( wizard , step ) ;
5968 } ) ;
6069 } ,
6170
@@ -64,6 +73,15 @@ var privates = {
6473 return "<li><a href=\"#" + tag + "\" role=\"menuitem\">" + label + "</a></li>" ;
6574 } ,
6675
76+ /**
77+ * Gets a specific step object by index.
78+ *
79+ * @static
80+ * @private
81+ * @method getStep
82+ * @param index {Integer} An integer that belongs to the position of a step
83+ * @return {Object } A specific step object
84+ **/
6785 getStep : function ( wizard , index )
6886 {
6987 var steps = wizard . data ( "steps" ) ;
@@ -76,6 +94,15 @@ var privates = {
7694 return steps [ index ] ;
7795 } ,
7896
97+ /**
98+ * Gets or creates if not exist an unique id from the given wizard instance.
99+ *
100+ * @static
101+ * @private
102+ * @method getUniqueId
103+ * @param wizard {Object} A jQuery wizard object
104+ * @return {String } Returns the unique id for the given wizard
105+ */
79106 getUniqueId : function ( wizard )
80107 {
81108 var uniqueId = wizard . data ( "uid" ) ;
@@ -274,7 +301,84 @@ var privates = {
274301 } ) ;
275302 } ,
276303
277- insertStep : function ( wizard , index , step )
304+ /**
305+ * Inserts a new step to a specific position.
306+ *
307+ * @static
308+ * @private
309+ * @method insertStep
310+ * @param wizard {Object} The jQuery wizard object
311+ * @param options {Object} Settings of the current wizard
312+ * @param state {Object} The state container of the current wizard
313+ * @param index {Integer} The position (zero-based) to add
314+ * @param step {Object} The step object to add
315+ * @example
316+ * $("#wizard").steps().insert(0, {
317+ * title: "Title",
318+ * content: "", // optional
319+ * contentMode: "async", // optional
320+ * contentUrl: "/Content/Step/1" // optional
321+ * });
322+ * @chainable
323+ **/
324+ insertStep : function ( wizard , options , state , index , step )
325+ {
326+ var uniqueId = privates . getUniqueId ( wizard ) ;
327+
328+ if ( index < 0 || index > state . stepCount )
329+ {
330+ throw new Error ( "Index out of range." ) ;
331+ }
332+
333+ // TODO: Validate step object
334+
335+ // Change data
336+ step = $ . extend ( { } , $ . fn . steps . stepModel , step ) ;
337+ privates . insertStepToCache ( wizard , index , step ) ;
338+ if ( state . currentIndex >= index )
339+ {
340+ state . currentIndex ++ ;
341+ privates . saveCurrentStateToCookie ( wizard , options , state ) ;
342+ }
343+ state . stepCount ++ ;
344+
345+ var contentContainer = wizard . find ( ".content" ) ,
346+ header = $ ( document . createElement ( options . headerTag ) ) . html ( step . title ) ,
347+ body = $ ( document . createElement ( options . bodyTag ) ) ;
348+
349+ if ( step . contentMode == null || step . contentMode === $ . fn . steps . contentMode . html )
350+ {
351+ body . html ( step . content ) ;
352+ }
353+
354+ if ( index === 0 )
355+ {
356+ contentContainer . prepend ( body ) . prepend ( header ) ;
357+ }
358+ else
359+ {
360+ contentContainer . find ( "#" + uniqueId + _tabpanelSuffix + ( index - 1 ) ) . after ( body ) . after ( header ) ;
361+ }
362+
363+ privates . renderBody ( wizard , body , index ) ;
364+ privates . renderTitle ( wizard , options , state , header , index ) ;
365+ privates . refreshSteps ( wizard , options , state , index ) ;
366+ privates . refreshPagination ( wizard , options , state ) ;
367+
368+ return wizard ;
369+ } ,
370+
371+ /**
372+ * Inserts a step object to the cache at a specific position.
373+ *
374+ * @static
375+ * @private
376+ * @method insertStepToCache
377+ * @param wizard {Object} A jQuery wizard object
378+ * @param index {Integer} The position (zero-based) to add
379+ * @param step {Object} The step object to add
380+ **/
381+ insertStepToCache : function ( wizard , index , step )
278382 {
279383 wizard . data ( "steps" ) . splice ( index , 0 , step ) ;
280384 } ,
@@ -567,7 +671,60 @@ var privates = {
567671 wizard . find ( ".actions a" ) . bind ( "click.steps" , privates . paginationClickHandler ) ;
568672 } ,
569673
570- removeStep : function ( wizard , index )
674+ /**
675+ * Removes a specific step by an given index.
676+ *
677+ * @static
678+ * @private
679+ * @method removeStep
680+ * @param wizard {Object} A jQuery wizard object
681+ * @param options {Object} Settings of the current wizard
682+ * @param state {Object} The state container of the current wizard
683+ * @param index {Integer} The position (zero-based) of the step to remove
684+ * @return Indecates whether the item is removed.
685+ **/
686+ removeStep : function ( wizard , options , state , index )
687+ {
688+ var uniqueId = privates . getUniqueId ( wizard ) ;
689+
690+ // Index out of range and try deleting current item will return false.
691+ if ( index < 0 || index >= state . stepCount || state . currentIndex === index )
692+ {
693+ return false ;
694+ }
695+
696+ // Change data
697+ privates . removeStepToCache ( wizard , index ) ;
698+ if ( state . currentIndex > index )
699+ {
700+ state . currentIndex -- ;
701+ privates . saveCurrentStateToCookie ( wizard , options , state ) ;
702+ }
703+ state . stepCount -- ;
704+
705+ wizard . find ( "#" + uniqueId + _titleSuffix + index ) . remove ( ) ;
706+ wizard . find ( "#" + uniqueId + _tabpanelSuffix + index ) . remove ( ) ;
707+ wizard . find ( "#" + uniqueId + _tabSuffix + index ) . parent ( ) . remove ( ) ;
708+
709+ // Set the "first" class to the new first step button
710+ if ( index === 0 )
711+ {
712+ wizard . find ( ".steps li" ) . first ( ) . addClass ( "first" ) ;
713+ }
714+
715+ // Set the "last" class to the new last step button
716+ if ( index === state . stepCount )
717+ {
718+ wizard . find ( ".steps li" ) . eq ( index ) . addClass ( "last" ) ;
719+ }
720+
721+ privates . refreshSteps ( wizard , options , state , index ) ;
722+ privates . refreshPagination ( wizard , options , state ) ;
723+
724+ return true ;
725+ } ,
726+
727+ removeStepToCache : function ( wizard , index )
571728 {
572729 wizard . data ( "steps" ) . splice ( index , 1 ) ;
573730 } ,
0 commit comments