No Description

bindfields.js 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. (function ($) {
  2. "use strict";
  3. function initItem(item) {
  4. var empty_label, chainfield = "#id_" + $(item).attr("data-chainfield"),
  5. url = $(item).attr("data-url"),
  6. id = "#" + $(item).attr("id"),
  7. value = JSON.parse($(item).attr("data-value")),
  8. auto_choose = $(item).attr("data-auto_choose");
  9. if ($(item).hasClass("chained-fk")) {
  10. empty_label = $(item).attr("data-empty_label");
  11. chainedfk.init(chainfield, url, id, value, empty_label, auto_choose);
  12. } else if ($(item).hasClass("chained")) {
  13. chainedm2m.init(chainfield, url, id, value, auto_choose);
  14. } else if ($(item).hasClass("filtered")) {
  15. // For the ManyToMany using horizontal=True added after the page load
  16. // using javascript.
  17. id = id.replace('_from', ''); // we need to remove the _from part
  18. chainedm2m.init(chainfield, url, id, value, auto_choose);
  19. }
  20. }
  21. $(window).on('load', function () {
  22. $.each($(".chained"), function (index, item) {
  23. initItem(item);
  24. });
  25. $.each($(".filtered"), function (index, item) {
  26. if (item.hasAttribute('data-chainfield')) {
  27. initItem(item);
  28. }
  29. });
  30. });
  31. $(document).ready(function () {
  32. $.each($(".chained-fk"), function (index, item) {
  33. initItem(item);
  34. });
  35. });
  36. function initFormset(chained) {
  37. var re = /\d+/g,
  38. prefix,
  39. match,
  40. chainfield = $(chained).attr("data-chainfield"),
  41. chainedId = $(chained).attr("id");
  42. if (chainfield.indexOf("__prefix__") > -1) {
  43. /*
  44. If we have several inlines with the same name, they will get an index, so we need to ignore that and get
  45. the last numeric value in the id
  46. */
  47. do {
  48. match = re.exec(chainedId);
  49. if (match) {
  50. prefix = match[0];
  51. }
  52. } while (match);
  53. chainfield = chainfield.replace("__prefix__", prefix);
  54. $(chained).attr("data-chainfield", chainfield);
  55. }
  56. initItem(chained);
  57. }
  58. $(document).on('formset:added', function (event, $row, formsetName) {
  59. // Fired every time a new inline formset is created
  60. var chainedFK, chainedM2M, filteredM2M;
  61. // For the ForeingKey
  62. chainedFK = $row.find(".chained-fk");
  63. $.each(chainedFK, function (index, chained) {
  64. initFormset(chained);
  65. });
  66. // For the ManyToMany
  67. chainedM2M = $row.find(".chained");
  68. $.each(chainedM2M, function (index, chained) {
  69. initFormset(chained);
  70. });
  71. // For the ManyToMany using horizontal=True added after the page load
  72. // using javascript.
  73. filteredM2M = $row.find(".filtered");
  74. $.each(filteredM2M, function (index, filtered) {
  75. if (filtered.hasAttribute('data-chainfield')) {
  76. initFormset(filtered);
  77. }
  78. });
  79. });
  80. }(jQuery || django.jQuery));