improved form and wizard plugins
[myslice.git] / plugins / form / form.js
1 /**
2  * Description: implements a form
3  * Copyright (c) 2013 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 (function($){
13
14     /***************************************************************************
15      * Method calling logic
16      ***************************************************************************/
17
18     $.fn.CreateForm = function( method ) {
19         if ( methods[method] ) {
20             return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
21         } else if ( typeof method === 'object' || ! method ) {
22             return methods.init.apply( this, arguments );
23         } else {
24             return undefined;
25             //$.error( 'Method ' +  method + ' does not exist on jQuery.CreateForm' );
26         }    
27     };
28
29     /***************************************************************************
30      * Public methods
31      ***************************************************************************/
32
33     var methods = {
34
35         /**
36          * @brief Plugin initialization
37          * @param options : an associative array of setting values
38          * @return : a jQuery collection of objects on which the plugin is
39          *     applied, which allows to maintain chainability of calls
40          */
41         init : function ( options ) {
42             return this.each(function() {
43                 var $this = $(this);
44
45                 /* An object that will hold private variables and methods */
46                 var form = new CreateForm(options);
47                 $this.data('plugin', form);
48
49                 $(this).on('validate.Form', form.validate);
50                 $(this).on('validate', form.validate);
51
52             }); // this.each
53         }, // init
54
55     };
56
57     /***************************************************************************
58      * CreateForm object
59      ***************************************************************************/
60
61     function CreateForm(options) {
62
63         /* save a reference to this */
64         var obj = this;
65
66         /* member variables */
67         this.options = options;
68
69         /* methods */
70
71         this.on_result = function(data) {
72
73         }
74         
75         /**
76          * \brief Validate the form
77          * \param validate_callback (function) a callback to be triggered when validation is done
78          * \return True if all fields match validation regex
79          */
80         this.validate = function(validate_callback) {
81             var frm = document.forms['form_'+options.plugin_uuid]
82
83             // $this = $('#' + options.plugin_uuid); // useless
84
85             // Loop on the fields and test regexp if present
86             var err = false;
87             var params = {}
88             $.each(options.columns, function(i, column) {
89                 var value = frm.elements[column['field']].value;
90                 var rx    = column['validate_rx'];
91                 var str   = '';
92                 if (rx && !value.match(rx)) {
93                     str = column['validate_err'];
94                     err = true;
95                 }
96                 params[column['field']] = value;
97                 $('#err-' + options.plugin_uuid + '-' + column['field']).html(str);
98             });
99
100             /* If the form correctly validates, we issue a create query */
101             if (!err) {
102                 /* Inform user about ongoing query: spinner */
103
104                 /* Issue json query and wait for callback */
105                 // this.on_result()
106             }
107
108             /* Note, if the create has already been done (or fails, or ... ?)
109              * shall we proceed to an update ? */
110
111             /* We always return false. Only the query callback is in charge of
112              * advancing to next step */
113             return false;
114         }
115
116     }
117
118 })( jQuery );