Skip to content

Commit bdfa21f

Browse files
committed
Added cancel button functionality
1 parent 080e5c8 commit bdfa21f

7 files changed

+133
-65
lines changed

build/jQuery.Steps.1.0.5.nupkg

179 Bytes
Binary file not shown.

build/jquery.steps-1.0.5.zip

236 Bytes
Binary file not shown.

build/jquery.steps.js

+66-32
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,19 @@ function analyzeData(wizard, options, state)
200200
});
201201
}
202202

203+
/**
204+
* Triggers the onCanceled event.
205+
*
206+
* @static
207+
* @private
208+
* @method cancel
209+
* @param wizard {Object} The jQuery wizard object
210+
**/
211+
function cancel(wizard)
212+
{
213+
wizard.triggerHandler("canceled");
214+
}
215+
203216
function decreaseCurrentIndexBy(state, decreaseBy)
204217
{
205218
return state.currentIndex - decreaseBy;
@@ -765,17 +778,21 @@ function paginationClickHandler(event)
765778
state = getState(wizard),
766779
href = anchor.attr("href");
767780

768-
switch (href.substring(href.lastIndexOf("#")))
781+
switch (href.substring(href.lastIndexOf("#") + 1))
769782
{
770-
case "#finish":
783+
case "cancel":
784+
cancel(wizard);
785+
break;
786+
787+
case "finish":
771788
finishStep(wizard, state);
772789
break;
773790

774-
case "#next":
791+
case "next":
775792
goToNextStep(wizard, options, state);
776793
break;
777794

778-
case "#previous":
795+
case "previous":
779796
goToPreviousStep(wizard, options, state);
780797
break;
781798
}
@@ -811,7 +828,7 @@ function refreshPagination(wizard, options, state)
811828
}
812829
else
813830
{
814-
finish._showAria(options.enableFinishButton && state.stepCount > (state.currentIndex + 1));
831+
finish._showAria(options.enableFinishButton && state.stepCount >= (state.currentIndex + 1));
815832
next._showAria(state.stepCount === 0 || state.stepCount > (state.currentIndex + 1)).
816833
_enableAria(state.stepCount > (state.currentIndex + 1) || !options.enableFinishButton);
817834
}
@@ -882,6 +899,7 @@ function registerEvents(wizard, options)
882899
{
883900
var eventNamespace = getEventNamespace(wizard);
884901

902+
wizard.bind("canceled" + eventNamespace, options.onCanceled);
885903
wizard.bind("finishing" + eventNamespace, options.onFinishing);
886904
wizard.bind("finished" + eventNamespace, options.onFinished);
887905
wizard.bind("stepChanging" + eventNamespace, options.onStepChanging);
@@ -1044,6 +1062,11 @@ function renderPagination(wizard, options, state)
10441062
buttons += buttonTemplate.format("finish", options.labels.finish);
10451063
}
10461064

1065+
if (options.enableCancelButton)
1066+
{
1067+
buttons += buttonTemplate.format("cancel", options.labels.cancel);
1068+
}
1069+
10471070
wizard.append(pagination.format(options.actionContainerTag, options.clearFixCssClass,
10481071
options.labels.pagination, buttons));
10491072

@@ -1305,10 +1328,8 @@ $.fn.steps = function (method)
13051328
**/
13061329
$.fn.steps.add = function (step)
13071330
{
1308-
var options = getOptions(this),
1309-
state = getState(this);
1310-
1311-
return insertStep(this, options, state, state.stepCount, step);
1331+
var state = getState(this);
1332+
return insertStep(this, getOptions(this), state, state.stepCount, step);
13121333
};
13131334

13141335
/**
@@ -1319,9 +1340,7 @@ $.fn.steps.add = function (step)
13191340
**/
13201341
$.fn.steps.destroy = function ()
13211342
{
1322-
var options = getOptions(this);
1323-
1324-
return destroy(this, options);
1343+
return destroy(this, getOptions(this));
13251344
};
13261345

