Нет описания

autocomplete.init.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. This script garantees that this will be called once in django admin.
  3. However, its the callback's responsability to clean up if the
  4. element was cloned with data - which should be the case.
  5. */
  6. ;(function ($) {
  7. $.fn.getFormPrefix = function() {
  8. /* Get the form prefix for a field.
  9. *
  10. * For example:
  11. *
  12. * $(':input[name$=owner]').getFormsetPrefix()
  13. *
  14. * Would return an empty string for an input with name 'owner' but would return
  15. * 'inline_model-0-' for an input named 'inline_model-0-owner'.
  16. */
  17. var parts = $(this).attr('name').split('-');
  18. var prefix = '';
  19. for (var i in parts) {
  20. var testPrefix = parts.slice(0, -i).join('-');
  21. if (! testPrefix.length) continue;
  22. testPrefix += '-';
  23. var result = $(':input[name^=' + testPrefix + ']')
  24. if (result.length) {
  25. return testPrefix;
  26. }
  27. }
  28. return '';
  29. }
  30. $.fn.getFormPrefixes = function() {
  31. /*
  32. * Get the form prefixes for a field, from the most specific to the least.
  33. *
  34. * For example:
  35. *
  36. * $(':input[name$=owner]').getFormPrefixes()
  37. *
  38. * Would return:
  39. * - [''] for an input named 'owner'.
  40. * - ['inline_model-0-', ''] for an input named 'inline_model-0-owner' (i.e. nested with a nested inline).
  41. * - ['sections-0-items-0-', 'sections-0-', ''] for an input named 'sections-0-items-0-product'
  42. * (i.e. nested multiple time with django-nested-admin).
  43. */
  44. var parts = $(this).attr('name').split('-').slice(0, -1);
  45. var prefixes = [];
  46. for (i = 0; i < parts.length; i += 2) {
  47. var testPrefix = parts.slice(0, -i || parts.length).join('-');
  48. if (!testPrefix.length)
  49. continue;
  50. testPrefix += '-';
  51. var result = $(':input[name^=' + testPrefix + ']')
  52. if (result.length)
  53. prefixes.push(testPrefix);
  54. }
  55. prefixes.push('');
  56. return prefixes;
  57. }
  58. var initialized = [];
  59. function initialize(element) {
  60. if (typeof element === 'undefined' || typeof element === 'number') {
  61. element = this;
  62. }
  63. if (window.__dal__initListenerIsSet !== true || initialized.indexOf(element) >= 0) {
  64. return;
  65. }
  66. $(element).trigger('autocompleteLightInitialize');
  67. initialized.push(element);
  68. }
  69. if (!window.__dal__initialize) {
  70. window.__dal__initialize = initialize;
  71. $(document).ready(function () {
  72. $('[data-autocomplete-light-function=select2]:not([id*="__prefix__"])').each(initialize);
  73. });
  74. $(document).bind('DOMNodeInserted', function (e) {
  75. $(e.target).find('[data-autocomplete-light-function=select2]').each(initialize);
  76. });
  77. }
  78. // using jQuery
  79. function getCookie(name) {
  80. var cookieValue = null;
  81. if (document.cookie && document.cookie != '') {
  82. var cookies = document.cookie.split(';');
  83. for (var i = 0; i < cookies.length; i++) {
  84. var cookie = $.trim(cookies[i]);
  85. // Does this cookie string begin with the name we want?
  86. if (cookie.substring(0, name.length + 1) == (name + '=')) {
  87. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  88. break;
  89. }
  90. }
  91. }
  92. return cookieValue;
  93. }
  94. document.csrftoken = getCookie('csrftoken');
  95. if (document.csrftoken === null) {
  96. // Try to get CSRF token from DOM when cookie is missing
  97. var $csrf = $('form :input[name="csrfmiddlewaretoken"]');
  98. if ($csrf.length > 0) {
  99. document.csrftoken = $csrf[0].value;
  100. }
  101. }
  102. })(yl.jQuery);
  103. // Does the same thing as django's admin/js/autocomplete.js, but uses yl.jQuery.
  104. (function($) {
  105. 'use strict';
  106. var init = function($element, options) {
  107. var settings = $.extend({
  108. ajax: {
  109. data: function(params) {
  110. return {
  111. term: params.term,
  112. page: params.page
  113. };
  114. }
  115. }
  116. }, options);
  117. $element.select2(settings);
  118. };
  119. $.fn.djangoAdminSelect2 = function(options) {
  120. var settings = $.extend({}, options);
  121. $.each(this, function(i, element) {
  122. var $element = $(element);
  123. init($element, settings);
  124. });
  125. return this;
  126. };
  127. $(function() {
  128. // Initialize all autocomplete widgets except the one in the template
  129. // form used when a new formset is added.
  130. $('.admin-autocomplete').not('[name*=__prefix__]').djangoAdminSelect2();
  131. });
  132. $(document).on('formset:added', (function() {
  133. return function(event, $newFormset) {
  134. return $newFormset.find('.admin-autocomplete').djangoAdminSelect2();
  135. };
  136. })(this));
  137. }(yl.jQuery));