暫無描述

chainedm2m.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. (function ($) {
  2. "use strict";
  3. window.chainedm2m = 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, initial_value, elem_id, url, initial_parent, auto_choose) {
  31. var $selectField = $(elem_id),
  32. $selectedto = $(elem_id + '_to'),
  33. cache_to = elem_id.replace('#', '') + '_to',
  34. cache_from = elem_id.replace('#', '') + '_from';
  35. if (!$selectField.length) {
  36. $selectField = $(elem_id + '_from');
  37. }
  38. function trigger_chosen_updated() {
  39. if ($.fn.chosen !== undefined) {
  40. $selectField.trigger('chosen:updated');
  41. }
  42. }
  43. // SelectBox is a global var from djangojs "admin/js/SelectBox.js"
  44. // Clear cache to avoid the elements duplication
  45. if (typeof SelectBox !== 'undefined') {
  46. if (typeof SelectBox.cache[cache_to] !== 'undefined') {
  47. SelectBox.cache[cache_to].splice(0);
  48. }
  49. if (typeof SelectBox.cache[cache_from] !== 'undefined') {
  50. SelectBox.cache[cache_from].splice(0);
  51. }
  52. }
  53. if (!val || val === '') {
  54. $selectField.html('');
  55. $selectedto.html('');
  56. trigger_chosen_updated();
  57. return;
  58. }
  59. // Make sure that these are always an arrays
  60. val = [].concat(val);
  61. initial_parent = [].concat(initial_parent);
  62. var target_url = url + "/" + val + "/",
  63. options = [],
  64. selectedoptions = [];
  65. $.getJSON(target_url, function (j) {
  66. var i, width;
  67. auto_choose = j.length === 1 && auto_choose;
  68. var selected_values = {};
  69. // if val and initial_parent have any common values, we need to set selected options.
  70. if ($(val).filter(initial_parent).length >= 0 && initial_value) {
  71. for (i = 0; i < initial_value.length; i = i + 1) {
  72. selected_values[initial_value[i]] = true;
  73. }
  74. }
  75. // select values which were previously selected (for many2many - many2many chain)
  76. $(elem_id + ' option:selected').each(function () {
  77. selected_values[$(this).val()] = true;
  78. });
  79. $.each(j, function (index, optionData) {
  80. var option = $('<option></option>')
  81. .attr('value', optionData.value)
  82. .text(optionData.display)
  83. .attr('title', optionData.display);
  84. if (auto_choose === "true" || auto_choose === "True") {
  85. auto_choose = true;
  86. } else if (auto_choose === "false" || auto_choose === "False") {
  87. auto_choose = false;
  88. }
  89. if (auto_choose || selected_values[optionData.value] === true) {
  90. if ($selectedto.length) {
  91. selectedoptions.push(option);
  92. } else {
  93. option.prop('selected', true);
  94. options.push(option);
  95. }
  96. } else {
  97. options.push(option);
  98. }
  99. });
  100. $selectField.html(options);
  101. if ($selectedto.length) {
  102. $selectedto.html(selectedoptions);
  103. var node;
  104. // SelectBox is a global var from djangojs "admin/js/SelectBox.js"
  105. for (i = 0, j = selectedoptions.length; i < j; i = i + 1) {
  106. node = selectedoptions[i];
  107. SelectBox.cache[cache_to].push({value: node.prop("value"), text: node.prop("text"), displayed: 1});
  108. }
  109. for (i = 0, j = options.length; i < j; i = i + 1) {
  110. node = options[i];
  111. SelectBox.cache[cache_from].push({value: node.prop("value"), text: node.prop("text"), displayed: 1});
  112. }
  113. }
  114. width = $selectField.outerWidth();
  115. if (navigator.appVersion.indexOf("MSIE") !== -1) {
  116. $selectField.width(width + 'px');
  117. }
  118. $selectField.trigger('change');
  119. trigger_chosen_updated();
  120. });
  121. },
  122. init: function (chainfield, url, id, value, auto_choose) {
  123. var fill_field, val, initial_parent = $(chainfield).val(),
  124. initial_value = value;
  125. if (!$(chainfield).hasClass("chained")) {
  126. val = $(chainfield).val();
  127. this.fill_field(val, initial_value, id, url, initial_parent, auto_choose);
  128. }
  129. fill_field = this.fill_field;
  130. $(chainfield).change(function () {
  131. var prefix, start_value, this_val, localID = id;
  132. if (localID.indexOf("__prefix__") > -1) {
  133. prefix = $(this).attr("id").match(/\d+/)[0];
  134. localID = localID.replace("__prefix__", prefix);
  135. }
  136. start_value = $(localID).val();
  137. this_val = $(this).val();
  138. fill_field(this_val, initial_value, localID, url, initial_parent, auto_choose);
  139. });
  140. // allait en bas, hors du documentready
  141. if (typeof(dismissAddAnotherPopup) !== 'undefined') {
  142. var oldDismissAddAnotherPopup = dismissAddAnotherPopup;
  143. dismissAddAnotherPopup = function (win, newId, newRepr) {
  144. oldDismissAddAnotherPopup(win, newId, newRepr);
  145. if ("#" + windowname_to_id(win.name) === chainfield) {
  146. $(chainfield).change();
  147. }
  148. };
  149. }
  150. if (typeof(dismissRelatedLookupPopup) !== 'undefined') {
  151. var oldDismissRelatedLookupPopup = dismissRelatedLookupPopup;
  152. dismissRelatedLookupPopup = function (win, chosenId) {
  153. oldDismissRelatedLookupPopup(win, chosenId);
  154. if ("#" + windowname_to_id(win.name) === chainfield) {
  155. $(chainfield).change();
  156. }
  157. };
  158. }
  159. }
  160. };
  161. }();
  162. }(jQuery || django.jQuery));