13271346
/**
@@ -1331,9 +1350,7 @@ $.fn.steps.destroy = function ()
13311350
**/
13321351
$.fn.steps.finish = function ()
13331352
{
1334-
var state = getState(this);
1335-
1336-
finishStep(this, state);
1353+
finishStep(this, getState(this));
13371354
};
13381355

13391356
/**
@@ -1388,10 +1405,7 @@ $.fn.steps.getStep = function (index)
13881405
**/
13891406
$.fn.steps.insert = function (index, step)
13901407
{
1391-
var options = getOptions(this),
1392-
state = getState(this);
1393-
1394-
return insertStep(this, options, state, index, step);
1408+
return insertStep(this, getOptions(this), getState(this), index, step);
13951409
};
13961410

13971411
/**
@@ -1402,11 +1416,7 @@ $.fn.steps.insert = function (index, step)
14021416
**/
14031417
$.fn.steps.next = function ()
14041418
{
1405-
var options = getOptions(this),
1406-
state = getState(this);
1407-
1408-
1409-
return goToNextStep(this, options, state);
1419+
return goToNextStep(this, getOptions(this), getState(this));
14101420
};
14111421

14121422
/**
@@ -1417,10 +1427,7 @@ $.fn.steps.next = function ()
14171427
**/
14181428
$.fn.steps.previous = function ()
14191429
{
1420-
var options = getOptions(this),
1421-
state = getState(this);
1422-
1423-
return goToPreviousStep(this, options, state);
1430+
return goToPreviousStep(this, getOptions(this), getState(this));
14241431
};
14251432

14261433
/**
@@ -1432,10 +1439,7 @@ $.fn.steps.previous = function ()
14321439
**/
14331440
$.fn.steps.remove = function (index)
14341441
{
1435-
var options = getOptions(this),
1436-
state = getState(this);
1437-
1438-
return removeStep(this, options, state, index);
1442+
return removeStep(this, getOptions(this), getState(this), index);
14391443
};
14401444

14411445
/**
@@ -1774,6 +1778,16 @@ var defaults = $.fn.steps.defaults = {
17741778
**/
17751779
enableContentCache: true,
17761780

1781+
/**
1782+
* Shows the cancel button if enabled.
1783+
*
1784+
* @property enableCancelButton
1785+
* @type Boolean
1786+
* @default true
1787+
* @for defaults
1788+
**/
1789+
enableCancelButton: true,
1790+
17771791
/**
17781792
* Shows the finish button if enabled.
17791793
*
@@ -1885,6 +1899,16 @@ var defaults = $.fn.steps.defaults = {
18851899
**/
18861900
onStepChanged: function (event, currentIndex, priorIndex) { },
18871901

