Açıklama Yok

chainedfk.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. (function ($) {
  2. "use strict";
  3. window.chainedfk = function () {
  4. return {
  5. fireEvent: function (element, event) {
  6. var evt, rtn;
  7. if (document.createEventObject) {
  8. // dispatch for IE
  9. evt = document.createEventObject();
  10. rtn = element.fireEvent('on' + event, evt);
  11. } else {
  12. // dispatch for firefox + others
  13. evt = document.createEvent("HTMLEvents");
  14. evt.initEvent(event, true, true); // event type,bubbling,cancelable
  15. rtn = !element.dispatchEvent(evt);
  16. }
  17. return rtn;
  18. },
  19. dismissRelatedLookupPopup: function (win, chosenId) {
  20. var name = windowname_to_id(win.name),
  21. elem = document.getElementById(name);
  22. if (elem.className.indexOf('vManyToManyRawIdAdminField') !== -1 && elem.value) {
  23. elem.value += ',' + chosenId;
  24. } else {
  25. elem.value = chosenId;
  26. }
  27. fireEvent(elem, 'change');
  28. win.close();
  29. },
  30. fill_field: function (val, init_value, elem_id, url, empty_label, auto_choose) {
  31. var $selectField = $(elem_id),
  32. options = [];
  33. url = url + "/" + val + "/";
  34. var empty_option = $('<option></option>')
  35. .attr('value', '')
  36. .text(empty_label);
  37. if (!val || val === '') {
  38. empty_option.prop('selected', true);
  39. options.push(empty_option);
  40. $selectField.html(options);
  41. $selectField.trigger('change');
  42. return;
  43. }
  44. $.getJSON(url, function (j) {
  45. auto_choose = j.length === 1 && auto_choose;
  46. // Append empty label as the first option
  47. if (!(init_value || auto_choose)) {
  48. empty_option.prop('selected', true);
  49. }
  50. options.push(empty_option);
  51. // Append each option to the select
  52. $.each(j, function (index, optionData) {
  53. var option = $('<option></option>')
  54. .prop('value', optionData.value)
  55. .text(optionData.display);
  56. if (auto_choose === "true" || auto_choose === "True") {
  57. auto_choose = true;
  58. } else if (auto_choose === "false" || auto_choose === "False") {
  59. auto_choose = false;
  60. }
  61. if (auto_choose || (init_value && optionData.value == init_value)) {
  62. option.prop('selected', true);
  63. }
  64. options.push(option);
  65. });
  66. $selectField.html(options);
  67. var width = $selectField.outerWidth();
  68. if (navigator.appVersion.indexOf("MSIE") !== -1) {
  69. $selectField.width(width + 'px');
  70. }
  71. $selectField.trigger('change');
  72. });
  73. },
  74. init: function (chainfield, url, id, init_value, empty_label, auto_choose) {
  75. var val, fill_field = this.fill_field;
  76. if (!$(chainfield).hasClass("chained")) {
  77. val = $(chainfield).val();
  78. fill_field(val, init_value, id, url, empty_label, auto_choose);
  79. }
  80. $(chainfield).change(function () {
  81. // Handle the case of inlines, where the ID will depend on which list item we are dealing with
  82. var prefix, start_value, this_val, localID = id;
  83. if (localID.indexOf("__prefix__") > -1) {
  84. prefix = $(this).attr("id").match(/\d+/)[0];
  85. localID = localID.replace("__prefix__", prefix);
  86. }
  87. start_value = $(localID).val();
  88. this_val = $(this).val();
  89. fill_field(this_val, start_value, localID, url, empty_label, auto_choose);
  90. });
  91. if (typeof(dismissAddAnotherPopup) !== 'undefined') {
  92. var oldDismissAddAnotherPopup = dismissAddAnotherPopup;
  93. dismissAddAnotherPopup = function (win, newId, newRepr) {
  94. oldDismissAddAnotherPopup(win, newId, newRepr);
  95. if (windowname_to_id(win.name) === chainfield) {
  96. $(chainfield).change();
  97. }
  98. };
  99. }
  100. }
  101. };
  102. }();
  103. }(jQuery || django.jQuery));