X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=plugins%2Fform%2Fform.js;h=e877e57825de0ae1254ff8e7af5a7d1ce6b3b5fb;hb=fba107aec1c17a089ef4af3ab8d149f36dcac5c2;hp=d02779f44eb7436ad10ffce0bf2f748b81a00715;hpb=6656b13932a99246b7e75af4ba2f0a137af1bfe1;p=unfold.git diff --git a/plugins/form/form.js b/plugins/form/form.js index d02779f4..e877e578 100644 --- a/plugins/form/form.js +++ b/plugins/form/form.js @@ -1,23 +1,118 @@ -// Jquery for the Registration page. /views/register/tmpl/deafult.php -jQuery(document).ready(function(){ - - jQuery("#commentForm").validate({ - rules: { - password: { - required: true - }, - confirmpassword: { - required: true, equalTo: "#password" - } +/** + * Description: implements a form + * Copyright (c) 2013 UPMC Sorbonne Universite + * License: GPLv3 + */ + +/* + * It's a best practice to pass jQuery to an IIFE (Immediately Invoked Function + * Expression) that maps it to the dollar sign so it can't be overwritten by + * another library in the scope of its execution. + */ +(function($){ + + /*************************************************************************** + * Method calling logic + ***************************************************************************/ + + $.fn.CreateForm = function( method ) { + if ( methods[method] ) { + return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 )); + } else if ( typeof method === 'object' || ! method ) { + return methods.init.apply( this, arguments ); + } else { + return undefined; + //$.error( 'Method ' + method + ' does not exist on jQuery.CreateForm' ); + } + }; + + /*************************************************************************** + * Public methods + ***************************************************************************/ + + var methods = { + + /** + * @brief Plugin initialization + * @param options : an associative array of setting values + * @return : a jQuery collection of objects on which the plugin is + * applied, which allows to maintain chainability of calls + */ + init : function ( options ) { + return this.each(function() { + var $this = $(this); + + /* An object that will hold private variables and methods */ + var form = new CreateForm(options); + $this.data('plugin', form); + + $(this).on('validate.Form', form.validate); + $(this).on('validate', form.validate); + + }); // this.each + }, // init + + }; + + /*************************************************************************** + * CreateForm object + ***************************************************************************/ + + function CreateForm(options) { + + /* save a reference to this */ + var obj = this; + + /* member variables */ + this.options = options; + + /* methods */ + + this.on_result = function(data) { + } - }); - // upload button - jQuery("#question").change(function(){ - if(this.value=="upload"){ - jQuery("#upload_key").show(); - }else{ - jQuery("#upload_key").hide(); + + /** + * \brief Validate the form + * \param validate_callback (function) a callback to be triggered when validation is done + * \return True if all fields match validation regex + */ + this.validate = function(validate_callback) { + var frm = document.forms['form_'+options.plugin_uuid] + + // $this = $('#' + options.plugin_uuid); // useless + + // Loop on the fields and test regexp if present + var err = false; + var params = {} + $.each(options.columns, function(i, column) { + var value = frm.elements[column['field']].value; + var rx = column['validate_rx']; + var str = ''; + if (rx && !value.match(rx)) { + str = column['validate_err']; + err = true; + } + params[column['field']] = value; + $('#err-' + options.plugin_uuid + '-' + column['field']).html(str); + }); + + /* If the form correctly validates, we issue a create query */ + if (!err) { + /* Inform user about ongoing query: spinner */ + + /* Issue json query and wait for callback */ + // this.on_result() + } + + /* Note, if the create has already been done (or fails, or ... ?) + * shall we proceed to an update ? */ + + /* We always return false. Only the query callback is in charge of + * advancing to next step */ + return false; } - }); -}); + } + +})( jQuery );