ajout de django-qbe pour tests
[auf_rh_dae.git] / project / media / django_qbe / js / jquery.formset.js
1 /**
2 * jQuery Formset 1.1
3 * @author Stanislaus Madueke (stan DOT madueke AT gmail DOT com)
4 * @requires jQuery 1.2.6 or later
5 *
6 * Copyright (c) 2009, Stanislaus Madueke
7 * All rights reserved.
8 *
9 * Licensed under the New BSD License
10 * See: http://www.opensource.org/licenses/bsd-license.php
11 */
12 ;(function($) {
13 $.fn.formset = function(opts)
14 {
15 var options = $.extend({}, $.fn.formset.defaults, opts),
16 flatExtraClasses = options.extraClasses.join(' '),
17 totalForms = $('#id_' + options.prefix + '-TOTAL_FORMS'),
18 maxForms = $('#id_' + options.prefix + '-MAX_NUM_FORMS'),
19 $$ = $(this),
20
21 applyExtraClasses = function(row, ndx) {
22 if (options.extraClasses) {
23 row.removeClass(flatExtraClasses);
24 row.addClass(options.extraClasses[ndx % options.extraClasses.length]);
25 }
26 },
27
28 updateElementIndex = function(elem, prefix, ndx) {
29 var idRegex = new RegExp('(' + prefix + '-\\d+-)|(^)'),
30 replacement = prefix + '-' + ndx + '-';
31 if (elem.attr("for")) elem.attr("for", elem.attr("for").replace(idRegex, replacement));
32 if (elem.attr('id')) elem.attr('id', elem.attr('id').replace(idRegex, replacement));
33 if (elem.attr('name')) elem.attr('name', elem.attr('name').replace(idRegex, replacement));
34 },
35
36 hasChildElements = function(row) {
37 return row.find('input,select,textarea,label').length > 0;
38 },
39
40 showAddButton = function() {
41 return maxForms.length == 0 || // For Django versions pre 1.2
42 (maxForms.val() == '' || (maxForms.val() - totalForms.val() > 0))
43 },
44
45 insertDeleteLink = function(row) {
46 if (row.is('TR')) {
47 // If the forms are laid out in table rows, insert
48 // the remove button into the last table cell:
49 row.children(':last').append('<a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + '</a>');
50 } else if (row.is('UL') || row.is('OL')) {
51 // If they're laid out as an ordered/unordered list,
52 // insert an <li> after the last list item:
53 row.append('<li><a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText +'</a></li>');
54 } else {
55 // Otherwise, just insert the remove button as the
56 // last child element of the form's container:
57 row.append('<a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText +'</a>');
58 }
59 row.find('a.' + options.deleteCssClass).click(function() {
60 var row = $(this).parents('.' + options.formCssClass),
61 del = row.find('input:hidden[id $= "-DELETE"]'),
62 buttonRow = row.siblings("a." + options.addCssClass + ', .' + options.formCssClass + '-add'),
63 forms;
64 if (del.length) {
65 // We're dealing with an inline formset.
66 // Rather than remove this form from the DOM, we'll mark it as deleted
67 // and hide it, then let Django handle the deleting:
68 del.val('on');
69 row.hide();
70 forms = $('.' + options.formCssClass).not(':hidden');
71 } else {
72 row.remove();
73 // Update the TOTAL_FORMS count:
74 forms = $('.' + options.formCssClass).not('.formset-custom-template');
75 totalForms.val(forms.length);
76 }
77 // Apply extraClasses to form rows so they're nicely alternating.
78 // Also update names and IDs for all child controls, if this isn't a delete-able
79 // inline formset, so they remain in sequence.
80 for (var i=0, formCount=forms.length; i<formCount; i++) {
81 applyExtraClasses(forms.eq(i), i);
82 if (!del.length) {
83 forms.eq(i).find('input,select,textarea,label').each(function() {
84 updateElementIndex($(this), options.prefix, i);
85 });
86 }
87 }
88 // Check if we need to show the add button:
89 if (buttonRow.is(':hidden') && showAddButton()) buttonRow.show();
90 // If a post-delete callback was provided, call it with the deleted form:
91 if (options.removed) options.removed(row);
92 return false;
93 });
94 };
95
96 $$.each(function(i) {
97 var row = $(this),
98 del = row.find('input:checkbox[id $= "-DELETE"]');
99 if (del.length) {
100 // If you specify "can_delete = True" when creating an inline formset,
101 // Django adds a checkbox to each form in the formset.
102 // Replace the default checkbox with a hidden field:
103 if (del.is(':checked')) {
104 // If an inline formset containing deleted forms fails validation, make sure
105 // we keep the forms hidden (thanks for the bug report and suggested fix Mike)
106 del.before('<input type="hidden" name="' + del.attr('name') +'" id="' + del.attr('id') +'" value="on" />');
107 row.hide();
108 } else {
109 del.before('<input type="hidden" name="' + del.attr('name') +'" id="' + del.attr('id') +'" />');
110 }
111 // Hide any labels associated with the DELETE checkbox:
112 $('label[for="' + del.attr('id') + '"]').hide();
113 del.remove();
114 }
115 if (hasChildElements(row)) {
116 row.addClass(options.formCssClass);
117 if (row.is(':visible')) {
118 insertDeleteLink(row);
119 applyExtraClasses(row, i);
120 }
121 }
122 });
123
124 if ($$.length) {
125 var hideAddButton = !showAddButton(),
126 addButton, template;
127 if (options.formTemplate) {
128 // If a form template was specified, we'll clone it to generate new form instances:
129 template = (options.formTemplate instanceof $) ? options.formTemplate : $(options.formTemplate);
130 template.removeAttr('id').addClass(options.formCssClass + ' formset-custom-template');
131 template.find('input,select,textarea,label').each(function() {
132 updateElementIndex($(this), options.prefix, 2012);
133 });
134 insertDeleteLink(template);
135 } else {
136 // Otherwise, use the last form in the formset; this works much better if you've got
137 // extra (>= 1) forms (thnaks to justhamade for pointing this out):
138 template = $('.' + options.formCssClass + ':last').clone(true).removeAttr('id');
139 template.find('input:hidden[id $= "-DELETE"]').remove();
140 template.find('input,select,textarea,label').each(function() {
141 var elem = $(this);
142 // If this is a checkbox or radiobutton, uncheck it.
143 // This fixes Issue 1, reported by Wilson.Andrew.J:
144 if (elem.is('input:checkbox') || elem.is('input:radio')) {
145 elem.attr('checked', false);
146 } else {
147 elem.val('');
148 }
149 });
150 }
151 // FIXME: Perhaps using $.data would be a better idea?
152 options.formTemplate = template;
153
154 if ($$.attr('tagName') == 'TR') {
155 // If forms are laid out as table rows, insert the
156 // "add" button in a new table row:
157 var numCols = $$.eq(0).children().length, // This is a bit of an assumption :|
158 buttonRow = $('<tr><td colspan="' + numCols + '"><a class="' + options.addCssClass + '" href="javascript:void(0)">' + options.addText + '</a></tr>')
159 .addClass(options.formCssClass + '-add');
160 $$.parent().append(buttonRow);
161 if (hideAddButton) buttonRow.hide();
162 addButton = buttonRow.find('a');
163 } else {
164 // Otherwise, insert it immediately after the last form:
165 $$.filter(':last').after('<a class="' + options.addCssClass + '" href="javascript:void(0)">' + options.addText + '</a>');
166 addButton = $$.filter(':last').next();
167 if (hideAddButton) addButton.hide();
168 }
169 addButton.click(function() {
170 var formCount = parseInt(totalForms.val()),
171 row = options.formTemplate.clone(true).removeClass('formset-custom-template'),
172 buttonRow = $($(this).parents('tr.' + options.formCssClass + '-add').get(0) || this);
173 applyExtraClasses(row, formCount);
174 row.insertBefore(buttonRow).show();
175 row.find('input,select,textarea,label').each(function() {
176 updateElementIndex($(this), options.prefix, formCount);
177 });
178 totalForms.val(formCount + 1);
179 // Check if we've exceeded the maximum allowed number of forms:
180 if (!showAddButton()) buttonRow.hide();
181 // If a post-add callback was supplied, call it with the added form:
182 if (options.added) options.added(row);
183 return false;
184 });
185 }
186
187 return $$;
188 }
189
190 /* Setup plugin defaults */
191 $.fn.formset.defaults = {
192 prefix: 'form', // The form prefix for your django formset
193 formTemplate: null, // The jQuery selection cloned to generate new form instances
194 addText: 'add another', // Text for the add link
195 deleteText: 'remove', // Text for the delete link
196 addCssClass: 'add-row', // CSS class applied to the add link
197 deleteCssClass: 'delete-row', // CSS class applied to the delete link
198 formCssClass: 'dynamic-form', // CSS class applied to each form in a formset
199 extraClasses: [], // Additional CSS classes, which will be applied to each form in turn
200 added: null, // Function called each time a new form is added
201 removed: null // Function called each time a form is deleted
202 };
203 })(jQuery)