Skip to content

Commit

Permalink
fix crucialfelix#307 Add new via popup broken
Browse files Browse the repository at this point in the history
Later versions of Django are appending sequence numbers: __1 to the window name.
These need to be stripped off to find the html id of the input that triggered
the pop up window.

If you open multiple windows for different ajax-select inputs of the same field
then you would get sequences like members__id__2 and it would not work.
  • Loading branch information
crucialfelix committed Dec 14, 2023
1 parent a6d4a5c commit 40c86d6
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions ajax_select/static/ajax_select/js/ajax_select.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function ($) {
(function ($, djangoJQuery) {
$.fn.autocompleteselect = function (options) {
return this.each(function () {
var id = this.id,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

0 comments on commit 40c86d6

Please sign in to comment.