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;
69 this.on_result = function(data) {
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
78 this.validate = function(validate_callback) {
79 var frm = document.forms['form_'+options.plugin_uuid]
81 // $this = $('#' + options.plugin_uuid); // useless
83 // Loop on the fields and test regexp if present
86 $.each(options.columns, function(i, column) {
87 var value = frm.elements[column['field']].value;
88 var rx = column['validate_rx'];
90 if (rx && !value.match(rx)) {
91 str = column['validate_err'];
94 params[column['field']] = value;
95 $('#err-' + options.plugin_uuid + '-' + column['field']).html(str);
98 /* If the form correctly validates, we issue a create query */
102 'object': 'local:user',
106 /* Inform user about ongoing query: spinner */
110 /* Issue json query and wait for callback */
111 manifold.forward(query, this.onresult);
114 /* Note, if the create has already been done (or fails, or ... ?)
115 * shall we proceed to an update ? */
117 /* We always return false. Only the query callback is in charge of
118 * advancing to next step */
123 * \brief Disable the form entirely, during a create query for example
125 this.disable = function() {