3 * https://github.com/ExactTarget/fuelux
5 * Copyright (c) 2012 ExactTarget
6 * Licensed under the MIT license.
9 define(['require','jquery'],function (require) {
11 var $ = require('jquery');
14 // WIZARD CONSTRUCTOR AND PROTOTYPE
16 var Wizard = function (element, options) {
19 this.$element = $(element);
20 this.options = $.extend({}, $.fn.wizard.defaults, options);
22 this.numSteps = this.$element.find('li').length;
23 this.$prevBtn = this.$element.find('button.btn-prev');
24 this.$nextBtn = this.$element.find('button.btn-next');
26 kids = this.$nextBtn.children().detach();
27 this.nextText = $.trim(this.$nextBtn.text());
28 this.$nextBtn.append(kids);
31 this.$prevBtn.on('click', $.proxy(this.previous, this));
32 this.$nextBtn.on('click', $.proxy(this.next, this));
33 this.$element.on('click', 'li.complete', $.proxy(this.stepclicked, this));
40 setState: function () {
41 var canMovePrev = (this.currentStep > 1);
42 var firstStep = (this.currentStep === 1);
43 var lastStep = (this.currentStep === this.numSteps);
45 // disable buttons based on current step
46 this.$prevBtn.attr('disabled', (firstStep === true || canMovePrev === false));
48 // change button text of last step, if specified
49 var data = this.$nextBtn.data();
50 if (data && data.last) {
51 this.lastText = data.last;
52 if (typeof this.lastText !== 'undefined') {
54 var text = (lastStep !== true) ? this.nextText : this.lastText;
55 var kids = this.$nextBtn.children().detach();
56 this.$nextBtn.text(text).append(kids);
60 // reset classes for all steps
61 var $steps = this.$element.find('li');
62 $steps.removeClass('active').removeClass('complete');
63 $steps.find('span.badge').removeClass('badge-info').removeClass('badge-success');
65 // set class for all previous steps
66 var prevSelector = 'li:lt(' + (this.currentStep - 1) + ')';
67 var $prevSteps = this.$element.find(prevSelector);
68 $prevSteps.addClass('complete');
69 $prevSteps.find('span.badge').addClass('badge-success');
71 // set class for current step
72 var currentSelector = 'li:eq(' + (this.currentStep - 1) + ')';
73 var $currentStep = this.$element.find(currentSelector);
74 $currentStep.addClass('active');
75 $currentStep.find('span.badge').addClass('badge-info');
77 // set display of target element
78 var target = $currentStep.data().target;
79 $('.step-pane').removeClass('active');
80 $(target).addClass('active');
82 this.$element.trigger('changed');
85 stepclicked: function (e) {
86 var li = $(e.currentTarget);
88 var index = $('.steps li').index(li);
90 var evt = $.Event('stepclick');
91 this.$element.trigger(evt, {step: index + 1});
92 if (evt.isDefaultPrevented()) return;
94 this.currentStep = (index + 1);
98 previous: function () {
99 var canMovePrev = (this.currentStep > 1);
101 var e = $.Event('change');
102 this.$element.trigger(e, {step: this.currentStep, direction: 'previous'});
103 if (e.isDefaultPrevented()) return;
105 this.currentStep -= 1;
111 var canMoveNext = (this.currentStep + 1 <= this.numSteps);
112 var lastStep = (this.currentStep === this.numSteps);
115 var e = $.Event('change');
116 this.$element.trigger(e, {step: this.currentStep, direction: 'next'});
118 if (e.isDefaultPrevented()) return;
120 this.currentStep += 1;
124 this.$element.trigger('finished');
128 selectedItem: function (val) {
130 step: this.currentStep
136 // WIZARD PLUGIN DEFINITION
138 $.fn.wizard = function (option, value) {
141 var $set = this.each(function () {
143 var data = $this.data('wizard');
144 var options = typeof option === 'object' && option;
146 if (!data) $this.data('wizard', (data = new Wizard(this, options)));
147 if (typeof option === 'string') methodReturn = data[option](value);
150 return (methodReturn === undefined) ? $set : methodReturn;
153 $.fn.wizard.defaults = {};
155 $.fn.wizard.Constructor = Wizard;
161 $('body').on('mousedown.wizard.data-api', '.wizard', function () {
163 if ($this.data('wizard')) return;
164 $this.wizard($this.data());