|
310 | 310 | }; |
311 | 311 |
|
312 | 312 | // Tabular inlines --------------------------------------------------------- |
313 | | - $.fn.tabularFormset = function (selector, options) { |
| 313 | + $.fn.tabularFormset = function (selector, options, callback = null) { |
314 | 314 | const $rows = $(this); |
315 | 315 |
|
316 | 316 | const reinitDateTimeShortCuts = function () { |
|
370 | 370 | addButton: options.addButton, |
371 | 371 | }); |
372 | 372 |
|
| 373 | + if (typeof callback === "function") { |
| 374 | + callback(); |
| 375 | + } |
| 376 | + |
373 | 377 | return $rows; |
374 | 378 | }; |
375 | 379 |
|
376 | 380 | // Stacked inlines --------------------------------------------------------- |
377 | | - $.fn.stackedFormset = function (selector, options) { |
| 381 | + $.fn.stackedFormset = function (selector, options, callback = null) { |
378 | 382 | const $rows = $(this); |
379 | 383 | const updateInlineLabel = function (row) { |
380 | 384 | $(selector) |
|
449 | 453 | addButton: options.addButton, |
450 | 454 | }); |
451 | 455 |
|
| 456 | + if (typeof callback === "function") { |
| 457 | + callback(); |
| 458 | + } |
| 459 | + |
452 | 460 | return $rows; |
453 | 461 | }; |
454 | 462 |
|
| 463 | + $(window).on("htmx:afterSettle", function (event) { |
| 464 | + if (event.target.classList.contains("js-inline-admin-formset")) { |
| 465 | + initInlines($(event.target), function () { |
| 466 | + if (typeof DateTimeShortcuts !== "undefined") { |
| 467 | + $(".datetimeshortcuts").remove(); |
| 468 | + DateTimeShortcuts.init(); |
| 469 | + } |
| 470 | + |
| 471 | + $(event.target).find(".admin-autocomplete").djangoAdminSelect2(); |
| 472 | + }); |
| 473 | + } |
| 474 | + }); |
| 475 | + |
455 | 476 | $(document).ready(function () { |
456 | 477 | $(".js-inline-admin-formset").each(function () { |
457 | | - const data = $(this).data(), |
458 | | - inlineOptions = data.inlineFormset; |
459 | | - let selector; |
460 | | - switch (data.inlineType) { |
461 | | - case "stacked": |
462 | | - selector = inlineOptions.name + "-group .inline-related"; |
463 | | - $(selector).stackedFormset(selector, inlineOptions.options); |
464 | | - break; |
465 | | - case "tabular": |
466 | | - selector = |
467 | | - inlineOptions.name + |
468 | | - "-group .tabular.inline-related tbody:last > tr.form-row"; |
469 | | - $(selector).tabularFormset(selector, inlineOptions.options); |
470 | | - break; |
471 | | - } |
| 478 | + initInlines(this); |
472 | 479 | }); |
473 | 480 | }); |
| 481 | + |
| 482 | + function initInlines(el, callback = null) { |
| 483 | + const data = $(el).data(), |
| 484 | + inlineOptions = data.inlineFormset; |
| 485 | + let selector; |
| 486 | + switch (data.inlineType) { |
| 487 | + case "stacked": |
| 488 | + selector = inlineOptions.name + "-group .inline-related"; |
| 489 | + $(selector).stackedFormset(selector, inlineOptions.options, callback); |
| 490 | + break; |
| 491 | + case "tabular": |
| 492 | + selector = |
| 493 | + inlineOptions.name + |
| 494 | + "-group .tabular.inline-related tbody:last > tr.form-row"; |
| 495 | + $(selector).tabularFormset(selector, inlineOptions.options, callback); |
| 496 | + break; |
| 497 | + } |
| 498 | + } |
474 | 499 | } |
0 commit comments