mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2024-11-25 08:41:51 +00:00
9 lines
4.0 KiB
JavaScript
9 lines
4.0 KiB
JavaScript
;(function($){$.fn.formset=function(opts)
|
|
{var options=$.extend({},$.fn.formset.defaults,opts),flatExtraClasses=options.extraClasses.join(' '),$$=$(this),applyExtraClasses=function(row,ndx){if(options.extraClasses){row.removeClass(flatExtraClasses);row.addClass(options.extraClasses[ndx%options.extraClasses.length]);}},updateElementIndex=function(elem,prefix,ndx){var idRegex=new RegExp('('+prefix+'-\\d+-)|(^)'),replacement=prefix+'-'+ndx+'-';if(elem.attr("for"))elem.attr("for",elem.attr("for").replace(idRegex,replacement));if(elem.attr('id'))elem.attr('id',elem.attr('id').replace(idRegex,replacement));if(elem.attr('name'))elem.attr('name',elem.attr('name').replace(idRegex,replacement));},hasChildElements=function(row){return row.find('input,select,textarea,label').length>0;},insertDeleteLink=function(row){if(row.is('TR')){row.children(':last').append('<a class="'+options.deleteCssClass+'" href="javascript:void(0)">'+options.deleteText+'</a>');}else if(row.is('UL')||row.is('OL')){row.append('<li><a class="'+options.deleteCssClass+'" href="javascript:void(0)">'+options.deleteText+'</a></li>');}else{row.append('<a class="'+options.deleteCssClass+'" href="javascript:void(0)">'+options.deleteText+'</a>');}
|
|
row.find('a.'+options.deleteCssClass).click(function(){var row=$(this).parents('.'+options.formCssClass),del=row.find('input:hidden[id $= "-DELETE"]');if(del.length){del.val('on');row.hide();}else{row.remove();var forms=$('.'+options.formCssClass).not('.formset-custom-template');$('#id_'+options.prefix+'-TOTAL_FORMS').val(forms.length);for(var i=0,formCount=forms.length;i<formCount;i++){applyExtraClasses(forms.eq(i),i);forms.eq(i).find('input,select,textarea,label').each(function(){updateElementIndex($(this),options.prefix,i);});}}
|
|
if(options.removed)options.removed(row);return false;});};$$.each(function(i){var row=$(this),del=row.find('input:checkbox[id $= "-DELETE"]');if(del.length){del.before('<input type="hidden" name="'+del.attr('name')+'" id="'+del.attr('id')+'" />');del.remove();}
|
|
if(hasChildElements(row)){insertDeleteLink(row);row.addClass(options.formCssClass);applyExtraClasses(row,i);}});if($$.length){var addButton,template;if(options.formTemplate){template=(options.formTemplate instanceof $)?options.formTemplate:$(options.formTemplate);template.removeAttr('id').addClass(options.formCssClass).addClass('formset-custom-template');template.find('input,select,textarea,label').each(function(){updateElementIndex($(this),options.prefix,2012);});insertDeleteLink(template);}else{template=$('.'+options.formCssClass+':last').clone(true).removeAttr('id');template.find('input:hidden[id $= "-DELETE"]').remove();template.find('input,select,textarea,label').each(function(){var elem=$(this);if(elem.is('input:checkbox')||elem.is('input:radio')){elem.attr('checked',false);}else{elem.val('');}});}
|
|
options.formTemplate=template;if($$.attr('tagName')=='TR'){var numCols=$$.eq(0).children().length;$$.parent().append('<tr><td colspan="'+numCols+'"><a class="'+options.addCssClass+'" href="javascript:void(0)">'+options.addText+'</a></tr>');addButton=$$.parent().find('tr:last a');addButton.parents('tr').addClass(options.formCssClass+'-add');}else{$$.filter(':last').after('<a class="'+options.addCssClass+'" href="javascript:void(0)">'+options.addText+'</a>');addButton=$$.filter(':last').next();}
|
|
addButton.click(function(){var formCount=parseInt($('#id_'+options.prefix+'-TOTAL_FORMS').val()),row=options.formTemplate.clone(true).removeClass('formset-custom-template'),buttonRow=$(this).parents('tr.'+options.formCssClass+'-add').get(0)||this;applyExtraClasses(row,formCount);row.insertBefore($(buttonRow)).show();row.find('input,select,textarea,label').each(function(){updateElementIndex($(this),options.prefix,formCount);});$('#id_'+options.prefix+'-TOTAL_FORMS').val(formCount+1);if(options.added)options.added(row);return false;});}
|
|
return $$;}
|
|
$.fn.formset.defaults={prefix:'form',formTemplate:null,addText:'add another',deleteText:'remove',addCssClass:'add-row',deleteCssClass:'delete-row',formCssClass:'dynamic-form',extraClasses:[],added:null,removed:null};})(jQuery) |