Skip to content

Commit

Permalink
Plugin creation
Browse files Browse the repository at this point in the history
  • Loading branch information
glaucocustodio committed Jul 3, 2014
0 parents commit 2a3ea22
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules
14 changes: 14 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"boss": true,
"curly": true,
"eqeqeq": true,
"eqnull": true,
"expr": true,
"immed": true,
"noarg": true,
"onevar": true,
"smarttabs": true,
"trailing": true,
"unused": true,
"node": true
}
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Contributing

1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Make your changes on the `src` folder, never on the `dist` folder.
4. Commit your changes: `git commit -m 'Add some feature'`
5. Push to the branch: `git push origin my-new-feature`
6. Submit a pull request
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# jQuery Ajax Wizard

### A form wizard plugin for when next step is gotten via AJAX based on user's input

## When to use

You have a multi steps form but just know the first step, the further steps rely on user's input. When user fills up first step and goes forward, an AJAX request is made posting user's input and next step is filled with the returned HTML.

A hash from each step is generated, so, a new AJAX request only is made to a already gotten step (in case of regression) if some data has been changed.

## Usage

1. Include jQuery:

```html
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
```

2. Include plugin's code:

```html
<script src="dist/jquery.ajaxWizard.min.js"></script>
```

3. Call the plugin:

```javascript
$("#element").ajaxWizard({
// options and callbacks
});
```

## Contributing

