diff --git a/ajax_select/static/ajax_select/js/ajax_select.js b/ajax_select/static/ajax_select/js/ajax_select.js index 1be7cd09eb..9ac063aa3e 100644 --- a/ajax_select/static/ajax_select/js/ajax_select.js +++ b/ajax_select/static/ajax_select/js/ajax_select.js @@ -1,4 +1,4 @@ -(function ($) { +(function ($, djangoJQuery) { $.fn.autocompleteselect = function (options) { return this.each(function () { var id = this.id, @@ -150,7 +150,6 @@ // allow html in the results menu // https://github.com/scottgonzalez/jquery-ui-extensions - console.log($, $.ui); var proto = $.ui.autocomplete.prototype, initSource = proto._initSource; @@ -184,30 +183,6 @@ }, }); - /* Called by the popup create object when it closes. - * For the popup this is opener.dismissAddRelatedObjectPopup - * Django implements this in RelatedObjectLookups.js - * In django >= 1.10 we can rely on input.trigger('change') - * and avoid this hijacking. - */ - var djangoDismissAddRelatedObjectPopup = - window.dismissAddRelatedObjectPopup || window.dismissAddAnotherPopup; - window.dismissAddRelatedObjectPopup = function (win, newId, newRepr) { - // Iff this is an ajax-select input then close the window and - // trigger didAddPopup - var input = $("#" + win.name); - if (input.data("ajax-select")) { - console.log("input", input, win, newId, newRepr); - win.close(); - // newRepr is django's repr of object - // not the Lookup's formatting of it. - input.trigger("didAddPopup", [newId, newRepr]); - } else { - // Call the normal django set and close function. - djangoDismissAddRelatedObjectPopup(win, newId, newRepr); - } - }; - // activate any on page $(window).bind("init-autocomplete", function () { $("input[data-ajax-select=autocomplete]").each(function (i, inp) { @@ -249,4 +224,32 @@ } ); }); -})(window.jQuery); + + /* Called by the popup create object when it closes. + * For the popup this is opener.dismissAddRelatedObjectPopup + * Django implements this in RelatedObjectLookups.js + * In django >= 1.10 we could try to rely on input.trigger('change') + * and avoid this hijacking, but the id and repr isn't passed. + */ + djangoJQuery(document).ready(function () { + var djangoDismissAddRelatedObjectPopup = + window.dismissAddRelatedObjectPopup; + if (!djangoDismissAddRelatedObjectPopup) { + throw new Error("dismissAddAnotherPopup not found"); + } + + window.dismissAddRelatedObjectPopup = function (win, newId, newRepr) { + // Iff this is an ajax-select input then close the window and + // trigger didAddPopup + var input = $("#" + win.name.replace("__1", "")); + if (input.length && input.data("ajax-select")) { + win.close(); + // note: newRepr is django's repr of object, not the Lookup's formatting of it. + input.trigger("didAddPopup", [newId, newRepr]); + } else { + // Call the normal django set and close function. + djangoDismissAddRelatedObjectPopup(win, newId, newRepr); + } + }; + }); +})(window.jQuery, window.django.jQuery); diff --git a/ajax_select/templates/ajax_select/autocomplete.html b/ajax_select/templates/ajax_select/autocomplete.html index 23b1e929b7..81aa08b936 100644 --- a/ajax_select/templates/ajax_select/autocomplete.html +++ b/ajax_select/templates/ajax_select/autocomplete.html @@ -7,9 +7,7 @@ autocomplete="off" data-ajax-select="autocomplete" data-plugin-options="{{ data_plugin_options }}" - {{ - extra_attrs - }} + {{ extra_attrs }} /> {% block extra_script %}{% endblock %} {% block help %}{% if help_text %}

{{ help_text }}

diff --git a/ajax_select/templates/ajax_select/autocompleteselect.html b/ajax_select/templates/ajax_select/autocompleteselect.html index acd4c8972b..8257fd94b7 100644 --- a/ajax_select/templates/ajax_select/autocompleteselect.html +++ b/ajax_select/templates/ajax_select/autocompleteselect.html @@ -5,9 +5,7 @@ id="{{html_id}}_text" value="" autocomplete="off" - {{ - extra_attrs - }} + {{ extra_attrs }} /> {% if add_link %} {% if add_link %}