2 * Description: implements a form
3 * Copyright (c) 2013 UPMC Sorbonne Universite
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.
14 /***************************************************************************
15 * Method calling logic
16 ***************************************************************************/
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 );
25 //$.error( 'Method ' + method + ' does not exist on jQuery.CreateForm' );
29 /***************************************************************************
31 ***************************************************************************/
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
41 init : function ( options ) {
42 return this.each(function() {
45 /* An object that will hold private variables and methods */
46 var form = new CreateForm(options);
47 $this.data('plugin', form);
54 /***************************************************************************
56 ***************************************************************************/
58 function CreateForm(options) {
60 /* save a reference to this */
62 var $obj = $('#' + options.plugin_uuid);
64 /* member variables */
65 this.options = options;
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
74 this.validate = function(validate_callback) {
75 var frm = document.forms['form_'+options.plugin_uuid]
77 // $this = $('#' + options.plugin_uuid); // useless
79 // Loop on the fields and test regexp if present
82 $.each(options.fields, function(i, field) {
83 var value = frm.elements[field['field']].value;
84 var rx = field['validate_rx'];
86 if (rx && !value.match(rx)) {
87 str = field['validate_err'];
90 params[field['field']] = value;
91 $('#err-' + options.plugin_uuid + '-' + field['field']).html(str);
94 /* If the form correctly validates, we issue a create query */
98 'object': options.object,
102 /* Inform user about ongoing query: spinner */
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);
114 validate_callback(true);
118 /* Note, if the create has already been done (or fails, or ... ?)
119 * shall we proceed to an update ? */
121 /* We always return false. Only the query callback is in charge of
122 * advancing to next step */
127 * \brief Disable the form entirely, during a create query for example
129 this.enable = function(is_enabled) {