Skip to content

Commit

Permalink
Merge pull request #309 from crucialfelix/feature/fix-dismissAddRelat…
Browse files Browse the repository at this point in the history
…edObjectPopup

feature/fix dismissAddRelatedObjectPopup
  • Loading branch information
crucialfelix committed Dec 14, 2023
2 parents 7af255d + 9a3949f commit 0522cf4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 37 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);
4 changes: 1 addition & 3 deletions ajax_select/templates/ajax_select/autocomplete.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
<p class="help">{{ help_text }}</p>
Expand Down
4 changes: 1 addition & 3 deletions ajax_select/templates/ajax_select/autocompleteselect.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
id="{{html_id}}_text"
value=""
autocomplete="off"
{{
extra_attrs
}}
{{ extra_attrs }}
/>
{% if add_link %}
<a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
id="{{html_id}}_text"
value=""
autocomplete="off"
{{
extra_attrs
}}
{{ extra_attrs }}
/>
{% if add_link %}
<a
Expand Down
2 changes: 1 addition & 1 deletion example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ cleandb:
help:
@echo make install
@echo or:
@echo make clean install DJANGO=1.11
@echo make clean install


.PHONY: install clean cleandb help

0 comments on commit 0522cf4

Please sign in to comment.