|
| 1 | +window.addEventListener('load', function() { |
1 | 2 |
|
2 | | -(function($) { |
3 | | - 'use strict'; |
| 3 | + var $ = window.jQuery; |
4 | 4 |
|
5 | 5 | $.fn.autocompleteselect = function(options) { |
6 | 6 | return this.each(function() { |
|
184 | 184 | /* Called by the popup create object when it closes. |
185 | 185 | * For the popup this is opener.dismissAddRelatedObjectPopup |
186 | 186 | * Django implements this in RelatedObjectLookups.js |
| 187 | + * In django >= 1.10 we can rely on input.trigger('change') |
| 188 | + * and avoid this hijacking. |
187 | 189 | */ |
188 | 190 | var djangoDismissAddRelatedObjectPopup = window.dismissAddRelatedObjectPopup || window.dismissAddAnotherPopup; |
189 | 191 | window.dismissAddRelatedObjectPopup = function(win, newId, newRepr) { |
190 | | - // This may be called for ajax-select inputs or for other inputs. |
191 | | - // Call the original which sets the input (just the pk) |
192 | | - // calls input.trigger('changed') if >= 1.10 |
193 | | - // and closes the window. |
194 | | - if (djangoDismissAddRelatedObjectPopup) { |
195 | | - djangoDismissAddRelatedObjectPopup(win, newId, newRepr); |
196 | | - } else { |
| 192 | + // Iff this is an ajax-select input then close the window and |
| 193 | + // trigger didAddPopup |
| 194 | + var name = window.windowname_to_id(win.name); |
| 195 | + var input = $('#' + name); |
| 196 | + if (input.data('ajax-select')) { |
197 | 197 | win.close(); |
| 198 | + // newRepr is django's repr of object |
| 199 | + // not the Lookup's formatting of it. |
| 200 | + input.trigger('didAddPopup', [newId, newRepr]); |
| 201 | + } else { |
| 202 | + // Call the normal django set and close function. |
| 203 | + djangoDismissAddRelatedObjectPopup(win, newId, newRepr); |
198 | 204 | } |
199 | | - var name = window.windowname_to_id(win.name); |
200 | | - // newRepr is django's repr of object |
201 | | - // not the Lookup's formatting of it. |
202 | | - $('#' + name).trigger('didAddPopup', [newId, newRepr]); |
203 | 205 | } |
204 | 206 | // Django renamed this function in 1.8 |
205 | 207 | window.dismissAddAnotherPopup = window.dismissAddRelatedObjectPopup; |
|
243 | 245 | }); |
244 | 246 | }); |
245 | 247 |
|
246 | | -})(window.jQuery); |
| 248 | +}, {once: true}); |
0 commit comments