Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/fix dismissAddRelatedObjectPopup #309

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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