Skip to content

Commit 01d441f

Browse files
author
Rafael J. Staib
committed
Add something that is not really working :-(
1 parent 5211f6e commit 01d441f

File tree

4 files changed

+175
-89
lines changed

4 files changed

+175
-89
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
"grunt-contrib-concat": "~0.3.0",
3636
"grunt-contrib-yuidoc": "~0.4.0",
3737
"grunt-contrib-compress": "~0.5.0",
38-
"grunt-contrib-clean": "~0.4.1"
38+
"grunt-contrib-clean": "~0.4.1",
39+
"grunt-cli": "~0.1.9"
3940
},
4041
"readmeFilename": "README.md",
4142
"gitHead": "0d9f4f8b6a0c3d3b8a73eabf6a4b8cececd52f7a"
42-
}
43+
}

src/privates.js

+162-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
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)
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
},

src/publics.js

+8-81
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ $.fn.steps = function (method)
3232
**/
3333
$.fn.steps.add = function (step)
3434
{
35-
return this.steps("insert", this.data("state").stepCount, step);
35+
var options = this.data("options"),
36+
state = this.data("state");
37+
38+
return privates.insertStep(this, options, state, state.stepCount, step);
3639
};
3740

3841
/**
@@ -109,51 +112,10 @@ $.fn.steps.getStep = function (index)
109112
**/
110113
$.fn.steps.insert = function (index, step)
111114
{
112-
var uniqueId = privates.getUniqueId(this),
113-
options = this.data("options"),
115+
var options = this.data("options"),
114116
state = this.data("state");
115117

116-
if (index < 0 || index > state.stepCount)
117-
{
118-
throw new Error("Index out of range.");
119-
}
120-
121-
// TODO: Validate step object
122-
123-
// Change data
124-
step = $.extend({}, $.fn.steps.stepModel, step);
125-
privates.insertStep(this, index, step);
126-
if (state.currentIndex >= index)
127-
{
128-
state.currentIndex++;
129-
privates.saveCurrentStateToCookie(this, options, state);
130-
}
131-
state.stepCount++;
132-
133-
var contentContainer = this.find(".content"),
134-
header = $(document.createElement(options.headerTag)).html(step.title),
135-
body = $(document.createElement(options.bodyTag));
136-
137-
if (step.contentMode == null || step.contentMode === $.fn.steps.contentMode.html)
138-
{
139-
body.html(step.content);
140-
}
141-
142-
if (index === 0)
143-
{
144-
contentContainer.prepend(body).prepend(header);
145-
}
146-
else
147-
{
148-
contentContainer.find("#" + uniqueId + _tabpanelSuffix + (index - 1)).after(body).after(header);
149-
}
150-
151-
privates.renderBody(this, body, index);
152-
privates.renderTitle(this, options, state, header, index);
153-
privates.refreshSteps(this, options, state, index);
154-
privates.refreshPagination(this, options, state);
155-
156-
return this;
118+
return privates.insertStep(this, options, state, state.stepCount, step);
157119
};
158120

159121
/**
@@ -187,45 +149,10 @@ $.fn.steps.previous = function ()
187149
**/
188150
$.fn.steps.remove = function (index)
189151
{
190-
var uniqueId = privates.getUniqueId(this),
191-
options = this.data("options"),
152+
var options = this.data("options"),
192153
state = this.data("state");
193154

194-
// Index out of range and try deleting current item will return false.
195-
if (index < 0 || index >= state.stepCount || state.currentIndex === index)
196-
{
197-
return false;
198-
}
199-
200-
// Change data
201-
privates.removeStep(this, index);
202-
if (state.currentIndex > index)
203-
{
204-
state.currentIndex--;
205-
privates.saveCurrentStateToCookie(this, options, state);
206-
}
207-
state.stepCount--;
208-
209-
this.find("#" + uniqueId + _titleSuffix + index).remove();
210-
this.find("#" + uniqueId + _tabpanelSuffix + index).remove();
211-
this.find("#" + uniqueId + _tabSuffix + index).parent().remove();
212-
213-
// Set the "first" class to the new first step button
214-
if (index === 0)
215-
{
216-
this.find(".steps li").first().addClass("first");
217-
}
218-
219-
// Set the "last" class to the new last step button
220-
if (index === state.stepCount)
221-
{
222-
this.find(".steps li").eq(index).addClass("last");
223-
}
224-
225-
privates.refreshSteps(this, options, state, index);
226-
privates.refreshPagination(this, options, state);
227-
228-
return true;
155+
return privates.removeStep(this, options, state, index);
229156
};
230157

231158
/**

test/tests.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
/*jshint -W117 */
2+
13
module("general");
24

35
test("contentMode", 5, function ()
46
{
5-
/*jshint -W024 */
67
throws(function() { $("#contentModeWithEmptyStringArgument").steps(); }, /The enum key/, "Empty string argument");
78
throws(function() { $("#contentModeWithWrongNumberArgument").steps(); }, /Invalid enum value/, "Invalid number argument");
89
throws(function() { $("#contentModeWithWrongStringArgument").steps(); }, /The enum key/, "Invalid string argument");

0 commit comments

Comments
 (0)