1 |
- (function(e){e.fn.formset=function(t){var n=e.extend({},e.fn.formset.defaults,t),r=e(this),i=r.parent(),s=function(t,n,r){var i=new RegExp("("+n+"-(\\d+|__prefix__))"),s=n+"-"+r;e(t).prop("for")&&e(t).prop("for",e(t).prop("for").replace(i,s)),t.id&&(t.id=t.id.replace(i,s)),t.name&&(t.name=t.name.replace(i,s))},o=e("#id_"+n.prefix+"-TOTAL_FORMS").prop("autocomplete","off"),u=parseInt(o.val(),10),a=e("#id_"+n.prefix+"-MAX_NUM_FORMS").prop("autocomplete","off"),f=a.val()===""||a.val()-o.val()>0;r.each(function(t){e(this).not("."+n.emptyCssClass).addClass(n.formCssClass)});if(r.length&&f){var l;if(r.prop("tagName")=="TR"){var c=this.eq(-1).children().length;i.append('<tr class="'+n.addCssClass+'"><td colspan="'+c+'"><a href="javascript:void(0)">'+n.addText+"</a></tr>"),l=i.find("tr:last a")}else r.filter(":last").after('<div class="'+n.addCssClass+'"><a href="javascript:void(0)">'+n.addText+"</a></div>"),l=r.filter(":last").next().find("a");l.click(function(t){t.preventDefault();var r=e("#id_"+n.prefix+"-TOTAL_FORMS"),i=e("#"+n.prefix+"-empty"),o=i.clone(!0);o.removeClass(n.emptyCssClass).addClass(n.formCssClass).attr("id",n.prefix+"-"+u),o.is("tr")?o.children(":last").append('<div><a class="'+n.deleteCssClass+'" href="javascript:void(0)">'+n.deleteText+"</a></div>"):o.is("ul")||o.is("ol")?o.append('<li><a class="'+n.deleteCssClass+'" href="javascript:void(0)">'+n.deleteText+"</a></li>"):o.children(":first").append('<span><a class="'+n.deleteCssClass+'" href="javascript:void(0)">'+n.deleteText+"</a></span>"),o.find("*").each(function(){s(this,n.prefix,r.val())}),o.insertBefore(e(i)),e(r).val(parseInt(r.val(),10)+1),u+=1,a.val()!==""&&a.val()-r.val()<=0&&l.parent().hide(),o.find("a."+n.deleteCssClass).click(function(t){t.preventDefault();var r=e(this).parents("."+n.formCssClass);r.remove(),u-=1,n.removed&&n.removed(r);var i=e("."+n.formCssClass);e("#id_"+n.prefix+"-TOTAL_FORMS").val(i.length),(a.val()===""||a.val()-i.length>0)&&l.parent().show();for(var o=0,f=i.length;o<f;o++)s(e(i).get(o),n.prefix,o),e(i.get(o)).find("*").each(function(){s(this,n.prefix,o)})}),n.added&&n.added(o)})}return this},e.fn.formset.defaults={prefix:"form",addText:"add another",deleteText:"remove",addCssClass:"add-row",deleteCssClass:"delete-row",emptyCssClass:"empty-row",formCssClass:"dynamic-form",added:null,removed:null},e.fn.tabularFormset=function(t){var n=e(this),r=function(t){e(n.selector).not(".add-row").removeClass("row1 row2").filter(":even").addClass("row1").end().filter(":odd").addClass("row2")},i=function(){typeof DateTimeShortcuts!="undefined"&&(e(".datetimeshortcuts").remove(),DateTimeShortcuts.init())},s=function(){typeof SelectFilter!="undefined"&&(e(".selectfilter").each(function(e,n){var r=n.name.split("-");SelectFilter.init(n.id,r[r.length-1],!1,t.adminStaticPrefix)}),e(".selectfilterstacked").each(function(e,n){var r=n.name.split("-");SelectFilter.init(n.id,r[r.length-1],!0,t.adminStaticPrefix)}))},o=function(t){t.find(".prepopulated_field").each(function(){var n=e(this),r=n.find("input, select, textarea"),i=r.data("dependency_list")||[],s=[];e.each(i,function(e,n){s.push("#"+t.find(".field-"+n).find("input, select, textarea").attr("id"))}),s.length&&r.prepopulate(s,r.attr("maxlength"))})};return n.formset({prefix:t.prefix,addText:t.addText,formCssClass:"dynamic-"+t.prefix,deleteCssClass:"inline-deletelink",deleteText:t.deleteText,emptyCssClass:"empty-form",removed:r,added:function(e){o(e),i(),s(),r(e)}}),n},e.fn.stackedFormset=function(t){var n=e(this),r=function(t){e(n.selector).find(".inline_label").each(function(t){var n=t+1;e(this).html(e(this).html().replace(/(#\d+)/g,"#"+n))})},i=function(){typeof DateTimeShortcuts!="undefined"&&(e(".datetimeshortcuts").remove(),DateTimeShortcuts.init())},s=function(){typeof SelectFilter!="undefined"&&(e(".selectfilter").each(function(e,n){var r=n.name.split("-");SelectFilter.init(n.id,r[r.length-1],!1,t.adminStaticPrefix)}),e(".selectfilterstacked").each(function(e,n){var r=n.name.split("-");SelectFilter.init(n.id,r[r.length-1],!0,t.adminStaticPrefix)}))},o=function(t){t.find(".prepopulated_field").each(function(){var n=e(this),r=n.find("input, select, textarea"),i=r.data("dependency_list")||[],s=[];e.each(i,function(e,n){s.push("#"+t.find(".form-row .field-"+n).find("input, select, textarea").attr("id"))}),s.length&&r.prepopulate(s,r.attr("maxlength"))})};return n.formset({prefix:t.prefix,addText:t.addText,formCssClass:"dynamic-"+t.prefix,deleteCssClass:"inline-deletelink",deleteText:t.deleteText,emptyCssClass:"empty-form",removed:function(e){r(e)},added:function(t){o(t),i(),s(),r(t);var n=t.closest(".stacked-inline").find(".stacked-inline-list"),u=n.find(".stacked-inline-list-item.empty"),a=u.clone();t.find(".inline-deletelink").remove(),a.removeClass("empty"),a.find(".stacked-inline-list-item-link").attr("data-inline-related-id",t.attr("id")),u.before(a),n.find(".inline_label").each(function(t){var n=t+1;e(this).html(e(this).html().replace(/(#\d+)/g,"#"+n))})}}),n}})(django.jQuery);
|