project/static/jquery/jquery-dynamic-form.js
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 13 Jul 2010 17:00:55 +0530
changeset 106 7ebd3a7e8ccf
parent 91 dd6426394a9a
permissions -rw-r--r--
Revert all leading /2009 from URL Patters since this is now handled in Apache on server.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
91
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
/**
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
 * @author Stéphane Roucheray
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
 * @extends jQuery
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
 */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
jQuery.fn.dynamicForm = function (plusElmnt, minusElmnt, options){
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
	var source = jQuery(this),
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
	minus = jQuery(minusElmnt),
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
	plus = jQuery(plusElmnt),
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
	template = source.clone(true),
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
	fieldId = 0,
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
	formFields = "input, checkbox, select, textarea",
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
	insertBefore = source.next(),
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
	clones = [],
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
	defaults = {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
		duration:1000
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
  	};
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
	
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
  	// Extend default options with those provided
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
  	options = $.extend(defaults, options);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
	
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
	isPlusDescendentOfTemplate = source.find("*").filter(function(){
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
		return this == plus.get(0);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
	});
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
	
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
	isPlusDescendentOfTemplate = isPlusDescendentOfTemplate.length > 0 ? true : false;
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
	
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    28
	function normalizeElmnt(elmnt){
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    29
        elmnt.find(formFields).each(function(){
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    30
            var nameAttr = jQuery(this).attr("name"), 
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    31
			idAttr = jQuery(this).attr("id");
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    32
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    33
            /* Normalize field name attributes */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    34
            if (!nameAttr) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    35
				jQuery(this).attr("name", "field" + fieldId + "[]");
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    36
			}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    37
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    38
			if (!/\[\]$/.exec(nameAttr)) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    39
				jQuery(this).attr("name", nameAttr + "[]");
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    40
			}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    41
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    42
            /* Normalize field id attributes */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    43
            if (idAttr) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    44
				/* Normalize attached label */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    45
				jQuery("label[for='"+idAttr+"']").each(function(){
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    46
					jQuery(this).attr("for", idAttr + fieldId);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    47
				});
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    48
				
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    49
                jQuery(this).attr("id", idAttr + fieldId);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    50
            }
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    51
            fieldId++;
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    52
        });
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    53
    };
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    54
	
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    55
	/* Hide minus element */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    56
	minus.hide();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    57
	
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    58
	/* If plus element is within the template */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    59
	if (isPlusDescendentOfTemplate) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    60
		function clickOnPlus(event){
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    61
			var clone,
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    62
			currentClone = clones[clones.length -1] || source;
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    63
			event.preventDefault();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    64
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    65
			/* On first add, normalize source */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    66
			if (clones.length == 0) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    67
				normalizeElmnt(source);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    68
				currentClone.find(minusElmnt).hide();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    69
				currentClone.find(plusElmnt).hide();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    70
			}else{
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    71
				currentClone.find(plusElmnt).hide();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    72
			}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    73
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    74
			/* Clone template and normalize it */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    75
			clone = template.clone(true).insertAfter(clones[clones.length - 1] || source);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    76
			normalizeElmnt(clone);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    77
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    78
			/* Normalize template id attribute */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    79
			if (clone.attr("id")) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    80
				clone.attr("id", clone.attr("id") + clones.length);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    81
			}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    82
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    83
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    84
			plus = clone.find(plusElmnt);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    85
			minus = clone.find(minusElmnt);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    86
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    87
			minus.get(0).removableClone = clone;
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    88
			minus.click(clickOnMinus);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    89
			plus.click(clickOnPlus);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    90
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    91
			if (options.limit && (options.limit - 2) > clones.length) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    92
				plus.show();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    93
			}else{
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    94
				plus.hide();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    95
			}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    96
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    97
			clones.push(clone);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    98
		}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    99
		
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   100
		function clickOnMinus(event){
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   101
			event.preventDefault();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   102
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   103
			if (this.removableClone.effect && options.removeColor) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   104
				that = this;
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   105
				this.removableClone.effect("highlight", {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   106
					color: options.removeColor
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   107
				}, options.duration, function(){that.removableClone.remove();});
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   108
			} else {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   109
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   110
				this.removableClone.remove();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   111
			}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   112
			clones.splice(clones.indexOf(this.removableClone),1);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   113
			if (clones.length == 0){
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   114
				source.find(plusElmnt).show();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   115
			}else{
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   116
				clones[clones.length -1].find(plusElmnt).show();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   117
			}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   118
		}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   119
		
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   120
		/* Handle click on plus */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   121
		plus.click(clickOnPlus);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   122
		
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   123
		/* Handle click on minus */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   124
		minus.click(function(event){
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   125
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   126
		});
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   127
		
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   128
	}else{
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   129
	/* If plus element is out of the template */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   130
		/* Handle click on plus */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   131
		plus.click(function(event){
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   132
			var clone;
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   133
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   134
			event.preventDefault();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   135
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   136
			/* On first add, normalize source */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   137
			if (clones.length == 0) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   138
				normalizeElmnt(source);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   139
				jQuery(minusElmnt).show();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   140
			}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   141
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   142
			/* Clone template and normalize it */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   143
			clone = template.clone(true).insertAfter(clones[clones.length - 1] || source);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   144
			if (clone.effect && options.createColor) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   145
				clone.effect("highlight", {color:options.createColor}, options.duration);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   146
			}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   147
			normalizeElmnt(clone);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   148
			
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   149
			/* Normalize template id attribute */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   150
			if (clone.attr("id")) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   151
				clone.attr("id", clone.attr("id") + clones.length);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   152
			}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   153
			if (options.limit && (options.limit - 3) < clones.length) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   154
				plus.hide();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   155
			}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   156
			clones.push(clone);
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   157
		});
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   158
		
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   159
		/* Handle click on minus */
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   160
		minus.click(function(event){
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   161
			event.preventDefault();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   162
			var clone = clones.pop();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   163
			if (clones.length >= 0) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   164
				if (clone.effect && options.removeColor) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   165
					that = this;
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   166
					clone.effect("highlight", {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   167
						color: options.removeColor, mode:"hide"
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   168
					}, options.duration, function(){clone.remove();});
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   169
				} else {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   170
					clone.remove();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   171
				}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   172
			}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   173
			if (clones.length == 0) {
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   174
				jQuery(minusElmnt).hide();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   175
			}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   176
			plus.show();
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   177
		});
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   178
	}
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   179
	
dd6426394a9a Added dynamic form jquery plugin to the repository.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   180
};