inlines.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * Django admin inlines
  3. *
  4. * Based on jQuery Formset 1.1
  5. * @author Stanislaus Madueke (stan DOT madueke AT gmail DOT com)
  6. * @requires jQuery 1.2.6 or later
  7. *
  8. * Copyright (c) 2009, Stanislaus Madueke
  9. * All rights reserved.
  10. *
  11. * Spiced up with Code from Zain Memon's GSoC project 2009
  12. * and modified for Django by Jannis Leidel, Travis Swicegood and Julien Phalip.
  13. *
  14. * Licensed under the New BSD License
  15. * See: http://www.opensource.org/licenses/bsd-license.php
  16. */
  17. (function($) {
  18. $.fn.formset = function(opts) {
  19. var options = $.extend({}, $.fn.formset.defaults, opts);
  20. var $this = $(this);
  21. var $parent = $this.parent();
  22. var updateElementIndex = function(el, prefix, ndx) {
  23. var id_regex = new RegExp("(" + prefix + "-(\\d+|__prefix__))");
  24. var replacement = prefix + "-" + ndx;
  25. if ($(el).prop("for")) {
  26. $(el).prop("for", $(el).prop("for").replace(id_regex, replacement));
  27. }
  28. if (el.id) {
  29. el.id = el.id.replace(id_regex, replacement);
  30. }
  31. if (el.name) {
  32. el.name = el.name.replace(id_regex, replacement);
  33. }
  34. };
  35. var totalForms = $("#id_" + options.prefix + "-TOTAL_FORMS").prop("autocomplete", "off");
  36. var nextIndex = parseInt(totalForms.val(), 10);
  37. var maxForms = $("#id_" + options.prefix + "-MAX_NUM_FORMS").prop("autocomplete", "off");
  38. // only show the add button if we are allowed to add more items,
  39. // note that max_num = None translates to a blank string.
  40. var showAddButton = maxForms.val() === '' || (maxForms.val()-totalForms.val()) > 0;
  41. $this.each(function(i) {
  42. $(this).not("." + options.emptyCssClass).addClass(options.formCssClass);
  43. });
  44. if ($this.length && showAddButton) {
  45. var addButton;
  46. if ($this.prop("tagName") == "TR") {
  47. // If forms are laid out as table rows, insert the
  48. // "add" button in a new table row:
  49. var numCols = this.eq(-1).children().length;
  50. $parent.append('<tr class="' + options.addCssClass + '"><td colspan="' + numCols + '"><a href="javascript:void(0)">' + options.addText + "</a></tr>");
  51. addButton = $parent.find("tr:last a");
  52. } else {
  53. // Otherwise, insert it immediately after the last form:
  54. $this.filter(":last").after('<div class="' + options.addCssClass + '"><a href="javascript:void(0)">' + options.addText + "</a></div>");
  55. addButton = $this.filter(":last").next().find("a");
  56. }
  57. addButton.click(function(e) {
  58. e.preventDefault();
  59. var totalForms = $("#id_" + options.prefix + "-TOTAL_FORMS");
  60. var template = $("#" + options.prefix + "-empty");
  61. var row = template.clone(true);
  62. row.removeClass(options.emptyCssClass)
  63. .addClass(options.formCssClass)
  64. .attr("id", options.prefix + "-" + nextIndex);
  65. if (row.is("tr")) {
  66. // If the forms are laid out in table rows, insert
  67. // the remove button into the last table cell:
  68. row.children(":last").append('<div><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></div>");
  69. } else if (row.is("ul") || row.is("ol")) {
  70. // If they're laid out as an ordered/unordered list,
  71. // insert an <li> after the last list item:
  72. row.append('<li><a class="' + options.deleteCssClass +'" href="javascript:void(0)">' + options.deleteText + "</a></li>");
  73. } else {
  74. // Otherwise, just insert the remove button as the
  75. // last child element of the form's container:
  76. row.children(":first").append('<span><a class="' + options.deleteCssClass + '" href="javascript:void(0)">' + options.deleteText + "</a></span>");
  77. }
  78. row.find("*").each(function() {
  79. updateElementIndex(this, options.prefix, totalForms.val());
  80. });
  81. // Insert the new form when it has been fully edited
  82. row.insertBefore($(template));
  83. // Update number of total forms
  84. $(totalForms).val(parseInt(totalForms.val(), 10) + 1);
  85. nextIndex += 1;
  86. // Hide add button in case we've hit the max, except we want to add infinitely
  87. if ((maxForms.val() !== '') && (maxForms.val()-totalForms.val()) <= 0) {
  88. addButton.parent().hide();
  89. }
  90. // The delete button of each row triggers a bunch of other things
  91. row.find("a." + options.deleteCssClass).click(function(e) {
  92. e.preventDefault();
  93. // Remove the parent form containing this button:
  94. var row = $(this).parents("." + options.formCssClass);
  95. row.remove();
  96. nextIndex -= 1;
  97. // If a post-delete callback was provided, call it with the deleted form:
  98. if (options.removed) {
  99. options.removed(row);
  100. }
  101. // Update the TOTAL_FORMS form count.
  102. var forms = $("." + options.formCssClass);
  103. $("#id_" + options.prefix + "-TOTAL_FORMS").val(forms.length);
  104. // Show add button again once we drop below max
  105. if ((maxForms.val() === '') || (maxForms.val()-forms.length) > 0) {
  106. addButton.parent().show();
  107. }
  108. // Also, update names and ids for all remaining form controls
  109. // so they remain in sequence:
  110. for (var i=0, formCount=forms.length; i<formCount; i++)
  111. {
  112. updateElementIndex($(forms).get(i), options.prefix, i);
  113. $(forms.get(i)).find("*").each(function() {
  114. updateElementIndex(this, options.prefix, i);
  115. });
  116. }
  117. });
  118. // If a post-add callback was supplied, call it with the added form:
  119. if (options.added) {
  120. options.added(row);
  121. }
  122. });
  123. }
  124. return this;
  125. };
  126. /* Setup plugin defaults */
  127. $.fn.formset.defaults = {
  128. prefix: "form", // The form prefix for your django formset
  129. addText: "add another", // Text for the add link
  130. deleteText: "remove", // Text for the delete link
  131. addCssClass: "add-row", // CSS class applied to the add link
  132. deleteCssClass: "delete-row", // CSS class applied to the delete link
  133. emptyCssClass: "empty-row", // CSS class applied to the empty row
  134. formCssClass: "dynamic-form", // CSS class applied to each form in a formset
  135. added: null, // Function called each time a new form is added
  136. removed: null // Function called each time a form is deleted
  137. };
  138. // Tabular inlines ---------------------------------------------------------
  139. $.fn.tabularFormset = function(options) {
  140. var $rows = $(this);
  141. var alternatingRows = function(row) {
  142. $($rows.selector).not(".add-row").removeClass("row1 row2")
  143. .filter(":even").addClass("row1").end()
  144. .filter(":odd").addClass("row2");
  145. };
  146. var reinitDateTimeShortCuts = function() {
  147. // Reinitialize the calendar and clock widgets by force
  148. if (typeof DateTimeShortcuts != "undefined") {
  149. $(".datetimeshortcuts").remove();
  150. DateTimeShortcuts.init();
  151. }
  152. };
  153. var updateSelectFilter = function() {
  154. // If any SelectFilter widgets are a part of the new form,
  155. // instantiate a new SelectFilter instance for it.
  156. if (typeof SelectFilter != 'undefined'){
  157. $('.selectfilter').each(function(index, value){
  158. var namearr = value.name.split('-');
  159. SelectFilter.init(value.id, namearr[namearr.length-1], false, options.adminStaticPrefix );
  160. });
  161. $('.selectfilterstacked').each(function(index, value){
  162. var namearr = value.name.split('-');
  163. SelectFilter.init(value.id, namearr[namearr.length-1], true, options.adminStaticPrefix );
  164. });
  165. }
  166. };
  167. var initPrepopulatedFields = function(row) {
  168. row.find('.prepopulated_field').each(function() {
  169. var field = $(this),
  170. input = field.find('input, select, textarea'),
  171. dependency_list = input.data('dependency_list') || [],
  172. dependencies = [];
  173. $.each(dependency_list, function(i, field_name) {
  174. dependencies.push('#' + row.find('.field-' + field_name).find('input, select, textarea').attr('id'));
  175. });
  176. if (dependencies.length) {
  177. input.prepopulate(dependencies, input.attr('maxlength'));
  178. }
  179. });
  180. };
  181. $rows.formset({
  182. prefix: options.prefix,
  183. addText: options.addText,
  184. formCssClass: "dynamic-" + options.prefix,
  185. deleteCssClass: "inline-deletelink",
  186. deleteText: options.deleteText,
  187. emptyCssClass: "empty-form",
  188. removed: alternatingRows,
  189. added: function(row) {
  190. initPrepopulatedFields(row);
  191. reinitDateTimeShortCuts();
  192. updateSelectFilter();
  193. alternatingRows(row);
  194. }
  195. });
  196. return $rows;
  197. };
  198. // Stacked inlines ---------------------------------------------------------
  199. $.fn.stackedFormset = function(options) {
  200. var $rows = $(this);
  201. var updateInlineLabel = function(row) {
  202. $($rows.selector).find(".inline_label").each(function(i) {
  203. var count = i + 1;
  204. $(this).html($(this).html().replace(/(#\d+)/g, "#" + count));
  205. });
  206. };
  207. var reinitDateTimeShortCuts = function() {
  208. // Reinitialize the calendar and clock widgets by force, yuck.
  209. if (typeof DateTimeShortcuts != "undefined") {
  210. $(".datetimeshortcuts").remove();
  211. DateTimeShortcuts.init();
  212. }
  213. };
  214. var updateSelectFilter = function() {
  215. // If any SelectFilter widgets were added, instantiate a new instance.
  216. if (typeof SelectFilter != "undefined"){
  217. $(".selectfilter").each(function(index, value){
  218. var namearr = value.name.split('-');
  219. SelectFilter.init(value.id, namearr[namearr.length-1], false, options.adminStaticPrefix);
  220. });
  221. $(".selectfilterstacked").each(function(index, value){
  222. var namearr = value.name.split('-');
  223. SelectFilter.init(value.id, namearr[namearr.length-1], true, options.adminStaticPrefix);
  224. });
  225. }
  226. };
  227. var initPrepopulatedFields = function(row) {
  228. row.find('.prepopulated_field').each(function() {
  229. var field = $(this),
  230. input = field.find('input, select, textarea'),
  231. dependency_list = input.data('dependency_list') || [],
  232. dependencies = [];
  233. $.each(dependency_list, function(i, field_name) {
  234. dependencies.push('#' + row.find('.form-row .field-' + field_name).find('input, select, textarea').attr('id'));
  235. });
  236. if (dependencies.length) {
  237. input.prepopulate(dependencies, input.attr('maxlength'));
  238. }
  239. });
  240. };
  241. $rows.formset({
  242. prefix: options.prefix,
  243. addText: options.addText,
  244. formCssClass: "dynamic-" + options.prefix,
  245. deleteCssClass: "inline-deletelink",
  246. deleteText: options.deleteText,
  247. emptyCssClass: "empty-form",
  248. removed: (function(row) {
  249. //hack
  250. updateInlineLabel(row);
  251. }),
  252. added: (function(row) {
  253. initPrepopulatedFields(row);
  254. reinitDateTimeShortCuts();
  255. updateSelectFilter();
  256. updateInlineLabel(row);
  257. //hack
  258. var $items = row.closest('.stacked-inline').find('.stacked-inline-list');
  259. var $emptyItem = $items.find('.stacked-inline-list-item.empty');
  260. var $item = $emptyItem.clone();
  261. row.find('.inline-deletelink').remove();
  262. $item.removeClass('empty');
  263. $item.find('.stacked-inline-list-item-link').attr('data-inline-related-id', row.attr('id'));
  264. $emptyItem.before($item);
  265. $items.find(".inline_label").each(function(i) {
  266. var count = i + 1;
  267. $(this).html($(this).html().replace(/(#\d+)/g, "#" + count));
  268. });
  269. })
  270. });
  271. return $rows;
  272. };
  273. })(django.jQuery);