1
1
var 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 )
3
12
{
4
13
wizard . data ( "steps" ) . push ( step ) ;
5
14
} ,
@@ -18,7 +27,7 @@ var privates = {
18
27
{
19
28
throw new Error ( "One or more corresponding step titles are missing." ) ;
20
29
}
21
-
30
+
22
31
var startIndex = options . startIndex ;
23
32
24
33
state . stepCount = stepTitles . length ;
@@ -55,7 +64,7 @@ var privates = {
55
64
contentLoaded : contentLoaded
56
65
} ) ;
57
66
58
- privates . addStep ( wizard , step ) ;
67
+ privates . addStepToCache ( wizard , step ) ;
59
68
} ) ;
60
69
} ,
61
70
@@ -64,6 +73,15 @@ var privates = {
64
73
return "<li><a href=\"#" + tag + "\" role=\"menuitem\">" + label + "</a></li>" ;
65
74
} ,
66
75
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
+ **/
67
85
getStep : function ( wizard , index )
68
86
{
69
87
var steps = wizard . data ( "steps" ) ;
@@ -76,6 +94,15 @@ var privates = {
76
94
return steps [ index ] ;
77
95
} ,
78
96
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
+ */
79
106
getUniqueId : function ( wizard )
80
107
{
81
108
var uniqueId = wizard . data ( "uid" ) ;
@@ -274,7 +301,84 @@ var privates = {
274
301
} ) ;
275
302
} ,
276
303
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 )
278
382
{
279
383
wizard . data ( "steps" ) . splice ( index , 0 , step ) ;
280
384
} ,
@@ -567,7 +671,60 @@ var privates = {
567
671
wizard . find ( ".actions a" ) . bind ( "click.steps" , privates . paginationClickHandler ) ;
568
672
} ,
569
673
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 )
571
728
{
572
729
wizard . data ( "steps" ) . splice ( index , 1 ) ;
573
730
} ,
0 commit comments