plugins: updated query_editor
[myslice.git] / plugins / form / static / js / 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         /**
70          * \brief Validate the form
71          * \param validate_callback (function) a callback to be triggered when validation is done
72          * \return True if all fields match validation regex
73          */
74         this.validate = function(validate_callback) {
75             var frm = document.forms['form_'+options.plugin_uuid]
76
77             // $this = $('#' + options.plugin_uuid); // useless
78
79             // Loop on the fields and test regexp if present
80             var err = false;
81             var params = {}
82             $.each(options.fields, function(i, field) {
83                 var value = frm.elements[field['field']].value;
84                 var rx    = field['validate_rx'];
85                 var str   = '';
86                 if (rx && !value.match(rx)) {
87                     str = field['validate_err'];
88                     err = true;
89                 }
90                 params[field['field']] = value;
91                 $('#err-' + options.plugin_uuid + '-' + field['field']).html(str);
92             });
93
94             /* If the form correctly validates, we issue a create query */
95             if (!err) {
96                 var query = {
97                     'action': 'create',
98                     'object': options.object,
99                     'params': params,
100                 };
101
102                 /* Inform user about ongoing query: spinner */
103                 this.enable(false);
104                 manifold.spin($obj);
105
106                 /* Issue json query and wait for callback */
107                 manifold.forward(query, function(data) {
108                     manifold.spin($obj, false);
109                     if (data.code != 0) { // ERROR OR WARNING, which we don't expect
110                         alert("ERROR IN CALLING THE API");
111                         validate_callback(false);
112                         return;
113                     }
114                     validate_callback(true);
115                 });
116             }
117
118             /* Note, if the create has already been done (or fails, or ... ?)
119              * shall we proceed to an update ? */
120
121             /* We always return false. Only the query callback is in charge of
122              * advancing to next step */
123             return false;
124         }
125
126         /**
127          * \brief Disable the form entirely, during a create query for example
128          */
129         this.enable = function(is_enabled) {
130
131         }
132
133     }
134
135 })( jQuery );