b50010144e41d0b9f3b34825c53df88a27e7c9fc
[myslice.git] / plugins / wizard / wizard.js
1 /**
2  * Description: implements a wizard-like interface
3  * Copyright (c) 2012 UPMC Sorbonne Universite
4  * License: GPLv3
5  */
6
7 /*
8  * It's a best practice to pass jQuery to an IIFE (Immediately Invoked Function
9  * Expression) that maps it to the dollar sign so it can't be overwritten by
10  * another library in the scope of its execution.
11  */
12
13 (function($){
14     // routing calls
15     $.fn.Wizard = function( method ) {
16         if ( methods[method] ) {
17             return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
18         } else if ( typeof method === 'object' || ! method ) {
19             return methods.init.apply( this, arguments );
20         } else {
21             $.error( 'Method ' +  method + ' does not exist on jQuery.Wizard' );
22         }    
23     };
24
25     /***************************************************************************
26      * Public methods
27      ***************************************************************************/
28
29     var methods = {
30
31         init : function ( options ) {
32             return this.each(function() {
33                 var $this = $(this);
34
35                 // Initialize the smart-wizard third-party jquery plugin
36                 $(wizard, this).smartWizard({
37                     selected    : options.start_step - 1,
38                     errorSteps  : [5],
39                     onLeaveStep : leaveAStepCallback,
40 //                  onFinish    : onFinishCallback
41                 });
42
43                 // XXX Mark some steps as done !
44                 $(wizard, this).smartWizard('setDone',{stepnum: 1, isdone:true});
45
46                 function leaveAStepCallback(obj){
47                     var step_num= obj.attr('rel')-1; // get the current step number
48                     func = options.validate_step_js[step_num];
49                     if (func == 'null')
50                         return true;
51                     return window[func]();
52                 }
53
54                 function onFinishCallback(){
55                     window.location.href('/');
56                 }
57
58
59             }); // this.each
60         } // init
61
62     };
63 })( jQuery );