Check [CONTRIBUTING.md](https://github.com/glaucocustodio/jquery-ajax-wizard/blob/master/CONTRIBUTING.md) for more information.

## License

This projected is licensed under the terms of the MIT license.
38 changes: 38 additions & 0 deletions ajaxWizard.jquery.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "ajaxWizard",
"title": "jQuery Ajax Wizard",
"description": "A form wizard plugin for when next step is gotten via AJAX based on user's input.",
"keywords": [
"jquery",
"form wizard",
"wizard",
"form",
"ajax",
],
"version": "0.0.1",
"author": {
"name": "Glauco Custódio",
"email": "glauco.custodio@gmail.com",
"url": "https://github.com/glaucocustodio"
},
"maintainers": [
{
"name": "Glauco Custódio",
"email": "glauco.custodio@gmail.com",
"url": "https://github.com/glaucocustodio/jquery-ajax-wizard"
}
],
"licenses": [
{
"type": "MIT",
"url": "http://mit-license.org/"
}
],
"bugs": "https://github.com/glaucocustodio/jquery-ajax-wizard/issues",
"homepage": "https://github.com/glaucocustodio/jquery-ajax-wizard",
"docs": "https://github.com/glaucocustodio/jquery-ajax-wizard#readme",
"download": "https://github.com/glaucocustodio/jquery-ajax-wizard/archive/master.zip",
"dependencies": {
"jquery": ">=1.4"
}
}
21 changes: 21 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "jquery-ajaxWizard",
"version": "0.0.1",
"homepage": "https://github.com/glaucocustodio/jquery-ajax-wizard",
"authors": [
"Glauco Custódio"
],
"description": "A form wizard plugin for when next step is gotten via AJAX based on user's input.",
"main": "src/jquery.ajaxWizard.js",
"keywords": [
"jquery",
"ajax",
"wizard",
"form",
"form wizard"
],
"license": "MIT",
"ignore": [
".jshintrc",
]
}
144 changes: 144 additions & 0 deletions dist/jquery.ajaxWizard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* jQuery Ajax Wizard - v0.0.1
*
* Made by Glauco Custódio
* Under MIT License
*/
;(function ( $, window, document, undefined ) {
// Create the defaults once
var pluginName = "ajaxWizard",
defaults = {
contentUrl: null,
controlSelectors: {
forward: '.forward',
backward: '.backward'
},
beforeForward: function() { return true; },
afterForward: function() { return true; },
afterCachedForward: function() { return true; },
beforeBackward: function() { return true; },
afterBackward: function() { return true; },
};

// The actual plugin constructor
function ajaxWizard ( element, options ) {
this.element = element;
this.settings = $.extend( {}, defaults, options );
this._defaults = defaults;
this._name = pluginName;
this.init();
}

$.extend(ajaxWizard.prototype, {
init: function () {
$(this.element).find('fieldset:first-child').addClass('current');
this.eventListeners();
},
currentStep: function(){
return $(this.element).find('fieldset.current');
},
getContentUrl: function(){
if(this.settings.contentUrl === null){
throw new Error('contentUrl not defined');
}else{
return this.settings.contentUrl;
}
},
generateUniqueKey: function(){
return (Math.random()*Math.random()).toString(16).substr(2,16);
},
whenCallingNextStep: function(that){
if(!$(that).data('ajax-wizard-key') || $(that).data('ajax-wizard-hash') !== this.currentStep().serialize()){
$('.' + $(that).data('ajax-wizard-key')).remove();
this.forward(that);
}else{
this.animate(this.currentStep(),
$('.' + $(that).data('ajax-wizard-key')));
this.afterCachedForwardCallback(this.element);
}
},
beforeForwardCallback: function(){
return this.settings.beforeForward.call(this, this.element);
},
afterForwardCallback: function(){
return this.settings.afterForward.call(this, this.element);
},
afterCachedForwardCallback: function(){
return this.settings.afterCachedForward.call(this, this.element);
},
beforeBackwardCallback: function(){
return this.settings.beforeBackward.call(this, this.element);
},
afterBackwardCallback: function(){
return this.settings.afterBackward.call(this, this.element);
},
ajaxRequest: function(sender){
var self = this;
var hash = this.currentStep().serialize();

$.ajax({
url: self.getContentUrl(),
dataType: 'html',
method: 'post',
data: hash,
success: function(data){
self.currentStep().after('<fieldset>' + data + '</fieldset>');
var newStep = self.currentStep().next('fieldset');
self.animate(self.currentStep(), newStep);
var key = self.generateUniqueKey();

$(sender).data('ajax-wizard-key', key);
$(sender).data('ajax-wizard-hash', hash);
newStep.addClass(key);
self.afterForwardCallback(this.element);
}
});
},
forward: function(sender){
if(this.beforeForwardCallback() === true){
this.ajaxRequest(sender);
}
},
backward: function(){
if(this.beforeBackwardCallback() === true){
this.animate(this.currentStep(),
this.currentStep().prev('fieldset'));
this.afterBackwardCallback();
}
},
eventListeners: function () {
var self = this;

$(this.element).on('submit', function(e){
e.preventDefault();
self.whenCallingNextStep(this);
});

$(this.element).on('click', this.settings.controlSelectors.forward, function(){
self.whenCallingNextStep(this);
});

$(this.element).on('click', this.settings.controlSelectors.backward, function(){
self.backward();
});
},
animate: function(from, to){
from.hide().removeClass('current');
to.fadeIn().addClass('current');
}
});

// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[ pluginName ] = function ( options ) {
this.each(function() {
if ( !$.data( this, "plugin_" + pluginName ) ) {
$.data( this, "plugin_" + pluginName, new ajaxWizard( this, options ) );
}
});

// chain jQuery functions
return this;
};

})( jQuery, window, document );
1 change: 1 addition & 0 deletions dist/jquery.ajaxWizard.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var gulp = require('gulp'),
jshint = require('gulp-jshint'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify');

gulp.task('default', function() {
// place code for your default task here
});

gulp.task('scripts', function() {
return gulp.src('src/**/*.js')
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('default'))
.pipe(concat('jquery.ajaxWizard.js'))
.pipe(gulp.dest('dist/'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest('dist/'))
.pipe(notify({ message: 'Scripts task complete' }));
});
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"private": true,
"devDependencies": {
"gulp": "^3.8.5",
"gulp-rename": "^1.2.0",
"gulp-concat": "^2.2.0",
"gulp-jshint": "^1.6.4",
"gulp-notify": "^1.4.0",
"gulp-uglify": "^0.3.1"
},
"scripts": {
"test": "grunt travis --verbose"
}
}
Loading

0 comments on commit 2a3ea22

Please sign in to comment.