1902+
/**
1903+
* Fires after cancelation.
1904+
*
1905+
* @property onCanceled
1906+
* @type Event
1907+
* @default function (event) { }
1908+
* @for defaults
1909+
**/
1910+
onCanceled: function (event) { },
1911+
18881912
/**
18891913
* Fires before finishing and can be used to prevent completion by returning `false`.
18901914
* Very useful for form validation.
@@ -1914,6 +1938,16 @@ var defaults = $.fn.steps.defaults = {
19141938
* @for defaults
19151939
**/
19161940
labels: {
1941+
/**
1942+
* Label for the cancel button.
1943+
*
1944+
* @property cancel
1945+
* @type String
1946+
* @default "Cancel"
1947+
* @for defaults
1948+
**/
1949+
cancel: "Cancel",
1950+
19171951
/**
19181952
* This label is important for accessability reasons.
19191953
* Indicates which step is activated.

build/jquery.steps.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/defaults.js

+30
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ var defaults = $.fn.steps.defaults = {
188188
**/
189189
enableContentCache: true,
190190

191+
/**
192+
* Shows the cancel button if enabled.
193+
*
194+
* @property enableCancelButton
195+
* @type Boolean
196+
* @default true
197+
* @for defaults
198+
**/
199+
enableCancelButton: true,
200+
191201
/**
192202
* Shows the finish button if enabled.
193203
*
@@ -299,6 +309,16 @@ var defaults = $.fn.steps.defaults = {
299309
**/
300310
onStepChanged: function (event, currentIndex, priorIndex) { },
301311

312+
/**
313+
* Fires after cancelation.
314+
*
315+
* @property onCanceled
316+
* @type Event
317+
* @default function (event) { }
318+
* @for defaults
319+
**/
320+
onCanceled: function (event) { },
321+
302322
/**
303323
* Fires before finishing and can be used to prevent completion by returning `false`.
304324
* Very useful for form validation.
@@ -328,6 +348,16 @@ var defaults = $.fn.steps.defaults = {
328348
* @for defaults
329349
**/
330350
labels: {
351+
/**
352+
* Label for the cancel button.
353+
*
354+
* @property cancel
355+
* @type String
356+
* @default "Cancel"
357+
* @for defaults
358+
**/
359+
cancel: "Cancel",
360+
331361
/**
332362
* This label is important for accessability reasons.
333363
* Indicates which step is activated.

src/privates.js

+28-5
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,19 @@ function analyzeData(wizard, options, state)
140140
});
141141
}
142142

143+
/**
144+
* Triggers the onCanceled event.
145+
*
146+
* @static
147+
* @private
148+
* @method cancel
149+
* @param wizard {Object} The jQuery wizard object
150+
**/
151+
function cancel(wizard)
152+
{
153+
wizard.triggerHandler("canceled");
154+
}
155+
143156
function decreaseCurrentIndexBy(state, decreaseBy)
144157
{
145158
return state.currentIndex - decreaseBy;
@@ -705,17 +718,21 @@ function paginationClickHandler(event)
705718
state = getState(wizard),
706719
href = anchor.attr("href");
707720

708-
switch (href.substring(href.lastIndexOf("#")))
721+
switch (href.substring(href.lastIndexOf("#") + 1))
709722
{
710-
case "#finish":
723+
case "cancel":
724+
cancel(wizard);
725+
break;
726+
727+
case "finish":
711728
finishStep(wizard, state);
712729
break;
713730

714-
case "#next":
731+
case "next":
715732
goToNextStep(wizard, options, state);
716733
break;
717734

718-
case "#previous":
735+
case "previous":
719736
goToPreviousStep(wizard, options, state);
720737
break;
721738
}
@@ -751,7 +768,7 @@ function refreshPagination(wizard, options, state)
751768
}
752769
else
753770
{
754-
finish._showAria(options.enableFinishButton && state.stepCount > (state.currentIndex + 1));
771+
finish._showAria(options.enableFinishButton && state.stepCount >= (state.currentIndex + 1));
755772
next._showAria(state.stepCount === 0 || state.stepCount > (state.currentIndex + 1)).
756773
_enableAria(state.stepCount > (state.currentIndex + 1) || !options.enableFinishButton);
757774
}
@@ -822,6 +839,7 @@ function registerEvents(wizard, options)
822839
{
823840
var eventNamespace = getEventNamespace(wizard);
824841

842+
wizard.bind("canceled" + eventNamespace, options.onCanceled);
825843
wizard.bind("finishing" + eventNamespace, options.onFinishing);
826844
wizard.bind("finished" + eventNamespace, options.onFinished);
827845
wizard.bind("stepChanging" + eventNamespace, options.onStepChanging);
@@ -984,6 +1002,11 @@ function renderPagination(wizard, options, state)
9841002
buttons += buttonTemplate.format("finish", options.labels.finish);
9851003
}
9861004

1005+
if (options.enableCancelButton)
1006+
{
1007+
buttons += buttonTemplate.format("cancel", options.labels.cancel);
1008+
}
1009+
9871010
wizard.append(pagination.format(options.actionContainerTag, options.clearFixCssClass,
9881011
options.labels.pagination, buttons));
9891012

0 commit comments

Comments
 (0)