updated plugins
[unfold.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.each
50         }, // init
51
52     };
53
54     /***************************************************************************
55      * CreateForm object
56      ***************************************************************************/
57
58     function CreateForm(options) {
59
60         /* save a reference to this */
61         var $this = this;
62         var $obj  = $('#' + options.plugin_uuid);
63
64         /* member variables */
65         this.options = options;
66
67         /* methods */
68
69         this.on_result = function(data) {
70
71         }
72         
73         /**
74          * \brief Validate the form
75          * \param validate_callback (function) a callback to be triggered when validation is done
76          * \return True if all fields match validation regex
77          */
78         this.validate = function(validate_callback) {
79             var frm = document.forms['form_'+options.plugin_uuid]
80
81             // $this = $('#' + options.plugin_uuid); // useless
82
83             // Loop on the fields and test regexp if present
84             var err = false;
85             var params = {}
86             $.each(options.columns, function(i, column) {
87                 var value = frm.elements[column['field']].value;
88                 var rx    = column['validate_rx'];
89                 var str   = '';
90                 if (rx && !value.match(rx)) {
91                     str = column['validate_err'];
92                     err = true;
93                 }
94                 params[column['field']] = value;
95                 $('#err-' + options.plugin_uuid + '-' + column['field']).html(str);
96             });
97
98             /* If the form correctly validates, we issue a create query */
99             if (!err) {
100                 var query = {
101                     'action': 'create',
102                     'object': 'local:user',
103                     'params': params,
104                 };
105
106                 /* Inform user about ongoing query: spinner */
107                 this.disable();
108                 manifold.spin($obj);
109
110                 /* Issue json query and wait for callback */
111                 manifold.forward(query, this.onresult);
112             }
113
114             /* Note, if the create has already been done (or fails, or ... ?)
115              * shall we proceed to an update ? */
116
117             /* We always return false. Only the query callback is in charge of
118              * advancing to next step */
119             return false;
120         }
121
122         /**
123          * \brief Disable the form entirely, during a create query for example
124          */
125         this.disable = function() {
126
127         }
128
129     }
130
131 })( jQuery );