/*! * jQuery Stepy - A Wizard Plugin * -------------------------------------------------------------- * * jQuery Stepy is a plugin that generates a customizable wizard. * * Licensed under The MIT License * * @version 1.1.0 * @since 2010-07-03 * @author Washington Botelho * @documentation wbotelhos.com/stepy * * -------------------------------------------------------------- * *
* * $('form').stepy(); * */ ;(function($) { var methods = { init: function(settings) { return this.each(function() { methods.destroy.call(this); this.opt = $.extend({}, $.fn.stepy.defaults, settings); var self = this, that = $(this), id = that.attr('id'); if (id === undefined || id === '') { var id = methods._hash.call(self); that.attr('id', id); } // Remove Validator... if (self.opt.validate) { jQuery.validator.setDefaults({ ignore: self.opt.ignore }); that.append(''); } self.header = methods._header.call(self); self.steps = that.children('fieldset'); self.steps.each(function(index) { methods._createHead.call(self, this, index); methods._createButtons.call(self, this, index); }); self.heads = self.header.children('li'); self.heads.first().addClass('stepy-active'); if (self.opt.finishButton) { methods._bindFinish.call(self); } // WIP... if (self.opt.titleClick) { self.heads.click(function() { var array = self.heads.filter('.stepy-active').attr('id').split('-'), // TODO: try keep the number in an attribute. current = parseInt(array[array.length - 1], 10), clicked = $(this).index(); if (clicked > current) { if (self.opt.next && !methods._execute.call(that, self.opt.next, clicked)) { return false; } } else if (clicked < current) { if (self.opt.back && !methods._execute.call(that, self.opt.back, clicked)) { return false; } } if (clicked != current) { methods.step.call(self, (clicked) + 1); } }); } else { self.heads.css('cursor', 'default'); } if (self.opt.enter) { methods._bindEnter.call(self); } self.steps.first().find(':input:visible:enabled').first().select().focus(); that.data({ 'settings': this.opt, 'stepy': true }); }); }, _bindEnter: function() { var self = this; self.steps.delegate('input[type="text"], input[type="password"]', 'keypress', function(evt) { var key = (evt.keyCode ? evt.keyCode : evt.which); if (key == 13) { evt.preventDefault(); var buttons = $(this).closest('fieldset').find('.stepy-navigator'); if (buttons.length) { var next = buttons.children('.button-next'); if (next.length) { next.click(); } else if (self.finish) { self.finish.click(); } } } }); }, _bindFinish: function() { var self = this, that = $(this), finish = that.children('input[type="submit"]'); self.finish = (finish.length === 1) ? finish : that.children('.btn .btn-succes .stepy-finish'); if (self.finish.length) { var isForm = that.is('form'), onSubmit = undefined; if (isForm && self.opt.finish) { onSubmit = that.attr('onsubmit'); that.attr('onsubmit', 'return false;'); } self.finish.on('click.stepy', function(evt) { if (self.opt.finish && !methods._execute.call(that, self.opt.finish, self.steps.length - 1)) { evt.preventDefault(); } else if (isForm) { if (onSubmit) { that.attr('onsubmit', onSubmit); } else { that.removeAttr('onsubmit'); } var isSubmit = self.finish.attr('type') === 'submit'; if (!isSubmit && (!self.opt.validate || methods.validate.call(that, self.steps.length - 1))) { that.submit(); } } }); self.steps.last().children('.stepy-navigator').append(self.finish); } else { $.error('Submit button or element with class "stepy-finish" missing!'); } }, _createBackButton: function(nav, index) { var self = this, that = $(this), attributes = { href: '#', 'class': 'btn btn-default', html: self.opt.backLabel }; $('', attributes).on('click.stepy', function(e) { e.preventDefault(); if (!self.opt.back || methods._execute.call(self, self.opt.back, index - 1)) { methods.step.call(self, (index - 1) + 1); } }).appendTo(nav); }, _createButtons: function(step, index) { var nav = methods._navigator.call(this).appendTo(step); if (index === 0) { if (this.steps.length > 1) { methods._createNextButton.call(this, nav, index); } } else { $(step).hide(); methods._createBackButton.call(this, nav, index); if (index < this.steps.length - 1) { methods._createNextButton.call(this, nav, index); } } }, _createHead: function(step, index) { var step = $(step).attr('id', $(this).attr('id') + '-step-' + index).addClass('stepy-step'), head = methods._head.call(this, index); head.append(methods._title.call(this, step)); if (this.opt.description) { head.append(methods._description.call(this, step)); } this.header.append(head); }, _createNextButton: function(nav, index) { var self = this, that = $(this), attributes = { href: '#', 'class': 'btn btn-primary', html: self.opt.nextLabel }; $('', attributes).on('click.stepy', function(e) { e.preventDefault(); if (!self.opt.next || methods._execute.call(that, self.opt.next, index + 1)) { methods.step.call(self, (index + 1) + 1); } }).appendTo(nav); }, _description: function(step) { var legend = step.children('legend'); if (!this.opt.legend) { legend.hide(); } if (legend.length) { return $('', { html: legend.html() }); } methods._error.call(this, ' element missing!'); }, _error: function(message) { $(this).html(message); $.error(message); }, _execute: function(callback, index) { var isValid = callback.call(this, index + 1); return isValid || isValid === undefined; }, _hash: function() { this.hash = 'stepy-' + Math.random().toString().substring(2) return this.hash; }, _head: function(index) { return $('', { id: $(this).attr('id') + '-head-' + index }); }, _header: function() { var header = $('